| 
 | 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