Skip to content

Commit e07a528

Browse files
committed
add integration test
1 parent 3fb3236 commit e07a528

File tree

10 files changed

+129
-5
lines changed

10 files changed

+129
-5
lines changed

.bazelrc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
# (Note, we cannot use `common --deleted_packages` because the bazel version command doesn't support it)
55
# To update these lines, execute
66
# `bazel run @rules_bazel_integration_test//tools:update_deleted_packages`
7-
build --deleted_packages=examples/build_file_generation,examples/build_file_generation/random_number_generator,examples/bzlmod,examples/bzlmod_build_file_generation,examples/bzlmod_build_file_generation/other_module/other_module/pkg,examples/bzlmod_build_file_generation/runfiles,examples/bzlmod/entry_points,examples/bzlmod/entry_points/tests,examples/bzlmod/libs/my_lib,examples/bzlmod/other_module,examples/bzlmod/other_module/other_module/pkg,examples/bzlmod/patches,examples/bzlmod/py_proto_library,examples/bzlmod/py_proto_library/example.com/another_proto,examples/bzlmod/py_proto_library/example.com/proto,examples/bzlmod/runfiles,examples/bzlmod/tests,examples/bzlmod/tests/other_module,examples/bzlmod/whl_mods,examples/multi_python_versions/libs/my_lib,examples/multi_python_versions/requirements,examples/multi_python_versions/tests,examples/pip_parse,examples/pip_parse_vendored,examples/pip_repository_annotations,examples/py_proto_library,examples/py_proto_library/example.com/another_proto,examples/py_proto_library/example.com/proto,gazelle,gazelle/manifest,gazelle/manifest/generate,gazelle/manifest/hasher,gazelle/manifest/test,gazelle/modules_mapping,gazelle/python,gazelle/pythonconfig,gazelle/python/private,tests/integration/compile_pip_requirements,tests/integration/compile_pip_requirements_test_from_external_repo,tests/integration/custom_commands,tests/integration/ignore_root_user_error,tests/integration/ignore_root_user_error/submodule,tests/integration/local_toolchains,tests/integration/pip_parse,tests/integration/pip_parse/empty,tests/integration/py_cc_toolchain_registered
8-
query --deleted_packages=examples/build_file_generation,examples/build_file_generation/random_number_generator,examples/bzlmod,examples/bzlmod_build_file_generation,examples/bzlmod_build_file_generation/other_module/other_module/pkg,examples/bzlmod_build_file_generation/runfiles,examples/bzlmod/entry_points,examples/bzlmod/entry_points/tests,examples/bzlmod/libs/my_lib,examples/bzlmod/other_module,examples/bzlmod/other_module/other_module/pkg,examples/bzlmod/patches,examples/bzlmod/py_proto_library,examples/bzlmod/py_proto_library/example.com/another_proto,examples/bzlmod/py_proto_library/example.com/proto,examples/bzlmod/runfiles,examples/bzlmod/tests,examples/bzlmod/tests/other_module,examples/bzlmod/whl_mods,examples/multi_python_versions/libs/my_lib,examples/multi_python_versions/requirements,examples/multi_python_versions/tests,examples/pip_parse,examples/pip_parse_vendored,examples/pip_repository_annotations,examples/py_proto_library,examples/py_proto_library/example.com/another_proto,examples/py_proto_library/example.com/proto,gazelle,gazelle/manifest,gazelle/manifest/generate,gazelle/manifest/hasher,gazelle/manifest/test,gazelle/modules_mapping,gazelle/python,gazelle/pythonconfig,gazelle/python/private,tests/integration/compile_pip_requirements,tests/integration/compile_pip_requirements_test_from_external_repo,tests/integration/custom_commands,tests/integration/ignore_root_user_error,tests/integration/ignore_root_user_error/submodule,tests/integration/local_toolchains,tests/integration/pip_parse,tests/integration/pip_parse/empty,tests/integration/py_cc_toolchain_registered
7+
build --deleted_packages=examples/build_file_generation,examples/build_file_generation/random_number_generator,examples/bzlmod,examples/bzlmod_build_file_generation,examples/bzlmod_build_file_generation/other_module/other_module/pkg,examples/bzlmod_build_file_generation/runfiles,examples/bzlmod/entry_points,examples/bzlmod/entry_points/tests,examples/bzlmod/libs/my_lib,examples/bzlmod/other_module,examples/bzlmod/other_module/other_module/pkg,examples/bzlmod/patches,examples/bzlmod/py_proto_library,examples/bzlmod/py_proto_library/example.com/another_proto,examples/bzlmod/py_proto_library/example.com/proto,examples/bzlmod/runfiles,examples/bzlmod/tests,examples/bzlmod/tests/other_module,examples/bzlmod/whl_mods,examples/multi_python_versions/libs/my_lib,examples/multi_python_versions/requirements,examples/multi_python_versions/tests,examples/pip_parse,examples/pip_parse_vendored,examples/pip_repository_annotations,examples/py_proto_library,examples/py_proto_library/example.com/another_proto,examples/py_proto_library/example.com/proto,gazelle,gazelle/manifest,gazelle/manifest/generate,gazelle/manifest/hasher,gazelle/manifest/test,gazelle/modules_mapping,gazelle/python,gazelle/pythonconfig,gazelle/python/private,tests/integration/compile_pip_requirements,tests/integration/compile_pip_requirements_test_from_external_repo,tests/integration/custom_commands,tests/integration/ignore_root_user_error,tests/integration/ignore_root_user_error/submodule,tests/integration/interpreter,tests/integration/local_toolchains,tests/integration/pip_parse,tests/integration/pip_parse/empty,tests/integration/py_cc_toolchain_registered
8+
query --deleted_packages=examples/build_file_generation,examples/build_file_generation/random_number_generator,examples/bzlmod,examples/bzlmod_build_file_generation,examples/bzlmod_build_file_generation/other_module/other_module/pkg,examples/bzlmod_build_file_generation/runfiles,examples/bzlmod/entry_points,examples/bzlmod/entry_points/tests,examples/bzlmod/libs/my_lib,examples/bzlmod/other_module,examples/bzlmod/other_module/other_module/pkg,examples/bzlmod/patches,examples/bzlmod/py_proto_library,examples/bzlmod/py_proto_library/example.com/another_proto,examples/bzlmod/py_proto_library/example.com/proto,examples/bzlmod/runfiles,examples/bzlmod/tests,examples/bzlmod/tests/other_module,examples/bzlmod/whl_mods,examples/multi_python_versions/libs/my_lib,examples/multi_python_versions/requirements,examples/multi_python_versions/tests,examples/pip_parse,examples/pip_parse_vendored,examples/pip_repository_annotations,examples/py_proto_library,examples/py_proto_library/example.com/another_proto,examples/py_proto_library/example.com/proto,gazelle,gazelle/manifest,gazelle/manifest/generate,gazelle/manifest/hasher,gazelle/manifest/test,gazelle/modules_mapping,gazelle/python,gazelle/pythonconfig,gazelle/python/private,tests/integration/compile_pip_requirements,tests/integration/compile_pip_requirements_test_from_external_repo,tests/integration/custom_commands,tests/integration/ignore_root_user_error,tests/integration/ignore_root_user_error/submodule,tests/integration/interpreter,tests/integration/local_toolchains,tests/integration/pip_parse,tests/integration/pip_parse/empty,tests/integration/py_cc_toolchain_registered
99

