8000 tests: libvcs.utils.subprocess · vcs-python/libvcs@03c16d4 · GitHub
[go: up one dir, main page]

Skip to content

Commit 03c16d4

Browse files
committed
tests: libvcs.utils.subprocess
1 parent 849e0b0 commit 03c16d4

File tree

1 file changed

+129
-0
lines changed

1 file changed

+129
-0
lines changed

tests/utils/test_subprocess.py

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
import pathlib
2+
import subprocess
3+
from typing import Any
4+
5+
import pytest
6+
7+
from libvcs.utils.subprocess import SubprocessCommand
8+
9+
10+
@pytest.fixture(autouse=True)
11+
def cwd_default(monkeypatch: pytest.MonkeyPatch, tmp_path: pathlib.Path):
12+
monkeypatch.chdir(tmp_path)
13+
14+
15+
@pytest.mark.parametrize(
16+
"args,kwargs,expected_result",
17+
[
18+
[["ls"], {}, SubprocessCommand("ls")],
19+
[[["ls", "-l"]], {}, SubprocessCommand(["ls", "-l"])],
20+
[[], {"args": ["ls", "-l"]}, SubprocessCommand(["ls", "-l"])],
21+
[["ls -l"], {"shell": True}, SubprocessCommand("ls -l", shell=True)],
22+
[[], {"args": "ls -l", "shell": True}, SubprocessCommand("ls -l", shell=True)],
23+
[
24+
[],
25+
{"args": ["ls", "-l"], "shell": True},
26+
SubprocessCommand(["ls", "-l"], shell=True),
27+
],
28+
],
29+
)
30+
def test_init(args: list, kwargs: dict, expected_result: Any):
31+
"""Test SubprocessCommand via list + kwargs, assert attributes"""
32+
cmd = SubprocessCommand(*args, **kwargs)
33+
assert cmd == expected_result
34+
35+
# Attributes in cmd should match whats passed in
36+
for k, v in kwargs.items():
37+
assert getattr(cmd, k) == v
38+
39+
proc = cmd.Popen()
40+
proc.communicate()
41+
assert proc.returncode == 0
42+
43+
44+
FIXTURES = [
45+
[["ls"], {}, SubprocessCommand("ls")],
46+
[[["ls", "-l"]], {}, SubprocessCommand(["ls", "-l"])],
47+
]
48+
49+
50+
@pytest.mark.parametrize(
51+
"args,kwargs,expected_result",
52+
FIXTURES,
53+
)
54+
def test_init_and_Popen(args: list, kwargs: dict, expected_result: Any):
55+
"""Test SubprocessCommand with Popen"""
56+
cmd = SubprocessCommand(*args, **kwargs)
57+
assert cmd == expected_result
58+
59+
cmd_proc = cmd.Popen()
60+
cmd_proc.communicate()
61+
assert cmd_proc.returncode == 0
62+
63+
proc = subprocess.Popen(*args, **kwargs)
64+
proc.communicate()
65+
assert proc.returncode == 0
66+
67+
68+
@pytest.mark.parametrize(
69+
"args,kwargs,expected_result",
70+
FIXTURES,
71+
)
72+
def test_init_and_Popen_run(args: list, kwargs: dict, expected_result: Any):
73+
"""Test SubprocessCommand with run"""
74+
cmd = SubprocessCommand(*args, **kwargs)
75+
assert cmd == expected_result
76+
77+
cmd_proc = cmd.Popen()
78+
cmd_proc.communicate()
79+
assert cmd_proc.returncode == 0
80+
81+
proc = subprocess.run(*args, **kwargs)
82+
assert proc.returncode == 0
83+
84+
85+
@pytest.mark.parametrize(
86+
"args,kwargs,expected_result",
87+
FIXTURES,
88+
)
89+
def test_init_and_check_call(args: list, kwargs: dict, expected_result: Any):
90+
"""Test SubprocessCommand with Popen.check_call"""
91+
cmd = SubprocessCommand(*args, **kwargs)
92+
assert cmd == expected_result
93+
94+
return_code = cmd.check_call()
95+
assert return_code == 0
96+
97+
proc = subprocess.check_call(*args, **kwargs)
98+
assert proc == return_code
99+
100+
101+
@pytest.mark.parametrize(
102+
"args,kwargs,expected_result",
103+
FIXTURES,
104+
)
105+
def test_init_and_check_output(args: list, kwargs: dict, expected_result: Any):
106+
"""Test SubprocessCommand with Popen.check_output"""
107+
cmd = SubprocessCommand(*args, **kwargs)
108+
assert cmd == expected_result
109+
110+
return_output = cmd.check_output()
111+
assert isinstance(return_output, bytes)
112+
113+
proc = subprocess.check_output(*args, **kwargs)
114+
assert proc == return_output
115+
116+
117+
@pytest.mark.parametrize(
118+
"args,kwargs,run_kwargs",
119+
[
120+
[["ls"], {}, {}],
121+
[[["ls", "-l"]], {}, {}],
122+
[[["ls", "-al"]], {}, {"stdout": subprocess.DEVNULL}],
123+
],
124+
)
125+
def test_run(tmp_path: pathlib.Path, args: list, kwargs: dict, run_kwargs: dict):
126+
cmd = SubprocessCommand(*args, cwd=tmp_path, **kwargs)
127+
response = cmd.run(**run_kwargs)
128+
129+
assert response.returncode == 0

0 commit comments

Comments
 (0)
0