Skip to content
Open
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
4 changes: 3 additions & 1 deletion java/private/create_jvm_test_suite.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def create_jvm_test_suite(
size = None,
package_prefixes = [],
test_suffixes_excludes = [],
test_name_prefix = "",
**kwargs):
"""Generate a test suite for rules that "feel" like `java_test`.

Expand All @@ -51,6 +52,7 @@ def create_jvm_test_suite(
srcs: A list of source files.
test_suffixes: A list of suffixes (eg. `["Test.kt"]`)
test_suffixes_excludes: A list of suffix excludes (eg. `["BaseTest.kt"]`)
test_name_prefix: Optional prefix for generated test names to avoid conflict between multiple suites.
package: The package name to use. If `None`, a value will be
calculated from the bazel package.
library_attributes: Attributes to pass to `define_library`.
Expand Down Expand Up @@ -122,7 +124,7 @@ def create_jvm_test_suite(

for src in test_srcs:
suffix = src.rfind(".")
test_name = src[:suffix]
test_name = test_name_prefix + src[:suffix]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The current implementation for test_name can lead to invalid Bazel target names. The src variable can contain slashes if source files are in subdirectories (e.g., from a glob like **/*.java). A target name with a slash is invalid and will cause a build failure.

To fix this, you should sanitize the generated test name by replacing slashes with another character, like an underscore. This will also make the rule more robust against a test_name_prefix that might contain slashes.

Suggested change
test_name = test_name_prefix + src[:suffix]
test_name = (test_name_prefix + src[:suffix]).replace("/", "_")

test_class = get_class_name(package, src, package_prefixes)

test_name = define_test(
Expand Down