Skip to content

Commit 4c1b4c9

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

1 file changed

Lines changed: 67 additions & 80 deletions

File tree

tests/test_subprocess.py

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

3+
import itertools
34
import os
45
import subprocess
5-
from pathlib import Path
66

77
import pytest
88

@@ -17,90 +17,77 @@ def subprocess_handler(self):
1717
"""Return an instance of `SubprocessWrapper` for testing."""
1818
return SubprocessWrapper()
1919

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
20+
# Parameterization
21+
@pytest.fixture(params=list(itertools.product([True, False], repeat=3)))
22+
def generate_params(self, request):
23+
possible_inputs = request.param
24+
return {
25+
"param_verbose": possible_inputs[0],
26+
"param_capture": possible_inputs[1],
27+
"param_file": possible_inputs[2],
28+
}
29+
30+
@pytest.fixture(params=[("echo foo", "foo\n"), ("echo foo 1>&2", "foo\n")])
31+
def generated_input(self, request, generate_params):
32+
command = request.param
33+
return generate_params | {
34+
"command": {
35+
"input": command[0],
36+
"expected": command[1],
37+
}
38+
}
39+
40+
def test_verbose(self, is_stdout_verbose, param_verbose, captured, command):
5941
assert not captured.err
60-
assert proc.stdout == "foo\n"
42+
expected = command["cmd"]
43+
if param_verbose:
44+
expected += f"\n{expected}"
45+
if is_stdout_verbose:
46+
assert captured.out == expected
47+
else:
48+
assert not captured.out
49+
50+
def test_capture_output(self, is_stdout_capture, proc, expected):
6151
assert not proc.stderr
52+
if is_stdout_capture:
53+
assert proc.stdout == expected
54+
else:
55+
assert not proc.stdout
56+
57+
def test_output_file(self, is_stdout_file, file_path, expected):
58+
if is_stdout_file:
59+
with file_path.open("r", encoding="utf-8") as file:
60+
assert file.read() == expected
61+
else:
62+
# TODO: Check for non-existent file
63+
pass
64+
65+
def test_subprocess_logic(self, subprocess_handler, generated_input, capfd):
66+
67+
is_stdout_verbose, is_stdout_output, is_stdout_capture = (False, None, False)
68+
file_path = None
69+
70+
if generated_input["capture_output"]:
71+
is_stdout_capture = True
72+
elif generated_input["output"]:
73+
is_stdout_output = True
74+
file_path = "out.txt"
75+
elif generated_input["verbose"]:
76+
is_stdout_verbose = True
6277

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
68-
assert not captured.err
69-
assert proc.stdout == "foo\n"
70-
assert not proc.stderr
71-
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"
78+
proc = subprocess_handler.run_cmd(
79+
generated_input["command"]["input"],
80+
verbose=generated_input["param_verbose"],
81+
capture_output=generated_input["param_capture"],
82+
output_file=file_path,
83+
)
8984
captured = capfd.readouterr()
90-
assert not captured.out
91-
assert not captured.err
9285

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
86+
self.test_verbose(
87+
is_stdout_verbose, input["param_verbose"], captured, input["command"]
88+
)
89+
self.test_capture_output(is_stdout_capture, proc, input["command"]["expected"])
90+
self.test_output_file(is_stdout_output, file_path, input["command"]["expected"])
10491

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

0 commit comments

Comments
 (0)