Skip to content

Commit c504355

Browse files
authored
test(core): Add analysis tests for base Python rules. (bazel-contrib#1102)
This is to provide some regression tests for the Starlark rewrite. These tests are approximately the same as Bazel's Java-implemented tests. Work towards bazel-contrib#1069
1 parent f97e008 commit c504355

14 files changed

+1011
-0
lines changed

internal_deps.bzl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,13 @@ def rules_python_internal_deps():
3939
],
4040
sha256 = "8a298e832762eda1830597d64fe7db58178aa84cd5926d76d5b744d6558941c2",
4141
)
42+
maybe(
43+
http_archive,
44+
name = "rules_testing",
45+
url = "https://github.com/bazelbuild/rules_testing/releases/download/v0.0.1/rules_testing-v0.0.1.tar.gz",
46+
sha256 = "47db8fc9c3c1837491333cdcedebf267285479bd709a1ff0a47b19a324817def",
47+
strip_prefix = "rules_testing-0.0.1",
48+
)
4249

4350
maybe(
4451
http_archive,

tools/build_defs/python/BUILD.bazel

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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.
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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+
load("@rules_cc//cc:defs.bzl", "cc_toolchain", "cc_toolchain_suite")
16+
load(":fake_cc_toolchain_config.bzl", "fake_cc_toolchain_config")
17+
18+
platform(
19+
name = "mac",
20+
constraint_values = [
21+
"@platforms//os:macos",
22+
],
23+
)
24+
25+
platform(
26+
name = "linux",
27+
constraint_values = [
28+
"@platforms//os:linux",
29+
],
30+
)
31+
32+
cc_toolchain_suite(
33+
name = "cc_toolchain_suite",
34+
tags = ["manual"],
35+
toolchains = {
36+
"darwin_x86_64": ":mac_toolchain",
37+
"k8": ":linux_toolchain",
38+
},
39+
)
40+
41+
filegroup(name = "empty")
42+
43+
cc_toolchain(
44+
name = "mac_toolchain",
45+
all_files = ":empty",
46+
compiler_files = ":empty",
47+
dwp_files = ":empty",
48+
linker_files = ":empty",
49+
objcopy_files = ":empty",
50+
strip_files = ":empty",
51+
supports_param_files = 0,
52+
toolchain_config = ":mac_toolchain_config",
53+
toolchain_identifier = "mac-toolchain",
54+
)
55+
56+
fake_cc_toolchain_config(
57+
name = "mac_toolchain_config",
58+
target_cpu = "darwin_x86_64",
59+
toolchain_identifier = "mac-toolchain",
60+
)
61+
62+
cc_toolchain(
63+
name = "linux_toolchain",
64+
all_files = ":empty",
65+
compiler_files = ":empty",
66+
dwp_files = ":empty",
67+
linker_files = ":empty",
68+
objcopy_files = ":empty",
69+
strip_files = ":empty",
70+
supports_param_files = 0,
71+
toolchain_config = ":linux_toolchain_config",
72+
toolchain_identifier = "linux-toolchain",
73+
)
74+
75+
fake_cc_toolchain_config(
76+
name = "linux_toolchain_config",
77+
target_cpu = "k8",
78+
toolchain_identifier = "linux-toolchain",
79+
)
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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+
"""Tests common to py_test, py_binary, and py_library rules."""
15+
16+
load("@rules_testing//lib:analysis_test.bzl", "analysis_test")
17+
load("@rules_testing//lib:truth.bzl", "matching")
18+
load("@rules_testing//lib:util.bzl", rt_util = "util")
19+
load("//python:defs.bzl", "PyInfo")
20+
load("//tools/build_defs/python/tests:py_info_subject.bzl", "py_info_subject")
21+
load("//tools/build_defs/python/tests:util.bzl", pt_util = "util")
22+
23+
_tests = []
24+
25+
def _produces_py_info_impl(ctx):
26+
return [PyInfo(transitive_sources = depset(ctx.files.srcs))]
27+
28+
_produces_py_info = rule(
29+
implementation = _produces_py_info_impl,
30+
attrs = {"srcs": attr.label_list(allow_files = True)},
31+
)
32+
33+
def _test_consumes_provider(name, config):
34+
rt_util.helper_target(
35+
config.base_test_rule,
36+
name = name + "_subject",
37+
deps = [name + "_produces_py_info"],
38+
)
39+
rt_util.helper_target(
40+
_produces_py_info,
41+
name = name + "_produces_py_info",
42+
srcs = [rt_util.empty_file(name + "_produce.py")],
43+
)
44+
analysis_test(
45+
name = name,
46+
target = name + "_subject",
47+
impl = _test_consumes_provider_impl,
48+
)
49+
50+
def _test_consumes_provider_impl(env, target):
51+
env.expect.that_target(target).provider(
52+
PyInfo,
53+
factory = py_info_subject,
54+
).transitive_sources().contains("{package}/{test_name}_produce.py")
55+
56+
_tests.append(_test_consumes_provider)
57+
58+
def _test_requires_provider(name, config):
59+
rt_util.helper_target(
60+
config.base_test_rule,
61+
name = name + "_subject",
62+
deps = [name + "_nopyinfo"],
63+
)
64+
rt_util.helper_target(
65+
native.filegroup,
66+
name = name + "_nopyinfo",
67+
)
68+
analysis_test(
69+
name = name,
70+
target = name + "_subject",
71+
impl = _test_requires_provider_impl,
72+
expect_failure = True,
73+
)
74+
75+
def _test_requires_provider_impl(env, target):
76+
env.expect.that_target(target).failures().contains_predicate(
77+
matching.str_matches("mandatory*PyInfo"),
78+
)
79+
80+
_tests.append(_test_requires_provider)
81+
82+
def _test_data_sets_uses_shared_library(name, config):
83+
rt_util.helper_target(
84+
config.base_test_rule,
85+
name = name + "_subject",
86+
data = [rt_util.empty_file(name + "_dso.so")],
87+
)
88+
analysis_test(
89+
name = name,
90+
target = name + "_subject",
91+
impl = _test_data_sets_uses_shared_library_impl,
92+
)
93+
94+
def _test_data_sets_uses_shared_library_impl(env, target):
95+
env.expect.that_target(target).provider(
96+
PyInfo,
97+
factory = py_info_subject,
98+
).uses_shared_libraries().equals(True)
99+
100+
_tests.append(_test_data_sets_uses_shared_library)
101+
102+
def create_base_tests(config):
103+
return pt_util.create_tests(_tests, config = config)
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
"""Fake for providing CcToolchainConfigInfo."""
16+
17+
def _impl(ctx):
18+
return cc_common.create_cc_toolchain_config_info(
19+
ctx = ctx,
20+
toolchain_identifier = ctx.attr.toolchain_identifier,
21+
host_system_name = "local",
22+
target_system_name = "local",
23+
target_cpu = ctx.attr.target_cpu,
24+
target_libc = "unknown",
25+
compiler = "clang",
26+
abi_version = "unknown",
27+
abi_libc_version = "unknown",
28+
)
29+
30+
fake_cc_toolchain_config = rule(
31+
implementation = _impl,
32+
attrs = {
33+
"target_cpu": attr.string(),
34+
"toolchain_identifier": attr.string(),
35+
},
36+
provides = [CcToolchainConfigInfo],
37+
)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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+
load(":py_binary_tests.bzl", "py_binary_test_suite")
16+
17+
py_binary_test_suite(name = "py_binary_tests")
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
"""Tests for py_binary."""
15+
16+
load("//python:defs.bzl", "py_binary")
17+
load(
18+
"//tools/build_defs/python/tests:py_executable_base_tests.bzl",
19+
"create_executable_tests",
20+
)
21+
22+
def py_binary_test_suite(name):
23+
config = struct(rule = py_binary)
24+
25+
native.test_suite(
26+
name = name,
27+
tests = create_executable_tests(config),
28+
)

0 commit comments

Comments
 (0)