Skip to content

Commit ebe1842

Browse files
committed
simplify the code a bit
1 parent 69d647b commit ebe1842

File tree

3 files changed

+31
-12
lines changed

3 files changed

+31
-12
lines changed

tests/interpreter/BUILD.bazel

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414

1515
load(":interpreter_tests.bzl", "PYTHON_VERSIONS_TO_TEST", "py_reconfig_interpreter_tests")
1616

17+
# For this test the interpreter is sourced from the current configuration. That
18+
# means both the interpreter and the test itself are expected to run under the
19+
# same Python version.
1720
py_reconfig_interpreter_tests(
1821
name = "interpreter_version_test",
1922
srcs = ["interpreter_test.py"],
@@ -23,26 +26,26 @@ py_reconfig_interpreter_tests(
2326
env = {
2427
"PYTHON_BIN": "$(rootpath //python/bin:python)",
2528
},
26-
# Both the interpreter and the test itself are expected to run under
27-
# the same version.
28-
expected_interpreter_version = None,
2929
main = "interpreter_test.py",
3030
python_versions = PYTHON_VERSIONS_TO_TEST,
3131
)
3232

33+
# For this test the interpreter is sourced from a binary pinned at a specific
34+
# Python version. That means the interpreter and the test itself can run
35+
# different Python versions.
3336
py_reconfig_interpreter_tests(
3437
name = "python_src_test",
3538
srcs = ["interpreter_test.py"],
3639
data = [
3740
"//python/bin:python",
3841
],
3942
env = {
43+
# Since we're grabbing the interpreter from a binary with a fixed
44+
# version, we expect to always see that version. It doesn't matter what
45+
# Python version the test itself is running with.
46+
"EXPECTED_INTERPRETER_VERSION": "3.11",
4047
"PYTHON_BIN": "$(rootpath //python/bin:python)",
4148
},
42-
# Since we're grabbing the interpreter from a binary with a fixed
43-
# version, we expect to always see that version. It doesn't matter what
44-
# Python version the test itself is running with.
45-
expected_interpreter_version = "3.11",
4649
main = "interpreter_test.py",
4750
python_src = "//tools/publish:twine",
4851
python_versions = PYTHON_VERSIONS_TO_TEST,

tests/interpreter/interpreter_test.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,17 @@ def setUp(self):
2323
super().setUp()
2424
self.interpreter = os.environ["PYTHON_BIN"]
2525

26+
v = sys.version_info
27+
self.version = f"{v.major}.{v.minor}"
28+
2629
def test_self_version(self):
2730
"""Performs a sanity check on the Python version used for this test."""
2831
expected_version = os.environ["EXPECTED_SELF_VERSION"]
29-
v = sys.version_info
30-
self.assertEqual(expected_version, f"{v.major}.{v.minor}")
32+
self.assertEqual(expected_version, self.version)
3133

3234
def test_interpreter_version(self):
3335
"""Validates that we can successfully execute arbitrary code from the CLI."""
34-
expected_version = os.environ["EXPECTED_INTERPRETER_VERSION"]
36+
expected_version = os.environ.get("EXPECTED_INTERPRETER_VERSION", self.version)
3537

3638
try:
3739
result = subprocess.check_output(

tests/interpreter/interpreter_tests.bzl

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,32 @@
1616

1717
load("//tests/support:sh_py_run_test.bzl", "py_reconfig_test")
1818

19+
# The versions of Python that we want to run the interpreter tests against.
1920
PYTHON_VERSIONS_TO_TEST = (
2021
"3.10",
2122
"3.11",
2223
"3.12",
2324
)
2425

25-
def py_reconfig_interpreter_tests(name, python_versions, expected_interpreter_version = None, env = {}, **kwargs):
26+
def py_reconfig_interpreter_tests(name, python_versions, env = {}, **kwargs):
27+
"""Runs the specified test against each of the specified Python versions.
28+
29+
One test gets generated for each Python version. The following environment
30+
variable gets set for the test:
31+
32+
EXPECTED_SELF_VERSION: Contains the Python version that the test itself
33+
is running under.
34+
35+
Args:
36+
name: Name of the test.
37+
python_versions: A list of Python versions to test.
38+
env: The environment to set on the test.
39+
**kwargs: Passed to the underlying py_reconfig_test targets.
40+
"""
2641
for python_version in python_versions:
2742
py_reconfig_test(
2843
name = "{}_{}".format(name, python_version),
2944
env = env | {
30-
"EXPECTED_INTERPRETER_VERSION": expected_interpreter_version or python_version,
3145
"EXPECTED_SELF_VERSION": python_version,
3246
},
3347
python_version = python_version,

0 commit comments

Comments
 (0)