From 296fe3d842cb253a03ee6c8157090749ef55af49 Mon Sep 17 00:00:00 2001 From: nickreid Date: Mon, 9 Oct 2023 12:23:30 -0700 Subject: Move some more testing utils under common/testing PiperOrigin-RevId: 572008882 --- kotlin/common/testing/analysis.bzl | 79 ++++++++++++++++++++++ kotlin/common/testing/testing_rules.bzl | 113 ++++++++++++++++++++++++++++++++ tests/analysis/assert_failure_test.bzl | 15 +---- tests/analysis/for_test.bzl | 24 ++----- tests/analysis/util.bzl | 109 ++---------------------------- 5 files changed, 205 insertions(+), 135 deletions(-) create mode 100644 kotlin/common/testing/analysis.bzl create mode 100644 kotlin/common/testing/testing_rules.bzl diff --git a/kotlin/common/testing/analysis.bzl b/kotlin/common/testing/analysis.bzl new file mode 100644 index 0000000..7774ead --- /dev/null +++ b/kotlin/common/testing/analysis.bzl @@ -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. + +"""kt_analysis""" + +load("//:visibility.bzl", "RULES_KOTLIN") + +def _get_action(actions, mnemonic): + """Get a specific action + + Args: + actions: [List[Action]] + mnemonic: [string] Identify the action whose args to search + + Returns: + [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: + return None + elif len(menmonic_actions) > 1: + fail("Expected a single '%s' action" % mnemonic) + + return menmonic_actions[0] + +def _get_arg(action, arg_name, style = "trim"): + """Get a named arg from a specific action + + Args: + action: [Optional[Action]] + arg_name: [string] + style: ["trim"|"next"|"list"] The style of commandline arg + + Returns: + [Optional[string]] The arg value, or None if it couldn't be found + """ + if not action: + return None + + args = action.argv + matches = [(i, a) for (i, a) in enumerate(args) if a.startswith(arg_name)] + if len(matches) == 0: + return None + elif len(matches) > 1: + fail("Expected a single '%s' arg" % arg_name) + (index, arg) = matches[0] + + if style == "trim": + return arg[len(arg_name):] + elif style == "next": + return args[index + 1] + elif style == "list": + result = [] + for i in range(index + 1, len(args)): + if args[i].startswith("--"): + break + result.append(args[i]) + return result + + else: + fail("Unrecognized arg style '%s" % style) + +kt_analysis = struct( + # go/keep-sorted start + get_action = _get_action, + get_arg = _get_arg, + # go/keep-sorted end +) diff --git a/kotlin/common/testing/testing_rules.bzl b/kotlin/common/testing/testing_rules.bzl new file mode 100644 index 0000000..2e9c3df --- /dev/null +++ b/kotlin/common/testing/testing_rules.bzl @@ -0,0 +1,113 @@ +# 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_testing_rules""" + +load("//:visibility.bzl", "RULES_KOTLIN") +load("@bazel_skylib//lib:unittest.bzl", "analysistest", "asserts") + +# Mark targets that's aren't expected to build, but are needed for analysis test assertions. +_ONLY_FOR_ANALYSIS_TAGS = ["manual", "nobuilder", "notap"] + +def _wrap_for_analysis(inner_rule): + """Wrap an existing rule to make it easier to use in analysis tests. + + Args: + inner_rule: [rule|macro] + + Returns: + [macro] Calls inner_rule with appropate tags, returning the target name + """ + + def wrapper(name, tags = [], **kwargs): + inner_rule( + name = name, + tags = tags + _ONLY_FOR_ANALYSIS_TAGS, + **kwargs + ) + return name + + return wrapper + +_assert_failure_test = analysistest.make( + impl = lambda ctx: _assert_failure_test_impl(ctx), + expect_failure = True, + attrs = dict( + msg_contains = attr.string(mandatory = True), + ), +) + +def _assert_failure_test_impl(ctx): + env = analysistest.begin(ctx) + asserts.expect_failure(env, ctx.attr.msg_contains) + return analysistest.end(env) + +def _create_file(name, content = ""): + """Declare a generated file with optional content. + + Args: + name: [string] The relative file path + content: [string] + + Returns: + [File] The label of the file + """ + + if content.startswith("\n"): + content = content[1:-1] + + native.genrule( + name = "gen_" + name, + outs = [name], + cmd = """ +cat > $@ < $@ < 1: - fail("Expected a single '%s' action" % mnemonic) - - return menmonic_actions[0] - -def get_arg(action, arg_name, style = "trim"): - """Get a named arg from a specific action - - Args: - action: [Optional[Action]] - arg_name: [string] - style: [Optional[string]] The style of commandline arg - - Returns: - [Optional[string]] The arg value, or None if it couldn't be found - """ - if not action: - return None - - args = action.argv - matches = [(i, a) for (i, a) in enumerate(args) if a.startswith(arg_name)] - if len(matches) == 0: - return None - elif len(matches) > 1: - fail("Expected a single '%s' arg" % arg_name) - (index, arg) = matches[0] - - if style == "trim": - return arg[len(arg_name):] - elif style == "next": - return args[index + 1] - elif style == "list": - result = [] - for i in range(index + 1, len(args)): - if args[i].startswith("--"): - break - result.append(args[i]) - return result - - else: - fail("Unrecognized arg style '%s" % style) +get_action = kt_analysis.get_action -- cgit v1.2.3