forked from bazel-contrib/rules_python
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbin.py
More file actions
123 lines (99 loc) · 4.49 KB
/
bin.py
File metadata and controls
123 lines (99 loc) · 4.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import importlib
import sys
import unittest
from pathlib import Path
class VenvSitePackagesLibraryTest(unittest.TestCase):
def setUp(self):
super().setUp()
if sys.prefix == sys.base_prefix:
raise AssertionError("Not running under a venv")
self.venv = sys.prefix
def assert_imported_from_venv(self, module_name):
module = importlib.import_module(module_name)
self.assertEqual(module.__name__, module_name)
self.assertIsNotNone(
module.__file__,
f"Expected module {module_name!r} to have"
+ f"__file__ set, but got None. {module=}",
)
self.assertTrue(
module.__file__.startswith(self.venv),
f"\n{module_name} was imported, but not from the venv.\n"
+ f" venv: {self.venv}\n"
+ f"module file: {module.__file__}\n"
+ "sys.path:\n"
+ "\n".join(sys.path),
)
return module
def test_imported_from_venv(self):
m = self.assert_imported_from_venv("pkgutil_top")
self.assertEqual(m.WHOAMI, "pkgutil_top")
m = self.assert_imported_from_venv("pkgutil_top.sub")
self.assertEqual(m.WHOAMI, "pkgutil_top.sub")
self.assert_imported_from_venv("nspkg.subnspkg.alpha")
self.assert_imported_from_venv("nspkg.subnspkg.beta")
self.assert_imported_from_venv("nspkg.subnspkg.gamma")
self.assert_imported_from_venv("nspkg.subnspkg.delta")
self.assert_imported_from_venv("single_file")
self.assert_imported_from_venv("simple")
m = self.assert_imported_from_venv("nested_with_pth")
self.assertEqual(m.WHOAMI, "nested_with_pth")
def test_data_is_included(self):
self.assert_imported_from_venv("simple")
module = importlib.import_module("simple")
module_path = Path(module.__file__)
site_packages = module_path.parent.parent
# Ensure that packages from simple v1 are not present
files = [p.name for p in site_packages.glob("*")]
self.assertIn("simple_v1_extras", files)
def test_override_pkg(self):
self.assert_imported_from_venv("simple")
module = importlib.import_module("simple")
self.assertEqual(
"1.0.0",
module.__version__,
)
def test_dirs_from_replaced_package_are_not_present(self):
self.assert_imported_from_venv("simple")
module = importlib.import_module("simple")
module_path = Path(module.__file__)
site_packages = module_path.parent.parent
dist_info_dirs = [p.name for p in site_packages.glob("simple*.dist-info")]
self.assertEqual(
["simple-1.0.0.dist-info"],
dist_info_dirs,
)
# Ensure that packages from simple v1 are not present
files = [p.name for p in site_packages.glob("*")]
self.assertNotIn("simple.libs", files)
def test_data_from_another_pkg_is_included_via_copy_file(self):
self.assert_imported_from_venv("simple")
module = importlib.import_module("simple")
module_path = Path(module.__file__)
site_packages = module_path.parent.parent
# Ensure that packages from simple v1 are not present
d = site_packages / "external_data"
files = [p.name for p in d.glob("*")]
self.assertIn("another_module_data.txt", files)
def test_whl_with_data_included(self):
module = self.assert_imported_from_venv("whl_with_data")
module_path = Path(module.__file__)
site_packages = module_path.parent.parent
# purelib
data_file = site_packages / "whl_with_data" / "data_file.txt"
self.assertTrue(data_file.exists(), f"Expected {data_file} to exist")
# platlib
platlib_file = site_packages / "whl_with_data" / "platlib_file.txt"
self.assertTrue(platlib_file.exists(), f"Expected {platlib_file} to exist")
venv_root = Path(self.venv)
# data
data_data_file = venv_root / "data" / "whl_with_data" / "data_data_file.txt"
self.assertTrue(data_data_file.exists(), f"Expected {data_data_file} to exist")
# scripts
script_file = venv_root / "bin" / "whl_script.sh"
self.assertTrue(script_file.exists(), f"Expected {script_file} to exist")
# headers
header_file = venv_root / "include" / "whl_with_data" / "header_file.h"
self.assertTrue(header_file.exists(), f"Expected {header_file} to exist")
if __name__ == "__main__":
unittest.main()