Merge "Add flag to not add top-level modules to PYTHONPATH"

This commit is contained in:
Cole Faust
2022-09-26 17:24:08 +00:00
committed by Gerrit Code Review
8 changed files with 68 additions and 80 deletions

View File

@@ -0,0 +1,9 @@
python_test_host {
name: "py_dont_add_top_level_dirs_test",
main: "main.py",
srcs: [
"main.py",
"mypkg/mymodule.py",
],
dont_add_top_level_directories_to_path: true,
}

View File

@@ -0,0 +1,17 @@
import unittest
import sys
print(sys.path, file=sys.stderr)
class TestProtoWithPkgPath(unittest.TestCase):
def test_cant_import_mymodule_directly(self):
with self.assertRaises(ImportError):
import mymodule
def test_can_import_mymodule_by_parent_package(self):
import mypkg.mymodule
if __name__ == '__main__':
unittest.main()