From 733d4b8c5bd37788b5914ccd8a12f20f1f1e925d Mon Sep 17 00:00:00 2001 From: Win Wang Date: Mon, 4 Apr 2022 17:03:22 -0400 Subject: [PATCH 01/10] Single-jar pack_sources should conform to Bazel behavior --- scala/private/phases/phase_compile.bzl | 36 +++++++++++++++++++------- 1 file changed, 26 insertions(+), 10 deletions(-) diff --git a/scala/private/phases/phase_compile.bzl b/scala/private/phases/phase_compile.bzl index b184f7b39..d59b6ad52 100644 --- a/scala/private/phases/phase_compile.bzl +++ b/scala/private/phases/phase_compile.bzl @@ -224,6 +224,12 @@ def _compile_or_empty( # so set ijar == jar ijar = ctx.outputs.jar + # print("MAYBE DML?!") + # if "library-immediate" in ctx.outputs.jar.basename: + # print("lib-imm") + # print(ctx.outputs) + # print(ctx.outputs.jar) + # print(in_srcjars) source_jar = _pack_source_jar(ctx, scala_srcs, in_srcjars) scala_compilation_provider = _create_scala_compilation_provider(ctx, ijar, source_jar, deps_providers) @@ -309,16 +315,26 @@ def _create_scala_compilation_provider(ctx, ijar, source_jar, deps_providers): ) def _pack_source_jar(ctx, scala_srcs, in_srcjars): - output_jar = ctx.outputs.jar - source_jar_name = output_jar.basename[:-len(output_jar.extension)] + "-src.jar" - output_source_jar = ctx.actions.declare_file(source_jar_name, sibling = output_jar) - return java_common.pack_sources( - ctx.actions, - output_source_jar = output_source_jar, - sources = scala_srcs, - source_jars = in_srcjars, - java_toolchain = find_java_toolchain(ctx, ctx.attr._java_toolchain), - ) + # https://github.com/bazelbuild/bazel/blob/ff6c0333e4f957bb9f7ab5401b01dbf3e9b515b1/src/main/java/com/google/devtools/build/lib/rules/java/JavaInfoBuildHelper.java#L180-L183 + # java_common.pack_sources checks for no srcs and only a single input jar + # if so, it checks that output_source_jar is null + # passing that, it will return the input source jar directly + # However, pack_sources will FAIL if both output_source_jar and + # the deprecated output_jar field are BOTH null + # Therefore, we can return the single input jar ourselves + if not scala_srcs and len(in_srcjars) == 1: + return in_srcjars[0] + else: + output_jar = ctx.outputs.jar + source_jar_name = output_jar.basename[:-len(output_jar.extension)] + "-src.jar" + output_source_jar = ctx.actions.declare_file(source_jar_name, sibling = output_jar) + return java_common.pack_sources( + ctx.actions, + output_source_jar = output_source_jar, + sources = scala_srcs, + source_jars = in_srcjars, + java_toolchain = find_java_toolchain(ctx, ctx.attr._java_toolchain), + ) def _try_to_compile_java_jar( ctx, From aaae8a0bc6d254dd45b335c3e49a5369b8abd8ca Mon Sep 17 00:00:00 2001 From: Win Wang Date: Mon, 4 Apr 2022 17:14:21 -0400 Subject: [PATCH 02/10] remove useless debugging --- scala/private/phases/phase_compile.bzl | 6 ------ 1 file changed, 6 deletions(-) diff --git a/scala/private/phases/phase_compile.bzl b/scala/private/phases/phase_compile.bzl index d59b6ad52..f5032d7a8 100644 --- a/scala/private/phases/phase_compile.bzl +++ b/scala/private/phases/phase_compile.bzl @@ -224,12 +224,6 @@ def _compile_or_empty( # so set ijar == jar ijar = ctx.outputs.jar - # print("MAYBE DML?!") - # if "library-immediate" in ctx.outputs.jar.basename: - # print("lib-imm") - # print(ctx.outputs) - # print(ctx.outputs.jar) - # print(in_srcjars) source_jar = _pack_source_jar(ctx, scala_srcs, in_srcjars) scala_compilation_provider = _create_scala_compilation_provider(ctx, ijar, source_jar, deps_providers) From 902a7a6322a1df590282f269bfa3d90761f2e313 Mon Sep 17 00:00:00 2001 From: Win Wang Date: Mon, 4 Apr 2022 17:58:34 -0400 Subject: [PATCH 03/10] Add tests for _pack_source_jar --- .../main/scala/scalarules/test/srcjars/BUILD | 11 +++ .../test/srcjars/pack_sources_test.bzl | 86 +++++++++++++++++++ 2 files changed, 97 insertions(+) create mode 100644 test/src/main/scala/scalarules/test/srcjars/pack_sources_test.bzl diff --git a/test/src/main/scala/scalarules/test/srcjars/BUILD b/test/src/main/scala/scalarules/test/srcjars/BUILD index 14f4ccd6b..ccd0a4946 100644 --- a/test/src/main/scala/scalarules/test/srcjars/BUILD +++ b/test/src/main/scala/scalarules/test/srcjars/BUILD @@ -1,4 +1,5 @@ load("//scala:scala.bzl", "scala_library") +load(":pack_sources_test.bzl", "pack_sources_test_suite") #TODO the way it SHOULD work (but isn't currently) is that # the source_jar target should make available a compiled jar, @@ -12,8 +13,18 @@ scala_library( srcs = ["SourceJar1.srcjar"], ) +scala_library( + name = "multi_source_jar", + # SourceJar1.jar was created by: + # jar -cfM test/src/main/scala/scalarules/test/srcjars/SourceJar1.srcjar \ + # test/src/main/scala/scalarules/test/srcjars/SourceJar1.scala + srcs = ["SourceJar1.srcjar", "SourceJar2.scala"], +) + scala_library( name = "use_source_jar", srcs = ["SourceJar2.scala"], deps = [":source_jar"], ) + +pack_sources_test_suite(name = "pack_sources_test") diff --git a/test/src/main/scala/scalarules/test/srcjars/pack_sources_test.bzl b/test/src/main/scala/scalarules/test/srcjars/pack_sources_test.bzl new file mode 100644 index 000000000..47cf1521a --- /dev/null +++ b/test/src/main/scala/scalarules/test/srcjars/pack_sources_test.bzl @@ -0,0 +1,86 @@ +load("@bazel_skylib//lib:unittest.bzl", "asserts", "analysistest") +load("@bazel_skylib//lib:new_sets.bzl", "sets") + +# Tests and documents the functionality of phase_compile.bzl's _pack_source_jar + +def _single_source_jar_test_impl(ctx): + env = analysistest.begin(ctx) + + target_under_test = analysistest.target_under_test(env) + + srcjar_names = sets.make( + [j.basename for j in target_under_test[JavaInfo].source_jars] + ) + + # In line with Bazel's java_common.pack_sources, + # We return the initial .srcjar file since there are no source files + # Not sure where the second -src.jar comes from, maybe due to java rules + expected_names = sets.make([ + "SourceJar1.srcjar", + "source_jar_java-src.jar" + ]) + + asserts.set_equals(env, expected = expected_names, actual = srcjar_names) + + return analysistest.end(env) + +single_source_jar_test = analysistest.make(_single_source_jar_test_impl) + +def _multi_source_jar_test_impl(ctx): + env = analysistest.begin(ctx) + + target_under_test = analysistest.target_under_test(env) + + srcjar_names = sets.make( + [j.basename for j in target_under_test[JavaInfo].source_jars] + ) + + # The first .-src.jar seems to have an incorrect name (with an extra ".") + expected_names = sets.make([ + "multi_source_jar.-src.jar", + "multi_source_jar_java-src.jar" + ]) + + asserts.set_equals(env, expected = expected_names, actual = srcjar_names) + + return analysistest.end(env) + +multi_source_jar_test = analysistest.make(_multi_source_jar_test_impl) + +def _source_jar_with_srcs_test_impl(ctx): + env = analysistest.begin(ctx) + + target_under_test = analysistest.target_under_test(env) + + srcjar_names = sets.make( + [j.basename for j in target_under_test[JavaInfo].source_jars] + ) + + # Since we have source files, we don't output a .srcjar + # Instead, we just return the bundle + expected_names = sets.make([ + "use_source_jar.-src.jar" + ]) + + asserts.set_equals(env, expected = expected_names, actual = srcjar_names) + + return analysistest.end(env) + +source_jar_with_srcs_test = analysistest.make(_source_jar_with_srcs_test_impl) + +def pack_sources_test_suite(name): + single_source_jar_test(name = "single_source_jar_test", + target_under_test = ":source_jar") + multi_source_jar_test(name = "multi_source_jar_test", + target_under_test = ":multi_source_jar") + source_jar_with_srcs_test(name = "source_jar_with_srcs_test", + target_under_test = ":use_source_jar") + + native.test_suite( + name = name, + tests = [ + ":single_source_jar_test", + ":multi_source_jar_test", + ":source_jar_with_srcs_test" + ], + ) \ No newline at end of file From 3652ec4744e3f469de0a84efc29671d4ae3855c3 Mon Sep 17 00:00:00 2001 From: Win Wang Date: Mon, 4 Apr 2022 18:00:42 -0400 Subject: [PATCH 04/10] Rename in_srcjars to input_srcjars --- scala/private/phases/phase_compile.bzl | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/scala/private/phases/phase_compile.bzl b/scala/private/phases/phase_compile.bzl index f5032d7a8..9a84481ba 100644 --- a/scala/private/phases/phase_compile.bzl +++ b/scala/private/phases/phase_compile.bzl @@ -308,7 +308,7 @@ def _create_scala_compilation_provider(ctx, ijar, source_jar, deps_providers): neverlink = ctx.attr.neverlink, ) -def _pack_source_jar(ctx, scala_srcs, in_srcjars): +def _pack_source_jar(ctx, scala_srcs, input_srcjars): # https://github.com/bazelbuild/bazel/blob/ff6c0333e4f957bb9f7ab5401b01dbf3e9b515b1/src/main/java/com/google/devtools/build/lib/rules/java/JavaInfoBuildHelper.java#L180-L183 # java_common.pack_sources checks for no srcs and only a single input jar # if so, it checks that output_source_jar is null @@ -316,8 +316,8 @@ def _pack_source_jar(ctx, scala_srcs, in_srcjars): # However, pack_sources will FAIL if both output_source_jar and # the deprecated output_jar field are BOTH null # Therefore, we can return the single input jar ourselves - if not scala_srcs and len(in_srcjars) == 1: - return in_srcjars[0] + if not scala_srcs and len(input_srcjars) == 1: + return input_srcjars[0] else: output_jar = ctx.outputs.jar source_jar_name = output_jar.basename[:-len(output_jar.extension)] + "-src.jar" @@ -326,7 +326,7 @@ def _pack_source_jar(ctx, scala_srcs, in_srcjars): ctx.actions, output_source_jar = output_source_jar, sources = scala_srcs, - source_jars = in_srcjars, + source_jars = input_srcjars, java_toolchain = find_java_toolchain(ctx, ctx.attr._java_toolchain), ) From a70229f24a9eab182b7e303f7be1d6ecf23230ae Mon Sep 17 00:00:00 2001 From: Win Wang Date: Mon, 4 Apr 2022 18:04:09 -0400 Subject: [PATCH 05/10] Remove trailing . from output_source_jar --- scala/private/phases/phase_compile.bzl | 7 ++++++- .../scala/scalarules/test/srcjars/pack_sources_test.bzl | 5 ++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/scala/private/phases/phase_compile.bzl b/scala/private/phases/phase_compile.bzl index 9a84481ba..3b89e7c17 100644 --- a/scala/private/phases/phase_compile.bzl +++ b/scala/private/phases/phase_compile.bzl @@ -320,8 +320,13 @@ def _pack_source_jar(ctx, scala_srcs, input_srcjars): return input_srcjars[0] else: output_jar = ctx.outputs.jar - source_jar_name = output_jar.basename[:-len(output_jar.extension)] + "-src.jar" + without_ext = output_jar.basename[:-len(output_jar.extension)] + if without_ext.endswith("."): + without_ext = without_ext[:-1] + source_jar_name = without_ext + "-src.jar" + output_source_jar = ctx.actions.declare_file(source_jar_name, sibling = output_jar) + return java_common.pack_sources( ctx.actions, output_source_jar = output_source_jar, diff --git a/test/src/main/scala/scalarules/test/srcjars/pack_sources_test.bzl b/test/src/main/scala/scalarules/test/srcjars/pack_sources_test.bzl index 47cf1521a..7c735e6ec 100644 --- a/test/src/main/scala/scalarules/test/srcjars/pack_sources_test.bzl +++ b/test/src/main/scala/scalarules/test/srcjars/pack_sources_test.bzl @@ -35,9 +35,8 @@ def _multi_source_jar_test_impl(ctx): [j.basename for j in target_under_test[JavaInfo].source_jars] ) - # The first .-src.jar seems to have an incorrect name (with an extra ".") expected_names = sets.make([ - "multi_source_jar.-src.jar", + "multi_source_jar-src.jar", "multi_source_jar_java-src.jar" ]) @@ -59,7 +58,7 @@ def _source_jar_with_srcs_test_impl(ctx): # Since we have source files, we don't output a .srcjar # Instead, we just return the bundle expected_names = sets.make([ - "use_source_jar.-src.jar" + "use_source_jar-src.jar" ]) asserts.set_equals(env, expected = expected_names, actual = srcjar_names) From 846eef4d1961b6dbd5cb543b4dc40e94f30de0d4 Mon Sep 17 00:00:00 2001 From: Win Wang Date: Mon, 4 Apr 2022 18:12:30 -0400 Subject: [PATCH 06/10] run lint_fix --- .../main/scala/scalarules/test/srcjars/BUILD | 5 +- .../test/srcjars/pack_sources_test.bzl | 52 +++++++++++-------- 2 files changed, 33 insertions(+), 24 deletions(-) diff --git a/test/src/main/scala/scalarules/test/srcjars/BUILD b/test/src/main/scala/scalarules/test/srcjars/BUILD index ccd0a4946..53fdbde82 100644 --- a/test/src/main/scala/scalarules/test/srcjars/BUILD +++ b/test/src/main/scala/scalarules/test/srcjars/BUILD @@ -18,7 +18,10 @@ scala_library( # SourceJar1.jar was created by: # jar -cfM test/src/main/scala/scalarules/test/srcjars/SourceJar1.srcjar \ # test/src/main/scala/scalarules/test/srcjars/SourceJar1.scala - srcs = ["SourceJar1.srcjar", "SourceJar2.scala"], + srcs = [ + "SourceJar1.srcjar", + "SourceJar2.scala", + ], ) scala_library( diff --git a/test/src/main/scala/scalarules/test/srcjars/pack_sources_test.bzl b/test/src/main/scala/scalarules/test/srcjars/pack_sources_test.bzl index 7c735e6ec..11aa3c98c 100644 --- a/test/src/main/scala/scalarules/test/srcjars/pack_sources_test.bzl +++ b/test/src/main/scala/scalarules/test/srcjars/pack_sources_test.bzl @@ -1,4 +1,4 @@ -load("@bazel_skylib//lib:unittest.bzl", "asserts", "analysistest") +load("@bazel_skylib//lib:unittest.bzl", "analysistest", "asserts") load("@bazel_skylib//lib:new_sets.bzl", "sets") # Tests and documents the functionality of phase_compile.bzl's _pack_source_jar @@ -7,79 +7,85 @@ def _single_source_jar_test_impl(ctx): env = analysistest.begin(ctx) target_under_test = analysistest.target_under_test(env) - + srcjar_names = sets.make( - [j.basename for j in target_under_test[JavaInfo].source_jars] + [j.basename for j in target_under_test[JavaInfo].source_jars], ) # In line with Bazel's java_common.pack_sources, # We return the initial .srcjar file since there are no source files # Not sure where the second -src.jar comes from, maybe due to java rules expected_names = sets.make([ - "SourceJar1.srcjar", - "source_jar_java-src.jar" + "SourceJar1.srcjar", + "source_jar_java-src.jar", ]) asserts.set_equals(env, expected = expected_names, actual = srcjar_names) return analysistest.end(env) - + single_source_jar_test = analysistest.make(_single_source_jar_test_impl) def _multi_source_jar_test_impl(ctx): env = analysistest.begin(ctx) target_under_test = analysistest.target_under_test(env) - + srcjar_names = sets.make( - [j.basename for j in target_under_test[JavaInfo].source_jars] + [j.basename for j in target_under_test[JavaInfo].source_jars], ) expected_names = sets.make([ - "multi_source_jar-src.jar", - "multi_source_jar_java-src.jar" + "multi_source_jar-src.jar", + "multi_source_jar_java-src.jar", ]) asserts.set_equals(env, expected = expected_names, actual = srcjar_names) return analysistest.end(env) - + multi_source_jar_test = analysistest.make(_multi_source_jar_test_impl) def _source_jar_with_srcs_test_impl(ctx): env = analysistest.begin(ctx) target_under_test = analysistest.target_under_test(env) - + srcjar_names = sets.make( - [j.basename for j in target_under_test[JavaInfo].source_jars] + [j.basename for j in target_under_test[JavaInfo].source_jars], ) # Since we have source files, we don't output a .srcjar # Instead, we just return the bundle expected_names = sets.make([ - "use_source_jar-src.jar" + "use_source_jar-src.jar", ]) asserts.set_equals(env, expected = expected_names, actual = srcjar_names) return analysistest.end(env) - + source_jar_with_srcs_test = analysistest.make(_source_jar_with_srcs_test_impl) def pack_sources_test_suite(name): - single_source_jar_test(name = "single_source_jar_test", - target_under_test = ":source_jar") - multi_source_jar_test(name = "multi_source_jar_test", - target_under_test = ":multi_source_jar") - source_jar_with_srcs_test(name = "source_jar_with_srcs_test", - target_under_test = ":use_source_jar") + single_source_jar_test( + name = "single_source_jar_test", + target_under_test = ":source_jar", + ) + multi_source_jar_test( + name = "multi_source_jar_test", + target_under_test = ":multi_source_jar", + ) + source_jar_with_srcs_test( + name = "source_jar_with_srcs_test", + target_under_test = ":use_source_jar", + ) native.test_suite( name = name, tests = [ ":single_source_jar_test", ":multi_source_jar_test", - ":source_jar_with_srcs_test" + ":source_jar_with_srcs_test", ], - ) \ No newline at end of file + ) From f83d7e24b8734d771353da36baa513abad66e788 Mon Sep 17 00:00:00 2001 From: Win Wang Date: Tue, 5 Apr 2022 14:18:53 -0400 Subject: [PATCH 07/10] Add test/doc for expect_java_output=False behavior for source jars --- .../main/scala/scalarules/test/srcjars/BUILD | 6 +++++ .../test/srcjars/pack_sources_test.bzl | 25 +++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/test/src/main/scala/scalarules/test/srcjars/BUILD b/test/src/main/scala/scalarules/test/srcjars/BUILD index 53fdbde82..de673158b 100644 --- a/test/src/main/scala/scalarules/test/srcjars/BUILD +++ b/test/src/main/scala/scalarules/test/srcjars/BUILD @@ -13,6 +13,12 @@ scala_library( srcs = ["SourceJar1.srcjar"], ) +scala_library( + name = "source_jar_no_expect_java_output", + srcs = ["SourceJar1.srcjar"], + expect_java_output = False, +) + scala_library( name = "multi_source_jar", # SourceJar1.jar was created by: diff --git a/test/src/main/scala/scalarules/test/srcjars/pack_sources_test.bzl b/test/src/main/scala/scalarules/test/srcjars/pack_sources_test.bzl index 11aa3c98c..cdcc9cab5 100644 --- a/test/src/main/scala/scalarules/test/srcjars/pack_sources_test.bzl +++ b/test/src/main/scala/scalarules/test/srcjars/pack_sources_test.bzl @@ -15,6 +15,7 @@ def _single_source_jar_test_impl(ctx): # In line with Bazel's java_common.pack_sources, # We return the initial .srcjar file since there are no source files # Not sure where the second -src.jar comes from, maybe due to java rules + # It can be squelched by adding the target attr expect_java_output = False expected_names = sets.make([ "SourceJar1.srcjar", "source_jar_java-src.jar", @@ -26,6 +27,25 @@ def _single_source_jar_test_impl(ctx): single_source_jar_test = analysistest.make(_single_source_jar_test_impl) +def _single_source_jar_no_java_output_test_impl(ctx): + env = analysistest.begin(ctx) + + target_under_test = analysistest.target_under_test(env) + + srcjar_names = sets.make( + [j.basename for j in target_under_test[JavaInfo].source_jars], + ) + + expected_names = sets.make([ + "SourceJar1.srcjar", + ]) + + asserts.set_equals(env, expected = expected_names, actual = srcjar_names) + + return analysistest.end(env) + +single_source_jar_no_java_output_test = analysistest.make(_single_source_jar_no_java_output_test_impl) + def _multi_source_jar_test_impl(ctx): env = analysistest.begin(ctx) @@ -72,6 +92,10 @@ def pack_sources_test_suite(name): name = "single_source_jar_test", target_under_test = ":source_jar", ) + single_source_jar_no_java_output_test( + name = "single_source_jar_no_java_output_test", + target_under_test = ":source_jar_no_expect_java_output", + ) multi_source_jar_test( name = "multi_source_jar_test", target_under_test = ":multi_source_jar", @@ -85,6 +109,7 @@ def pack_sources_test_suite(name): name = name, tests = [ ":single_source_jar_test", + ":single_source_jar_no_java_output_test", ":multi_source_jar_test", ":source_jar_with_srcs_test", ], From a9c8790eba7ada01e8fbbd055b017622f2b586d7 Mon Sep 17 00:00:00 2001 From: Win Wang Date: Tue, 5 Apr 2022 14:51:07 -0400 Subject: [PATCH 08/10] Add additional tests and factor out common functionality --- .../main/scala/scalarules/test/srcjars/BUILD | 16 ++- .../scalarules/test/srcjars/SourceJar2.srcjar | Bin 0 -> 316 bytes .../test/srcjars/pack_sources_test.bzl | 132 +++++++----------- 3 files changed, 64 insertions(+), 84 deletions(-) create mode 100644 test/src/main/scala/scalarules/test/srcjars/SourceJar2.srcjar diff --git a/test/src/main/scala/scalarules/test/srcjars/BUILD b/test/src/main/scala/scalarules/test/srcjars/BUILD index de673158b..7e2b181eb 100644 --- a/test/src/main/scala/scalarules/test/srcjars/BUILD +++ b/test/src/main/scala/scalarules/test/srcjars/BUILD @@ -7,7 +7,7 @@ load(":pack_sources_test.bzl", "pack_sources_test_suite") scala_library( name = "source_jar", - # SourceJar1.jar was created by: + # SourceJar1.srcjar was created by: # jar -cfM test/src/main/scala/scalarules/test/srcjars/SourceJar1.srcjar \ # test/src/main/scala/scalarules/test/srcjars/SourceJar1.scala srcs = ["SourceJar1.srcjar"], @@ -21,9 +21,17 @@ scala_library( scala_library( name = "multi_source_jar", - # SourceJar1.jar was created by: - # jar -cfM test/src/main/scala/scalarules/test/srcjars/SourceJar1.srcjar \ - # test/src/main/scala/scalarules/test/srcjars/SourceJar1.scala + # SourceJar2.srcjar was created the same way as SourceJar1.srcjar, i.e. by: + # jar -cfM test/src/main/scala/scalarules/test/srcjars/SourceJar2.srcjar \ + # test/src/main/scala/scalarules/test/srcjars/SourceJar2.scala + srcs = [ + "SourceJar1.srcjar", + "SourceJar2.srcjar", + ], +) + +scala_library( + name = "mixed_source_jar", srcs = [ "SourceJar1.srcjar", "SourceJar2.scala", diff --git a/test/src/main/scala/scalarules/test/srcjars/SourceJar2.srcjar b/test/src/main/scala/scalarules/test/srcjars/SourceJar2.srcjar new file mode 100644 index 0000000000000000000000000000000000000000..6a0e0c5b205b8663a93d3a04c7e437ee375c8720 GIT binary patch literal 316 zcmWIWW@Zs#;Nak3xR%)x$bbZF7+4reQj1IUi;I%=a}zW3^ox@da}ptBQE5(Uu|7;X zE3v3pKRCa%C^^+DvB*ddtnl9{1_o^ppL2db{-GLverG&&w9b0!Ueor})w`&z=XK6Y zN9)<6Q~syCeDwl3>uV`F-`$F$!AoEGWRk(>M5pu3z{DR}YC-jI2Adb}|HbvvZsc uef-`F=)xc%4)A7V5@A5N99a(Ja#R3wD7pR!@MdKLnaBu)c|dvx*mwYYcwKw| literal 0 HcmV?d00001 diff --git a/test/src/main/scala/scalarules/test/srcjars/pack_sources_test.bzl b/test/src/main/scala/scalarules/test/srcjars/pack_sources_test.bzl index cdcc9cab5..94aff2f54 100644 --- a/test/src/main/scala/scalarules/test/srcjars/pack_sources_test.bzl +++ b/test/src/main/scala/scalarules/test/srcjars/pack_sources_test.bzl @@ -3,106 +3,77 @@ load("@bazel_skylib//lib:new_sets.bzl", "sets") # Tests and documents the functionality of phase_compile.bzl's _pack_source_jar -def _single_source_jar_test_impl(ctx): - env = analysistest.begin(ctx) +def _source_jar_test_impl(ctx): + env = analysistest.begin(ctx) - target_under_test = analysistest.target_under_test(env) + target_under_test = analysistest.target_under_test(env) - srcjar_names = sets.make( - [j.basename for j in target_under_test[JavaInfo].source_jars], - ) - - # In line with Bazel's java_common.pack_sources, - # We return the initial .srcjar file since there are no source files - # Not sure where the second -src.jar comes from, maybe due to java rules - # It can be squelched by adding the target attr expect_java_output = False - expected_names = sets.make([ - "SourceJar1.srcjar", - "source_jar_java-src.jar", - ]) - - asserts.set_equals(env, expected = expected_names, actual = srcjar_names) - - return analysistest.end(env) + srcjar_names = sets.make( + [j.basename for j in target_under_test[JavaInfo].source_jars], + ) -single_source_jar_test = analysistest.make(_single_source_jar_test_impl) + expected_names = sets.make(ctx.attr.expected_basenames) -def _single_source_jar_no_java_output_test_impl(ctx): - env = analysistest.begin(ctx) + asserts.set_equals(env, expected = expected_names, actual = srcjar_names) - target_under_test = analysistest.target_under_test(env) + return analysistest.end(env) - srcjar_names = sets.make( - [j.basename for j in target_under_test[JavaInfo].source_jars], +def _make_source_jar_test(): + return analysistest.make( + impl = _source_jar_test_impl, + attrs = { + "expected_basenames": attr.string_list( + mandatory=True, + allow_empty=True + ) + } ) - - expected_names = sets.make([ - "SourceJar1.srcjar", - ]) - - asserts.set_equals(env, expected = expected_names, actual = srcjar_names) - - return analysistest.end(env) - -single_source_jar_no_java_output_test = analysistest.make(_single_source_jar_no_java_output_test_impl) - -def _multi_source_jar_test_impl(ctx): - env = analysistest.begin(ctx) - - target_under_test = analysistest.target_under_test(env) - - srcjar_names = sets.make( - [j.basename for j in target_under_test[JavaInfo].source_jars], - ) - - expected_names = sets.make([ - "multi_source_jar-src.jar", - "multi_source_jar_java-src.jar", - ]) - - asserts.set_equals(env, expected = expected_names, actual = srcjar_names) - - return analysistest.end(env) - -multi_source_jar_test = analysistest.make(_multi_source_jar_test_impl) - -def _source_jar_with_srcs_test_impl(ctx): - env = analysistest.begin(ctx) - - target_under_test = analysistest.target_under_test(env) - - srcjar_names = sets.make( - [j.basename for j in target_under_test[JavaInfo].source_jars], - ) - - # Since we have source files, we don't output a .srcjar - # Instead, we just return the bundle - expected_names = sets.make([ - "use_source_jar-src.jar", - ]) - - asserts.set_equals(env, expected = expected_names, actual = srcjar_names) - - return analysistest.end(env) - -source_jar_with_srcs_test = analysistest.make(_source_jar_with_srcs_test_impl) + +source_jar_test = _make_source_jar_test() def pack_sources_test_suite(name): - single_source_jar_test( + source_jar_test( name = "single_source_jar_test", target_under_test = ":source_jar", + + # In line with Bazel's java_common.pack_sources, + # We return the initial .srcjar file since there are no source files + # Not sure where the second -src.jar comes from, maybe due to java rules + # It can be removed by adding the target attr expect_java_output = False + expected_basenames = [ + "SourceJar1.srcjar", + "source_jar_java-src.jar", + ] ) - single_source_jar_no_java_output_test( + source_jar_test( name = "single_source_jar_no_java_output_test", target_under_test = ":source_jar_no_expect_java_output", + expected_basenames = [ + "SourceJar1.srcjar", + ] ) - multi_source_jar_test( + source_jar_test( name = "multi_source_jar_test", target_under_test = ":multi_source_jar", + expected_basenames = [ + "multi_source_jar-src.jar", + "multi_source_jar_java-src.jar", + ], ) - source_jar_with_srcs_test( + source_jar_test( + name = "mixed_source_jar_test", + target_under_test = ":mixed_source_jar", + expected_basenames = [ + "mixed_source_jar-src.jar", + "mixed_source_jar_java-src.jar", + ], + ) + source_jar_test( name = "source_jar_with_srcs_test", target_under_test = ":use_source_jar", + expected_basenames = [ + "use_source_jar-src.jar" + ], ) native.test_suite( @@ -111,6 +82,7 @@ def pack_sources_test_suite(name): ":single_source_jar_test", ":single_source_jar_no_java_output_test", ":multi_source_jar_test", + ":mixed_source_jar_test", ":source_jar_with_srcs_test", ], ) From 2f5d9fc0a9fd5a1d411f296c4505fc08e8b02524 Mon Sep 17 00:00:00 2001 From: Win Wang Date: Tue, 5 Apr 2022 14:56:45 -0400 Subject: [PATCH 09/10] Refactor output_jar logic to be more correct and readable --- scala/private/phases/phase_compile.bzl | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/scala/private/phases/phase_compile.bzl b/scala/private/phases/phase_compile.bzl index 3b89e7c17..351617fdd 100644 --- a/scala/private/phases/phase_compile.bzl +++ b/scala/private/phases/phase_compile.bzl @@ -320,9 +320,10 @@ def _pack_source_jar(ctx, scala_srcs, input_srcjars): return input_srcjars[0] else: output_jar = ctx.outputs.jar - without_ext = output_jar.basename[:-len(output_jar.extension)] - if without_ext.endswith("."): - without_ext = without_ext[:-1] + without_ext = output_jar.basename + if output_jar.extension: + ext_len = len("." + output_jar.extension) + without_ext = output_jar.basename[:-ext_len] source_jar_name = without_ext + "-src.jar" output_source_jar = ctx.actions.declare_file(source_jar_name, sibling = output_jar) From 151cf8b58dd2c7206e074bc0a700332c52619645 Mon Sep 17 00:00:00 2001 From: Win Wang Date: Tue, 5 Apr 2022 15:00:16 -0400 Subject: [PATCH 10/10] Run lint, add Co-author Co-authored-by: Shane Delmore --- .../test/srcjars/pack_sources_test.bzl | 32 +++++++++---------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/test/src/main/scala/scalarules/test/srcjars/pack_sources_test.bzl b/test/src/main/scala/scalarules/test/srcjars/pack_sources_test.bzl index 94aff2f54..62bb97d7b 100644 --- a/test/src/main/scala/scalarules/test/srcjars/pack_sources_test.bzl +++ b/test/src/main/scala/scalarules/test/srcjars/pack_sources_test.bzl @@ -4,31 +4,31 @@ load("@bazel_skylib//lib:new_sets.bzl", "sets") # Tests and documents the functionality of phase_compile.bzl's _pack_source_jar def _source_jar_test_impl(ctx): - env = analysistest.begin(ctx) + env = analysistest.begin(ctx) - target_under_test = analysistest.target_under_test(env) + target_under_test = analysistest.target_under_test(env) - srcjar_names = sets.make( - [j.basename for j in target_under_test[JavaInfo].source_jars], - ) + srcjar_names = sets.make( + [j.basename for j in target_under_test[JavaInfo].source_jars], + ) - expected_names = sets.make(ctx.attr.expected_basenames) + expected_names = sets.make(ctx.attr.expected_basenames) - asserts.set_equals(env, expected = expected_names, actual = srcjar_names) + asserts.set_equals(env, expected = expected_names, actual = srcjar_names) - return analysistest.end(env) + return analysistest.end(env) def _make_source_jar_test(): return analysistest.make( impl = _source_jar_test_impl, attrs = { "expected_basenames": attr.string_list( - mandatory=True, - allow_empty=True - ) - } + mandatory = True, + allow_empty = True, + ), + }, ) - + source_jar_test = _make_source_jar_test() def pack_sources_test_suite(name): @@ -43,14 +43,14 @@ def pack_sources_test_suite(name): expected_basenames = [ "SourceJar1.srcjar", "source_jar_java-src.jar", - ] + ], ) source_jar_test( name = "single_source_jar_no_java_output_test", target_under_test = ":source_jar_no_expect_java_output", expected_basenames = [ "SourceJar1.srcjar", - ] + ], ) source_jar_test( name = "multi_source_jar_test", @@ -72,7 +72,7 @@ def pack_sources_test_suite(name): name = "source_jar_with_srcs_test", target_under_test = ":use_source_jar", expected_basenames = [ - "use_source_jar-src.jar" + "use_source_jar-src.jar", ], )