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

gazelle: Fix java_binary targets in test trees #240

Merged
merged 1 commit into from
Jan 16, 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
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!");
}
}