Skip to content

Commit

Permalink
Fix generationtest with Bzlmod
Browse files Browse the repository at this point in the history
  • Loading branch information
fmeum committed Jan 9, 2025
1 parent ef2e600 commit 4c9a320
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 33 deletions.
2 changes: 1 addition & 1 deletion internal/generationtest/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@ go_library(
visibility = ["//visibility:public"],
deps = [
"//testtools",
"@io_bazel_rules_go//go/tools/bazel:go_default_library",
"@io_bazel_rules_go//go/runfiles",
],
)
76 changes: 45 additions & 31 deletions internal/generationtest/generation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,18 @@ package generationtest

import (
"flag"
"io/fs"
"path/filepath"
"strings"
"testing"
"time"

"github.com/bazelbuild/bazel-gazelle/testtools"
"github.com/bazelbuild/rules_go/go/tools/bazel"
"github.com/bazelbuild/rules_go/go/runfiles"
)

var (
gazelleBinaryPath = flag.String("gazelle_binary_path", "", "Path to the gazelle binary to test.")
gazelleBinaryPath = flag.String("gazelle_binary_path", "", "rlocationpath to the gazelle binary to test.")
buildInSuffix = flag.String("build_in_suffix", ".in", "The suffix on the test input BUILD.bazel files. Defaults to .in. "+
" By default, will use files named BUILD.in as the BUILD files before running gazelle.")
buildOutSuffix = flag.String("build_out_suffix", ".out", "The suffix on the expected BUILD.bazel files after running gazelle. Defaults to .out. "+
Expand All @@ -38,47 +40,59 @@ var (
// workspaces and confirms that the generated BUILD files match expectation.
func TestFullGeneration(t *testing.T) {
tests := []*testtools.TestGazelleGenerationArgs{}
runfiles, err := bazel.ListRunfiles()
relativeGazelleBinary, err := runfiles.Rlocation(*gazelleBinaryPath)
if err != nil {
t.Fatalf("bazel.ListRunfiles() error: %v", err)
t.Fatalf("Failed to find gazelle binary %s. Error: %v", *gazelleBinaryPath, err)
}
// Convert workspace relative path for gazelle binary into an absolute path.
// E.g. path/to/gazelle_binary -> /absolute/path/to/workspace/path/to/gazelle/binary.
absoluteGazelleBinary, err := bazel.Runfile(*gazelleBinaryPath)
absoluteGazelleBinary, err := filepath.Abs(relativeGazelleBinary)
if err != nil {
t.Fatalf("Could not convert gazelle binary path %s to absolute path. Error: %v", *gazelleBinaryPath, err)
t.Fatalf("Could not convert gazelle binary path %s to absolute path. Error: %v", relativeGazelleBinary, err)
}
testNames := map[string]struct{}{}
for _, f := range runfiles {
// Look through runfiles for WORKSPACE or MODULE.bazel files. Each such file specifies a test case.
if filepath.Base(f.Path) == "WORKSPACE" || filepath.Base(f.Path) == "MODULE.bazel" {
// absolutePathToTestDirectory is the absolute
// path to the test case directory. For example, /home/<user>/wksp/path/to/test_data/my_test_case
absolutePathToTestDirectory := filepath.Dir(f.Path)
// relativePathToTestDirectory is the workspace relative path
// to this test case directory. For example, path/to/test_data/my_test_case
relativePathToTestDirectory := filepath.Dir(f.ShortPath)
runfilesFS, err := runfiles.New()
if err != nil {
t.Fatalf("Failed to create runfiles filesystem. Error: %v", err)
}
err = fs.WalkDir(runfilesFS, ".", func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if d.IsDir() {
return nil
}
if d.Name() == "WORKSPACE" || d.Name() == "MODULE.bazel" {
actualFilePath, err := runfiles.Rlocation(path)
if err != nil {
t.Fatalf("Failed to find runfile %s. Error: %v", path, err)
}
absolutePathToTestDirectory := filepath.Dir(actualFilePath)
// name is the name of the test directory. For example, my_test_case.
// The name of the directory doubles as the name of the test.
name := filepath.Base(absolutePathToTestDirectory)

// Don't add a test if it was already added. That could be the case if a directory has
// both a WORKSPACE and a MODULE.bazel file in it.
if _, exists := testNames[name]; exists {
continue
}
testNames[name] = struct{}{}
// both a WORKSPACE and a MODULE.bazel file in it, or due to multiple apparent names
// mapping to the same canonical name.
if _, exists := testNames[name]; !exists {
testNames[name] = struct{}{}

tests = append(tests, &testtools.TestGazelleGenerationArgs{
Name: name,
TestDataPathAbsolute: absolutePathToTestDirectory,
TestDataPathRelative: relativePathToTestDirectory,
GazelleBinaryPath: absoluteGazelleBinary,
BuildInSuffix: *buildInSuffix,
BuildOutSuffix: *buildOutSuffix,
Timeout: *timeout,
})
tests = append(tests, &testtools.TestGazelleGenerationArgs{
Name: name,
TestDataPathAbsolute: absolutePathToTestDirectory,
// Drop the repository name from the rlocationpath.
TestDataPathRelative: path[strings.IndexRune(path, '/')+1:],
GazelleBinaryPath: absoluteGazelleBinary,
BuildInSuffix: *buildInSuffix,
BuildOutSuffix: *buildOutSuffix,
Timeout: *timeout,
})
}
return fs.SkipDir
}
return nil
})
if err != nil {
t.Fatalf("Failed to walk runfiles. Error: %v", err)
}
if len(tests) == 0 {
t.Fatal("no tests found")
Expand Down
2 changes: 1 addition & 1 deletion internal/generationtest/generationtest.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def gazelle_generation_test(name, gazelle_binary, test_data, build_in_suffix = "
name = name,
embed = [Label(":generationtest_test")],
args = [
"-gazelle_binary_path=$(rootpath %s)" % gazelle_binary,
"-gazelle_binary_path=$(rlocationpath %s)" % gazelle_binary,
"-build_in_suffix=%s" % build_in_suffix,
"-build_out_suffix=%s" % build_out_suffix,
"-timeout=%ds" % gazelle_timeout_seconds,
Expand Down

0 comments on commit 4c9a320

Please sign in to comment.