-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathtest_SubprocessCommand.py
More file actions
300 lines (253 loc) · 7.35 KB
/
test_SubprocessCommand.py
File metadata and controls
300 lines (253 loc) · 7.35 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
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
import pathlib
import subprocess
import sys
import textwrap
from typing import Any
from unittest import mock
import pytest
from libvcs._internal.subprocess import SubprocessCommand
def idfn(val: Any) -> str:
if isinstance(val, list):
if len(val):
return str(val[0])
return "[]]"
return str(val)
@pytest.mark.parametrize(
"args,kwargs,expected_result",
[
[["ls"], {}, SubprocessCommand("ls")],
[[["ls", "-l"]], {}, SubprocessCommand(["ls", "-l"])],
[[], {"args": ["ls", "-l"]}, SubprocessCommand(["ls", "-l"])],
[["ls -l"], {"shell": True}, SubprocessCommand("ls -l", shell=True)],
[[], {"args": "ls -l", "shell": True}, SubprocessCommand("ls -l", shell=True)],
[
[],
{"args": ["ls", "-l"], "shell": True},
SubprocessCommand(["ls", "-l"], shell=True),
],
],
ids=idfn,
)
def test_init(args: list, kwargs: dict, expected_result: Any):
"""Test SubprocessCommand via list + kwargs, assert attributes"""
cmd = SubprocessCommand(*args, **kwargs)
assert cmd == expected_result
# Attributes in cmd should match what's passed in
for k, v in kwargs.items():
assert getattr(cmd, k) == v
proc = cmd.Popen()
proc.communicate()
assert proc.returncode == 0
FIXTURES = [
[["ls"], {}, SubprocessCommand("ls")],
[[["ls", "-l"]], {}, SubprocessCommand(["ls", "-l"])],
]
@pytest.mark.parametrize(
"args,kwargs,expected_result",
FIXTURES,
ids=idfn,
)
def test_init_and_Popen(args: list, kwargs: dict, expected_result: Any):
"""Test SubprocessCommand with Popen"""
cmd = SubprocessCommand(*args, **kwargs)
assert cmd == expected_result
cmd_proc = cmd.Popen()
cmd_proc.communicate()
assert cmd_proc.returncode == 0
proc = subprocess.Popen(*args, **kwargs)
proc.communicate()
assert proc.returncode == 0
@pytest.mark.parametrize(
"args,kwargs,expected_result",
FIXTURES,
ids=idfn,
)
def test_init_and_Popen_run(args: list, kwargs: dict, expected_result: Any):
"""Test SubprocessCommand with run"""
cmd = SubprocessCommand(*args, **kwargs)
assert cmd == expected_result
cmd_proc = cmd.Popen()
cmd_proc.communicate()
assert cmd_proc.returncode == 0
proc = subprocess.run(*args, **kwargs)
assert proc.returncode == 0
@pytest.mark.parametrize(
"args,kwargs,expected_result",
FIXTURES,
ids=idfn,
)
def test_init_and_check_call(args: list, kwargs: dict, expected_result: Any):
"""Test SubprocessCommand with Popen.check_call"""
cmd = SubprocessCommand(*args, **kwargs)
assert cmd == expected_result
return_code = cmd.check_call()
assert return_code == 0
proc = subprocess.check_call(*args, **kwargs)
assert proc == return_code
@pytest.mark.parametrize(
"args,kwargs,expected_result",
FIXTURES,
)
def test_init_and_check_output(args: list, kwargs: dict, expected_result: Any):
"""Test SubprocessCommand with Popen.check_output"""
cmd = SubprocessCommand(*args, **kwargs)
assert cmd == expected_result
return_output = cmd.check_output()
assert isinstance(return_output, bytes)
proc = subprocess.check_output(*args, **kwargs)
assert proc == return_output
@pytest.mark.parametrize(
"args,kwargs,run_kwargs",
[
[["ls"], {}, {}],
[[["ls", "-l"]], {}, {}],
[[["ls", "-al"]], {}, {"stdout": subprocess.DEVNULL}],
],
ids=idfn,
)
def test_run(tmp_path: pathlib.Path, args: list, kwargs: dict, run_kwargs: dict):
kwargs["cwd"] = tmp_path
cmd = SubprocessCommand(*args, **kwargs)
response = cmd.run(**run_kwargs)
assert response.returncode == 0
@pytest.mark.parametrize(
"args,kwargs,run_kwargs",
[
[
["ls"],
{},
{},
],
[[["ls", "-l"]], {}, {}],
[[["ls", "-al"]], {}, {"stdout": subprocess.DEVNULL}],
],
ids=idfn,
)
@mock.patch("subprocess.Popen")
def test_Popen_mock(
mock_subprocess_popen,
tmp_path: pathlib.Path,
args: list,
kwargs: dict,
run_kwargs: dict,
capsys: pytest.LogCaptureFixture,
):
process_mock = mock.Mock()
attrs = {"communicate.return_value": ("output", "error"), "returncode": 1}
process_mock.configure_mock(**attrs)
mock_subprocess_popen.return_value = process_mock
kwargs["cwd"] = tmp_path
cmd = SubprocessCommand(*args, **kwargs)
response = cmd.Popen(**run_kwargs)
assert response.returncode == 1
@pytest.mark.parametrize(
"args,kwargs,run_kwargs",
[
[[["git", "pull", "--progress"]], {}, {}],
],
ids=idfn,
)
@mock.patch("subprocess.Popen")
def test_Popen_git_mock(
mock_subprocess_popen,
tmp_path: pathlib.Path,
args: list,
kwargs: dict,
run_kwargs: dict,
capsys: pytest.LogCaptureFixture,
):
process_mock = mock.Mock()
attrs = {"communicate.return_value": ("output", "error"), "returncode": 1}
process_mock.configure_mock(**attrs)
mock_subprocess_popen.return_value = process_mock
kwargs["cwd"] = tmp_path
cmd = SubprocessCommand(*args, **kwargs)
response = cmd.Popen(**run_kwargs)
stdout, stderr = response.communicate()
assert response.returncode == 1
assert stdout == "output"
assert stderr == "error"
CODE = (
textwrap.dedent(
r"""
import sys
import time
size = 10
for i in range(10):
time.sleep(.01)
sys.stderr.write(
'[' + "#" * i + "." * (size-i) + ']' + f' {i}/{size}' + '\n'
)
sys.stderr.flush()
"""
)
.strip("\n")
.lstrip()
)
def test_Popen_stderr(
tmp_path: pathlib.Path,
capsys: pytest.LogCaptureFixture,
):
cmd = SubprocessCommand(
[
sys.executable,
"-c",
CODE,
],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
cwd=tmp_path,
)
response = cmd.Popen()
while response.poll() is None:
stdout, stderr = response.communicate()
assert stdout != "output"
assert stderr != "1"
assert response.returncode == 0
def test_CaptureStderrMixin(
tmp_path: pathlib.Path,
capsys: pytest.LogCaptureFixture,
):
cmd = SubprocessCommand(
[
sys.executable,
"-c",
CODE,
],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
cwd=tmp_path,
)
response = cmd.Popen()
while response.poll() is None:
if response.stderr is not None:
line = response.stderr.readline().decode("utf-8").strip()
if line:
assert line.startswith("[")
assert response.returncode == 0
def test_CaptureStderrMixin_error(
tmp_path: pathlib.Path,
capsys: pytest.LogCaptureFixture,
):
cmd = SubprocessCommand(
[
sys.executable,
"-c",
CODE
+ textwrap.dedent(
"""
sys.exit("FATAL")
"""
),
],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
cwd=tmp_path,
)
response = cmd.Popen()
while response.poll() is None:
if response.stderr is not None:
line = response.stderr.readline().decode("utf-8").strip()
if line:
assert line.startswith("[") or line == "FATAL"
assert response.returncode == 1