bzl: bazel test sourcegraph/lib (#47605)

Run all tests using bazel under `sourcegraph/lib`
This commit is contained in:
Jean-Hadrien Chabran 2023-02-15 20:06:28 +01:00 committed by GitHub
parent 151b0ca8de
commit 3fb9b64cfe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 73 additions and 12 deletions

View File

@ -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

View File

@ -15,7 +15,7 @@ func BazelOperations() *operations.Set {
ops.Append(bazelBuild(
"//...",
))
ops.Append(bazelTest("//monitoring/..."))
ops.Append(bazelTest("//monitoring/...", "//lib/..."))
return ops
}

View File

@ -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",

View File

@ -0,0 +1,5 @@
filegroup(
name = "data",
srcs = glob(["*.lsif"]),
visibility = ["//lib/codeintel:__subpackages__"],
)

View File

@ -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",

View File

@ -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) }
}

View File

@ -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"],
)