Skip to content

Commit

Permalink
Fix java_binary targets in test trees (#240)
Browse files Browse the repository at this point in the history
Before they were generated with a dependency on a target which didn't
exist.

Instead, add the dependency on the -test-lib target of the
java_test_suite in their package, and mark them as testonly.

This is hopefully a pretty rare edge-case, but has been observed.
  • Loading branch information
illicitonion authored Jan 16, 2024
1 parent 8a16bb1 commit b04853d
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 2 deletions.
22 changes: 20 additions & 2 deletions java/gazelle/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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()))
Expand Down
1 change: 1 addition & 0 deletions java/gazelle/testdata/bin_in_test_tree/BUILD.in
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# gazelle:resolve java org.junit @maven//:junit_junit
1 change: 1 addition & 0 deletions java/gazelle/testdata/bin_in_test_tree/BUILD.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# gazelle:resolve java org.junit @maven//:junit_junit
Empty file.
1 change: 1 addition & 0 deletions java/gazelle/testdata/bin_in_test_tree/expectedStderr.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"level":"warn","_c":"maven-resolver","error":"open %WORKSPACEPATH%/maven_install.json: no such file or directory","message":"not loading maven dependencies"}
Original file line number Diff line number Diff line change
@@ -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"],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.example.test;

import org.junit.Test;

public class SomeOtherTest {
@Test
public void testPasses() {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.example.test;

public class SomeTestBinary {
public static void main(String[] args) {
System.out.println("Test passed!");
}
}

0 comments on commit b04853d

Please sign in to comment.