Skip to content

Commit

Permalink
Support MODULE.bazel test cases for gazelle_generation_test. (#1948)
Browse files Browse the repository at this point in the history
**What type of PR is this?**

Feature (testing enhancement for extension developers)

**What package or component does this PR mostly affect?**

gazelle_generation_test

**What does this PR do? Why is it needed?**

gazelle_generation_test uses the existence of a WORKSPACE file to
indicate a directory is a test case. This PR extends
gazelle_generation_test to also look for directories with a MODULE.bazel
file.

A test case is modified to check the logic.

**Which issues(s) does this PR fix?**

Fixes # 1947

**Other notes for review**

Added as part of kotlin gazelle development.
  • Loading branch information
gonzojive authored Oct 7, 2024
1 parent 27bbbc6 commit 4e16eed
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 11 deletions.
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.

0 comments on commit 4e16eed

Please sign in to comment.