• 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")
19load("//tests:cc_info_subject.bzl", "cc_info_subject")
20load("//tests:default_info_subject.bzl", "default_info_subject")
21load("//tests:py_cc_toolchain_info_subject.bzl", "PyCcToolchainInfoSubject")
22
23_tests = []
24
25def _py_cc_toolchain_test(name):
26    analysis_test(
27        name = name,
28        impl = _py_cc_toolchain_test_impl,
29        target = "//tests/cc:fake_py_cc_toolchain_impl",
30        attrs = {
31            "header": attr.label(
32                default = "//tests/cc:fake_header.h",
33                allow_single_file = True,
34            ),
35        },
36    )
37
38def _py_cc_toolchain_test_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    # TODO: Once subjects.default_info is available, do
67    # default_info = headers_providers.get("DefaultInfo", factory=subjects.default_info)
68    # https://github.com/bazelbuild/rules_python/issues/1297
69    default_info = default_info_subject(
70        headers_providers.get("DefaultInfo", factory = lambda v, meta: v),
71        meta = env.expect.meta.derive(expr = "default_info"),
72    )
73    default_info.runfiles().contains_predicate(
74        matching.str_matches("*/cc/data.txt"),
75    )
76
77_tests.append(_py_cc_toolchain_test)
78
79def py_cc_toolchain_test_suite(name):
80    test_suite(
81        name = name,
82        tests = _tests,
83    )
84