File tree Expand file tree Collapse file tree 6 files changed +73
-2
lines changed
test/src/main/scala/scalarules/test/java_classpath_order Expand file tree Collapse file tree 6 files changed +73
-2
lines changed Original file line number Diff line number Diff line change @@ -27,6 +27,11 @@ scala_toolchain(
2727 name = "minimal_direct_source_deps_impl" ,
2828 dependency_mode = "plus-one" ,
2929 dependency_tracking_method = "ast" ,
30+ # Exclude the shadowing test package from strict-deps to allow classpath-order coverage without disabling strict-deps globally in CI runs.
31+ dependency_tracking_strict_deps_patterns = [
32+ "" , # keep default include
33+ "-//test/src/main/scala/scalarules/test/java_classpath_order" ,
34+ ],
3035 strict_deps_mode = "error" ,
3136 unused_dependency_checker_mode = "error" ,
3237)
Original file line number Diff line number Diff line change @@ -251,10 +251,10 @@ def _compile_or_empty(
251251
252252 full_jars = [ctx .outputs .jar ]
253253 if java_jar :
254- full_jars .append ( java_jar .jar )
254+ full_jars .insert ( 0 , java_jar .jar )
255255
256256 if java_jar :
257- merged_provider = java_common .merge ([scala_compilation_provider , java_jar .java_compilation_provider ])
257+ merged_provider = java_common .merge ([java_jar .java_compilation_provider , scala_compilation_provider ])
258258 else :
259259 merged_provider = scala_compilation_provider
260260
Original file line number Diff line number Diff line change 1+ load ("//scala:scala.bzl" , "scala_library" , "scala_test" )
2+
3+ # Original library with the class to be shadowed
4+ scala_library (
5+ name = "original" ,
6+ srcs = ["Original.scala" ],
7+ )
8+
9+ # Library that shadows the Overridable class with a Java file
10+ # The Java file (Overridable.java) should take precedence over
11+ # the Overridable class from the :original dependency
12+ scala_library (
13+ name = "with_java_override" ,
14+ srcs = ["Overridable.java" ],
15+ deps = [":original" ],
16+ )
17+
18+ # Test that verifies the Java override works correctly
19+ # Note: This test is incompatible with strict deps checking because the checker
20+ # doesn't understand class shadowing - it sees Overridable as coming from :original
21+ # rather than the shadowed version from :with_java_override
22+ scala_test (
23+ name = "verify_override_test" ,
24+ srcs = ["VerifyOverride.scala" ],
25+ unused_dependency_checker_mode = "off" ,
26+ deps = [":with_java_override" ],
27+ )
Original file line number Diff line number Diff line change 1+ package scalarules .test .java_classpath_order
2+
3+ class Overridable {
4+ def getValue (): String = " original"
5+ }
6+
Original file line number Diff line number Diff line change 1+ package scalarules .test .java_classpath_order ;
2+
3+ public class Overridable {
4+ public String getValue () {
5+ return "overridden" ;
6+ }
7+ }
8+
Original file line number Diff line number Diff line change 1+ package scalarules .test .java_classpath_order
2+
3+ import org .scalatest .flatspec .AnyFlatSpec
4+ import org .scalatest .matchers .should .Matchers
5+
6+ /**
7+ * This test verifies that the Java class (Overridable) shadows
8+ * the Scala class from the dependency.
9+ *
10+ * The expected behavior is:
11+ * - The Java file compiled in the scala_library with_java_override should
12+ * appear first on the classpath
13+ * - When instantiating Overridable, we should get the Java version
14+ * which returns "overridden"
15+ */
16+ class VerifyOverrideTest extends AnyFlatSpec with Matchers {
17+
18+ " Java class in scala_library" should " shadow the dependency's class" in {
19+ val instance = new Overridable ()
20+ val value = instance.getValue()
21+
22+ value shouldBe " overridden"
23+ }
24+ }
25+
You can’t perform that action at this time.
0 commit comments