Skip to content

Commit ce3f15f

Browse files
committed
Provide high level overview of design logic (draft)
1 parent 2dfc9f8 commit ce3f15f

1 file changed

Lines changed: 74 additions & 79 deletions

File tree

tests/test_subprocess.py

Lines changed: 74 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""`pytest` tests for `utils/subprocess.py`."""
22

3+
import itertools
34
import os
45
import subprocess
56
from pathlib import Path
@@ -17,90 +18,84 @@ def subprocess_handler(self):
1718
"""Return an instance of `SubprocessWrapper` for testing."""
1819
return SubprocessWrapper()
1920

20-
def test_stdout_is_suppressed_in_non_verbose_mode(self, subprocess_handler, capfd):
21-
"""Success case: test stdout is suppressed in non-verbose mode."""
22-
subprocess_handler.run_cmd("echo foo")
23-
captured = capfd.readouterr()
24-
assert not captured.out
25-
assert not captured.err
26-
27-
def test_stderr_is_suppressed_in_non_verbose_mode(self, subprocess_handler, capfd):
28-
"""Success case: test stderr is suppressed in non-verbose mode."""
29-
subprocess_handler.run_cmd("echo foo 1>&2")
30-
captured = capfd.readouterr()
31-
assert not captured.out
32-
assert not captured.err
33-
34-
def test_command_and_stdout_is_printed_in_verbose_mode(
35-
self, subprocess_handler, capfd
36-
):
37-
"""Success case: test command and stdout is printed in verbose mode."""
38-
subprocess_handler.run_cmd("echo foo", verbose=True)
39-
captured = capfd.readouterr()
40-
assert captured.out == "echo foo\nfoo\n"
41-
assert not captured.err
42-
43-
def test_command_and_stderr_is_redirected_to_stdout_in_verbose_mode(
44-
self, subprocess_handler, capfd
45-
):
46-
"""Success case: test command and stderr is redirected to stdout in verbose mode."""
47-
subprocess_handler.run_cmd("echo foo 1>&2", verbose=True)
48-
captured = capfd.readouterr()
49-
assert captured.out == "echo foo 1>&2\nfoo\n"
50-
assert not captured.err
51-
52-
def test_output_is_captured_with_capture_output_enabled(
53-
self, subprocess_handler, capfd
54-
):
55-
"""Success case: test output is captured with capture_output enabled."""
56-
proc = subprocess_handler.run_cmd("echo foo", capture_output=True)
57-
captured = capfd.readouterr()
58-
assert not captured.out
59-
assert not captured.err
60-
assert proc.stdout == "foo\n"
61-
assert not proc.stderr
62-
63-
def test_stderr_captured_to_stdout(self, subprocess_handler, capfd):
64-
"""Success case: test stderr is captured to stdout with capture_output enabled."""
65-
proc = subprocess_handler.run_cmd("echo foo 1>&2", capture_output=True)
66-
captured = capfd.readouterr()
67-
assert not captured.out
21+
# Parameterization
22+
@pytest.fixture(params=list(itertools.product([False, True], repeat=3)))
23+
def generate_params(self, request):
24+
possible_inputs = request.param
25+
return {
26+
"param_verbose": possible_inputs[0],
27+
"param_capture": possible_inputs[1],
28+
"param_file": possible_inputs[2],
29+
}
30+
31+
@pytest.fixture(params=[("echo foo", "foo\n"), ("echo foo 1>&2", "foo\n")])
32+
def generated_input(self, request, generate_params):
33+
command = request.param
34+
return generate_params | {
35+
"command": {
36+
"input": command[0],
37+
"expected": command[1],
38+
}
39+
}
40+
41+
def _test_verbose(self, is_stdout_verbose, param_verbose, captured, command):
6842
assert not captured.err
69-
assert proc.stdout == "foo\n"
43+
expected = command["input"] + "\n"
44+
if param_verbose and is_stdout_verbose:
45+
expected += command["expected"]
46+
if is_stdout_verbose or param_verbose:
47+
assert captured.out == expected
48+
else:
49+
assert not captured.out
50+
51+
def _test_capture_output(self, is_stdout_capture, proc, expected):
7052
assert not proc.stderr
53+
if is_stdout_capture:
54+
assert proc.stdout == expected
55+
else:
56+
assert not proc.stdout
57+
58+
def _test_output_file(self, is_stdout_file, file_path, expected):
59+
if is_stdout_file:
60+
with file_path.open("r", encoding="utf-8") as file:
61+
assert file.read() == expected
62+
else:
63+
# TODO: Check for non-existent file
64+
pass
65+
66+
def test_subprocess_logic(self, subprocess_handler, generated_input, capfd):
67+
68+
is_stdout_verbose, is_stdout_output, is_stdout_capture = (False, False, False)
69+
file_path = None
70+
71+
if generated_input["param_capture"]:
72+
is_stdout_capture = True
73+
elif generated_input["param_file"]:
74+
is_stdout_output = True
75+
file_path = Path("out.txt")
76+
elif generated_input["param_verbose"]:
77+
is_stdout_verbose = True
7178

72-
def test_command_is_printed_and_stdout_is_captured_in_verbose_mode(
73-
self, subprocess_handler, capfd
74-
):
75-
"""Success case: test command is printed and stdout is captured in verbose mode."""
76-
proc = subprocess_handler.run_cmd("echo foo", capture_output=True, verbose=True)
77-
captured = capfd.readouterr()
78-
assert captured.out == "echo foo\n"
79-
assert not captured.err
80-
assert proc.stdout == "foo\n"
81-
assert not proc.stderr
82-
83-
def test_stdout_is_redirected_to_file(self, subprocess_handler, capfd):
84-
"""Success case: test stdout is redirected to file."""
85-
file_path = Path("out.txt")
86-
subprocess_handler.run_cmd("echo foo", output_file=file_path)
87-
with file_path.open("r", encoding="utf-8") as file:
88-
assert file.read() == "foo\n"
79+
proc = subprocess_handler.run_cmd(
80+
generated_input["command"]["input"],
81+
verbose=generated_input["param_verbose"],
82+
capture_output=generated_input["param_capture"],
83+
output_file=file_path,
84+
)
8985
captured = capfd.readouterr()
90-
assert not captured.out
91-
assert not captured.err
9286

93-
def test_command_is_printed_and_stdout_is_redirected_to_file_in_verbose_mode(
94-
self, subprocess_handler, capfd
95-
):
96-
"""Success case: test command is printed and stdout is redirected to file in verbose mode."""
97-
file_path = Path("out.txt")
98-
subprocess_handler.run_cmd("echo foo", output_file=file_path, verbose=True)
99-
with file_path.open("r", encoding="utf-8") as file:
100-
assert file.read() == "foo\n"
101-
captured = capfd.readouterr()
102-
assert captured.out == "echo foo\n"
103-
assert not captured.err
87+
self._test_verbose(
88+
is_stdout_verbose,
89+
generated_input["param_verbose"],
90+
captured,
91+
generated_input["command"],
92+
)
93+
self._test_capture_output(
94+
is_stdout_capture, proc, generated_input["command"]["expected"]
95+
)
96+
self._test_output_file(
97+
is_stdout_output, file_path, generated_input["command"]["expected"]
98+
)
10499

105100
def test_command_is_run_with_environment(self, subprocess_handler):
106101
"""Success case: test command is run with environment."""

0 commit comments

Comments
 (0)