diff --git a/java/private/create_jvm_test_suite.bzl b/java/private/create_jvm_test_suite.bzl index f318a0f5..8c37a9aa 100644 --- a/java/private/create_jvm_test_suite.bzl +++ b/java/private/create_jvm_test_suite.bzl @@ -89,6 +89,9 @@ def create_jvm_test_suite( ) if not _contains_label(deps or [], lib_dep_label): deps = deps + [lib_dep_label] + if "resources" in library_attrs: + # Prevent duplicate resources from appearing in classpath + kwargs.pop("resources") tests = [] @@ -100,7 +103,6 @@ def create_jvm_test_suite( visibility = ["//visibility:private"], tags = tags, testonly = True, - **library_attrs ) runtime_deps_lib_name = "%s-test-runtime-deps-lib" % name define_library( @@ -109,7 +111,6 @@ def create_jvm_test_suite( visibility = ["//visibility:private"], tags = tags, testonly = True, - **library_attrs ) # Get any deps referenced in make vars diff --git a/java/test/com/github/bazel_contrib/contrib_rules_jvm/junit5/BUILD.bazel b/java/test/com/github/bazel_contrib/contrib_rules_jvm/junit5/BUILD.bazel index 76294bd8..699d8929 100644 --- a/java/test/com/github/bazel_contrib/contrib_rules_jvm/junit5/BUILD.bazel +++ b/java/test/com/github/bazel_contrib/contrib_rules_jvm/junit5/BUILD.bazel @@ -30,6 +30,7 @@ java_test_suite( ), exclude_engines = ["junit-vintage"], include_engines = ["junit-jupiter"], + resources = glob(["resources/*"]), runner = "junit5", test_suffixes_excludes = ["BaseTest.java"], deps = [ diff --git a/java/test/com/github/bazel_contrib/contrib_rules_jvm/junit5/JavaTestSuiteTest.java b/java/test/com/github/bazel_contrib/contrib_rules_jvm/junit5/JavaTestSuiteTest.java new file mode 100644 index 00000000..c6cd5f51 --- /dev/null +++ b/java/test/com/github/bazel_contrib/contrib_rules_jvm/junit5/JavaTestSuiteTest.java @@ -0,0 +1,44 @@ +package com.github.bazel_contrib.contrib_rules_jvm.junit5; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.fail; + +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import java.util.zip.ZipFile; +import org.junit.jupiter.api.Test; + +public class JavaTestSuiteTest { + + @Test + public void testClasspathDoesNotContainDuplicateResources() throws IOException { + final String resourceName = "duplicate_resource_test.txt"; + String[] classpath = System.getProperty("java.class.path").split(":"); + List resourcesFound = new ArrayList<>(); + for (String entry : classpath) { + if (entry.endsWith(".jar")) { + try (ZipFile zipfile = new ZipFile(new File(entry))) { + zipfile + .entries() + .asIterator() + .forEachRemaining( + zipEntry -> { + if (zipEntry.getName().endsWith(resourceName)) { + resourcesFound.add(entry); + } + }); + } + } else { + fail("Classpath contains non-jar files"); + } + } + assertEquals( + 1, + resourcesFound.size(), + "Expected a single instance of a resource to be on the classpath, instead found entries in:" + + " " + + resourcesFound); + } +} diff --git a/java/test/com/github/bazel_contrib/contrib_rules_jvm/junit5/resources/duplicate_resource_test.txt b/java/test/com/github/bazel_contrib/contrib_rules_jvm/junit5/resources/duplicate_resource_test.txt new file mode 100644 index 00000000..6732ef43 --- /dev/null +++ b/java/test/com/github/bazel_contrib/contrib_rules_jvm/junit5/resources/duplicate_resource_test.txt @@ -0,0 +1 @@ +Simple resource to validate suites only pick up a single resource \ No newline at end of file