-
Notifications
You must be signed in to change notification settings - Fork 21
/
tests.py
193 lines (152 loc) · 5.86 KB
/
tests.py
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import os
import random
import shutil
import string
import sys
import tempfile
import unittest
from virtualenvapi.manage import VirtualEnvironment
packages_for_tests = ['pep8']
non_lowercase_packages_for_test = ['Pillow']
all_packages_for_tests = packages_for_tests + non_lowercase_packages_for_test
def which(program):
def is_exe(fpath):
return os.path.isfile(fpath) and os.access(fpath, os.X_OK)
fpath, fname = os.path.split(program)
if fpath:
if is_exe(program):
return program
else:
for path in os.environ["PATH"].split(os.pathsep):
path = path.strip('"')
exe_file = os.path.join(path, program)
if is_exe(exe_file):
return exe_file
return None
class TestBase(unittest.TestCase):
"""
Base class for test cases to inherit from.
"""
env_path = None
def setUp(self):
self.env_path = tempfile.mkdtemp()
self.virtual_env_obj = VirtualEnvironment(self.env_path)
def tearDown(self):
if os.path.exists(self.env_path):
shutil.rmtree(self.env_path)
def _install_packages(self, packages):
for pack in packages:
self.virtual_env_obj.install(pack)
def _uninstall_packages(self, packages):
for pack in packages:
self.virtual_env_obj.uninstall(pack)
class InstalledTestCase(TestBase):
"""
Test install/uninstall operations.
"""
def test_installed(self):
self.assertFalse(self.virtual_env_obj.is_installed(''.join(random.sample(string.ascii_letters, 30))))
def test_install(self):
for pack in all_packages_for_tests:
self.virtual_env_obj.install(pack)
self.assertTrue(self.virtual_env_obj.is_installed(pack))
def test_install_requirements(self):
"""test installing Python packages from a pip requirements file"""
# create a temporary requirements.txt file with some packages
with tempfile.NamedTemporaryFile('w') as tmp_requirements_file:
tmp_requirements_file.write('\n'.join(packages_for_tests))
tmp_requirements_file.flush()
self.virtual_env_obj.install('-r {}'.format(tmp_requirements_file.name))
for pack in packages_for_tests:
self.assertTrue(self.virtual_env_obj.is_installed(pack))
def test_uninstall(self):
self._install_packages(all_packages_for_tests)
for pack in all_packages_for_tests:
if pack.endswith('.git'):
pack = pack.split('/')[-1].replace('.git', '')
self.virtual_env_obj.uninstall(pack)
self.assertFalse(self.virtual_env_obj.is_installed(pack))
def test_wheel(self):
self.virtual_env_obj.install('wheel') # required for this test
for pack in packages_for_tests:
self.virtual_env_obj.wheel(pack, options=['--wheel-dir=/tmp/wheelhouse'])
self.virtual_env_obj.install(pack, options=['--no-index', '--find-links=/tmp/wheelhouse'])
self.assertTrue(self.virtual_env_obj.is_installed(pack))
class SearchTestCase(TestBase):
"""
Test pip search.
"""
def test_search(self):
for pack in all_packages_for_tests:
result = self.virtual_env_obj.search(pack)
self.assertIsInstance(result, dict)
self.assertTrue(bool(result))
if result:
self.assertIn(pack.lower(), [k.split(' (')[0].lower() for k in result.keys()])
def test_search_names(self):
for pack in all_packages_for_tests:
result = self.virtual_env_obj.search_names(pack)
self.assertIsInstance(result, list)
self.assertIn(pack.lower(), [k.split(' (')[0].lower() for k in result])
class PythonArgumentTestCase(TestBase):
"""
Test passing a different interpreter path to `VirtualEnvironment` (`virtualenv -p`).
"""
def setUp(self):
self.env_path = tempfile.mkdtemp()
self.python = which('python')
self.assertIsNotNone(self.python)
self.virtual_env_obj = VirtualEnvironment(self.env_path, python=self.python)
def test_python_version(self):
self.assertEqual(self.virtual_env_obj.python, self.python)
self.assertEqual(
os.path.dirname(self.python),
os.path.dirname(which('pip'))
)
class SystemSitePackagesTest(unittest.TestCase):
"""
test coverage for using system-site-packages flag
This verifies the presence of the no-global-site-packages
file in the venv for non-system packages venv and
verified it is not present for the true case
"""
def setUp(self):
"""
snapshot system package list
"""
self.dir = tempfile.mkdtemp()
self.no_global = (
"lib/python{}.{}"
"/no-global-site-packages.txt"
).format(
sys.version_info.major,
sys.version_info.minor
)
def tearDown(self):
if os.path.exists(self.dir):
shutil.rmtree(self.dir)
def test_system_site_packages(self):
"""
test that creating a venv with system_site_packages=True
results in a venv that does not contain the no-global-site-packages file
"""
venv = VirtualEnvironment(self.dir, system_site_packages=True)
venv._create()
expected = os.path.join(venv.path, self.no_global)
self.assertTrue(
not os.path.exists(expected)
)
def test_no_system_site_packages(self):
"""
test that creating a venv with system_site_packages=False
results in a venv that contains the no-global-site-packages
file
"""
venv = VirtualEnvironment(self.dir)
venv._create()
expected = os.path.join(venv.path, self.no_global)
self.assertTrue(
os.path.exists(expected)
)
if __name__ == '__main__':
unittest.main()