Skip to content

Commit

Permalink
explain: Add test for transitive invalidation (#8070)
Browse files Browse the repository at this point in the history
Also add test cases for existing test data that previously wasn't used
in tests.
  • Loading branch information
fmeum authored Dec 18, 2024
1 parent b02f2e9 commit 3718745
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 22 deletions.
62 changes: 45 additions & 17 deletions cli/explain/compactgraph/compactgraph_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,24 +267,52 @@ func TestJavaHeaderChange(t *testing.T) {
}

func TestEnvChange(t *testing.T) {
spawnDiffs := diffLogs(t, "env_change", "7.3.1")
require.Len(t, spawnDiffs, 1)
for _, bazelVersion := range []string{"7.3.1", "8.0.0"} {
t.Run(bazelVersion, func(t *testing.T) {
spawnDiffs := diffLogs(t, "env_change", "7.3.1")
require.Len(t, spawnDiffs, 1)

sd := spawnDiffs[0]
assert.Regexp(t, "^bazel-out/[^/]+/bin/pkg/out$", sd.PrimaryOutput)
assert.Equal(t, "//pkg:gen", sd.TargetLabel)
assert.Equal(t, "Genrule", sd.Mnemonic)
assert.Empty(t, sd.GetModified().GetTransitivelyInvalidated())
require.Len(t, sd.GetModified().GetDiffs(), 1)
d := sd.GetModified().Diffs[0]
assert.Equal(t, map[string]string{
"OLD_AND_NEW": "old",
"OLD_ONLY": "old_only",
}, d.GetEnv().GetOldChanged())
assert.Equal(t, map[string]string{
"OLD_AND_NEW": "new",
"NEW_ONLY": "new_only",
}, d.GetEnv().GetNewChanged())
})
}
}

sd := spawnDiffs[0]
assert.Regexp(t, "^bazel-out/[^/]+/bin/pkg/out$", sd.PrimaryOutput)
assert.Equal(t, "//pkg:gen", sd.TargetLabel)
assert.Equal(t, "Genrule", sd.Mnemonic)
assert.Empty(t, sd.GetModified().GetTransitivelyInvalidated())
require.Len(t, sd.GetModified().GetDiffs(), 1)
d := sd.GetModified().Diffs[0]
assert.Equal(t, map[string]string{
"OLD_AND_NEW": "old",
"OLD_ONLY": "old_only",
}, d.GetEnv().GetOldChanged())
assert.Equal(t, map[string]string{
"OLD_AND_NEW": "new",
"NEW_ONLY": "new_only",
}, d.GetEnv().GetNewChanged())
func TestTransitiveInvalidation(t *testing.T) {
spawnDiffs := diffLogs(t, "transitive_invalidation", "8.0.0")
require.Len(t, spawnDiffs, 2)

{
sd := spawnDiffs[0]
assert.Regexp(t, "^bazel-out/[^/]+/bin/pkg/out_direct1$", sd.PrimaryOutput)
assert.Equal(t, "//pkg:direct1", sd.TargetLabel)
assert.Equal(t, "Genrule", sd.Mnemonic)
assert.Equal(t, map[string]uint32{"Genrule": 3}, sd.GetModified().GetTransitivelyInvalidated())
require.Len(t, sd.GetModified().GetDiffs(), 1)
assert.Len(t, sd.GetModified().GetDiffs()[0].GetInputContents().GetFileDiffs(), 1)
}
{
sd := spawnDiffs[1]
assert.Regexp(t, "^bazel-out/[^/]+/bin/pkg/out_direct2$", sd.PrimaryOutput)
assert.Equal(t, "//pkg:direct2", sd.TargetLabel)
assert.Equal(t, "Genrule", sd.Mnemonic)
assert.Equal(t, map[string]uint32{"Genrule": 3}, sd.GetModified().GetTransitivelyInvalidated())
require.Len(t, sd.GetModified().GetDiffs(), 1)
assert.Len(t, sd.GetModified().GetDiffs()[0].GetInputContents().GetFileDiffs(), 1)
}
}

func TestNonHermetic(t *testing.T) {
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
54 changes: 49 additions & 5 deletions cli/explain/compactgraph/testdata/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ genrule(
`,
baselineArgs: []string{"--action_env=EXTRA=foo", "--action_env=OLD_ONLY=old_only", "--action_env=OLD_AND_NEW=old"},
changedArgs: []string{"--action_env=NEW_ONLY=new_only", "--action_env=OLD_AND_NEW=new", "--action_env=EXTRA=foo"},
bazelVersions: []string{"8.0.0"},
bazelVersions: []string{"7.3.1", "8.0.0"},
},
{
name: "non_hermetic",
Expand Down Expand Up @@ -817,6 +817,54 @@ echo "Test"
changedArgs: []string{"--legacy_external_runfiles", "--noenable_bzlmod", "--enable_workspace"},
bazelVersions: []string{"8.0.0"},
},
{
name: "transitive_invalidation",
baseline: `
-- MODULE.bazel --
-- pkg/BUILD --
genrule(
name = "direct1",
srcs = ["in1.txt"],
outs = ["out_direct1"],
cmd = "cat $< > $@",
)
genrule(
name = "direct2",
srcs = ["in2.txt"],
outs = ["out_direct2"],
cmd = "cat $< > $@",
)
genrule(
name = "intermediate",
srcs = [":direct1", ":direct2"],
outs = ["out_intermediate"],
cmd = "cat $(SRCS) > $@",
)
genrule(
name = "top1",
srcs = [":intermediate"],
outs = ["out_top1"],
cmd = "cat $< > $@",
)
genrule(
name = "top2",
srcs = [":intermediate"],
outs = ["out_top2"],
cmd = "cat $< > $@",
)
-- pkg/in1.txt --
old
-- pkg/in2.txt --
old
`,
changes: `
-- pkg/in1.txt --
new
-- pkg/in2.txt --
new
`,
bazelVersions: []string{"8.0.0"},
},
} {
if toGenerate != nil && !toGenerate[tc.name] {
continue
Expand Down Expand Up @@ -872,10 +920,6 @@ func collectLog(bazelisk string, args []string, projectDir, logPath, bazelVersio
cmd.Dir = projectDir
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
// TODO: Update to 8.0.0 when it's released.
if bazelVersion == "8.0.0" {
bazelVersion = "8.0.0rc1"
}
cmd.Env = append(os.Environ(), "USE_BAZEL_VERSION="+bazelVersion)
if err = cmd.Run(); err != nil {
// Allow failures due to no tests as we always run with `bazel test`.
Expand Down

0 comments on commit 3718745

Please sign in to comment.