• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2023 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 py_cc_toolchain."""
16
17load("@rules_testing//lib:analysis_test.bzl", "analysis_test", "test_suite")
18load("@rules_testing//lib:truth.bzl", "matching", "subjects")
19load("//python/cc:py_cc_toolchain.bzl", "py_cc_toolchain")
20load("//tests/support:cc_info_subject.bzl", "cc_info_subject")
21load("//tests/support:py_cc_toolchain_info_subject.bzl", "PyCcToolchainInfoSubject")
22
23_tests = []
24
25def _test_py_cc_toolchain(name):
26    analysis_test(
27        name = name,
28        impl = _test_py_cc_toolchain_impl,
29        target = "//tests/support/cc_toolchains:fake_py_cc_toolchain_impl",
30        attrs = {
31            "header": attr.label(
32                default = "//tests/support/cc_toolchains:fake_header.h",
33                allow_single_file = True,
34            ),
35        },
36    )
37
38def _test_py_cc_toolchain_impl(env, target):
39    env.expect.that_target(target).has_provider(platform_common.ToolchainInfo)
40
41    toolchain = PyCcToolchainInfoSubject.new(
42        target[platform_common.ToolchainInfo].py_cc_toolchain,
43        meta = env.expect.meta.derive(expr = "py_cc_toolchain_info"),
44    )
45    toolchain.python_version().equals("3.999")
46
47    headers_providers = toolchain.headers().providers_map()
48    headers_providers.keys().contains_exactly(["CcInfo", "DefaultInfo"])
49
50    cc_info = headers_providers.get("CcInfo", factory = cc_info_subject)
51
52    compilation_context = cc_info.compilation_context()
53    compilation_context.direct_headers().contains_exactly([
54        env.ctx.file.header,
55    ])
56    compilation_context.direct_public_headers().contains_exactly([
57        env.ctx.file.header,
58    ])
59
60    # NOTE: The include dir gets added twice, once for the source path,
61    # and once for the config-specific path, but we don't care about that.
62    compilation_context.system_includes().contains_at_least_predicates([
63        matching.str_matches("*/fake_include"),
64    ])
65
66    default_info = headers_providers.get("DefaultInfo", factory = subjects.default_info)
67    default_info.runfiles().contains_predicate(
68        matching.str_matches("*/cc_toolchains/data.txt"),
69    )
70
71    libs_providers = toolchain.libs().providers_map()
72    libs_providers.keys().contains_exactly(["CcInfo", "DefaultInfo"])
73
74    cc_info = libs_providers.get("CcInfo", factory = cc_info_subject)
75
76    cc_info.linking_context().linker_inputs().has_size(2)
77
78    default_info = libs_providers.get("DefaultInfo", factory = subjects.default_info)
79    default_info.runfiles().contains("{workspace}/tests/support/cc_toolchains/libdata.txt")
80    default_info.runfiles().contains_predicate(
81        matching.str_matches("/libpython3."),
82    )
83
84_tests.append(_test_py_cc_toolchain)
85
86def _test_libs_optional(name):
87    py_cc_toolchain(
88        name = name + "_subject",
89        libs = None,
90        headers = "//tests/support/cc_toolchains:fake_headers",
91        python_version = "4.5",
92    )
93    analysis_test(
94        name = name,
95        target = name + "_subject",
96        impl = _test_libs_optional_impl,
97    )
98
99def _test_libs_optional_impl(env, target):
100    libs = target[platform_common.ToolchainInfo].py_cc_toolchain.libs
101    env.expect.that_bool(libs == None).equals(True)
102
103_tests.append(_test_libs_optional)
104
105def py_cc_toolchain_test_suite(name):
106    test_suite(
107        name = name,
108        tests = _tests,
109    )
110