Skip to content

Commit 612baef

Browse files
authored
tests: move various supporting code under tests/support (bazel-contrib#2183)
tests: move various supporting code under tests/support * Move subject classes under support * Switch to using rules_testing DefaultInfo subject * Move fake cc toolchains under tests/support/cc_toolchains (these are used in several places, not just tests/cc) * Make literal references of old //tests/cc locations use the constants from support.bzl This is both for code clarity (support code being in a common location), but also to make it easier to import the support code for the subset of tests Google imports and runs.
1 parent fe1d9a7 commit 612baef

20 files changed

+188
-182
lines changed

tests/base_rules/base_tests.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ load("@rules_testing//lib:truth.bzl", "matching")
1818
load("@rules_testing//lib:util.bzl", "PREVENT_IMPLICIT_BUILDING_TAGS", rt_util = "util")
1919
load("//python:defs.bzl", "PyInfo")
2020
load("//python/private:reexports.bzl", "BuiltinPyInfo") # buildifier: disable=bzl-visibility
21-
load("//tests/base_rules:py_info_subject.bzl", "py_info_subject")
2221
load("//tests/base_rules:util.bzl", pt_util = "util")
22+
load("//tests/support:py_info_subject.bzl", "py_info_subject")
2323

2424
_tests = []
2525

tests/base_rules/precompile/precompile_tests.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ load("//python:py_binary.bzl", "py_binary")
2323
load("//python:py_info.bzl", "PyInfo")
2424
load("//python:py_library.bzl", "py_library")
2525
load("//python:py_test.bzl", "py_test")
26-
load("//tests/base_rules:py_info_subject.bzl", "py_info_subject")
26+
load("//tests/support:py_info_subject.bzl", "py_info_subject")
2727
load(
2828
"//tests/support:support.bzl",
2929
"CC_TOOLCHAIN",

tests/base_rules/py_executable_base_tests.bzl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ load("//python/private:util.bzl", "IS_BAZEL_7_OR_HIGHER") # buildifier: disable
2323
load("//tests/base_rules:base_tests.bzl", "create_base_tests")
2424
load("//tests/base_rules:util.bzl", "WINDOWS_ATTR", pt_util = "util")
2525
load("//tests/support:py_executable_info_subject.bzl", "PyExecutableInfoSubject")
26-
load("//tests/support:support.bzl", "LINUX_X86_64", "WINDOWS_X86_64")
26+
load("//tests/support:support.bzl", "CC_TOOLCHAIN", "CROSSTOOL_TOP", "LINUX_X86_64", "WINDOWS_X86_64")
2727

2828
_BuiltinPyRuntimeInfo = PyRuntimeInfo
2929

@@ -51,8 +51,8 @@ def _test_basic_windows(name, config):
5151
# platforms.
5252
"//command_line_option:build_python_zip": "true",
5353
"//command_line_option:cpu": "windows_x86_64",
54-
"//command_line_option:crosstool_top": Label("//tests/cc:cc_toolchain_suite"),
55-
"//command_line_option:extra_toolchains": [str(Label("//tests/cc:all"))],
54+
"//command_line_option:crosstool_top": CROSSTOOL_TOP,
55+
"//command_line_option:extra_toolchains": [CC_TOOLCHAIN],
5656
"//command_line_option:platforms": [WINDOWS_X86_64],
5757
},
5858
attr_values = {"target_compatible_with": target_compatible_with},
@@ -96,8 +96,8 @@ def _test_basic_zip(name, config):
9696
# platforms.
9797
"//command_line_option:build_python_zip": "true",
9898
"//command_line_option:cpu": "linux_x86_64",
99-
"//command_line_option:crosstool_top": Label("//tests/cc:cc_toolchain_suite"),
100-
"//command_line_option:extra_toolchains": [str(Label("//tests/cc:all"))],
99+
"//command_line_option:crosstool_top": CROSSTOOL_TOP,
100+
"//command_line_option:extra_toolchains": [CC_TOOLCHAIN],
101101
"//command_line_option:platforms": [LINUX_X86_64],
102102
},
103103
attr_values = {"target_compatible_with": target_compatible_with},

tests/base_rules/py_test/py_test_tests.bzl

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,7 @@ load(
2121
"create_executable_tests",
2222
)
2323
load("//tests/base_rules:util.bzl", pt_util = "util")
24-
load("//tests/support:support.bzl", "LINUX_X86_64", "MAC_X86_64")
25-
26-
# Explicit Label() calls are required so that it resolves in @rules_python
27-
# context instead of @rules_testing context.
28-
_FAKE_CC_TOOLCHAIN = Label("//tests/cc:cc_toolchain_suite")
29-
_FAKE_CC_TOOLCHAINS = [str(Label("//tests/cc:all"))]
24+
load("//tests/support:support.bzl", "CC_TOOLCHAIN", "CROSSTOOL_TOP", "LINUX_X86_64", "MAC_X86_64")
3025

