Skip to content

Commit c1fc1ed

Browse files
committed
!squash test_mixins: Stub out trying to mock subprocess
1 parent 7be40cf commit c1fc1ed

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import pathlib
2+
import subprocess
3+
from typing import Any
4+
from unittest import mock
5+
6+
import pytest
7+
8+
from libvcs._internal.subprocess import SubprocessCommand
9+
10+
11+
def idfn(val: Any) -> str:
12+
if isinstance(val, list):
13+
if len(val):
14+
return str(val[0])
15+
return "[]]"
16+
17+
return str(val)
18+
19+
20+
@pytest.mark.parametrize(
21+
"args,kwargs,run_kwargs",
22+
[
23+
[["ls"], {}, {}],
24+
[[["ls", "-l"]], {}, {}],
25+
[[["ls", "-al"]], {}, {"stdout": subprocess.DEVNULL}],
26+
],
27+
ids=idfn,
28+
)
29+
@mock.patch("subprocess.Popen")
30+
def test_run(
31+
mock_subprocess_popen,
32+
tmp_path: pathlib.Path,
33+
args: list,
34+
kwargs: dict,
35+
run_kwargs: dict,
36+
capsys: pytest.LogCaptureFixture,
37+
):
38+
process_mock = mock.Mock()
39+
attrs = {"communicate.return_value": ("output", "error"), "returncode": 0}
40+
process_mock.configure_mock(**attrs)
41+
mock_subprocess_popen.return_value = process_mock
42+
43+
print(f"args: {args}, args[1:]: {args[1:]}")
44+
cmd = args[0] if isinstance(args, list) else [args]
45+
print(f"cmd: {cmd}")
46+
cmd = SubprocessCommand(*args, cwd=tmp_path, **kwargs)
47+
response = cmd.Popen(**run_kwargs)
48+
49+
assert response.returncode == 0

0 commit comments

Comments
 (0)