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 /tests | |
parent | cb486c326eacd22dad98160fb36d0b706e10fbbb (diff) | |
download | bazelbuild-kotlin-rules-09d89590f3f7e36b2c5d7c7fcfd7791e8bbb17eb.tar.gz |
Define kt_compiler_plugin rule for kotlinc plugins.
PiperOrigin-RevId: 464101180
Change-Id: If46a509f5c830096dc47d6d74daab110e2000cbf
Diffstat (limited to 'tests')
9 files changed, 358 insertions, 0 deletions
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(), + ), +) |