Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support MODULE.bazel test cases for gazelle_generation_test. #1948

Merged
merged 2 commits into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion extend.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ The generation test expects a file structure like the following:
```
|-- <testDataPath>
|-- 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.
Expand Down
12 changes: 10 additions & 2 deletions internal/generationtest/generation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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/<user>/wksp/path/to/test_data/my_test_case
absolutePathToTestDirectory := filepath.Dir(f.Path)
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion internal/generationtest/generationtest.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def gazelle_generation_test(name, gazelle_binary, test_data, build_in_suffix = "
```
|-- <testDataPath>
|-- 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.
Expand Down
21 changes: 15 additions & 6 deletions tests/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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",
])
]))]
2 changes: 1 addition & 1 deletion tests/bazelignore/BUILD.out
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ filegroup(
testonly = True,
srcs = [
".bazelignore",
"WORKSPACE",
"MODULE.bazel",
"//sub2:all_files",
],
visibility = ["//visibility:public"],
Expand Down
File renamed without changes.