Skip to content

Commit

Permalink
fix(bazel-contrib#104): Allow custom package prefixes (bazel-contrib#121
Browse files Browse the repository at this point in the history
)
  • Loading branch information
hjellek authored Jan 11, 2023
1 parent e470d8b commit f0a9a36
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 8 deletions.
3 changes: 2 additions & 1 deletion java/private/create_jvm_test_suite.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ def create_jvm_test_suite(
tags = [],
visibility = None,
size = None,
package_prefixes = [],
**kwargs):
"""Generate a test suite for rules that "feel" like `java_test`.
Expand Down Expand Up @@ -92,7 +93,7 @@ def create_jvm_test_suite(
suffix = src.rfind(".")
test_name = src[:suffix]
tests.append(test_name)
test_class = get_class_name(package, src)
test_class = get_class_name(package, src, package_prefixes)

define_test(
name = test_name,
Expand Down
4 changes: 2 additions & 2 deletions java/private/junit5.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ JUNIT5_DEPS = junit5_deps()

JUNIT5_VINTAGE_DEPS = junit5_vintage_deps()

def java_junit5_test(name, test_class = None, runtime_deps = [], **kwargs):
def java_junit5_test(name, test_class = None, runtime_deps = [], package_prefixes = [], **kwargs):
"""Run junit5 tests using Bazel.
This is designed to be a drop-in replacement for `java_test`, but
Expand Down Expand Up @@ -51,7 +51,7 @@ def java_junit5_test(name, test_class = None, runtime_deps = [], **kwargs):
if test_class:
clazz = test_class
else:
clazz = get_package_name() + name
clazz = get_package_name(package_prefixes) + name

java_test(
name = name,
Expand Down
15 changes: 10 additions & 5 deletions java/private/package.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,28 @@ _PREFIXES = (".com.", ".org.", ".net.", ".io.", ".ai.", ".co.", ".me.")
# By default bazel computes the name of test classes based on the
# standard Maven directory structure, which we may not always use,
# so try to compute the correct package name.
def get_package_name():
def get_package_name(prefixes = []):
pkg = native.package_name().replace("/", ".")
if len(prefixes) == 0:
prefixes = _PREFIXES

for prefix in _PREFIXES:
for prefix in prefixes:
idx = pkg.find(prefix)
if idx != -1:
return pkg[idx + 1:] + "."

return ""

# Converts a file name into what is hopefully a valid class name.
def get_class_name(package, src):
def get_class_name(package, src, prefixes = []):
# Strip the suffix from the source
idx = src.rindex(".")
name = src[:idx].replace("/", ".")

for prefix in _PREFIXES:
if len(prefixes) == 0:
prefixes = _PREFIXES

for prefix in prefixes:
idx = name.find(prefix)
if idx != -1:
return name[idx + 1:]
Expand All @@ -29,7 +34,7 @@ def get_class_name(package, src):
# safe to add the class name. While `get_package_name` does
# the right thing, the parameter passed by a user may not
# so we shall check once we have `pkg` just to be safe.
pkg = package if package else get_package_name()
pkg = package if package else get_package_name(prefixes)
if len(pkg) and not pkg.endswith("."):
pkg = pkg + "."

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
load("@rules_jvm_external//:defs.bzl", "artifact")
load("//java:defs.bzl", "java_test_suite", "junit5_deps")

# Ignore this directory because of the wrong package name.
# gazelle:ignore

PACKAGE_PREFIX_NAME_TEST = [
"CustomPackageNameTest.java",
]

# Test that we can set known package prefixes. We do this by
# setting the `package_prefixes` property, overriding the
# assumed known prefixes
java_test_suite(
name = "custom-prefix-tests",
size = "small",
srcs = PACKAGE_PREFIX_NAME_TEST,
package_prefixes = [".custom."],
runner = "junit5",
deps = [
artifact("org.junit.jupiter:junit-jupiter-api", "contrib_rules_jvm_tests"),
] + junit5_deps("contrib_rules_jvm_tests"),
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Note: the package name is not the same as the directory name for a reason.
// See this package's `BUILD.bazel` file for more information
package custom.github.bazel_contrib.contrib_rules_jvm;

import org.junit.jupiter.api.Test;

public class CustomPackageNameTest {

@Test
void shouldBeAbleToBeRun() {
// This test does nothing
}
}

0 comments on commit f0a9a36

Please sign in to comment.