3126
# The Windows CI currently runs as root, which breaks when
3227
# the analysis tests try to install (but not use, because
@@ -63,8 +58,8 @@ def _test_mac_requires_darwin_for_execution(name, config):
6358
target = name + "_subject",
6459
config_settings = {
6560
"//command_line_option:cpu": "darwin_x86_64",
66-
"//command_line_option:crosstool_top": _FAKE_CC_TOOLCHAIN,
67-
"//command_line_option:extra_toolchains": _FAKE_CC_TOOLCHAINS,
61+
"//command_line_option:crosstool_top": CROSSTOOL_TOP,
62+
"//command_line_option:extra_toolchains": CC_TOOLCHAIN,
6863
"//command_line_option:platforms": [MAC_X86_64],
6964
},
7065
attr_values = _SKIP_WINDOWS,
@@ -96,8 +91,8 @@ def _test_non_mac_doesnt_require_darwin_for_execution(name, config):
9691
target = name + "_subject",
9792
config_settings = {
9893
"//command_line_option:cpu": "k8",
99-
"//command_line_option:crosstool_top": _FAKE_CC_TOOLCHAIN,
100-
"//command_line_option:extra_toolchains": _FAKE_CC_TOOLCHAINS,
94+
"//command_line_option:crosstool_top": CROSSTOOL_TOP,
95+
"//command_line_option:extra_toolchains": CC_TOOLCHAIN,
10196
"//command_line_option:platforms": [LINUX_X86_64],
10297
},
10398
attr_values = _SKIP_WINDOWS,

tests/cc/BUILD.bazel

Lines changed: 0 additions & 137 deletions
Original file line numberDiff line numberDiff line change
@@ -11,140 +11,3 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14-
15-
load("@rules_cc//cc:defs.bzl", "cc_toolchain", "cc_toolchain_suite")
16-
load("@rules_testing//lib:util.bzl", "PREVENT_IMPLICIT_BUILDING_TAGS")
17-
load("//python/cc:py_cc_toolchain.bzl", "py_cc_toolchain")
18-
load(":fake_cc_toolchain_config.bzl", "fake_cc_toolchain_config")
19-
20-
package(default_visibility = ["//:__subpackages__"])
21-
22-
exports_files(["fake_header.h"])
23-
24-
filegroup(
25-
name = "libpython",
26-
srcs = ["libpython-fake.so"],
27-
tags = PREVENT_IMPLICIT_BUILDING_TAGS,
28-
)
29-
30-
toolchain(
31-
name = "fake_py_cc_toolchain",
32-
tags = PREVENT_IMPLICIT_BUILDING_TAGS,
33-
toolchain = ":fake_py_cc_toolchain_impl",
34-
toolchain_type = "@rules_python//python/cc:toolchain_type",
35-
)
36-
37-
py_cc_toolchain(
38-
name = "fake_py_cc_toolchain_impl",
39-
headers = ":fake_headers",
40-
libs = ":fake_libs",
41-
python_version = "3.999",
42-
tags = PREVENT_IMPLICIT_BUILDING_TAGS,
43-
)
44-
45-
# buildifier: disable=native-cc
46-
cc_library(
47-
name = "fake_headers",
48-
hdrs = ["fake_header.h"],
49-
data = ["data.txt"],
50-
includes = ["fake_include"],
51-
tags = PREVENT_IMPLICIT_BUILDING_TAGS,
52-
)
53-
54-
# buildifier: disable=native-cc
55-
cc_library(
56-
name = "fake_libs",
57-
srcs = ["libpython3.so"],
58-
data = ["libdata.txt"],
59-
tags = PREVENT_IMPLICIT_BUILDING_TAGS,
60-
)
61-
62-
cc_toolchain_suite(
63-
name = "cc_toolchain_suite",
64-
tags = ["manual"],
65-
toolchains = {
66-
"darwin_x86_64": ":mac_toolchain",
67-
"k8": ":linux_toolchain",
68-
"windows_x86_64": ":windows_toolchain",
69-
},
70-
)
71-
72-
filegroup(name = "empty")
73-
74-
cc_toolchain(
75-
name = "mac_toolchain",
76-
all_files = ":empty",
77-
compiler_files = ":empty",
78-
dwp_files = ":empty",
79-
linker_files = ":empty",
80-
objcopy_files = ":empty",
81-
strip_files = ":empty",
82-
supports_param_files = 0,
83-
toolchain_config = ":mac_toolchain_config",
84-
toolchain_identifier = "mac-toolchain",
85-
)
86-
87-
toolchain(
88-
name = "mac_toolchain_definition",
89-
target_compatible_with = ["@platforms//os:macos"],
90-
toolchain = ":mac_toolchain",
91-
toolchain_type = "@bazel_tools//tools/cpp:toolchain_type",
92-
)
93-
94-
fake_cc_toolchain_config(
95-
name = "mac_toolchain_config",
96-
target_cpu = "darwin_x86_64",
97-
toolchain_identifier = "mac-toolchain",
98-
)
99-
100-
cc_toolchain(
101-
name = "linux_toolchain",
102-
all_files = ":empty",
103-
compiler_files = ":empty",
104-
dwp_files = ":empty",
105-
linker_files = ":empty",
106-
objcopy_files = ":empty",
107-
strip_files = ":empty",
108-
supports_param_files = 0,
109-
toolchain_config = ":linux_toolchain_config",
110-
toolchain_identifier = "linux-toolchain",
111-
)
112-
113-
toolchain(
114-
name = "linux_toolchain_definition",
115-
target_compatible_with = ["@platforms//os:linux"],
116-
toolchain = ":linux_toolchain",
117-
toolchain_type = "@bazel_tools//tools/cpp:toolchain_type",
118-
)
119-
120-
fake_cc_toolchain_config(
121-
name = "linux_toolchain_config",
122-
target_cpu = "k8",
123-
toolchain_identifier = "linux-toolchain",
124-
)
125-
126-
cc_toolchain(
127-
name = "windows_toolchain",
128-
all_files = ":empty",
129-
compiler_files = ":empty",
130-
dwp_files = ":empty",
131-
linker_files = ":empty",
132-
objcopy_files = ":empty",
133-
strip_files = ":empty",
134-
supports_param_files = 0,
135-
toolchain_config = ":windows_toolchain_config",
136-
toolchain_identifier = "windows-toolchain",
137-
)
138-
139-
toolchain(
140-
name = "windows_toolchain_definition",
141-
target_compatible_with = ["@platforms//os:windows"],
142-
toolchain = ":windows_toolchain",
143-
toolchain_type = "@bazel_tools//tools/cpp:toolchain_type",
144-
)
145-
146-
fake_cc_toolchain_config(
147-
name = "windows_toolchain_config",
148-
target_cpu = "windows_x86_64",
149-
toolchain_identifier = "windows-toolchain",
150-
)

