Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 039fc2d

Browse filesBrowse files
committed
tests(pystar): py_runtime_pair and py_runtime analysis tests
These analysis tests verify that py_runtime and py_runtime_pair are working as intended for both the native Bazel and Starlark implementations. Work towards #1069
1 parent 4dfb78d commit 039fc2d
Copy full SHA for 039fc2d

File tree

Expand file treeCollapse file tree

8 files changed

+426
-3
lines changed
Filter options
Expand file treeCollapse file tree

8 files changed

+426
-3
lines changed

‎python/BUILD.bazel

Copy file name to clipboardExpand all lines: python/BUILD.bazel
+5-1Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,11 @@ bzl_library(
147147
bzl_library(
148148
name = "py_runtime_pair_bzl",
149149
srcs = ["py_runtime_pair.bzl"],
150-
deps = ["//python/private:bazel_tools_bzl"],
150+
deps = [
151+
"//python/private:bazel_tools_bzl",
152+
"//python/private:py_runtime_pair_macro_bzl",
153+
"@rules_python_internal//:rules_python_config_bzl",
154+
],
151155
)
152156

153157
bzl_library(

‎python/private/BUILD.bazel

Copy file name to clipboardExpand all lines: python/private/BUILD.bazel
+2-1Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,8 @@ bzl_library(
104104

105105
bzl_library(
106106
name = "py_runtime_pair_macro_bzl",
107-
srcs = ["py_runtime_pair_rule.bzl"],
107+
srcs = ["py_runtime_pair_macro.bzl"],
108+
visibility = ["//:__subpackages__"],
108109
deps = [":py_runtime_pair_rule_bzl"],
109110
)
110111

‎python/py_runtime_pair.bzl

Copy file name to clipboardExpand all lines: python/py_runtime_pair.bzl
+5-1Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@
1414

1515
"""Public entry point for py_runtime_pair."""
1616

17-
load("@bazel_tools//tools/python:toolchain.bzl", _py_runtime_pair = "py_runtime_pair")
17+
load("@bazel_tools//tools/python:toolchain.bzl", _bazel_tools_impl = "py_runtime_pair")
18+
load("@rules_python_internal//:rules_python_config.bzl", "config")
19+
load("//python/private:py_runtime_pair_macro.bzl", _starlark_impl = "py_runtime_pair")
20+
21+
_py_runtime_pair = _bazel_tools_impl if not config.enable_pystar else _starlark_impl
1822

1923
# NOTE: This doc is copy/pasted from the builtin py_runtime_pair rule so our
2024
# doc generator gives useful API docs.

‎tests/py_runtime/BUILD.bazel

Copy file name to clipboard
+17Lines 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_runtime_tests.bzl", "py_runtime_test_suite")
16+
17+
py_runtime_test_suite(name = "py_runtime_tests")

‎tests/py_runtime/py_runtime_tests.bzl

Copy file name to clipboard
+214Lines changed: 214 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,214 @@
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_python_internal//:rules_python_config.bzl", "config")
16+
load("@rules_testing//lib:analysis_test.bzl", "analysis_test")
17+
load("@rules_testing//lib:test_suite.bzl", "test_suite")
18+
load("@rules_testing//lib:truth.bzl", "matching")
19+
load("@rules_testing//lib:util.bzl", rt_util = "util")
20+
load("//tests:py_runtime_info_subject.bzl", "py_runtime_info_subject")
21+
load("//python:py_runtime_info.bzl", "PyRuntimeInfo")
22+
load("//python:py_runtime.bzl", "py_runtime")
23+
24+
_tests = []
25+
26+
def _test_bootstrap_template(name):
27+
# The bootstrap_template arg isn't present in older Bazel versions, so
28+
# we have to conditionally pass the arg and mark the test incompatible.
29+
if config.enable_pystar:
30+
py_runtime_kwargs = {
31+
"bootstrap_template": "bootstrap.txt",
32+
}
33+
attr_values_kwargs = {}
34+
else:
35+
py_runtime_kwargs = {}
36+
attr_values_kwargs = {
37+
"target_compatible_with": ["@platforms//:incompatible"],
38+
}
39+
40+
rt_util.helper_target(
41+
py_runtime,
42+
name = name + "_subject",
43+
interpreter_path = "/py",
44+
python_version = "PY3",
45+
**py_runtime_kwargs
46+
)
47+
analysis_test(
48+
name = name,
49+
target = name + "_subject",
50+
impl = _test_bootstrap_template_impl,
51+
attr_values = attr_values_kwargs,
52+
)
53+
54+
def _test_bootstrap_template_impl(env, target):
55+
env.expect.that_target(target).provider(
56+
PyRuntimeInfo,
57+
factory = py_runtime_info_subject,
58+
).bootstrap_template().path().contains("bootstrap.txt")
59+
60+
_tests.append(_test_bootstrap_template)
61+
62+
def _test_cannot_have_both_inbuild_and_system_interpreter(name):
63+
rt_util.helper_target(
64+
py_runtime,
65+
name = name + "_subject",
66+
interpreter = "fake_interpreter",
67+
interpreter_path = "/some/path",
68+
python_version = "PY3",
69+
)
70+
analysis_test(
71+
name = name,
72+
target = name + "_subject",
73+
impl = _test_cannot_have_both_inbuild_and_system_interpreter_impl,
74+
expect_failure = True,
75+
)
76+
77+
def _test_cannot_have_both_inbuild_and_system_interpreter_impl(env, target):
78+
env.expect.that_target(target).failures().contains_predicate(
79+
matching.str_matches("one of*interpreter*interpreter_path"),
80+
)
81+
82+
_tests.append(_test_cannot_have_both_inbuild_and_system_interpreter)
83+
84+
def _test_cannot_specify_files_for_system_interpreter(name):
85+
rt_util.helper_target(
86+
py_runtime,
87+
name = name + "_subject",
88+
interpreter_path = "/foo",
89+
python_version = "PY3",
90+
files = ["foo.txt"],
91+
)
92+
analysis_test(
93+
name = name,
94+
target = name + "_subject",
95+
impl = _test_cannot_specify_files_for_system_interpreter_impl,
96+
expect_failure = True,
97+
)
98+
99+
def _test_cannot_specify_files_for_system_interpreter_impl(env, target):
100+
env.expect.that_target(target).failures().contains_predicate(
101+
matching.str_matches("files*must be empty"),
102+
)
103+
104+
_tests.append(_test_cannot_specify_files_for_system_interpreter)
105+
106+
def _test_in_build_interpreter(name):
107+
rt_util.helper_target(
108+
py_runtime,
109+
name = name + "_subject",
110+
interpreter = "fake_interpreter",
111+
python_version = "PY3",
112+
files = ["file1.txt"],
113+
)
114+
analysis_test(
115+
name = name,
116+
target = name + "_subject",
117+
impl = _test_in_build_interpreter_impl,
118+
)
119+
120+
def _test_in_build_interpreter_impl(env, target):
121+
info = env.expect.that_target(target).provider(PyRuntimeInfo, factory = py_runtime_info_subject)
122+
info.python_version().equals("PY3")
123+
info.files().contains_predicate(matching.file_basename_equals("file1.txt"))
124+
info.interpreter().path().contains("fake_interpreter")
125+
126+
_tests.append(_test_in_build_interpreter)
127+
128+
def _test_must_have_either_inbuild_or_system_interpreter(name):
129+
rt_util.helper_target(
130+
py_runtime,
131+
name = name + "_subject",
132+
python_version = "PY3",
133+
)
134+
analysis_test(
135+
name = name,
136+
target = name + "_subject",
137+
impl = _test_must_have_either_inbuild_or_system_interpreter_impl,
138+
expect_failure = True,
139+
)
140+
141+
def _test_must_have_either_inbuild_or_system_interpreter_impl(env, target):
142+
env.expect.that_target(target).failures().contains_predicate(
143+
matching.str_matches("one of*interpreter*interpreter_path"),
144+
)
145+
146+
_tests.append(_test_must_have_either_inbuild_or_system_interpreter)
147+
148+
def _test_python_version_required(name):
149+
rt_util.helper_target(
150+
py_runtime,
151+
name = name + "_subject",
152+
interpreter_path = "/math/pi",
153+
)
154+
analysis_test(
155+
name = name,
156+
target = name + "_subject",
157+
impl = _test_python_version_required_impl,
158+
expect_failure = True,
159+
)
160+
161+
def _test_python_version_required_impl(env, target):
162+
env.expect.that_target(target).failures().contains_predicate(
163+
matching.str_matches("must be set*PY2*PY3"),
164+
)
165+
166+
_tests.append(_test_python_version_required)
167+
168+
def _test_system_interpreter(name):
169+
rt_util.helper_target(
170+
py_runtime,
171+
name = name + "_subject",
172+
interpreter_path = "/system/python",
173+
python_version = "PY3",
174+
)
175+
analysis_test(
176+
name = name,
177+
target = name + "_subject",
178+
impl = _test_system_interpreter_impl,
179+
)
180+
181+
def _test_system_interpreter_impl(env, target):
182+
env.expect.that_target(target).provider(
183+
PyRuntimeInfo,
184+
factory = py_runtime_info_subject,
185+
).interpreter_path().equals("/system/python")
186+
187+
_tests.append(_test_system_interpreter)
188+
189+
def _test_system_interpreter_must_be_absolute(name):
190+
rt_util.helper_target(
191+
py_runtime,
192+
name = name + "_subject",
193+
interpreter_path = "relative/path",
194+
python_version = "PY3",
195+
)
196+
analysis_test(
197+
name = name,
198+
target = name + "_subject",
199+
impl = _test_system_interpreter_must_be_absolute_impl,
200+
expect_failure = True,
201+
)
202+
203+
def _test_system_interpreter_must_be_absolute_impl(env, target):
204+
env.expect.that_target(target).failures().contains_predicate(
205+
matching.str_matches("must be*absolute"),
206+
)
207+
208+
_tests.append(_test_system_interpreter_must_be_absolute)
209+
210+
def py_runtime_test_suite(name):
211+
test_suite(
212+
name = name,
213+
tests = _tests,
214+
)

‎tests/py_runtime_info_subject.bzl

Copy file name to clipboard
+101Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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+
"""PyRuntimeInfo testing subject."""
15+
16+
load("@rules_testing//lib:truth.bzl", "subjects")
17+
18+
def py_runtime_info_subject(info, *, meta):
19+
"""Creates a new `PyRuntimeInfoSubject` for a PyRuntimeInfo provider instance.
20+
21+
Method: PyRuntimeInfoSubject.new
22+
23+
Args:
24+
info: The PyRuntimeInfo object
25+
meta: ExpectMeta object.
26+
27+
Returns:
28+
A `PyRuntimeInfoSubject` struct
29+
"""
30+
31+
# buildifier: disable=uninitialized
32+
public = struct(
33+
# go/keep-sorted start
34+
bootstrap_template = lambda *a, **k: _py_runtime_info_subject_bootstrap_template(self, *a, **k),
35+
coverage_files = lambda *a, **k: _py_runtime_info_subject_coverage_files(self, *a, **k),
36+
coverage_tool = lambda *a, **k: _py_runtime_info_subject_coverage_tool(self, *a, **k),
37+
files = lambda *a, **k: _py_runtime_info_subject_files(self, *a, **k),
38+
interpreter = lambda *a, **k: _py_runtime_info_subject_interpreter(self, *a, **k),
39+
interpreter_path = lambda *a, **k: _py_runtime_info_subject_interpreter_path(self, *a, **k),
40+
python_version = lambda *a, **k: _py_runtime_info_subject_python_version(self, *a, **k),
41+
stub_shebang = lambda *a, **k: _py_runtime_info_subject_stub_shebang(self, *a, **k),
42+
# go/keep-sorted end
43+
)
44+
self = struct(
45+
actual = info,
46+
meta = meta,
47+
)
48+
return public
49+
50+
def _py_runtime_info_subject_bootstrap_template(self):
51+
return subjects.file(
52+
self.actual.bootstrap_template,
53+
meta = self.meta.derive("bootstrap_template()"),
54+
)
55+
56+
def _py_runtime_info_subject_coverage_files(self):
57+
"""Returns a `DepsetFileSubject` for the `coverage_files` attribute.
58+
59+
Args:
60+
self: implicitly added.
61+
"""
62+
return subjects.depset_file(
63+
self.actual.coverage_files,
64+
meta = self.meta.derive("coverage_files()"),
65+
)
66+
67+
def _py_runtime_info_subject_coverage_tool(self):
68+
return subjects.file(
69+
self.actual.coverage_tool,
70+
meta = self.meta.derive("coverage_tool()"),
71+
)
72+
73+
def _py_runtime_info_subject_files(self):
74+
return subjects.depset_file(
75+
self.actual.files,
76+
meta = self.meta.derive("files()"),
77+
)
78+
79+
def _py_runtime_info_subject_interpreter(self):
80+
return subjects.file(
81+
self.actual.interpreter,
82+
meta = self.meta.derive("interpreter()"),
83+
)
84+
85+
def _py_runtime_info_subject_interpreter_path(self):
86+
return subjects.str(
87+
self.actual.interpreter_path,
88+
meta = self.meta.derive("interpreter_path()"),
89+
)
90+
91+
def _py_runtime_info_subject_python_version(self):
92+
return subjects.str(
93+
self.actual.python_version,
94+
meta = self.meta.derive("python_version()"),
95+
)
96+
97+
def _py_runtime_info_subject_stub_shebang(self):
98+
return subjects.str(
99+
self.actual.stub_shebang,
100+
meta = self.meta.derive("stub_shebang()"),
101+
)

‎tests/py_runtime_pair/BUILD.bazel

Copy file name to clipboard
+17Lines 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_runtime_pair_tests.bzl", "py_runtime_pair_test_suite")
16+
17+
py_runtime_pair_test_suite(name = "py_runtime_pair_tests")

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.