From f6b4e0a967d58e6750fb9d12f3e46f4914b47243 Mon Sep 17 00:00:00 2001 From: Brian McNamara Date: Tue, 15 Apr 2025 22:19:13 +0000 Subject: [PATCH 1/4] Remove library attributes from dependency export libraries --- java/private/create_jvm_test_suite.bzl | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/java/private/create_jvm_test_suite.bzl b/java/private/create_jvm_test_suite.bzl index c29458ad..4b897d5a 100644 --- a/java/private/create_jvm_test_suite.bzl +++ b/java/private/create_jvm_test_suite.bzl @@ -98,8 +98,7 @@ def create_jvm_test_suite( exports = deps, visibility = ["//visibility:private"], tags = tags, - testonly = True, - **library_attrs + testonly = True ) runtime_deps_lib_name = "%s-test-runtime-deps-lib" % name define_library( @@ -107,8 +106,7 @@ def create_jvm_test_suite( exports = runtime_deps, visibility = ["//visibility:private"], tags = tags, - testonly = True, - **library_attrs + testonly = True ) for src in test_srcs: From 89828e517a929ae2c8b915c528eef9ee9b6ff4e6 Mon Sep 17 00:00:00 2001 From: Brian McNamara Date: Tue, 15 Apr 2025 22:53:03 +0000 Subject: [PATCH 2/4] Add a test case --- .../contrib_rules_jvm/junit5/BUILD.bazel | 1 + .../junit5/JavaTestSuiteTest.java | 38 +++++++++++++++++++ .../resources/duplicate_resource_test.txt | 1 + 3 files changed, 40 insertions(+) create mode 100644 java/test/com/github/bazel_contrib/contrib_rules_jvm/junit5/JavaTestSuiteTest.java create mode 100644 java/test/com/github/bazel_contrib/contrib_rules_jvm/junit5/resources/duplicate_resource_test.txt 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..c3b9bb68 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 @@ -43,6 +43,7 @@ java_test_suite( artifact("org.mockito:mockito-core", "contrib_rules_jvm_tests"), artifact("org.opentest4j:opentest4j", "contrib_rules_jvm_tests"), ] + junit5_vintage_deps("contrib_rules_jvm_tests"), + resources = glob(["resources/*"]) ) java_test_suite( 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..c2965ec1 --- /dev/null +++ b/java/test/com/github/bazel_contrib/contrib_rules_jvm/junit5/JavaTestSuiteTest.java @@ -0,0 +1,38 @@ +package com.github.bazel_contrib.contrib_rules_jvm.junit5; + +import org.junit.jupiter.api.Test; + +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.atomic.AtomicInteger; +import java.util.zip.ZipFile; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.fail; + +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 From 6ce95d756391347b19fe310561179dbdb81dc122 Mon Sep 17 00:00:00 2001 From: Brian McNamara Date: Fri, 18 Apr 2025 21:23:05 +0000 Subject: [PATCH 3/4] Filter out duplicate resources --- java/private/create_jvm_test_suite.bzl | 3 +++ .../contrib_rules_jvm/junit5/JavaTestSuiteTest.java | 1 - 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/java/private/create_jvm_test_suite.bzl b/java/private/create_jvm_test_suite.bzl index 4b897d5a..d9821179 100644 --- a/java/private/create_jvm_test_suite.bzl +++ b/java/private/create_jvm_test_suite.bzl @@ -88,6 +88,9 @@ def create_jvm_test_suite( ) if not _contains_label(deps or [], lib_dep_label): deps.append(lib_dep_label) + if "resources" in library_attrs: + # Prevent duplicate resources from appearing in classpath + kwargs.pop("resources") tests = [] 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 index c2965ec1..d3085233 100644 --- 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 @@ -6,7 +6,6 @@ import java.io.IOException; import java.util.ArrayList; import java.util.List; -import java.util.concurrent.atomic.AtomicInteger; import java.util.zip.ZipFile; import static org.junit.jupiter.api.Assertions.assertEquals; From 64321ee64e815b5c76f2a840f8df7703fea78469 Mon Sep 17 00:00:00 2001 From: Brian McNamara Date: Fri, 18 Apr 2025 21:44:14 +0000 Subject: [PATCH 4/4] Formatter --- java/private/create_jvm_test_suite.bzl | 4 +- .../contrib_rules_jvm/junit5/BUILD.bazel | 2 +- .../junit5/JavaTestSuiteTest.java | 53 +++++++++++-------- 3 files changed, 33 insertions(+), 26 deletions(-) diff --git a/java/private/create_jvm_test_suite.bzl b/java/private/create_jvm_test_suite.bzl index d9821179..5aabec47 100644 --- a/java/private/create_jvm_test_suite.bzl +++ b/java/private/create_jvm_test_suite.bzl @@ -101,7 +101,7 @@ def create_jvm_test_suite( exports = deps, visibility = ["//visibility:private"], tags = tags, - testonly = True + testonly = True, ) runtime_deps_lib_name = "%s-test-runtime-deps-lib" % name define_library( @@ -109,7 +109,7 @@ def create_jvm_test_suite( exports = runtime_deps, visibility = ["//visibility:private"], tags = tags, - testonly = True + testonly = True, ) for src in test_srcs: 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 c3b9bb68..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 = [ @@ -43,7 +44,6 @@ java_test_suite( artifact("org.mockito:mockito-core", "contrib_rules_jvm_tests"), artifact("org.opentest4j:opentest4j", "contrib_rules_jvm_tests"), ] + junit5_vintage_deps("contrib_rules_jvm_tests"), - resources = glob(["resources/*"]) ) java_test_suite( 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 index d3085233..c6cd5f51 100644 --- 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 @@ -1,37 +1,44 @@ package com.github.bazel_contrib.contrib_rules_jvm.junit5; -import org.junit.jupiter.api.Test; +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 static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.fail; +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"); - } + @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); + } + }); } - assertEquals(1, resourcesFound.size(), - "Expected a single instance of a resource to be on the classpath, instead found entries in: " + resourcesFound); + } 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); + } }