-
Notifications
You must be signed in to change notification settings - Fork 88
/
Copy pathrequirements_test.py
240 lines (178 loc) · 7.96 KB
/
requirements_test.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import platform
import pytest
import requirements
import shutil
import subprocess
import sys
import virtualenv
from functools import partial
class PopenStub:
def __init__(self, returncode):
self.returncode = returncode
def communicate(self):
self.returncode = self.returncode
@pytest.fixture
def mock_virtualenv(monkeypatch):
virtualenv_calls = []
if hasattr(virtualenv, "main"):
def mock_virtualenv_main():
virtualenv_calls.append(sys.argv[:])
monkeypatch.setattr(virtualenv, "main", mock_virtualenv_main)
else:
def mock_virtualenv_cli_run(args):
virtualenv_calls.append([""] + args)
monkeypatch.setattr(virtualenv, "cli_run", mock_virtualenv_cli_run)
return virtualenv_calls
@pytest.fixture
def mock_system(monkeypatch):
calls = []
def mock_any(func, retval, *args, **kwargs):
calls.append((func, args))
if hasattr(retval, "__call__"):
return retval(*args)
else:
return retval
monkeypatch.setattr(
os, "listdir", partial(mock_any, "os.listdir", ["dir1", "dir2", "file1"])
)
monkeypatch.setattr(os, "mkdir", partial(mock_any, "os.mkdir", None))
monkeypatch.setattr(os, "remove", partial(mock_any, "os.remove", None))
monkeypatch.setattr(os.path, "isfile", partial(mock_any, "os.path.isfile", True))
monkeypatch.setattr(
os.path,
"isdir",
partial(mock_any, "os.path.isdir", lambda filename: filename != "/tmp/file1"),
)
monkeypatch.setattr(os.path, "exists", partial(mock_any, "os.path.exists", True))
monkeypatch.setattr(shutil, "rmtree", partial(mock_any, "shutil.rmtree", None))
monkeypatch.setattr(shutil, "copytree", partial(mock_any, "shutil.copytree", None))
monkeypatch.setattr(shutil, "move", partial(mock_any, "shutil.move", None))
monkeypatch.setattr(
platform, "system", partial(mock_any, "platform.system", "Linux")
)
monkeypatch.setattr(
subprocess, "Popen", partial(mock_any, "subprocess.Popen", PopenStub(0))
)
return calls
def test_package(mock_system, mock_virtualenv):
requirements.package(["/path1/requirements.txt", "/path2/requirements.txt"], "/tmp")
assert len(mock_virtualenv) == 1
assert mock_virtualenv[0] == ["", "/tmp/.venv", "--quiet", "-p", sys.executable]
# Checks that requirements files exist
assert mock_system.pop(0) == ("os.path.isfile", ("/path1/requirements.txt",))
assert mock_system.pop(0) == ("os.path.isfile", ("/path2/requirements.txt",))
# Checks that output dir exists
assert mock_system.pop(0) == ("os.path.exists", ("/tmp",))
assert mock_system.pop(0) == ("os.path.isdir", ("/tmp",))
assert mock_system.pop(0) == ("shutil.rmtree", ("/tmp",))
assert mock_system.pop(0) == ("os.mkdir", ("/tmp",))
# Checks and removes existing venv/tmp dirs
assert mock_system.pop(0) == ("os.path.exists", ("/tmp/.venv",))
assert mock_system.pop(0) == ("shutil.rmtree", ("/tmp/.venv",))
assert mock_system.pop(0) == ("os.path.exists", ("/tmp/.tmp",))
assert mock_system.pop(0) == ("shutil.rmtree", ("/tmp/.tmp",))
# Looks up system type
assert mock_system.pop(0) == ("platform.system", ())
# Looks for pip installation
assert mock_system.pop(0) == ("os.listdir", ("/tmp/.venv/lib",))
assert mock_system.pop(0) == ("os.path.isdir", ("/tmp/.venv/lib/dir1",))
assert mock_system.pop(0) == ("os.path.isdir", ("/tmp/.venv/lib/dir2",))
assert mock_system.pop(0) == ("os.path.isdir", ("/tmp/.venv/lib/file1",))
assert mock_system.pop(0) == ("os.path.isfile", ("/tmp/.venv/bin/pip",))
# Invokes pip for package installation
assert mock_system.pop(0) == (
"subprocess.Popen",
(["/tmp/.venv/bin/pip", "install", "-r", "/path1/requirements.txt"],),
)
assert mock_system.pop(0) == (
"subprocess.Popen",
(["/tmp/.venv/bin/pip", "install", "-r", "/path2/requirements.txt"],),
)
# Copies installed packages to temporary directory
assert mock_system.pop(0) == (
"os.path.isdir",
("/tmp/.venv/lib/dir1/site-packages",),
)
assert mock_system.pop(0) == (
"shutil.copytree",
("/tmp/.venv/lib/dir1/site-packages", "/tmp/.tmp"),
)
# Lists installed packages
assert mock_system.pop(0) == ("os.listdir", ("/tmp/.tmp",))
# Clears existing installation of package 1
assert mock_system.pop(0) == ("os.path.isdir", ("/tmp/dir1",))
assert mock_system.pop(0) == ("shutil.rmtree", ("/tmp/dir1",))
# Moves package 1 into place
assert mock_system.pop(0) == ("shutil.move", ("/tmp/.tmp/dir1", "/tmp"))
# Clears existing installation of package 2
assert mock_system.pop(0) == ("os.path.isdir", ("/tmp/dir2",))
assert mock_system.pop(0) == ("shutil.rmtree", ("/tmp/dir2",))
# Moves package 2 into place
assert mock_system.pop(0) == ("shutil.move", ("/tmp/.tmp/dir2", "/tmp"))
# Clears existing installation of package 3
assert mock_system.pop(0) == ("os.path.isdir", ("/tmp/file1",))
assert mock_system.pop(0) == ("os.path.exists", ("/tmp/file1",))
assert mock_system.pop(0) == ("os.remove", ("/tmp/file1",))
# Moves package 3 into place
assert mock_system.pop(0) == ("shutil.move", ("/tmp/.tmp/file1", "/tmp"))
# Performs final cleanup
assert mock_system.pop(0) == ("shutil.rmtree", ("/tmp/.venv",))
assert mock_system.pop(0) == ("shutil.rmtree", ("/tmp/.tmp",))
def test_package_missing_requirements_file(mock_system, mock_virtualenv, monkeypatch):
monkeypatch.setattr(os.path, "isfile", lambda f: False)
with pytest.raises(SystemExit):
requirements.package(["/path1/requirements.txt"], "/tmp")
def test_package_existing_target_file(mock_system, mock_virtualenv, monkeypatch):
monkeypatch.setattr(os.path, "isdir", lambda f: False)
with pytest.raises(SystemExit):
requirements.package(["/path1/requirements.txt"], "/tmp")
def test_package_missing_deps(mock_system, mock_virtualenv, monkeypatch):
monkeypatch.setattr(
os.path, "isdir", lambda f: f != "/tmp/.venv/lib/dir1/site-packages"
)
with pytest.raises(SystemExit):
requirements.package(["/path1/requirements.txt"], "/tmp")
def test_package_missing_pip(mock_system, mock_virtualenv, monkeypatch):
monkeypatch.setattr(os.path, "isfile", lambda f: f != "/tmp/.venv/bin/pip")
with pytest.raises(SystemExit):
requirements.package(["/path1/requirements.txt"], "/tmp")
def test_package_missing_python_dir(mock_system, mock_virtualenv, monkeypatch):
monkeypatch.setattr(os, "listdir", lambda f: [])
with pytest.raises(SystemExit):
requirements.package(["/path1/requirements.txt"], "/tmp")
def test_package_windows(mock_system, mock_virtualenv, monkeypatch):
monkeypatch.setattr(platform, "system", lambda: "Windows")
requirements.package(["/path1/requirements.txt"], "/tmp")
pip_calls = [c for c in mock_system if c[0] == "subprocess.Popen"]
assert pip_calls[0] == (
"subprocess.Popen",
(["/tmp/.venv/Scripts/pip.exe", "install", "-r", "/path1/requirements.txt"],),
)
def test_pip_error(mock_system, mock_virtualenv, monkeypatch):
monkeypatch.setattr(subprocess, "Popen", lambda *args, **kwargs: PopenStub(1))
with pytest.raises(SystemExit):
requirements.package(["/path1/requirements.txt"], "/tmp")
def test_package_with_pip_args(mock_system, mock_virtualenv):
requirements.package(
["/path1/requirements.txt"],
"/tmp",
"--no-deps --imaginary-arg 'imaginary \"value\"'",
)
# Invokes pip for package installation
assert mock_system[15] == (
"subprocess.Popen",
(
[
"/tmp/.venv/bin/pip",
"install",
"-r",
"/path1/requirements.txt",
"--no-deps",
"--imaginary-arg",
'imaginary "value"',
],
),
)