diff --git a/extend.md b/extend.md index 29d97951e..b7c272b9c 100644 --- a/extend.md +++ b/extend.md @@ -152,7 +152,7 @@ The generation test expects a file structure like the following: ``` |-- |-- some_test - |-- WORKSPACE + |-- WORKSPACE and/or MODULE.bazel -> Indicates the directory is a test case. |-- README.md --> README describing what the test does. |-- arguments.txt --> newline delimited list of arguments to pass in (ignored if empty). |-- expectedStdout.txt --> Expected stdout for this test. diff --git a/internal/generationtest/generation_test.go b/internal/generationtest/generation_test.go index 55d1b549c..104a9c2f2 100644 --- a/internal/generationtest/generation_test.go +++ b/internal/generationtest/generation_test.go @@ -48,9 +48,10 @@ func TestFullGeneration(t *testing.T) { if err != nil { t.Fatalf("Could not convert gazelle binary path %s to absolute path. Error: %v", *gazelleBinaryPath, err) } + testNames := map[string]struct{}{} for _, f := range runfiles { - // Look through runfiles for WORKSPACE files. Each WORKSPACE is a test case. - if filepath.Base(f.Path) == "WORKSPACE" { + // 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//wksp/path/to/test_data/my_test_case absolutePathToTestDirectory := filepath.Dir(f.Path) @@ -61,6 +62,13 @@ func TestFullGeneration(t *testing.T) { // 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{}{} + tests = append(tests, &testtools.TestGazelleGenerationArgs{ Name: name, TestDataPathAbsolute: absolutePathToTestDirectory, diff --git a/internal/generationtest/generationtest.bzl b/internal/generationtest/generationtest.bzl index 303140645..023c9ea70 100644 --- a/internal/generationtest/generationtest.bzl +++ b/internal/generationtest/generationtest.bzl @@ -27,7 +27,7 @@ def gazelle_generation_test(name, gazelle_binary, test_data, build_in_suffix = " ``` |-- |-- some_test - |-- WORKSPACE + |-- WORKSPACE and/or MODULE.bazel -> Indicates the directory is a test case. |-- README.md --> README describing what the test does. |-- arguments.txt --> newline delimited list of arguments to pass in (ignored if empty). |-- expectedStdout.txt --> Expected stdout for this test. diff --git a/tests/BUILD.bazel b/tests/BUILD.bazel index 15cf8d51a..2204386d6 100644 --- a/tests/BUILD.bazel +++ b/tests/BUILD.bazel @@ -4,6 +4,8 @@ load( "gazelle_generation_test", ) load("//tests:tools.bzl", "get_binary") +load("@bazel_skylib//lib:paths.bzl", "paths") +load("@bazel_skylib//lib:sets.bzl", "sets") # Exclude this entire directly from having anything gnerated by Gazelle. That # way the test cases won't be fixed by `bazel run //:gazelle` when run in this @@ -46,14 +48,21 @@ gazelle_binary( [gazelle_generation_test( # Name the test the path to the directory containing the WORKSPACE file. - name = file[0:-len("/WORKSPACE")], - gazelle_binary = get_binary(file), + name = test_dir, + gazelle_binary = get_binary(test_dir), # This is a noop as the default is False. However, it does confirm that # gazelle_generation_test accepts setting common test attributes. local = False, test_data = glob( - include = [file[0:-len("/WORKSPACE")] + "/**"], + include = [test_dir + "/**"], ), -) for file in glob( - include = ["**/WORKSPACE"], -)] +) for test_dir in sets.to_list(sets.make([ + paths.dirname(p) + # Note that glob matches "this package's directories and non-subpackage + # subdirectories," so any directory with a BUILD or BUILD.bazel file + # will not match, but those with BUILD.in and BUILD.out will. + for p in glob([ + "**/WORKSPACE", + "**/MODULE.bazel", + ]) + ]))] diff --git a/tests/bazelignore/BUILD.out b/tests/bazelignore/BUILD.out index fa5b576e4..4099b4981 100644 --- a/tests/bazelignore/BUILD.out +++ b/tests/bazelignore/BUILD.out @@ -3,7 +3,7 @@ filegroup( testonly = True, srcs = [ ".bazelignore", - "WORKSPACE", + "MODULE.bazel", "//sub2:all_files", ], visibility = ["//visibility:public"], diff --git a/tests/bazelignore/WORKSPACE b/tests/bazelignore/MODULE.bazel similarity index 100% rename from tests/bazelignore/WORKSPACE rename to tests/bazelignore/MODULE.bazel