tests/cc/current_py_cc_headers/current_py_cc_headers_tests.bzl

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
load("@rules_cc//cc:defs.bzl", "CcInfo")
1818
load("@rules_testing//lib:analysis_test.bzl", "analysis_test", "test_suite")
1919
load("@rules_testing//lib:truth.bzl", "matching")
20-
load("//tests:cc_info_subject.bzl", "cc_info_subject")
20+
load("//tests/support:cc_info_subject.bzl", "cc_info_subject")
21+
load("//tests/support:support.bzl", "CC_TOOLCHAIN")
2122

2223
_tests = []
2324

@@ -27,11 +28,11 @@ def _test_current_toolchain_headers(name):
2728
impl = _test_current_toolchain_headers_impl,
2829
target = "//python/cc:current_py_cc_headers",
2930
config_settings = {
30-
"//command_line_option:extra_toolchains": [str(Label("//tests/cc:all"))],
31+
"//command_line_option:extra_toolchains": [CC_TOOLCHAIN],
3132
},
3233
attrs = {
3334
"header": attr.label(
34-
default = "//tests/cc:fake_header.h",
35+
default = "//tests/support/cc_toolchains:fake_header.h",
3536
allow_single_file = True,
3637
),
3738
},
@@ -58,7 +59,7 @@ def _test_current_toolchain_headers_impl(env, target):
5859

5960
# Check that the forward DefaultInfo looks correct
6061
env.expect.that_target(target).runfiles().contains_predicate(
61-
matching.str_matches("*/cc/data.txt"),
62+
matching.str_matches("*/cc_toolchains/data.txt"),
6263
)
6364

6465
_tests.append(_test_current_toolchain_headers)

tests/cc/current_py_cc_libs/current_py_cc_libs_tests.bzl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
load("@rules_cc//cc:defs.bzl", "CcInfo")
1818
load("@rules_testing//lib:analysis_test.bzl", "analysis_test", "test_suite")
1919
load("@rules_testing//lib:truth.bzl", "matching")
20-
load("//tests:cc_info_subject.bzl", "cc_info_subject")
20+
load("//tests/support:cc_info_subject.bzl", "cc_info_subject")
2121

2222
_tests = []
2323

