Skip to content

Commit 5f46437

Browse files
committed
add tests
1 parent 200e5e8 commit 5f46437

File tree

3 files changed

+118
-0
lines changed

3 files changed

+118
-0
lines changed

tests/repl/BUILD.bazel

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
load("//python:py_library.bzl", "py_library")
2+
load("//tests/support:sh_py_run_test.bzl", "py_reconfig_test")
3+
4+
# A library that adds a special import path only when this is specified as a
5+
# dependency. This makes it easy for a dependency to have this import path
6+
# available without the top-level target being able to import the module.
7+
py_library(
8+
name = "helper/test_module",
9+
srcs = [
10+
"helper/test_module.py",
11+
],
12+
imports = [
13+
"helper",
14+
],
15+
)
16+
17+
py_reconfig_test(
18+
name = "repl_without_dep_test",
19+
srcs = ["repl_test.py"],
20+
main = "repl_test.py",
21+
data = [
22+
"//python/bin:repl",
23+
],
24+
env = {
25+
# The helper/test_module should _not_ be importable for this test.
26+
"EXPECT_TEST_MODULE_IMPORTABLE": "0",
27+
},
28+
python_version = "3.12",
29+
)
30+
31+
py_reconfig_test(
32+
name = "repl_with_dep_test",
33+
srcs = ["repl_test.py"],
34+
main = "repl_test.py",
35+
data = [
36+
"//python/bin:repl",
37+
],
38+
env = {
39+
# The helper/test_module _should_ be importable for this test.
40+
"EXPECT_TEST_MODULE_IMPORTABLE": "1",
41+
},
42+
python_version = "3.12",
43+
repl_dep = ":helper/test_module",
44+
)

tests/repl/helper/test_module.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
"""This is a file purely intended for validating //python/bin:repl."""
2+
3+
def print_hello():
4+
print("Hello World")

tests/repl/repl_test.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import os
2+
import subprocess
3+
import sys
4+
import unittest
5+
from typing import Iterable
6+
7+
from python import runfiles
8+
9+
rfiles = runfiles.Create()
10+
11+
# Signals the tests below whether we should be expecting the import of
12+
# helpers/test_module.py on the REPL to work or not.
13+
EXPECT_TEST_MODULE_IMPORTABLE = (os.environ["EXPECT_TEST_MODULE_IMPORTABLE"] == "1")
14+
15+
class ReplTest(unittest.TestCase):
16+
def setUp(self):
17+
self.repl = rfiles.Rlocation("rules_python/python/bin/repl")
18+
assert self.repl
19+
20+
21+
def run_code_in_repl(self, lines: Iterable[str]) -> str:
22+
"""Runs the lines of code in the REPL and returns the text output."""
23+
return subprocess.check_output(
24+
[self.repl],
25+
text=True,
26+
stderr=subprocess.STDOUT,
27+
input="\n".join(lines),
28+
).strip()
29+
30+
31+
32+
def test_repl_version(self):
33+
"""Validates that we can successfully execute arbitrary code on the REPL."""
34+
35+
result = self.run_code_in_repl([
36+
"import sys",
37+
"v = sys.version_info",
38+
"print(f'version: {v.major}.{v.minor}')",
39+
])
40+
self.assertIn("version: 3.12", result)
41+
42+
def test_cannot_import_test_module_directly(self):
43+
"""Validates that we cannot import helper/test_module.py since it's not a direct dep."""
44+
with self.assertRaises(ModuleNotFoundError):
45+
import test_module
46+
47+
48+
49+
50+
51+
52+
@unittest.skipIf(not EXPECT_TEST_MODULE_IMPORTABLE, "test only works without repl_dep set")
53+
def test_import_test_module_success(self):
54+
"""Validates that we can import helper/test_module.py when repl_dep is set."""
55+
result = self.run_code_in_repl([
56+
"import test_module",
57+
"test_module.print_hello()",
58+
])
59+
self.assertIn("Hello World", result)
60+
@unittest.skipIf(EXPECT_TEST_MODULE_IMPORTABLE, "test only works without repl_dep set")
61+
def test_import_test_module_failure(self):
62+
"""Validates that we cannot import helper/test_module.py when repl_dep isn't set."""
63+
result = self.run_code_in_repl([
64+
"import test_module",
65+
])
66+
self.assertIn("ModuleNotFoundError: No module named 'test_module'", result)
67+
68+
69+
if __name__ == "__main__":
70+
unittest.main()

0 commit comments

Comments
 (0)