diff options
Diffstat (limited to 'tests/analysis')
16 files changed, 370 insertions, 26 deletions
diff --git a/tests/analysis/assert_failure_test.bzl b/tests/analysis/assert_failure_test.bzl index b266c2a..2a742c3 100644 --- a/tests/analysis/assert_failure_test.bzl +++ b/tests/analysis/assert_failure_test.bzl @@ -15,6 +15,7 @@ """An assertion for analysis failure.""" load("@bazel_skylib//lib:unittest.bzl", "analysistest", "asserts") +load("//:visibility.bzl", "RULES_KOTLIN") def _assert_failure_test_impl(ctx): env = analysistest.begin(ctx) diff --git a/tests/analysis/compiler_plugin/propagation/assert_propagation_test.bzl b/tests/analysis/compiler_plugin/propagation/assert_propagation_test.bzl index fe8c9dc..0c1b02a 100644 --- a/tests/analysis/compiler_plugin/propagation/assert_propagation_test.bzl +++ b/tests/analysis/compiler_plugin/propagation/assert_propagation_test.bzl @@ -17,6 +17,7 @@ load("@bazel_skylib//lib:sets.bzl", "sets") load("@bazel_skylib//rules:build_test.bzl", "build_test") load("//kotlin:traverse_exports.bzl", "kt_traverse_exports") +load("//:visibility.bzl", "RULES_KOTLIN") def _assert_propagation_impl(ctx): expected_ids = sets.make(ctx.attr.expected_plugin_ids) diff --git a/tests/analysis/compiler_plugin/provider_ctor/fake_compiler_plugin.bzl b/tests/analysis/compiler_plugin/provider_ctor/fake_compiler_plugin.bzl index d4a6bac..194025d 100644 --- a/tests/analysis/compiler_plugin/provider_ctor/fake_compiler_plugin.bzl +++ b/tests/analysis/compiler_plugin/provider_ctor/fake_compiler_plugin.bzl @@ -15,6 +15,7 @@ """A fake impl of kt_compiler_plugin.""" load("//kotlin:compiler_plugin.bzl", "KtCompilerPluginInfo") +load("//:visibility.bzl", "RULES_KOTLIN") def _kt_fake_compiler_plugin_impl(ctx): return [ 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 index 9b2951e..f4e1849 100644 --- a/tests/analysis/compiler_plugin/provider_output/assert_compiler_plugin_test.bzl +++ b/tests/analysis/compiler_plugin/provider_output/assert_compiler_plugin_test.bzl @@ -16,6 +16,7 @@ load("//kotlin:compiler_plugin.bzl", "KtCompilerPluginInfo") load("@bazel_skylib//lib:unittest.bzl", "analysistest", "asserts") +load("//:visibility.bzl", "RULES_KOTLIN") def _test_impl(ctx): env = analysistest.begin(ctx) diff --git a/tests/analysis/for_test.bzl b/tests/analysis/for_test.bzl index 434c1a4..2a75726 100644 --- a/tests/analysis/for_test.bzl +++ b/tests/analysis/for_test.bzl @@ -16,6 +16,7 @@ load("//kotlin:jvm_library.bzl", "kt_jvm_library") load("//tests/analysis:util.bzl", "ONLY_FOR_ANALYSIS_TEST_TAGS") +load("//:visibility.bzl", "RULES_KOTLIN") def _kt_jvm_library_for_test(name, **kwargs): kt_jvm_library( diff --git a/tests/analysis/internal_do_not_use/util/file_factory/BUILD b/tests/analysis/internal_do_not_use/util/file_factory/BUILD new file mode 100644 index 0000000..d92e379 --- /dev/null +++ b/tests/analysis/internal_do_not_use/util/file_factory/BUILD @@ -0,0 +1,41 @@ +# 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(":happy_test.bzl", "file_factory_happy_test") +load(":check_base_file_valid.bzl", "check_base_file_valid") +load("//tests/analysis:assert_failure_test.bzl", "assert_failure_test") + +licenses(["notice"]) + +file_factory_happy_test( + name = "happy_test", +) + +assert_failure_test( + name = "base_without_extension_test", + msg_contains = "file must have an extension", + target_under_test = check_base_file_valid( + name = "base_without_extension", + base_file = "BUILD", + ), +) + +assert_failure_test( + name = "base_from_other_package_test", + msg_contains = "file must be from ctx package", + target_under_test = check_base_file_valid( + name = "base_from_other_package", + base_file = "//tests/analysis/internal_do_not_use/util/file_factory/sub", + ), +) diff --git a/tests/analysis/internal_do_not_use/util/file_factory/check_base_file_valid.bzl b/tests/analysis/internal_do_not_use/util/file_factory/check_base_file_valid.bzl new file mode 100644 index 0000000..9ca1f05 --- /dev/null +++ b/tests/analysis/internal_do_not_use/util/file_factory/check_base_file_valid.bzl @@ -0,0 +1,38 @@ +# 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. + +"""Happy tests for FileFactory.""" + +load("//kotlin/jvm/internal_do_not_use/util:file_factory.bzl", "FileFactory") +load("//tests/analysis:util.bzl", "ONLY_FOR_ANALYSIS_TEST_TAGS") +load("//:visibility.bzl", "RULES_KOTLIN") + +def _check_base_file_valid_impl(ctx): + FileFactory(ctx, ctx.file.base_file) + return [] + +_check_base_file_valid = rule( + implementation = _check_base_file_valid_impl, + attrs = dict( + base_file = attr.label(allow_single_file = True, mandatory = True), + ), +) + +def check_base_file_valid(name, tags = [], **kwargs): + _check_base_file_valid( + name = name, + tags = tags + ONLY_FOR_ANALYSIS_TEST_TAGS, + **kwargs + ) + return name diff --git a/tests/analysis/internal_do_not_use/util/file_factory/happy_test.bzl b/tests/analysis/internal_do_not_use/util/file_factory/happy_test.bzl new file mode 100644 index 0000000..1a2a655 --- /dev/null +++ b/tests/analysis/internal_do_not_use/util/file_factory/happy_test.bzl @@ -0,0 +1,78 @@ +# 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. + +"""file_factory_happy_test""" + +load("//kotlin/jvm/internal_do_not_use/util:file_factory.bzl", "FileFactory") +load("//:visibility.bzl", "RULES_KOTLIN") + +def _test_base_from_file(ctx, pkg_path): + base_file = ctx.actions.declare_file("file/base.txt") + factory = FileFactory(ctx, base_file) + + _assert_equals(pkg_path + "/file/base", factory.base_as_path) + + return [base_file] + +def _test_declare(ctx, pkg_path): + factory = FileFactory(ctx, "string/base") + + _assert_equals(pkg_path + "/string/base", factory.base_as_path) + + a_file = factory.declare_file("a.txt") + _assert_equals(pkg_path + "/string/basea.txt", a_file.path) + + b_dir = factory.declare_directory("b_dir") + _assert_equals(pkg_path + "/string/baseb_dir", b_dir.path) + + return [a_file, b_dir] + +def _test_derive(ctx, pkg_path): + factory = FileFactory(ctx, "") + + # Once + factory_once = factory.derive("once") + _assert_equals(pkg_path + "/once", factory_once.base_as_path) + + # Twice + factory_twice = factory_once.derive("/twice") + _assert_equals(pkg_path + "/once/twice", factory_twice.base_as_path) + +def _assert_equals(expected, actual): + if expected != actual: + fail("Expected '%s' but was '%s'" % (expected, actual)) + +def _file_factory_happy_test_impl(ctx): + pkg_path = ctx.bin_dir.path + "/" + ctx.label.package + all_files = [] + + all_files.extend(_test_base_from_file(ctx, pkg_path)) + all_files.extend(_test_declare(ctx, pkg_path)) + _test_derive(ctx, pkg_path) + + ctx.actions.run_shell( + outputs = all_files, + command = "exit 1", + ) + + test_script = ctx.actions.declare_file(ctx.label.name + "_test.sh") + ctx.actions.write(test_script, "#!/bin/bash", True) + return [ + DefaultInfo(executable = test_script), + ] + +file_factory_happy_test = rule( + implementation = _file_factory_happy_test_impl, + test = True, +) diff --git a/tests/analysis/internal_do_not_use/util/file_factory/sub/BUILD b/tests/analysis/internal_do_not_use/util/file_factory/sub/BUILD new file mode 100644 index 0000000..db873cf --- /dev/null +++ b/tests/analysis/internal_do_not_use/util/file_factory/sub/BUILD @@ -0,0 +1,22 @@ +# 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 = "sub", + outs = ["sub.txt"], + cmd = "touch $(OUTS)", + visibility = ["//tests/analysis/internal_do_not_use/util/file_factory:__pkg__"], +) diff --git a/tests/analysis/jvm_compile_test.bzl b/tests/analysis/jvm_compile_test.bzl index b361f5a..a8fd460 100644 --- a/tests/analysis/jvm_compile_test.bzl +++ b/tests/analysis/jvm_compile_test.bzl @@ -22,6 +22,7 @@ load("//toolchains/kotlin_jvm:java_toolchains.bzl", "java_toolchains") load("//toolchains/kotlin_jvm:kt_jvm_toolchains.bzl", "kt_jvm_toolchains") load("@bazel_skylib//rules:build_test.bzl", "build_test") load(":assert_failure_test.bzl", "assert_failure_test") +load("//:visibility.bzl", "RULES_KOTLIN") def _impl(ctx): # As additional capabilites need to be tested, this rule should support diff --git a/tests/analysis/jvm_import_test.bzl b/tests/analysis/jvm_import_test.bzl index a3bfd19..6e56c02 100644 --- a/tests/analysis/jvm_import_test.bzl +++ b/tests/analysis/jvm_import_test.bzl @@ -19,6 +19,7 @@ load("//kotlin:jvm_library.bzl", "kt_jvm_library") load("@bazel_skylib//lib:unittest.bzl", "analysistest", "asserts") load("//tests/analysis:util.bzl", "ONLY_FOR_ANALYSIS_TEST_TAGS", "create_file") load(":assert_failure_test.bzl", "assert_failure_test") +load("//:visibility.bzl", "RULES_KOTLIN") def _impl(ctx): env = analysistest.begin(ctx) diff --git a/tests/analysis/jvm_library/treeartifacts_srcs/BUILD b/tests/analysis/jvm_library/treeartifacts_srcs/BUILD new file mode 100644 index 0000000..3d724c4 --- /dev/null +++ b/tests/analysis/jvm_library/treeartifacts_srcs/BUILD @@ -0,0 +1,104 @@ +# 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:for_test.bzl", "rules_for_test") +load("//tests/analysis:assert_failure_test.bzl", "assert_failure_test") +load("//tests/analysis:jvm_library_test.bzl", "jvm_library_test") +load("//tests/analysis:util.bzl", "create_dir") + +package(default_testonly = True) + +licenses(["notice"]) + +jvm_library_test( + name = "treeartifact_basename_kotlin_test", + target_under_test = rules_for_test.kt_jvm_library( + name = "treeartifact_basename_kotlin", + srcs = [ + create_dir( + name = "treeartifact_extension/kotlin", + srcs = [ + "Input.java", # TODO: Reject this source + ], + ), + ], + ), +) + +jvm_library_test( + name = "treeartifact_basename_java_test", + target_under_test = rules_for_test.kt_jvm_library( + name = "treeartifact_basename_java", + srcs = [ + create_dir( + name = "treeartifact_extension/java", + srcs = [ + "Input.kt", # TODO: Reject this source + ], + ), + ], + ), +) + +assert_failure_test( + name = "treeartifact_extension_kt_test", + msg_contains = "/treeartifact_extension.kt", + target_under_test = rules_for_test.kt_jvm_library( + name = "treeartifact_extension_kt", + srcs = [ + create_dir( + name = "treeartifact_extension.kt", + ), + ], + ), +) + +assert_failure_test( + name = "treeartifact_extension_java_test", + msg_contains = "/treeartifact_extension.java", + target_under_test = rules_for_test.kt_jvm_library( + name = "treeartifact_extension_java", + srcs = [ + create_dir( + name = "treeartifact_extension.java", + ), + ], + ), +) + +assert_failure_test( + name = "treeartifact_extension_srcjar_test", + msg_contains = "/treeartifact_extension.srcjar", + target_under_test = rules_for_test.kt_jvm_library( + name = "treeartifact_extension_srcjar", + srcs = [ + create_dir( + name = "treeartifact_extension.srcjar", + ), + ], + ), +) + +assert_failure_test( + name = "treeartifact_no_extension_test", + msg_contains = "/treeartifact_no_extension", + target_under_test = rules_for_test.kt_jvm_library( + name = "treeartifact_no_extension", + srcs = [ + create_dir( + name = "treeartifact_no_extension_dir", + ), + ], + ), +) diff --git a/tests/analysis/jvm_library/treeartifacts_srcs/Input.java b/tests/analysis/jvm_library/treeartifacts_srcs/Input.java new file mode 100644 index 0000000..e675bc1 --- /dev/null +++ b/tests/analysis/jvm_library/treeartifacts_srcs/Input.java @@ -0,0 +1,16 @@ +/* + * * 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. + */ + diff --git a/tests/analysis/jvm_library/treeartifacts_srcs/Input.kt b/tests/analysis/jvm_library/treeartifacts_srcs/Input.kt new file mode 100644 index 0000000..e675bc1 --- /dev/null +++ b/tests/analysis/jvm_library/treeartifacts_srcs/Input.kt @@ -0,0 +1,16 @@ +/* + * * 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. + */ + diff --git a/tests/analysis/jvm_library_test.bzl b/tests/analysis/jvm_library_test.bzl index 6435cc5..d51e6e1 100644 --- a/tests/analysis/jvm_library_test.bzl +++ b/tests/analysis/jvm_library_test.bzl @@ -15,10 +15,11 @@ """Kotlin kt_jvm_library rule tests.""" load("//kotlin:jvm_library.bzl", "kt_jvm_library") -load("//tests/analysis:util.bzl", "ONLY_FOR_ANALYSIS_TEST_TAGS", "create_file", "get_action_arg") +load("//tests/analysis:util.bzl", "ONLY_FOR_ANALYSIS_TEST_TAGS", "create_file", "get_action", "get_arg") load("@bazel_skylib//lib:sets.bzl", "sets") load("@bazel_skylib//lib:unittest.bzl", "analysistest", "asserts") load(":assert_failure_test.bzl", "assert_failure_test") +load("//:visibility.bzl", "RULES_KOTLIN") _DEFAULT_LIST = ["__default__"] @@ -84,8 +85,17 @@ def _test_impl(ctx): 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_action_arg(actions, "Kt2JavaCompile", "-Xfriend-paths=") + friend_paths_arg = get_arg(kt_2_java_compile, "-Xfriend-paths=") friend_jar_names = [p.rsplit("/", 1)[1] for p in friend_paths_arg.split(",")] if friend_paths_arg else [] asserts.set_equals(env, sets.make(ctx.attr.expected_friend_jar_names), sets.make(friend_jar_names)) diff --git a/tests/analysis/util.bzl b/tests/analysis/util.bzl index cec0cc3..5e7e487 100644 --- a/tests/analysis/util.bzl +++ b/tests/analysis/util.bzl @@ -14,6 +14,8 @@ """Some utils""" +load("//:visibility.bzl", "RULES_KOTLIN") + # Mark targets that's aren't expected to build, but are needed for analysis test assertions. ONLY_FOR_ANALYSIS_TEST_TAGS = ["manual", "nobuilder", "only_for_analysis_test"] @@ -35,23 +37,17 @@ EOF def _create_dir_impl(ctx): dir = ctx.actions.declare_directory(ctx.attr.name) - if ctx.files.srcs: - ctx.actions.run_shell( - command = "mkdir -p {0} && cp {1} {0}".format( - dir.path + "/" + ctx.attr.subdir, - " ".join([s.path for s in ctx.files.srcs]), - ), - inputs = ctx.files.srcs, - outputs = [dir], - ) - else: - ctx.actions.run_shell( - command = "mkdir -p {0}".format( - dir.path + "/" + ctx.attr.subdir, - ), - inputs = ctx.files.srcs, - outputs = [dir], - ) + + command = "mkdir -p {0} " + ("&& cp {1} {0}" if ctx.files.srcs else "# {1}") + ctx.actions.run_shell( + command = command.format( + dir.path + "/" + ctx.attr.subdir, + " ".join([s.path for s in ctx.files.srcs]), + ), + inputs = ctx.files.srcs, + outputs = [dir], + ) + return [DefaultInfo(files = depset([dir]))] _create_dir = rule( @@ -62,7 +58,10 @@ _create_dir = rule( ), ) -def create_dir(name, subdir, srcs): +def create_dir( + name, + subdir = None, + srcs = None): _create_dir( name = name, subdir = subdir, @@ -70,16 +69,15 @@ def create_dir(name, subdir, srcs): ) return name -def get_action_arg(actions, mnemonic, arg_name): - """Get a named arg from a specific action +def get_action(actions, mnemonic): + """Get a specific action Args: actions: [List[Action]] mnemonic: [string] Identify the action whose args to search - arg_name: [string] Returns: - [Optional[string]] The arg value, or None if it couldn't be found + [Optional[action]] The arg value, or None if it couldn't be found """ menmonic_actions = [a for a in actions if a.mnemonic == mnemonic] if len(menmonic_actions) == 0: @@ -87,8 +85,22 @@ def get_action_arg(actions, mnemonic, arg_name): elif len(menmonic_actions) > 1: fail("Expected a single '%s' action" % mnemonic) - mnemonic_action = menmonic_actions[0] - arg_values = [a for a in mnemonic_action.argv if a.startswith(arg_name)] + return menmonic_actions[0] + +def get_arg(action, arg_name): + """Get a named arg from a specific action + + Args: + action: [Optional[Action]] + arg_name: [string] + + Returns: + [Optional[string]] The arg value, or None if it couldn't be found + """ + if not action: + return None + + arg_values = [a for a in action.argv if a.startswith(arg_name)] if len(arg_values) == 0: return None elif len(arg_values) > 1: |