Skip to content

Commit 4f04e04

Browse files
committed
test: add tests for cmd.run and cmd.run_interactive
1 parent ad80d51 commit 4f04e04

1 file changed

Lines changed: 85 additions & 36 deletions

File tree

tests/test_cmd.py

Lines changed: 85 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -57,39 +57,88 @@ def decode(self, encoding="utf-8", errors="strict"):
5757
cmd._try_decode(_bytes())
5858

5959

60-
def test_run_returns_command_with_shell_false():
61-
"""Test that cmd.run executes a list-based command without shell."""
62-
c = cmd.run(["python", "-c", "print('hello')"])
63-
assert c.return_code == 0
64-
assert "hello" in c.out
65-
66-
67-
def test_run_shell_returns_command_with_shell_true():
68-
"""Test that cmd.run_shell executes a string command via the shell."""
69-
c = cmd.run_shell("python -c \"print('hello')\"")
70-
assert c.return_code == 0
71-
assert "hello" in c.out
72-
73-
74-
def test_run_with_env():
75-
"""Test that cmd.run passes extra environment variables."""
76-
c = cmd.run(
77-
["python", "-c", "import os; print(os.environ['CZ_TEST_VAR'])"],
78-
env={"CZ_TEST_VAR": "test_value"},
79-
)
80-
assert c.return_code == 0
81-
assert "test_value" in c.out
82-
83-
84-
def test_run_with_string_emits_deprecation_warning():
85-
"""Test that passing a string to cmd.run() emits a DeprecationWarning."""
86-
import warnings
87-
88-
with warnings.catch_warnings(record=True) as w:
89-
warnings.simplefilter("always")
90-
c = cmd.run("python -c \"print('deprecated')\"")
91-
assert c.return_code == 0
92-
assert "deprecated" in c.out
93-
assert len(w) == 1
94-
assert issubclass(w[0].category, DeprecationWarning)
95-
assert "cmd.run()" in str(w[0].message)
60+
class TestRun:
61+
def test_stdout_captured(self):
62+
result = cmd.run(["python", "-c", "\"print('hello')\""])
63+
assert "hello" in result.out
64+
assert isinstance(result.stdout, bytes)
65+
assert b"hello" in result.stdout
66+
67+
def test_stderr_captured(self):
68+
result = cmd.run(
69+
["python", "-c", "\"import sys; print('err msg', file=sys.stderr)\""]
70+
)
71+
assert "err msg" in result.err
72+
assert isinstance(result.stderr, bytes)
73+
assert b"err msg" in result.stderr
74+
75+
def test_zero_return_code_on_success(self):
76+
result = cmd.run(["python", "-c", '"import sys; sys.exit(0)"'])
77+
assert result.return_code == 0
78+
79+
def test_nonzero_return_code_on_failure(self):
80+
result = cmd.run(["python", "-c", '"import sys; sys.exit(42)"'])
81+
assert result.return_code == 42
82+
83+
def test_env_passed_to_subprocess(self):
84+
result = cmd.run(
85+
["python", "-c", "\"import os; print(os.environ['CZ_TEST_VAR'])\""],
86+
env={"CZ_TEST_VAR": "sentinelvalue"},
87+
)
88+
assert "sentinelvalue" in result.out
89+
assert result.return_code == 0
90+
91+
def test_env_merged_with_os_environ(self, monkeypatch):
92+
monkeypatch.setenv("CZ_EXISTING_VAR", "fromenv")
93+
result = cmd.run(
94+
["python", "-c", "\"import os; print(os.environ['CZ_EXISTING_VAR'])\""],
95+
env={"CZ_EXTRA_VAR": "extra"},
96+
)
97+
assert "fromenv" in result.out
98+
99+
def test_empty_stdout_and_stderr(self):
100+
result = cmd.run(["python", "-c", '"pass"'])
101+
assert result.out == ""
102+
assert result.err == ""
103+
assert result.stdout == b""
104+
assert result.stderr == b""
105+
106+
def test_no_env_uses_os_environ(self, monkeypatch):
107+
monkeypatch.setenv("CZ_NO_ENV_TEST", "inherited")
108+
result = cmd.run(
109+
["python", "-c", "\"import os; print(os.environ['CZ_NO_ENV_TEST'])\""]
110+
)
111+
assert "inherited" in result.out
112+
113+
114+
class TestRunInteractive:
115+
def test_zero_return_code_on_success(self):
116+
return_code = cmd.run_interactive(["python", "-c", '"import sys; sys.exit(0)"'])
117+
assert return_code == 0
118+
119+
def test_nonzero_return_code_on_failure(self):
120+
return_code = cmd.run_interactive(["python", "-c", '"import sys; sys.exit(3)"'])
121+
assert return_code == 3
122+
123+
def test_env_passed_to_subprocess(self):
124+
return_code = cmd.run_interactive(
125+
[
126+
"python",
127+
"-c",
128+
"\"import os, sys; sys.exit(0 if os.environ['CZ_ITEST_VAR'] == 'val' else 1)\"",
129+
],
130+
env={"CZ_ITEST_VAR": "val"},
131+
)
132+
assert return_code == 0
133+
134+
def test_env_merged_with_os_environ(self, monkeypatch):
135+
monkeypatch.setenv("CZ_ITEST_EXISTING", "yes")
136+
return_code = cmd.run_interactive(
137+
[
138+
"python",
139+
"-c",
140+
"\"import os, sys; sys.exit(0 if os.environ['CZ_ITEST_EXISTING'] == 'yes' else 1)\"",
141+
],
142+
env={"CZ_ITEST_EXTRA": "extra"},
143+
)
144+
assert return_code == 0

0 commit comments

Comments
 (0)