Skip to content

Commit

Permalink
Fix error when the test class is not found for a java_test at the wor…
Browse files Browse the repository at this point in the history
…kspace root

Fixes relativization of the target label with respect to the package

Fixes bazelbuild#20378

Closes bazelbuild#20383.

PiperOrigin-RevId: 586662989
Change-Id: I14f517e69a0137ab4dc232f29a5ac6c70b9a80d6
  • Loading branch information
hvadehra authored and copybara-github committed Nov 30, 2023
1 parent d7adb9a commit c49fa45
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ def _bazel_base_binary_impl(ctx, is_test_rule_class):
if test_class == "":
test_class = helper.primary_class(ctx)
if test_class == None:
fail("cannot determine test class")
fail("cannot determine test class. You might want to rename the " +
" rule or add a 'test_class' attribute.")
jvm_flags.extend([
"-ea",
"-Dbazel.test_suite=" + helper.shell_escape(test_class),
Expand Down
2 changes: 1 addition & 1 deletion src/main/starlark/builtins_bzl/common/java/java_helper.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def _primary_class(ctx):
for src in ctx.files.srcs:
if src.basename == main:
return _full_classname(_strip_extension(src))
return _full_classname(ctx.label.package + "/" + ctx.label.name)
return _full_classname(paths.get_relative(ctx.label.package, ctx.label.name))

def _strip_extension(file):
return file.dirname + "/" + (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import static com.google.common.collect.MoreCollectors.onlyElement;
import static com.google.common.truth.Truth.assertThat;
import static com.google.devtools.build.lib.testutil.TestConstants.TOOLS_REPOSITORY;
import static org.junit.Assert.assertThrows;

import com.google.common.base.Joiner;
import com.google.devtools.build.lib.analysis.ConfiguredTarget;
Expand Down Expand Up @@ -93,4 +94,15 @@ public void javaTestSetsSecurityManagerPropertyOnVersion17() throws Exception {
assertThat(jvmFlags).contains("-Djava.security.manager=allow");
}
}

// regression test for https://github.com/bazelbuild/bazel/issues/20378
@Test
public void javaTestInvalidTestClassAtRootPackage() throws Exception {
scratch.file("BUILD", "java_test(name = 'some_test', srcs = ['SomeTest.java'])");

AssertionError error =
assertThrows(AssertionError.class, () -> getConfiguredTarget("//:some_test"));

assertThat(error).hasMessageThat().contains("cannot determine test class");
}
}

0 comments on commit c49fa45

Please sign in to comment.