1010
test --test_output=errors
1111

python/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ filegroup(
3535
name = "distribution",
3636
srcs = glob(["**"]) + [
3737
"//python/api:distribution",
38+
"//python/bin:distribution",
3839
"//python/cc:distribution",
3940
"//python/config_settings:distribution",
4041
"//python/constraints:distribution",

python/bin/BUILD.bazel

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,22 @@
11
load("//python:defs.bzl", "py_binary")
22
load(":interpreter.bzl", "interpreter")
33

4+
package(default_visibility = ["//visibility:public"])
5+
6+
filegroup(
7+
name = "distribution",
8+
srcs = glob(["**"]),
9+
visibility = ["//python:__pkg__"],
10+
)
11+
412
interpreter(
513
name = "interpreter",
14+
binary = ":interpreter_src",
615
)
716

17+
# The user can modify this flag to source different interpreters for the
18+
# `interpreter` target above.
819
label_flag(
9-
name = "bin",
20+
name = "interpreter_src",
1021
build_setting_default = "//python:none",
1122
)

python/bin/interpreter.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ interpreter = rule(
3434
executable = True,
3535
attrs = {
3636
"binary": attr.label(
37-
default = "//tools/run:bin",
37+
mandatory = True,
3838
),
3939
},
4040
)

tests/integration/BUILD.bazel

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,11 @@ rules_python_integration_test(
120120
py_main = "custom_commands_test.py",
121121
)
122122

123+
rules_python_integration_test(
124+
name = "interpreter_test",
125+
py_main = "interpreter_test.py",
126+
)
127+
123128
py_library(
124129
name = "runner_lib",
125130
srcs = ["runner.py"],
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Copyright 2024 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: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Copyright 2024 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+
module(name = "module_under_test")
16+
17+
bazel_dep(name = "rules_python", version = "0.0.0")
18+
local_path_override(
19+
module_name = "rules_python",
20+
path = "../../..",
21+
)

tests/integration/interpreter/WORKSPACE

Whitespace-only changes.
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# Copyright 2024 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+
import logging
16+
import unittest
17+
18+
from tests.integration import runner
19+
20+
21+
class InterpreterTest(runner.TestCase):
22+
def _run_version_test(self, expected_version):
23+
"""Validates that we can successfully execute arbitrary code from the CLI."""
24+
result = self.run_bazel(
25+
"run",
26+
f"--@rules_python//python/config_settings:python_version={expected_version}",
27+
"@rules_python//python/bin:interpreter",
28+
input = "\r".join([
29+
"import sys",
30+
"v = sys.version_info",
31+
"print(f'version: {v.major}.{v.minor}')",
32+
]),
33+
)
34+
self.assert_result_matches(result, f"version: {expected_version}")
35+
36+
def test_run_interpreter_3_10(self):
37+
self._run_version_test("3.10")
38+
39+
def test_run_interpreter_3_11(self):
40+
self._run_version_test("3.11")
41+
42+
def test_run_interpreter_3_12(self):
43+
self._run_version_test("3.12")
44+
45+
def _run_module_test(self, version):
46+
"""Validates that we can successfully invoke a module from the CLI."""
47+
result = self.run_bazel(
48+
"run",
49+
f"--@rules_python//python/config_settings:python_version={version}",
50+
"@rules_python//python/bin:interpreter",
51+
"--",
52+
"-m",
53+
"json.tool",
54+
input = '{"json":"obj"}',
55+
)
56+
self.assert_result_matches(result, r'{\n "json": "obj"\n}')
57+
58+
def test_run_module_3_10(self):
59+
self._run_module_test("3.10")
60+
61+
def test_run_module_3_11(self):
62+
self._run_module_test("3.11")
63+
64+
def test_run_module_3_12(self):
65+
self._run_module_test("3.12")
66+
67+
68+
69+
if __name__ == "__main__":
70+
# Enabling this makes the runner log subprocesses as the test goes along.
71+
# logging.basicConfig(level = "INFO")
72+
unittest.main()

tests/integration/runner.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ def setUp(self):
8686
"RUNFILES_DIR": os.environ["TEST_SRCDIR"]
8787
}
8888

89-
def run_bazel(self, *args: str, check: bool = True) -> ExecuteResult:
89+
def run_bazel(self, *args: str, input=None, check: bool = True) -> ExecuteResult:
9090
"""Run a bazel invocation.
9191
9292
Args:
@@ -104,6 +104,7 @@ def run_bazel(self, *args: str, check: bool = True) -> ExecuteResult:
104104
proc_result = subprocess.run(
105105
args=args,
106106
text=True,
107+
input=input,
107108
capture_output=True,
108109
cwd=cwd,
109110
env=env,

0 commit comments

Comments
 (0)