@@ -27,11 +27,11 @@ def _test_current_toolchain_libs(name):
2727
impl = _test_current_toolchain_libs_impl,
2828
target = "//python/cc:current_py_cc_libs",
2929
config_settings = {
30-
"//command_line_option:extra_toolchains": [str(Label("//tests/cc:all"))],
30+
"//command_line_option:extra_toolchains": [str(Label("//tests/support/cc_toolchains:all"))],
3131
},
3232
attrs = {
3333
"lib": attr.label(
34-
default = "//tests/cc:libpython",
34+
default = "//tests/support/cc_toolchains:libpython",
3535
allow_single_file = True,
3636
),
3737
},

tests/cc/py_cc_toolchain/py_cc_toolchain_tests.bzl

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,19 @@
1616

1717
load("@rules_testing//lib:analysis_test.bzl", "analysis_test", "test_suite")
1818
load("@rules_testing//lib:truth.bzl", "matching", "subjects")
19-
load("//tests:cc_info_subject.bzl", "cc_info_subject")
20-
load("//tests:default_info_subject.bzl", "default_info_subject")
21-
load("//tests:py_cc_toolchain_info_subject.bzl", "PyCcToolchainInfoSubject")
19+
load("//tests/support:cc_info_subject.bzl", "cc_info_subject")
20+
load("//tests/support:py_cc_toolchain_info_subject.bzl", "PyCcToolchainInfoSubject")
2221

2322
_tests = []
2423

2524
def _py_cc_toolchain_test(name):
2625
analysis_test(
2726
name = name,
2827
impl = _py_cc_toolchain_test_impl,
29-
target = "//tests/cc:fake_py_cc_toolchain_impl",
28+
target = "//tests/support/cc_toolchains:fake_py_cc_toolchain_impl",
3029
attrs = {
3130
"header": attr.label(
32-
default = "//tests/cc:fake_header.h",
31+
default = "//tests/support/cc_toolchains:fake_header.h",
3332
allow_single_file = True,
3433
),
3534
},
@@ -63,15 +62,9 @@ def _py_cc_toolchain_test_impl(env, target):
6362
matching.str_matches("*/fake_include"),
6463
])
6564

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-
)
65+
default_info = headers_providers.get("DefaultInfo", factory = subjects.default_info)
7366
default_info.runfiles().contains_predicate(
74-
matching.str_matches("*/cc/data.txt"),
67+
matching.str_matches("*/cc_toolchains/data.txt"),
7568
)
7669

7770
libs_providers = toolchain.libs().providers_map()
@@ -82,7 +75,7 @@ def _py_cc_toolchain_test_impl(env, target):
8275
cc_info.linking_context().linker_inputs().has_size(2)
8376

8477
default_info = libs_providers.get("DefaultInfo", factory = subjects.default_info)
85-
default_info.runfiles().contains("{workspace}/tests/cc/libdata.txt")
78+
default_info.runfiles().contains("{workspace}/tests/support/cc_toolchains/libdata.txt")
8679
default_info.runfiles().contains_predicate(
8780
matching.str_matches("/libpython3."),
8881
)

tests/config_settings/transition/multi_version_tests.bzl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ load("//python:py_info.bzl", "PyInfo")
2020
load("//python/config_settings:transition.bzl", py_binary_transitioned = "py_binary", py_test_transitioned = "py_test")
2121
load("//python/private:reexports.bzl", "BuiltinPyInfo") # buildifier: disable=bzl-visibility
2222
load("//python/private:util.bzl", "IS_BAZEL_7_OR_HIGHER") # buildifier: disable=bzl-visibility
23+
load("//tests/support:support.bzl", "CC_TOOLCHAIN")
2324

2425
# NOTE @aignas 2024-06-04: we are using here something that is registered in the MODULE.Bazel
2526
# and if you find tests failing, it could be because of the toolchain resolution issues here.
@@ -87,7 +88,7 @@ def _setup_py_binary_windows(name, *, impl, build_python_zip):
8788
impl = impl,
8889
config_settings = {
8990
"//command_line_option:build_python_zip": build_python_zip,
90-
"//command_line_option:extra_toolchains": "//tests/cc:all",
91+
"//command_line_option:extra_toolchains": CC_TOOLCHAIN,
9192
"//command_line_option:platforms": str(Label("//tests/support:windows_x86_64")),
9293
},
9394
)

tests/py_runtime/py_runtime_tests.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ load("@rules_testing//lib:truth.bzl", "matching")
2020
load("@rules_testing//lib:util.bzl", rt_util = "util")
2121
load("//python:py_runtime.bzl", "py_runtime")
2222
load("//python:py_runtime_info.bzl", "PyRuntimeInfo")
23-
load("//tests:py_runtime_info_subject.bzl", "py_runtime_info_subject")
2423
load("//tests/base_rules:util.bzl", br_util = "util")
24+
load("//tests/support:py_runtime_info_subject.bzl", "py_runtime_info_subject")
2525

2626
_tests = []
2727

0 commit comments

Comments
 (0)