diff --git a/.bazelignore b/.bazelignore index 38444c881d4..025edaf4217 100644 --- a/.bazelignore +++ b/.bazelignore @@ -31,7 +31,5 @@ cmd/symbols/squirrel/test_repos/starlark cmd/sitemap docker-images/syntax-highlighter/crates/sg-syntax/src/snapshots/ -lib/codeintel/precise/diff/testdata/ - # temporary ignores internal/cmd/progress-bot diff --git a/enterprise/dev/ci/internal/ci/bazel_operations.go b/enterprise/dev/ci/internal/ci/bazel_operations.go index 0c3e1ad1b9e..9e6456eeeee 100644 --- a/enterprise/dev/ci/internal/ci/bazel_operations.go +++ b/enterprise/dev/ci/internal/ci/bazel_operations.go @@ -15,7 +15,7 @@ func BazelOperations() *operations.Set { ops.Append(bazelBuild( "//...", )) - ops.Append(bazelTest("//monitoring/...")) + ops.Append(bazelTest("//monitoring/...", "//lib/...")) return ops } diff --git a/lib/codeintel/lsif/conversion/BUILD.bazel b/lib/codeintel/lsif/conversion/BUILD.bazel index 08a320b1497..52485d337e1 100644 --- a/lib/codeintel/lsif/conversion/BUILD.bazel +++ b/lib/codeintel/lsif/conversion/BUILD.bazel @@ -32,6 +32,10 @@ go_test( "group_test.go", "prune_test.go", ], + # The testdata folders lives in our parent folder, which cannot be referenced directly. + # therefore, we have to create a filegroup with the correct visibility and reference + # it manually below. + data = ["//lib/codeintel/lsif/testdata:data"], # keep embed = [":conversion"], deps = [ "//lib/codeintel/lsif/conversion/datastructures", diff --git a/lib/codeintel/lsif/testdata/BUILD.bazel b/lib/codeintel/lsif/testdata/BUILD.bazel new file mode 100644 index 00000000000..a1d1532aed9 --- /dev/null +++ b/lib/codeintel/lsif/testdata/BUILD.bazel @@ -0,0 +1,5 @@ +filegroup( + name = "data", + srcs = glob(["*.lsif"]), + visibility = ["//lib/codeintel:__subpackages__"], +) diff --git a/lib/codeintel/precise/diff/BUILD.bazel b/lib/codeintel/precise/diff/BUILD.bazel index de6a5388ac4..56178ef53be 100644 --- a/lib/codeintel/precise/diff/BUILD.bazel +++ b/lib/codeintel/precise/diff/BUILD.bazel @@ -1,5 +1,7 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") +# gazelle:exclude testdata + go_library( name = "diff", srcs = ["diff.go"], @@ -15,6 +17,7 @@ go_test( name = "diff_test", srcs = ["diff_test.go"], embed = [":diff"], + data = ["//lib/codeintel/precise/diff/testdata:data"], deps = [ "//lib/codeintel/lsif/conversion", "//lib/codeintel/precise", diff --git a/lib/codeintel/precise/diff/diff_test.go b/lib/codeintel/precise/diff/diff_test.go index cdce00719e0..1805a3d8bbb 100644 --- a/lib/codeintel/precise/diff/diff_test.go +++ b/lib/codeintel/precise/diff/diff_test.go @@ -2,6 +2,8 @@ package diff import ( "context" + "os" + "os/exec" "path/filepath" "testing" @@ -17,6 +19,13 @@ var dumpOldPath = "./testdata/project1/dump-old.lsif" var dumpNewPath = "./testdata/project1/dump-new.lsif" func TestNoDiffOnPermutedDumps(t *testing.T) { + cwd, _ := os.Getwd() + defer func() { os.Chdir(cwd) }() + + tmpdir1, teardown := createTmpRepo(t, dumpPath) + t.Cleanup(teardown) + os.Chdir(tmpdir1) + bundle1, err := conversion.CorrelateLocalGit( context.Background(), dumpPath, @@ -26,6 +35,10 @@ func TestNoDiffOnPermutedDumps(t *testing.T) { t.Fatalf("Unexpected error reading dump path: %v", err) } + tmpdir2, teardown := createTmpRepo(t, dumpPermutedPath) + t.Cleanup(teardown) + os.Chdir(tmpdir2) + bundle2, err := conversion.CorrelateLocalGit( context.Background(), dumpPermutedPath, @@ -41,6 +54,13 @@ func TestNoDiffOnPermutedDumps(t *testing.T) { } func TestDiffOnEditedDumps(t *testing.T) { + cwd, _ := os.Getwd() + defer func() { os.Chdir(cwd) }() + + tmpdir1, teardown := createTmpRepo(t, dumpOldPath) + t.Cleanup(teardown) + os.Chdir(tmpdir1) + bundle1, err := conversion.CorrelateLocalGit( context.Background(), dumpOldPath, @@ -50,6 +70,10 @@ func TestDiffOnEditedDumps(t *testing.T) { t.Fatalf("Unexpected error reading dump: %v", err) } + tmpdir2, teardown := createTmpRepo(t, dumpNewPath) + t.Cleanup(teardown) + os.Chdir(tmpdir2) + bundle2, err := conversion.CorrelateLocalGit( context.Background(), dumpNewPath, @@ -66,3 +90,39 @@ func TestDiffOnEditedDumps(t *testing.T) { autogold.Equal(t, autogold.Raw(computedDiff)) } + +// createTmpRepo returns a temp directory with the testdata copied over from the +// enclosing folder of path, with a newly initialized git repository. +func createTmpRepo(t *testing.T, path string) (string, func()) { + tmpdir, err := os.MkdirTemp("", "") + if err != nil { + t.Fatalf("Unexpected error creating dump tmp folder: %v", err) + } + + fullpath := filepath.Join(tmpdir, "testdata") + os.MkdirAll(fullpath, os.ModePerm) + + if err := exec.Command("cp", "-R", filepath.Dir(path), fullpath).Run(); err != nil { + t.Fatalf("Unexpected error copying dump tmp folder: %v", err) + } + + gitcmd := exec.Command("git", "init") + gitcmd.Dir = tmpdir + if err := gitcmd.Run(); err != nil { + t.Fatalf("Unexpected error git: %v", err) + } + + gitcmd = exec.Command("git", "add", ".") + gitcmd.Dir = tmpdir + if err := gitcmd.Run(); err != nil { + t.Fatalf("Unexpected error git: %v", err) + } + + gitcmd = exec.Command("git", "commit", "-m", "initial commit") + gitcmd.Dir = tmpdir + if err := gitcmd.Run(); err != nil { + t.Fatalf("Unexpected error git: %v", err) + } + + return tmpdir, func() { os.RemoveAll(tmpdir) } +} diff --git a/lib/codeintel/precise/diff/testdata/project1/BUILD.bazel b/lib/codeintel/precise/diff/testdata/project1/BUILD.bazel deleted file mode 100644 index a78ad670262..00000000000 --- a/lib/codeintel/precise/diff/testdata/project1/BUILD.bazel +++ /dev/null @@ -1,9 +0,0 @@ -load("@io_bazel_rules_go//go:def.bzl", "go_library") - -go_library( - name = "project1", - srcs = ["test.go"], - importpath = "github.com/sourcegraph/sourcegraph/lib/codeintel/precise/diff/testdata/project1", - visibility = ["//visibility:public"], - deps = ["@com_github_google_go_cmp//cmp"], -)