diff options
author | nickreid <nickreid@google.com> | 2023-10-09 14:24:37 -0700 |
---|---|---|
committer | Copybara-Service <copybara-worker@google.com> | 2023-10-09 14:25:15 -0700 |
commit | 229f001106d37eb474aae58b562b0582edcf6d0c (patch) | |
tree | c7bd7e2a43a57acd1142be2e17eb869d6b1c4e52 | |
parent | 7c7909c47b809dfa29e66d93219d7e916ac58f27 (diff) | |
download | bazelbuild-kotlin-rules-229f001106d37eb474aae58b562b0582edcf6d0c.tar.gz |
Move kt_jvm_library_test rule to its own file, and rename it to kt_jvm_library_analysis_test
PiperOrigin-RevId: 572040497
-rw-r--r-- | kotlin/jvm/testing/BUILD | 1 | ||||
-rw-r--r-- | kotlin/jvm/testing/jvm_library_analysis_test.bzl | 138 | ||||
-rw-r--r-- | tests/analysis/jvm_library_test.bzl | 154 |
3 files changed, 154 insertions, 139 deletions
diff --git a/kotlin/jvm/testing/BUILD b/kotlin/jvm/testing/BUILD index 61722ff..59bd24c 100644 --- a/kotlin/jvm/testing/BUILD +++ b/kotlin/jvm/testing/BUILD @@ -32,6 +32,7 @@ bzl_library( "//:visibility_bzl", "//kotlin:rules_bzl", "//kotlin/common/testing:testing_bzl", + "@bazel_skylib//lib:sets", "@bazel_skylib//lib:unittest", ], ) diff --git a/kotlin/jvm/testing/jvm_library_analysis_test.bzl b/kotlin/jvm/testing/jvm_library_analysis_test.bzl new file mode 100644 index 0000000..2b13533 --- /dev/null +++ b/kotlin/jvm/testing/jvm_library_analysis_test.bzl @@ -0,0 +1,138 @@ +# Copyright 2022 Google LLC. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the License); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""kt_jvm_library_analysis_test""" + +load("//:visibility.bzl", "RULES_KOTLIN") +load("//kotlin/common/testing:analysis.bzl", "kt_analysis") +load("//kotlin/common/testing:asserts.bzl", "kt_asserts") +load("@bazel_skylib//lib:sets.bzl", "sets") +load("@bazel_skylib//lib:unittest.bzl", "analysistest", "asserts") + +kt_jvm_library_analysis_test = analysistest.make( + impl = lambda ctx: _kt_jvm_library_analysis_test_impl(ctx), + attrs = dict( + expected_al_ruleset_names = attr.string_list( + doc = "Android Lint rule JARs reported as run on the given target", + default = kt_analysis.DEFAULT_LIST, + ), + expected_compile_jar_names = attr.string_list( + doc = "Names of all JavaInfo::compile_jars for the given target", + default = kt_analysis.DEFAULT_LIST, + ), + expected_exported_processor_jar_names = attr.string_list( + doc = "Names of all JavaInfo::plugins JARs returned by the given target", + default = kt_analysis.DEFAULT_LIST, + ), + expected_exported_processor_classes = attr.string_list( + doc = "Annotation processors reported as to be run on depending targets", + ), + expected_processor_classes = attr.string_list( + doc = "Annotation processors reported as run on the given target", + ), + expected_friend_jar_names = attr.string_list( + doc = "Names of all -Xfriend-paths= JARs", + default = kt_analysis.DEFAULT_LIST, + ), + expected_runfile_names = attr.string_list( + doc = "Names of all runfiles", + default = kt_analysis.DEFAULT_LIST, + ), + expect_jdeps = attr.bool(default = True), + expect_processor_classpath = attr.bool(), + expect_neverlink = attr.bool(), + required_mnemonic_counts = attr.string_dict( + doc = "Expected mnemonics to expected action count; unlisted mnemonics are ignored", + ), + ), +) + +def _kt_jvm_library_analysis_test_impl(ctx): + env = analysistest.begin(ctx) + actual = ctx.attr.target_under_test + + actions = analysistest.target_actions(env) + kt_al_action = kt_analysis.get_action(actions, "KtAndroidLint") + + asserts.true( + env, + JavaInfo in actual, + "kt_jvm_library did not produce JavaInfo provider.", + ) + asserts.true( + env, + ProguardSpecProvider in actual, + "Expected a ProguardSpecProvider provider.", + ) + + if ctx.attr.expected_runfile_names != kt_analysis.DEFAULT_LIST: + asserts.set_equals( + env, + sets.make(ctx.attr.expected_runfile_names), + sets.make([ + f.basename + for f in actual[DefaultInfo].data_runfiles.files.to_list() + ]), + ) + + if ctx.attr.expected_compile_jar_names != kt_analysis.DEFAULT_LIST: + asserts.set_equals( + env, + sets.make(ctx.attr.expected_compile_jar_names), + sets.make([f.basename for f in actual[JavaInfo].compile_jars.to_list()]), + "kt_jvm_library JavaInfo::compile_jars", + ) + + if ctx.attr.expected_exported_processor_jar_names != kt_analysis.DEFAULT_LIST: + asserts.set_equals( + env, + sets.make(ctx.attr.expected_exported_processor_jar_names), + sets.make([f.basename for f in actual[JavaInfo].plugins.processor_jars.to_list()]), + ) + + asserts.set_equals( + env, + sets.make(ctx.attr.expected_exported_processor_classes), + sets.make(actual[JavaInfo].plugins.processor_classes.to_list()), + ) + + kt_2_java_compile = kt_analysis.get_action(actions, "Kt2JavaCompile") + + if kt_2_java_compile: + asserts.true( + env, + kt_2_java_compile.outputs.to_list()[0].basename.endswith(".jar"), + "Expected first output to be a JAR (this affects the param file name).", + ) + + if ctx.attr.expected_friend_jar_names != kt_analysis.DEFAULT_LIST: + friend_paths_arg = kt_analysis.get_arg(kt_2_java_compile, "-Xfriend-paths=") + kt_asserts.list_matches( + env, + expected = ctx.attr.expected_friend_jar_names, + actual = ["/" + x for x in (friend_paths_arg.split(",") if friend_paths_arg else [])], + matcher = lambda expected, actual: actual.endswith(expected), + items_name = "friend JARs", + ) + + asserts.equals( + env, + ctx.attr.expect_neverlink, + len(actual[JavaInfo].transitive_runtime_jars.to_list()) == 0, + "Mismatch: Expected transitive_runtime_jars iff (neverlink == False)", + ) + + kt_asserts.required_mnemonic_counts(env, ctx.attr.required_mnemonic_counts, actions) + + return analysistest.end(env) diff --git a/tests/analysis/jvm_library_test.bzl b/tests/analysis/jvm_library_test.bzl index e9d9330..c7ce5ec 100644 --- a/tests/analysis/jvm_library_test.bzl +++ b/tests/analysis/jvm_library_test.bzl @@ -16,136 +16,12 @@ load("//:visibility.bzl", "RULES_KOTLIN") load("//kotlin:jvm_library.bzl", "kt_jvm_library") -load("//kotlin/common/testing:asserts.bzl", "kt_asserts") -load("//tests/analysis:util.bzl", "ONLY_FOR_ANALYSIS_TEST_TAGS", "create_file", "get_action", "get_arg") -load("@bazel_skylib//lib:sets.bzl", "sets") +load("//kotlin/jvm/testing:jvm_library_analysis_test.bzl", "kt_jvm_library_analysis_test") +load("//tests/analysis:util.bzl", "ONLY_FOR_ANALYSIS_TEST_TAGS", "create_file") load("@bazel_skylib//lib:unittest.bzl", "analysistest", "asserts") load(":assert_failure_test.bzl", "assert_failure_test") -_DEFAULT_LIST = ["__default__"] - -def _test_impl(ctx): - env = analysistest.begin(ctx) - actual = ctx.attr.target_under_test - - actions = analysistest.target_actions(env) - kt_al_action = get_action(actions, "KtAndroidLint") - - asserts.true( - env, - JavaInfo in actual, - "kt_jvm_library did not produce JavaInfo provider.", - ) - asserts.true( - env, - ProguardSpecProvider in actual, - "Expected a ProguardSpecProvider provider.", - ) - - if ctx.attr.expected_runfile_names != _DEFAULT_LIST: - asserts.set_equals( - env, - sets.make(ctx.attr.expected_runfile_names), - sets.make([ - f.basename - for f in actual[DefaultInfo].data_runfiles.files.to_list() - ]), - ) - - if ctx.attr.expected_compile_jar_names != _DEFAULT_LIST: - asserts.set_equals( - env, - sets.make(ctx.attr.expected_compile_jar_names), - sets.make([f.basename for f in actual[JavaInfo].compile_jars.to_list()]), - "kt_jvm_library JavaInfo::compile_jars", - ) - - if ctx.attr.expected_exported_processor_jar_names != _DEFAULT_LIST: - asserts.set_equals( - env, - sets.make(ctx.attr.expected_exported_processor_jar_names), - sets.make([f.basename for f in actual[JavaInfo].plugins.processor_jars.to_list()]), - ) - - asserts.set_equals( - env, - sets.make(ctx.attr.expected_exported_processor_classes), - sets.make(actual[JavaInfo].plugins.processor_classes.to_list()), - ) - - kt_2_java_compile = get_action(actions, "Kt2JavaCompile") - - if kt_2_java_compile: - asserts.true( - env, - kt_2_java_compile.outputs.to_list()[0].basename.endswith(".jar"), - "Expected first output to be a JAR (this affects the param file name).", - ) - - if ctx.attr.expected_friend_jar_names != _DEFAULT_LIST: - friend_paths_arg = get_arg(kt_2_java_compile, "-Xfriend-paths=") - kt_asserts.list_matches( - env, - expected = ctx.attr.expected_friend_jar_names, - actual = ["/" + x for x in (friend_paths_arg.split(",") if friend_paths_arg else [])], - matcher = lambda expected, actual: actual.endswith(expected), - items_name = "friend JARs", - ) - - asserts.equals( - env, - ctx.attr.expect_neverlink, - len(actual[JavaInfo].transitive_runtime_jars.to_list()) == 0, - "Mismatch: Expected transitive_runtime_jars iff (neverlink == False)", - ) - - kt_asserts.required_mnemonic_counts(env, ctx.attr.required_mnemonic_counts, actions) - - return analysistest.end(env) - -_test = analysistest.make( - impl = _test_impl, - attrs = dict( - expected_al_ruleset_names = attr.string_list( - doc = "Android Lint rule JARs reported as run on the given target", - default = _DEFAULT_LIST, - ), - expected_compile_jar_names = attr.string_list( - doc = "Names of all JavaInfo::compile_jars for the given target", - default = _DEFAULT_LIST, - ), - expected_exported_processor_jar_names = attr.string_list( - doc = "Names of all JavaInfo.plugins JARs returned by the given target", - default = _DEFAULT_LIST, - ), - expected_exported_processor_classes = attr.string_list( - doc = "Annotation processors reported as to be run on depending targets", - ), - expected_processor_classes = attr.string_list( - doc = "Annotation processors reported as run on the given target", - ), - expected_friend_jar_names = attr.string_list( - doc = "Names of all -Xfriend-paths= JARs", - default = _DEFAULT_LIST, - ), - expected_runfile_names = attr.string_list( - doc = "Names of all runfiles", - default = _DEFAULT_LIST, - ), - expect_jdeps = attr.bool(default = True), - expect_processor_classpath = attr.bool(), - expect_neverlink = attr.bool(), - required_mnemonic_counts = attr.string_dict( - doc = """ - Mnemonics that must be registered (or must not be, if starting with '-'). - - No assertions are made about mnemonics absent from this list. - """, - ), - ), -) - -jvm_library_test = _test +jvm_library_test = kt_jvm_library_analysis_test def _coverage_test_impl(ctx): env = analysistest.begin(ctx) @@ -198,7 +74,7 @@ fun greeting(): String = "Hello World!" test_name + "/salutations.pgcfg", ], ) - _test( + kt_jvm_library_analysis_test( name = test_name, target_under_test = test_name + "_tut", ) @@ -230,7 +106,7 @@ Hi! test_name + "/salutations.txt", ], ) - _test( + kt_jvm_library_analysis_test( name = test_name, target_under_test = test_name + "_tut", ) @@ -256,7 +132,7 @@ fun greeting(): String = "Hello World!" plugins = ["//bazel:auto_value_plugin"], ) - _test( + kt_jvm_library_analysis_test( name = test_name, target_under_test = test_name + "_tut", expected_processor_classes = ["com.google.auto.value.processor.AutoValueProcessor"], @@ -277,7 +153,7 @@ def _test_kt_jvm_library_no_kt_srcs_with_plugin(): plugins = [":%s_plugin" % test_name], tags = ONLY_FOR_ANALYSIS_TEST_TAGS, ) - _test( + kt_jvm_library_analysis_test( name = test_name, target_under_test = test_name + "_tut", expected_processor_classes = [test_name], @@ -309,7 +185,7 @@ fun greeting(): String = "Hello World!" plugins = [":%s_plugin" % test_name], ) - _test( + kt_jvm_library_analysis_test( name = test_name, target_under_test = test_name + "_tut", expected_processor_classes = [], # no processor class so no processing @@ -339,7 +215,7 @@ fun greeting(): String = "Hello World!" exported_plugins = [":%s_plugin" % test_name], ) - _test( + kt_jvm_library_analysis_test( name = test_name, target_under_test = test_name + "_tut", expected_exported_processor_classes = [test_name], @@ -377,7 +253,7 @@ fun greeting(): String = "Hello World!" tags = ONLY_FOR_ANALYSIS_TEST_TAGS, ) - _test( + kt_jvm_library_analysis_test( name = test_name, target_under_test = test_name + "_tut", expected_processor_classes = [test_name], @@ -413,7 +289,7 @@ fun greeting(): String = "Hello World!" tags = ONLY_FOR_ANALYSIS_TEST_TAGS, ) - _test( + kt_jvm_library_analysis_test( name = test_name, target_under_test = test_name + "_tut", expected_processor_classes = [test_name], @@ -449,7 +325,7 @@ fun greeting(): String = "Hello World!" ":%s_javaexp" % test_name, ], ) - _test( + kt_jvm_library_analysis_test( name = test_name, target_under_test = test_name + "_tut", expected_compile_jar_names = [ @@ -487,7 +363,7 @@ fun greeting(): String = "Hello World!" ], exports = [":%s_exports_plugin" % test_name], ) - _test( + kt_jvm_library_analysis_test( name = test_name, target_under_test = test_name + "_tut", expected_compile_jar_names = [ @@ -524,7 +400,7 @@ fun greeting(): String = "Hello World!" ], exports = [":%s_exports_plugin" % test_name], ) - _test( + kt_jvm_library_analysis_test( name = test_name, target_under_test = test_name + "_tut", expected_compile_jar_names = [ @@ -617,7 +493,7 @@ def _test_kt_jvm_library_with_no_sources_with_exports(): tags = ONLY_FOR_ANALYSIS_TEST_TAGS, exports = [test_name + "_exp"], ) - _test( + kt_jvm_library_analysis_test( name = test_name, target_under_test = test_name + "_tut", expect_jdeps = False, |