diff --git a/java/gazelle/generate.go b/java/gazelle/generate.go index 02658f33..cafb6ce9 100644 --- a/java/gazelle/generate.go +++ b/java/gazelle/generate.go @@ -190,8 +190,22 @@ func (l javaLang) GenerateRules(args language.GenerateArgs) language.GenerateRes l.generateJavaLibrary(args.File, args.Rel, filepath.Base(args.Rel), productionJavaFiles.SortedSlice(), allPackageNames, nonLocalProductionJavaImports, nonLocalJavaExports, false, javaLibraryKind, &res) } + var testHelperJavaClasses *sorted_set.SortedSet[types.ClassName] for _, m := range allMains.SortedSlice() { - l.generateJavaBinary(args.File, m, filepath.Base(args.Rel), &res) + // Lazily populate because java_binaries are pretty rare + if testHelperJavaClasses == nil { + testHelperJavaClasses = sorted_set.NewSortedSetFn[types.ClassName]([]types.ClassName{}, types.ClassNameLess) + for _, testHelperJavaFile := range testHelperJavaFiles.SortedSlice() { + testHelperJavaClasses.Add(*testHelperJavaFile.ClassName()) + } + } + isTestOnly := false + libName := filepath.Base(args.Rel) + if testHelperJavaClasses.Contains(m) { + isTestOnly = true + libName += "-test-lib" + } + l.generateJavaBinary(args.File, m, libName, isTestOnly, &res) } // We add special packages to point to testonly libraries which - this accumulates them, @@ -450,12 +464,16 @@ func (l javaLang) generateJavaLibrary(file *rule.File, pathToPackageRelativeToBa res.Imports = append(res.Imports, resolveInput) } -func (l javaLang) generateJavaBinary(file *rule.File, m types.ClassName, libName string, res *language.GenerateResult) { +func (l javaLang) generateJavaBinary(file *rule.File, m types.ClassName, libName string, testonly bool, res *language.GenerateResult) { const ruleKind = "java_binary" name := m.BareOuterClassName() r := rule.NewRule("java_binary", name) // FIXME check collision on name r.SetAttr("main_class", m.FullyQualifiedClassName()) + if testonly { + r.SetAttr("testonly", true) + } + runtimeDeps := l.collectRuntimeDeps(ruleKind, name, file) runtimeDeps.Add(label.Label{Name: libName, Relative: true}) r.SetAttr("runtime_deps", labelsToStrings(runtimeDeps.SortedSlice())) diff --git a/java/gazelle/testdata/bin_in_test_tree/BUILD.in b/java/gazelle/testdata/bin_in_test_tree/BUILD.in new file mode 100644 index 00000000..58876c2f --- /dev/null +++ b/java/gazelle/testdata/bin_in_test_tree/BUILD.in @@ -0,0 +1 @@ +# gazelle:resolve java org.junit @maven//:junit_junit diff --git a/java/gazelle/testdata/bin_in_test_tree/BUILD.out b/java/gazelle/testdata/bin_in_test_tree/BUILD.out new file mode 100644 index 00000000..58876c2f --- /dev/null +++ b/java/gazelle/testdata/bin_in_test_tree/BUILD.out @@ -0,0 +1 @@ +# gazelle:resolve java org.junit @maven//:junit_junit diff --git a/java/gazelle/testdata/bin_in_test_tree/WORKSPACE b/java/gazelle/testdata/bin_in_test_tree/WORKSPACE new file mode 100644 index 00000000..e69de29b diff --git a/java/gazelle/testdata/bin_in_test_tree/expectedStderr.txt b/java/gazelle/testdata/bin_in_test_tree/expectedStderr.txt new file mode 100644 index 00000000..b3cb3407 --- /dev/null +++ b/java/gazelle/testdata/bin_in_test_tree/expectedStderr.txt @@ -0,0 +1 @@ +{"level":"warn","_c":"maven-resolver","error":"open %WORKSPACEPATH%/maven_install.json: no such file or directory","message":"not loading maven dependencies"} diff --git a/java/gazelle/testdata/bin_in_test_tree/src/test/java/com/example/test/BUILD.out b/java/gazelle/testdata/bin_in_test_tree/src/test/java/com/example/test/BUILD.out new file mode 100644 index 00000000..23726467 --- /dev/null +++ b/java/gazelle/testdata/bin_in_test_tree/src/test/java/com/example/test/BUILD.out @@ -0,0 +1,19 @@ +load("@rules_java//java:defs.bzl", "java_binary") +load("@contrib_rules_jvm//java:defs.bzl", "java_test_suite") + +java_binary( + name = "SomeTestBinary", + testonly = True, + main_class = "com.example.test.SomeTestBinary", + visibility = ["//visibility:public"], + runtime_deps = [":test-test-lib"], +) + +java_test_suite( + name = "test", + srcs = [ + "SomeOtherTest.java", + "SomeTestBinary.java", + ], + deps = ["@maven//:junit_junit"], +) diff --git a/java/gazelle/testdata/bin_in_test_tree/src/test/java/com/example/test/SomeOtherTest.java b/java/gazelle/testdata/bin_in_test_tree/src/test/java/com/example/test/SomeOtherTest.java new file mode 100644 index 00000000..87fd448f --- /dev/null +++ b/java/gazelle/testdata/bin_in_test_tree/src/test/java/com/example/test/SomeOtherTest.java @@ -0,0 +1,8 @@ +package com.example.test; + +import org.junit.Test; + +public class SomeOtherTest { + @Test + public void testPasses() {} +} diff --git a/java/gazelle/testdata/bin_in_test_tree/src/test/java/com/example/test/SomeTestBinary.java b/java/gazelle/testdata/bin_in_test_tree/src/test/java/com/example/test/SomeTestBinary.java new file mode 100644 index 00000000..d67ce509 --- /dev/null +++ b/java/gazelle/testdata/bin_in_test_tree/src/test/java/com/example/test/SomeTestBinary.java @@ -0,0 +1,7 @@ +package com.example.test; + +public class SomeTestBinary { + public static void main(String[] args) { + System.out.println("Test passed!"); + } +}