|
| 1 | +# Copyright 2026 The Bazel Authors. All rights reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +"Tests for the warning emitted by coverage_dep when no wheel is available." |
| 16 | + |
| 17 | +load("@rules_testing//lib:test_suite.bzl", "test_suite") |
| 18 | +load("//python/private:coverage_deps.bzl", "coverage_dep") # buildifier: disable=bzl-visibility |
| 19 | +load("//python/private:repo_utils.bzl", "REPO_DEBUG_ENV_VAR", "REPO_VERBOSITY_ENV_VAR", "repo_utils") # buildifier: disable=bzl-visibility |
| 20 | + |
| 21 | +_tests = [] |
| 22 | + |
| 23 | +def _capturing_logger(): |
| 24 | + """Build a (logger, captured_messages_list) pair. |
| 25 | +
|
| 26 | + The logger has its verbosity set to INFO so WARN messages are captured but |
| 27 | + nothing noisier than necessary is emitted. The printer collects the second |
| 28 | + positional argument from each printer invocation (the formatted message). |
| 29 | + """ |
| 30 | + captured = [] |
| 31 | + logger = repo_utils.logger( |
| 32 | + struct( |
| 33 | + getenv = { |
| 34 | + REPO_DEBUG_ENV_VAR: None, |
| 35 | + REPO_VERBOSITY_ENV_VAR: "INFO", |
| 36 | + }.get, |
| 37 | + ), |
| 38 | + name = "unit-test", |
| 39 | + printer = lambda _key, message: captured.append(message), |
| 40 | + ) |
| 41 | + return logger, captured |
| 42 | + |
| 43 | +def _test_unsupported_python_version_warns(env): |
| 44 | + # cp37 is not in the bundled wheel set; coverage_dep should return None |
| 45 | + # and emit a warning describing the misconfiguration. |
| 46 | + logger, captured = _capturing_logger() |
| 47 | + result = coverage_dep( |
| 48 | + name = "unused_for_test", |
| 49 | + python_version = "3.7", |
| 50 | + platform = "aarch64-apple-darwin", |
| 51 | + visibility = ["//visibility:public"], |
| 52 | + logger = logger, |
| 53 | + ) |
| 54 | + env.expect.that_bool(result == None).equals(True) |
| 55 | + env.expect.that_int(len(captured)).equals(1) |
| 56 | + env.expect.that_str(captured[0]).contains("no wheel for") |
| 57 | + env.expect.that_str(captured[0]).contains("python_version=3.7") |
| 58 | + env.expect.that_str(captured[0]).contains("platform=aarch64-apple-darwin") |
| 59 | + |
| 60 | +_tests.append(_test_unsupported_python_version_warns) |
| 61 | + |
| 62 | +def _test_windows_platform_is_silent(env): |
| 63 | + # Windows is intentionally unsupported and not actionable; coverage_dep |
| 64 | + # must return None without logging anything. |
| 65 | + logger, captured = _capturing_logger() |
| 66 | + result = coverage_dep( |
| 67 | + name = "unused_for_test", |
| 68 | + python_version = "3.10", |
| 69 | + platform = "x86_64-pc-windows-msvc", |
| 70 | + visibility = ["//visibility:public"], |
| 71 | + logger = logger, |
| 72 | + ) |
| 73 | + env.expect.that_bool(result == None).equals(True) |
| 74 | + env.expect.that_int(len(captured)).equals(0) |
| 75 | + |
| 76 | +_tests.append(_test_windows_platform_is_silent) |
| 77 | + |
| 78 | +# NOTE: there is intentionally no unit test for the supported-wheel path |
| 79 | +# (where coverage_dep returns a non-None label and emits no warning). |
| 80 | +# That path calls `maybe(http_archive, ...)`, which calls |
| 81 | +# `native.existing_rule()`. `native.existing_rule()` is only valid during |
| 82 | +# BUILD file, legacy macro, or rule finalizer evaluation -- not during |
| 83 | +# rule analysis, which is the phase rules_testing analysis tests run in. |
| 84 | +# Calling coverage_dep with supported args from here therefore fails with |
| 85 | +# "existing_rule() can only be used while evaluating a BUILD file, ...". |
| 86 | +# The supported-wheel path is exercised end-to-end by `bazel coverage` |
| 87 | +# against a real py_test target during ordinary use of the toolchain. |
| 88 | + |
| 89 | +def coverage_deps_test_suite(name): |
| 90 | + """Create the test suite. |
| 91 | +
|
| 92 | + Args: |
| 93 | + name: the name of the test suite. |
| 94 | + """ |
| 95 | + test_suite(name = name, basic_tests = _tests) |
0 commit comments