diff --git a/examples/tests_and_lints/MODULE.bazel b/examples/tests_and_lints/MODULE.bazel index a10b6fec..69ef1f2c 100644 --- a/examples/tests_and_lints/MODULE.bazel +++ b/examples/tests_and_lints/MODULE.bazel @@ -47,3 +47,8 @@ use_repo( maven, "maven", ) + +register_toolchains( + "@@bazel_skylib//toolchains/unittest:cmd_toolchain", + "@@bazel_skylib//toolchains/unittest:bash_toolchain", +) diff --git a/examples/tests_and_lints/src/test/com/github/bazel_contrib/contrib_rules_jvm/examples/tests_and_lints/BUILD.bazel b/examples/tests_and_lints/src/test/com/github/bazel_contrib/contrib_rules_jvm/examples/tests_and_lints/BUILD.bazel index 1e96685a..5635853e 100644 --- a/examples/tests_and_lints/src/test/com/github/bazel_contrib/contrib_rules_jvm/examples/tests_and_lints/BUILD.bazel +++ b/examples/tests_and_lints/src/test/com/github/bazel_contrib/contrib_rules_jvm/examples/tests_and_lints/BUILD.bazel @@ -1,5 +1,6 @@ load("@contrib_rules_jvm//java:defs.bzl", "checkstyle_test", "java_junit5_test") load("@rules_jvm_external//:defs.bzl", "artifact") +load(":java_junit5_test_test_suite.bzl", "java_junit5_test_test_suite") java_junit5_test( name = "ExampleTest", @@ -16,6 +17,10 @@ java_junit5_test( ], ) +java_junit5_test_test_suite( + name = "java_junit5_test_test", +) + checkstyle_test( name = "example_test_checkstyle", srcs = ["ExampleTest.java"], diff --git a/examples/tests_and_lints/src/test/com/github/bazel_contrib/contrib_rules_jvm/examples/tests_and_lints/java_junit5_test_test_suite.bzl b/examples/tests_and_lints/src/test/com/github/bazel_contrib/contrib_rules_jvm/examples/tests_and_lints/java_junit5_test_test_suite.bzl new file mode 100644 index 00000000..945d6214 --- /dev/null +++ b/examples/tests_and_lints/src/test/com/github/bazel_contrib/contrib_rules_jvm/examples/tests_and_lints/java_junit5_test_test_suite.bzl @@ -0,0 +1,108 @@ +load("@bazel_skylib//lib:unittest.bzl", "analysistest", "asserts") +load("@contrib_rules_jvm//java:defs.bzl", "java_junit5_test", "junit5_jvm_flags") +load("@rules_java//java:java_test.bzl", "java_test") + +TargetInfo = provider( + doc = "Information relating to the target under test.", + fields = ["attr"], +) + +def _target_info_aspect_impl(target, ctx): + return TargetInfo( + attr = ctx.rule.attr, + ) + +target_info_aspect = aspect( + implementation = _target_info_aspect_impl, +) + +def _attr_string_value_test_impl(ctx): + env = analysistest.begin(ctx) + target_under_test = analysistest.target_under_test(env) + + asserts.equals(env, ctx.attr.check_value, getattr(target_under_test[TargetInfo].attr, ctx.attr.check_name)) + + return analysistest.end(env) + +attr_string_value_test = analysistest.make( + _attr_string_value_test_impl, + extra_target_under_test_aspects = [target_info_aspect], + attrs = { + "check_name": attr.string(mandatory = True), + "check_value": attr.string(mandatory = True), + }, +) + +def _attr_string_list_value_test_impl(ctx): + env = analysistest.begin(ctx) + target_under_test = analysistest.target_under_test(env) + + asserts.equals(env, ctx.attr.check_value, getattr(target_under_test[TargetInfo].attr, ctx.attr.check_name)) + + return analysistest.end(env) + +attr_string_list_value_test = analysistest.make( + _attr_string_list_value_test_impl, + extra_target_under_test_aspects = [target_info_aspect], + attrs = { + "check_name": attr.string(mandatory = True), + "check_value": attr.string_list(mandatory = True), + }, +) + +def custom_junit5_test(name, **kwargs): + jvm_flags = junit5_jvm_flags( + jvm_flags = kwargs.pop("jvm_flags", []), + include_tags = kwargs.pop("include_tags", []), + exclude_tags = kwargs.pop("exclude_tags", []), + include_engines = kwargs.pop("include_engines", []), + exclude_engines = kwargs.pop("exclude_engines", []), + ) + + java_test( + name = name, + main_class = "com.example.CustomMainClass", + jvm_flags = jvm_flags, + **kwargs + ) + +def java_junit5_test_test_suite(name): + java_junit5_test( + name = "StandardMainClassTest", + tags = ["manual"], + ) + + custom_junit5_test( + name = "CustomMainClassTest", + include_tags = ["include_junit5_test"], + tags = ["manual"], + ) + + attr_string_value_test( + name = "custom_main_class_test", + target_under_test = ":CustomMainClassTest", + check_name = "main_class", + check_value = "com.example.CustomMainClass", + ) + + attr_string_list_value_test( + name = "custom_jvm_flags_test", + target_under_test = ":CustomMainClassTest", + check_name = "jvm_flags", + check_value = ["-DJUNIT5_INCLUDE_TAGS=include_junit5_test", "-Djava.security.manager=allow"], + ) + + attr_string_value_test( + name = "standard_main_class_test", + target_under_test = ":StandardMainClassTest", + check_name = "main_class", + check_value = "com.github.bazel_contrib.contrib_rules_jvm.junit5.JUnit5Runner", + ) + + native.test_suite( + name = name, + tests = [ + ":custom_main_class_test", + ":standard_main_class_test", + ], + ) diff --git a/java/defs.bzl b/java/defs.bzl index 6b834742..3b2de294 100644 --- a/java/defs.bzl +++ b/java/defs.bzl @@ -9,9 +9,11 @@ load("//java/private:java_test_suite.bzl", _java_test_suite = "java_test_suite") load( "//java/private:junit5.bzl", _JUNIT5_DEPS = "JUNIT5_DEPS", + _JUNIT5_RUNTIME_DEPS = "JUNIT5_RUNTIME_DEPS", _JUNIT5_VINTAGE_DEPS = "JUNIT5_VINTAGE_DEPS", _java_junit5_test = "java_junit5_test", _junit5_deps = "junit5_deps", + _junit5_jvm_flags = "junit5_jvm_flags", _junit5_vintage_deps = "junit5_vintage_deps", ) load( @@ -41,8 +43,10 @@ java_junit5_test = _java_junit5_test java_test = _java_test java_test_suite = _java_test_suite junit5_deps = _junit5_deps +junit5_jvm_flags = _junit5_jvm_flags junit5_vintage_deps = _junit5_vintage_deps JUNIT5_DEPS = _JUNIT5_DEPS +JUNIT5_RUNTIME_DEPS = _JUNIT5_RUNTIME_DEPS JUNIT5_VINTAGE_DEPS = _JUNIT5_VINTAGE_DEPS pmd_binary = _pmd_binary pmd_ruleset = _pmd_ruleset diff --git a/java/private/junit5.bzl b/java/private/junit5.bzl index b1082276..cb4a92dc 100644 --- a/java/private/junit5.bzl +++ b/java/private/junit5.bzl @@ -2,6 +2,10 @@ load("@rules_jvm_external//:defs.bzl", "DEFAULT_REPOSITORY_NAME", "artifact") load("//java/private:library.bzl", "java_test") load("//java/private:package.bzl", "get_package_name") +JUNIT5_RUNTIME_DEPS = [ + "@contrib_rules_jvm//java/src/com/github/bazel_contrib/contrib_rules_jvm/junit5", +] + def junit5_deps(repository_name = DEFAULT_REPOSITORY_NAME): return [ artifact("org.junit.jupiter:junit-jupiter-engine", repository_name), @@ -14,6 +18,28 @@ def junit5_vintage_deps(repository_name = DEFAULT_REPOSITORY_NAME): artifact("org.junit.vintage:junit-vintage-engine", repository_name), ] +def junit5_jvm_flags(jvm_flags, include_tags = [], exclude_tags = [], include_engines = [], exclude_engines = []): + if include_tags: + jvm_flags = jvm_flags + ["-DJUNIT5_INCLUDE_TAGS=" + ",".join(include_tags)] + + if exclude_tags: + jvm_flags = jvm_flags + ["-DJUNIT5_EXCLUDE_TAGS=" + ",".join(exclude_tags)] + + if include_engines: + jvm_flags = jvm_flags + ["-DJUNIT5_INCLUDE_ENGINES=%s" % ",".join(include_engines)] + + if exclude_engines: + jvm_flags = jvm_flags + ["-DJUNIT5_EXCLUDE_ENGINES=%s" % ",".join(exclude_engines)] + + security_manager_flag_seen = False + for flag in jvm_flags: + if flag.startswith("-Djava.security.manager="): + security_manager_flag_seen = True + if not security_manager_flag_seen: + jvm_flags = jvm_flags + ["-Djava.security.manager=allow"] + + return jvm_flags + """Dependencies typically required by JUnit 5 tests. See `java_junit5_test` for more details. @@ -79,32 +105,13 @@ def java_junit5_test( else: clazz = get_package_name(package_prefixes) + name - if include_tags: - jvm_flags = jvm_flags + ["-DJUNIT5_INCLUDE_TAGS=" + ",".join(include_tags)] - - if exclude_tags: - jvm_flags = jvm_flags + ["-DJUNIT5_EXCLUDE_TAGS=" + ",".join(exclude_tags)] - - if include_engines: - jvm_flags = jvm_flags + ["-DJUNIT5_INCLUDE_ENGINES=%s" % ",".join(include_engines)] - - if exclude_engines: - jvm_flags = jvm_flags + ["-DJUNIT5_EXCLUDE_ENGINES=%s" % ",".join(exclude_engines)] - - security_manager_flag_seen = False - for flag in jvm_flags: - if flag.startswith("-Djava.security.manager="): - security_manager_flag_seen = True - if not security_manager_flag_seen: - jvm_flags = jvm_flags + ["-Djava.security.manager=allow"] + jvm_flags = junit5_jvm_flags(jvm_flags, include_tags, exclude_tags, include_engines, exclude_engines) java_test( name = name, main_class = "com.github.bazel_contrib.contrib_rules_jvm.junit5.JUnit5Runner", test_class = clazz, - runtime_deps = runtime_deps + [ - "@contrib_rules_jvm//java/src/com/github/bazel_contrib/contrib_rules_jvm/junit5", - ], + runtime_deps = runtime_deps + JUNIT5_RUNTIME_DEPS, jvm_flags = jvm_flags, **kwargs )