diff options
author | nickreid <nickreid@google.com> | 2022-07-29 10:01:34 -0700 |
---|---|---|
committer | Copybara-Service <copybara-worker@google.com> | 2022-07-29 10:02:02 -0700 |
commit | 09d89590f3f7e36b2c5d7c7fcfd7791e8bbb17eb (patch) | |
tree | 72279136b16dc6603a93c387ad784f9265a49b99 | |
parent | cb486c326eacd22dad98160fb36d0b706e10fbbb (diff) | |
download | bazelbuild-kotlin-rules-09d89590f3f7e36b2c5d7c7fcfd7791e8bbb17eb.tar.gz |
Define kt_compiler_plugin rule for kotlinc plugins.
PiperOrigin-RevId: 464101180
Change-Id: If46a509f5c830096dc47d6d74daab110e2000cbf
-rw-r--r-- | kotlin/compiler_plugin.bzl | 81 | ||||
-rw-r--r-- | kotlin/jvm_compile.bzl | 6 | ||||
-rw-r--r-- | kotlin/jvm_import.bzl | 11 | ||||
-rw-r--r-- | kotlin/jvm_library.internal.bzl | 26 | ||||
-rw-r--r-- | kotlin/traverse_exports.bzl | 4 | ||||
-rw-r--r-- | tests/analysis/assert_failure_test.bzl | 30 | ||||
-rw-r--r-- | tests/analysis/compiler_plugin/BUILD | 23 | ||||
-rw-r--r-- | tests/analysis/compiler_plugin/forbidden_target/BUILD | 19 | ||||
-rw-r--r-- | tests/analysis/compiler_plugin/propagation/BUILD | 79 | ||||
-rw-r--r-- | tests/analysis/compiler_plugin/propagation/assert_propagation_test.bzl | 56 | ||||
-rw-r--r-- | tests/analysis/compiler_plugin/provider_ctor/BUILD | 19 | ||||
-rw-r--r-- | tests/analysis/compiler_plugin/provider_ctor/fake_compiler_plugin.bzl | 37 | ||||
-rw-r--r-- | tests/analysis/compiler_plugin/provider_output/BUILD | 58 | ||||
-rw-r--r-- | tests/analysis/compiler_plugin/provider_output/assert_compiler_plugin_test.bzl | 37 |
14 files changed, 469 insertions, 17 deletions
diff --git a/kotlin/compiler_plugin.bzl b/kotlin/compiler_plugin.bzl new file mode 100644 index 0000000..433ce82 --- /dev/null +++ b/kotlin/compiler_plugin.bzl @@ -0,0 +1,81 @@ +# 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. + +"""A rule for declaring and passing kotlinc plugins.""" + +_KtCompilerPluginInfo = provider( + doc = "Info for running a plugin that directly registers itself to kotlinc extension points", + fields = dict( + plugin_id = "string", + jar = "File", + args = "list[string]", + ), +) + +KtCompilerPluginInfo, _make_kt_compiler_plugin_info = (_KtCompilerPluginInfo, _KtCompilerPluginInfo) + +def _kt_compiler_plugin_impl(ctx): + + return [ + JavaPluginInfo( + runtime_deps = [], + processor_class = None, + ), + _make_kt_compiler_plugin_info( + plugin_id = ctx.attr.plugin_id, + jar = ctx.file.jar or ctx.attr.jar[JavaInfo].output_jar, + args = [ + "plugin:%s:%s=%s" % (ctx.attr.plugin_id, k, v) + for (k, v) in ctx.attr.args.items() + ], + ), + ] + +kt_compiler_plugin = rule( + implementation = _kt_compiler_plugin_impl, + attrs = dict( + plugin_id = attr.string( + doc = "ID used to register this plugin with kotlinc", + mandatory = True, + ), + jar = attr.label( + doc = "JAR that provides the plugin implementation", + mandatory = True, + allow_single_file = [".jar"], + ), + args = attr.string_dict( + doc = """Args to pass to the plugin + + The rule impl will format key-value pairs for the koltinc + CLI. All plugin invocations will receive the same args. + """, + default = {}, + ), + ), + provides = [ + JavaPluginInfo, # Allow this rule to be passed to java rules + KtCompilerPluginInfo, + ], +) + +def _get_exported_plugins(_target, ctx): + return [t[KtCompilerPluginInfo] for t in getattr(ctx.rule.attr, "exported_plugins", []) if (KtCompilerPluginInfo in t)] + +kt_compiler_plugin_visitor = struct( + name = "compiler_plugins", + visit_target = _get_exported_plugins, + filter_export = None, + finish_expansion = None, + process_unvisited_target = None, +) diff --git a/kotlin/jvm_compile.bzl b/kotlin/jvm_compile.bzl index cf49710..ef337d3 100644 --- a/kotlin/jvm_compile.bzl +++ b/kotlin/jvm_compile.bzl @@ -72,7 +72,7 @@ def kt_jvm_compile( android_lint_plugins: List of targets. An list of android lint plugins to execute as a part of linting. resource_files: List of Files. The list of Android Resource files. - exported_plugins: List of exported javac plugins + exported_plugins: List of exported javac/kotlinc plugins manifest: A File. The raw Android manifest. Optional. merged_manifest: A File. The merged Android manifest. Optional. classpath_resources: List of Files. The list of classpath resources (kt_jvm_library only). @@ -196,7 +196,7 @@ def kt_jvm_compile( coverage_srcs = coverage_srcs, deps = r_java_info + java_infos, disable_lint_checks = disable_lint_checks, - exported_plugins = [e[JavaPluginInfo] for e in exported_plugins], + exported_plugins = [e[JavaPluginInfo] for e in exported_plugins if (JavaPluginInfo in e)], # Not all exported targets contain a JavaInfo (e.g. some only have CcInfo) exports = r_java_info + [e[JavaInfo] for e in exports if JavaInfo in e], friend_jars = friend_jars, @@ -212,7 +212,7 @@ def kt_jvm_compile( output = output, output_srcjar = output_srcjar, plugins = common.kt_plugins_map( - java_plugin_infos = [plugin[JavaPluginInfo] for plugin in plugins], + java_plugin_infos = [plugin[JavaPluginInfo] for plugin in plugins if (JavaPluginInfo in plugin)], kt_plugin_configs = kt_plugin_configs, ), resource_files = resource_files, diff --git a/kotlin/jvm_import.bzl b/kotlin/jvm_import.bzl index 500e78a..aa5a8c9 100644 --- a/kotlin/jvm_import.bzl +++ b/kotlin/jvm_import.bzl @@ -19,6 +19,7 @@ load(":traverse_exports.bzl", "kt_traverse_exports") load("@//toolchains/kotlin_jvm:kt_jvm_toolchains.bzl", "kt_jvm_toolchains") load("@//toolchains/kotlin_jvm:java_toolchains.bzl", "java_toolchains") load("@bazel_skylib//lib:dicts.bzl", "dicts") +load(":compiler_plugin.bzl", "KtCompilerPluginInfo") def _kt_jvm_import_impl(ctx): kt_jvm_toolchain = kt_jvm_toolchains.get(ctx) @@ -100,6 +101,16 @@ _KT_JVM_IMPORT_ATTRS = dicts.add( `java_library`. Any build failures resulting from this requirement will include the missing dependencies and a command to fix the rule.""", ), + exported_plugins = attr.label_list( + providers = [[KtCompilerPluginInfo]], + cfg = "exec", + doc = """JVM plugins to export to users. + + + Every plugin listed will run during compliations that depend on this target, as + if it were listed directly in that target's `plugins` attribute. `java_*` targets + will not run kotlinc plugins""", + ), jars = attr.label_list( allow_files = common.JAR_FILE_TYPE, allow_empty = False, diff --git a/kotlin/jvm_library.internal.bzl b/kotlin/jvm_library.internal.bzl index 4abb686..2af3c57 100644 --- a/kotlin/jvm_library.internal.bzl +++ b/kotlin/jvm_library.internal.bzl @@ -19,6 +19,7 @@ load("@//toolchains/kotlin_jvm:java_toolchains.bzl", "java_toolchains") load("@//toolchains/kotlin_jvm:kt_jvm_toolchains.bzl", "kt_jvm_toolchains") load("@bazel_skylib//lib:dicts.bzl", "dicts") load(":common.bzl", "common") +load(":compiler_plugin.bzl", "KtCompilerPluginInfo") load(":traverse_exports.bzl", "kt_traverse_exports") load(":jvm_compile.bzl", "compile") @@ -159,15 +160,13 @@ _KT_JVM_LIBRARY_ATTRS = dicts.add( doc = """A list of lint checks to be skipped for this target.""", ), exported_plugins = attr.label_list( - providers = [ - [JavaPluginInfo], - ], + providers = [[JavaPluginInfo], [KtCompilerPluginInfo]], cfg = "exec", - doc = """The list of [java_plugin](https://docs.bazel.build/versions/main/be/java.html#java_plugin)s - (e.g. annotation processors) to export to libraries that directly depend on this library. - The specified list of `java_plugin`s will be applied to any library which directly depends on - this library, just as if that library had explicitly declared these labels in - [plugins].""", + doc = """JVM plugins to export to users. + + Every plugin listed will run during compliations that depend on this target, as + if it were listed directly in that target's `plugins` attribute. `java_*` targets + will not run kotlinc plugins""", ), exports = attr.label_list( allow_rules = common.ALLOWED_JVM_RULES, @@ -187,12 +186,13 @@ _KT_JVM_LIBRARY_ATTRS = dicts.add( rule.""", ), plugins = attr.label_list( - providers = [JavaPluginInfo], + providers = [[JavaPluginInfo], [KtCompilerPluginInfo]], cfg = "exec", - doc = """Java annotation processors to run at compile-time. Every `java_plugin` specified in - the `plugins` attribute will be run whenever this library is built. A library may - also inherit plugins from dependencies that use `exported_plugins`. Resources - generated by the plugin will be included in the output Jar.""", + doc = """JVM plugins to run during compilation. + + Every plugin listed will run whenever this library is built. Resources generated by the + plugin will be included in the output JAR. A library may also inherit plugins from + dependencies that use `exported_plugins`.""", ), proguard_specs = attr.label_list( allow_files = True, diff --git a/kotlin/traverse_exports.bzl b/kotlin/traverse_exports.bzl index 982883e..00356db 100644 --- a/kotlin/traverse_exports.bzl +++ b/kotlin/traverse_exports.bzl @@ -14,8 +14,9 @@ """Combined aspect for all rules_kotlin behaviours that need to traverse exports.""" -load(":forbidden_deps.bzl", "kt_forbidden_deps_visitor") +load(":compiler_plugin.bzl", "kt_compiler_plugin_visitor") load(":direct_jdeps.bzl", "kt_direct_jdeps_visitor") +load(":forbidden_deps.bzl", "kt_forbidden_deps_visitor") # java_xxx_proto_library don't populate java_outputs but we can get them through # required_aspect_providers from their proto_library deps. @@ -40,6 +41,7 @@ _NO_SRCS_DEPS_AS_EXPORTS_RULES = [ _VISITORS = [ kt_forbidden_deps_visitor, kt_direct_jdeps_visitor, + kt_compiler_plugin_visitor, ] _KtTraverseExportsInfo = provider( diff --git a/tests/analysis/assert_failure_test.bzl b/tests/analysis/assert_failure_test.bzl new file mode 100644 index 0000000..b266c2a --- /dev/null +++ b/tests/analysis/assert_failure_test.bzl @@ -0,0 +1,30 @@ +# 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. + +"""An assertion for analysis failure.""" + +load("@bazel_skylib//lib:unittest.bzl", "analysistest", "asserts") + +def _assert_failure_test_impl(ctx): + env = analysistest.begin(ctx) + asserts.expect_failure(env, ctx.attr.msg_contains) + return analysistest.end(env) + +assert_failure_test = analysistest.make( + _assert_failure_test_impl, + expect_failure = True, + attrs = dict( + msg_contains = attr.string(mandatory = True), + ), +) diff --git a/tests/analysis/compiler_plugin/BUILD b/tests/analysis/compiler_plugin/BUILD new file mode 100644 index 0000000..c3bad7d --- /dev/null +++ b/tests/analysis/compiler_plugin/BUILD @@ -0,0 +1,23 @@ +# 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. + +licenses(["notice"]) + +genrule( + name = "empty_jar", + outs = ["empty.jar"], + cmd = """$(location @bazel_tools//tools/zip:zipper) c $@ "assets/_empty=" """, + tools = ["@bazel_tools//tools/zip:zipper"], + visibility = ["@//tests/analysis/compiler_plugin:__subpackages__"], +) diff --git a/tests/analysis/compiler_plugin/forbidden_target/BUILD b/tests/analysis/compiler_plugin/forbidden_target/BUILD new file mode 100644 index 0000000..282aab9 --- /dev/null +++ b/tests/analysis/compiler_plugin/forbidden_target/BUILD @@ -0,0 +1,19 @@ +# 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. + +load("@//kotlin:compiler_plugin.bzl", "kt_compiler_plugin") +load("@//tests/analysis:assert_failure_test.bzl", "assert_failure_test") +load("@//tests/analysis:util.bzl", "ONLY_FOR_ANALYSIS_TEST_TAGS") + +licenses(["notice"]) diff --git a/tests/analysis/compiler_plugin/propagation/BUILD b/tests/analysis/compiler_plugin/propagation/BUILD new file mode 100644 index 0000000..bf1896e --- /dev/null +++ b/tests/analysis/compiler_plugin/propagation/BUILD @@ -0,0 +1,79 @@ +# 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. + +load("@//kotlin:compiler_plugin.bzl", "kt_compiler_plugin") +load(":assert_propagation_test.bzl", "assert_propagation_test") + +licenses(["notice"]) + +assert_propagation_test( + name = "f", + expected_plugin_ids = ["1"], + deps = [":e"], +) + +assert_propagation_test( + name = "e", + expected_plugin_ids = [], + exports = [":a"], +) + +assert_propagation_test( + name = "d", + expected_plugin_ids = [ + "1", + "2", + ], + deps = [ + ":a", + ":b", + ], +) + +assert_propagation_test( + name = "c", + expected_plugin_ids = ["2"], + deps = [":b"], +) + +assert_propagation_test( + name = "b", + expected_plugin_ids = ["1"], + exported_plugins = [":2"], + deps = [":a"], +) + +assert_propagation_test( + name = "a", + expected_plugin_ids = [], + exported_plugins = [":1"], +) + +kt_compiler_plugin( + name = "1", + jar = "@//tests/analysis/compiler_plugin:empty_jar", + plugin_id = "1", +) + +kt_compiler_plugin( + name = "2", + jar = "@//tests/analysis/compiler_plugin:empty_jar", + plugin_id = "2", +) + +kt_compiler_plugin( + name = "3", + jar = "@//tests/analysis/compiler_plugin:empty_jar", + plugin_id = "3", +) diff --git a/tests/analysis/compiler_plugin/propagation/assert_propagation_test.bzl b/tests/analysis/compiler_plugin/propagation/assert_propagation_test.bzl new file mode 100644 index 0000000..7fb0ff2 --- /dev/null +++ b/tests/analysis/compiler_plugin/propagation/assert_propagation_test.bzl @@ -0,0 +1,56 @@ +# 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. + +"""Rule for asserting plugin propagation.""" + +load("@bazel_skylib//lib:sets.bzl", "sets") +load("@bazel_skylib//rules:build_test.bzl", "build_test") +load("@//kotlin:traverse_exports.bzl", "kt_traverse_exports") + +def _assert_propagation_impl(ctx): + expected_ids = sets.make(ctx.attr.expected_plugin_ids) + actual_ids = sets.make([ + p.plugin_id + for p in kt_traverse_exports.expand_compiler_plugins(ctx.attr.deps).to_list() + ]) + + if not sets.is_equal(expected_ids, actual_ids): + fail("Expected IDs %s, actual IDs %s" % (sets.to_list(expected_ids), sets.to_list(actual_ids))) + + return [ + # Needed for kt_traverse_exports.aspect + JavaInfo( + compile_jar = ctx.file._empty_jar, + output_jar = ctx.file._empty_jar, + ), + ] + +_assert_propagation = rule( + implementation = _assert_propagation_impl, + attrs = dict( + exports = attr.label_list(), + exported_plugins = attr.label_list(), + expected_plugin_ids = attr.string_list(), + deps = attr.label_list(aspects = [kt_traverse_exports.aspect]), + _empty_jar = attr.label( + allow_single_file = True, + default = "@//tests/analysis/compiler_plugin:empty_jar", + ), + ), +) + +def assert_propagation_test(name, **kwargs): + _assert_propagation(name = name, **kwargs) + + build_test(name = name + "_build", targets = [name]) diff --git a/tests/analysis/compiler_plugin/provider_ctor/BUILD b/tests/analysis/compiler_plugin/provider_ctor/BUILD new file mode 100644 index 0000000..eed998a --- /dev/null +++ b/tests/analysis/compiler_plugin/provider_ctor/BUILD @@ -0,0 +1,19 @@ +# 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. + +load("@//tests/analysis:assert_failure_test.bzl", "assert_failure_test") +load("@//tests/analysis:util.bzl", "ONLY_FOR_ANALYSIS_TEST_TAGS") +load(":fake_compiler_plugin.bzl", "kt_fake_compiler_plugin") + +licenses(["notice"]) diff --git a/tests/analysis/compiler_plugin/provider_ctor/fake_compiler_plugin.bzl b/tests/analysis/compiler_plugin/provider_ctor/fake_compiler_plugin.bzl new file mode 100644 index 0000000..62136ff --- /dev/null +++ b/tests/analysis/compiler_plugin/provider_ctor/fake_compiler_plugin.bzl @@ -0,0 +1,37 @@ +# 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. + +"""A fake impl of kt_compiler_plugin.""" + +load("@//kotlin:compiler_plugin.bzl", "KtCompilerPluginInfo") + +def _kt_fake_compiler_plugin_impl(ctx): + return [ + KtCompilerPluginInfo( + plugin_id = "fake", + jar = ctx.file._jar, + args = [], + ), + ] + +kt_fake_compiler_plugin = rule( + implementation = _kt_fake_compiler_plugin_impl, + attrs = dict( + _jar = attr.label( + allow_single_file = True, + default = "@//tests/analysis/compiler_plugin:empty_jar", + ), + ), + provides = [KtCompilerPluginInfo], +) diff --git a/tests/analysis/compiler_plugin/provider_output/BUILD b/tests/analysis/compiler_plugin/provider_output/BUILD new file mode 100644 index 0000000..9dd3c5c --- /dev/null +++ b/tests/analysis/compiler_plugin/provider_output/BUILD @@ -0,0 +1,58 @@ +# 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. + +load("@//kotlin:compiler_plugin.bzl", "kt_compiler_plugin") +load("@//tests/analysis:util.bzl", "create_file") +load("@bazel_skylib//rules:build_test.bzl", "build_test") +load(":assert_compiler_plugin_test.bzl", "assert_compiler_plugin_test") + +licenses(["notice"]) + +assert_compiler_plugin_test( + name = "example_plugin_test", + expected_args = [ + "plugin:com.google.example:key=value", + ], + expected_id = "com.google.example", + expected_jar = "@//tests/analysis/compiler_plugin:empty_jar", + target_under_test = ":example_plugin", +) + +build_test( + name = "example_plugin_in_java_library_build_test", + targets = [ + ":example_plugin_in_java_library", + ], +) + +java_library( + name = "example_plugin_in_java_library", + srcs = [create_file( + name = "Tmp.java", + content = """ + @SuppressWarnings("DefaultPackage") + class Tmp { } + """, + )], + plugins = [":example_plugin"], +) + +kt_compiler_plugin( + name = "example_plugin", + args = { + "key": "value", + }, + jar = "@//tests/analysis/compiler_plugin:empty_jar", + plugin_id = "com.google.example", +) diff --git a/tests/analysis/compiler_plugin/provider_output/assert_compiler_plugin_test.bzl b/tests/analysis/compiler_plugin/provider_output/assert_compiler_plugin_test.bzl new file mode 100644 index 0000000..819abd1 --- /dev/null +++ b/tests/analysis/compiler_plugin/provider_output/assert_compiler_plugin_test.bzl @@ -0,0 +1,37 @@ +# 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. + +"""An assertion on kt_compiler_plugin analysis.""" + +load("@//kotlin:compiler_plugin.bzl", "KtCompilerPluginInfo") +load("@bazel_skylib//lib:unittest.bzl", "analysistest", "asserts") + +def _test_impl(ctx): + env = analysistest.begin(ctx) + info = ctx.attr.target_under_test[KtCompilerPluginInfo] + + asserts.equals(env, info.plugin_id, ctx.attr.expected_id) + asserts.equals(env, info.jar, ctx.file.expected_jar) + asserts.equals(env, info.args, ctx.attr.expected_args) + + return analysistest.end(env) + +assert_compiler_plugin_test = analysistest.make( + impl = _test_impl, + attrs = dict( + expected_id = attr.string(), + expected_jar = attr.label(allow_single_file = True), + expected_args = attr.string_list(), + ), +) |