From 72d1bca8a1642c41fa9b266ffdc2f0ff980d84a2 Mon Sep 17 00:00:00 2001 From: Ignas Anikevicius <240938+aignas@users.noreply.github.com> Date: Wed, 31 Jan 2024 22:35:08 +0900 Subject: [PATCH] test: config_settings matching latest minor versions This is a followup to #1720 to add better tests as commented in the PR and we ensure that we match correctly when the config setting is configured to a minor version. --- .../construct_config_settings_tests.bzl | 50 +++++++++++++++++ tests/toolchains/config_settings/BUILD.bazel | 54 ------------------- tests/toolchains/config_settings/want | 27 ---------- 3 files changed, 50 insertions(+), 81 deletions(-) delete mode 100644 tests/toolchains/config_settings/BUILD.bazel delete mode 100644 tests/toolchains/config_settings/want diff --git a/tests/config_settings/construct_config_settings_tests.bzl b/tests/config_settings/construct_config_settings_tests.bzl index 8e4217139..61beb9cd4 100644 --- a/tests/config_settings/construct_config_settings_tests.bzl +++ b/tests/config_settings/construct_config_settings_tests.bzl @@ -13,6 +13,7 @@ # limitations under the License. """Tests for construction of Python version matching config settings.""" +load("@//python:versions.bzl", "MINOR_MAPPING") load("@rules_testing//lib:analysis_test.bzl", "analysis_test") load("@rules_testing//lib:test_suite.bzl", "test_suite") load("@rules_testing//lib:truth.bzl", "subjects") @@ -30,6 +31,7 @@ _subject = rule( "match_micro": attr.string(), "match_minor": attr.string(), "no_match": attr.string(), + "no_match_micro": attr.string(), }, ) @@ -74,6 +76,54 @@ def _test_minor_version_matching_impl(env, target): _tests.append(_test_minor_version_matching) +def _test_latest_micro_version_matching(name): + rt_util.helper_target( + _subject, + name = name + "_subject", + match_minor = select({ + "//python/config_settings:is_python_3.12": "matched-3.12", + "//conditions:default": "matched-default", + }), + match_micro = select({ + "//python/config_settings:is_python_" + MINOR_MAPPING["3.12"]: "matched-3.12", + "//conditions:default": "matched-default", + }), + no_match_micro = select({ + "//python/config_settings:is_python_3.12.0": "matched-3.12", + "//conditions:default": "matched-default", + }), + no_match = select({ + "//python/config_settings:is_python_" + MINOR_MAPPING["3.11"]: "matched-3.11", + "//conditions:default": "matched-default", + }), + ) + + analysis_test( + name = name, + target = name + "_subject", + impl = _test_latest_micro_version_matching_impl, + config_settings = { + str(Label("//python/config_settings:python_version")): "3.12", + }, + ) + +def _test_latest_micro_version_matching_impl(env, target): + target = env.expect.that_target(target) + target.attr("match_minor", factory = subjects.str).equals( + "matched-3.12", + ) + target.attr("match_micro", factory = subjects.str).equals( + "matched-3.12", + ) + target.attr("no_match_micro", factory = subjects.str).equals( + "matched-default", + ) + target.attr("no_match", factory = subjects.str).equals( + "matched-default", + ) + +_tests.append(_test_latest_micro_version_matching) + def construct_config_settings_test_suite(name): test_suite( name = name, diff --git a/tests/toolchains/config_settings/BUILD.bazel b/tests/toolchains/config_settings/BUILD.bazel deleted file mode 100644 index babd19f17..000000000 --- a/tests/toolchains/config_settings/BUILD.bazel +++ /dev/null @@ -1,54 +0,0 @@ -# Copyright 2024 The Bazel Authors. 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("@bazel_skylib//rules:diff_test.bzl", "diff_test") - -# Do bazel query for the following targets and put the results into a file -_TARGETS = [ - "is_python_3.12.0", - "is_python_3.12.1", - "_is_python_3.12.1", - "_python_version_flag_equals_3.12.1", - "_python_version_flag_equals_3.12", -] - -[ - genquery( - name = target, - expression = "//python/config_settings:" + target, - opts = ["--output=build"], - scope = ["//python/config_settings:" + target], - ) - for target in _TARGETS -] - -genrule( - name = "config_settings_query", - srcs = _TARGETS, - outs = ["config_settings_query.txt"], - # Strip comments that are specific to the host it is being run on and make the - # expectation output more maintainable. - cmd = "sed -e '/^#/d' -e '/^ generator_/d' $(SRCS) >$@", - target_compatible_with = select({ - # We don't have sed available on Windows - "@platforms//os:windows": ["@platforms//:incompatible"], - "//conditions:default": [], - }), -) - -diff_test( - name = "config_settings_test", - file1 = "want", - file2 = "config_settings_query", -) diff --git a/tests/toolchains/config_settings/want b/tests/toolchains/config_settings/want deleted file mode 100644 index 0ecbd67b6..000000000 --- a/tests/toolchains/config_settings/want +++ /dev/null @@ -1,27 +0,0 @@ -config_setting( - name = "is_python_3.12.0", - visibility = ["//visibility:public"], - flag_values = {"//python/config_settings:python_version": "3.12.0"}, -) - -alias( - name = "is_python_3.12.1", - visibility = ["//visibility:public"], - actual = "//python/config_settings:_is_python_3.12.1", -) - -alias( - name = "_is_python_3.12.1", - actual = select({"//python/config_settings:_python_version_flag_equals_3.12.1": "//python/config_settings:_python_version_flag_equals_3.12.1", "//conditions:default": "//python/config_settings:_python_version_flag_equals_3.12"}), -) - -config_setting( - name = "_python_version_flag_equals_3.12.1", - flag_values = {"//python/config_settings:python_version": "3.12.1"}, -) - -config_setting( - name = "_python_version_flag_equals_3.12", - flag_values = {"//python/config_settings:python_version": "3.12"}, -) -