Skip to content

Commit c40d37a

Browse files
committed
test: add tests for cmd.run and cmd.run_interactive
1 parent 0224762 commit c40d37a

1 file changed

Lines changed: 73 additions & 36 deletions

File tree

tests/test_cmd.py

Lines changed: 73 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -57,39 +57,76 @@ 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("python -c \"import sys; print('err msg', file=sys.stderr)\"")
69+
assert "err msg" in result.err
70+
assert isinstance(result.stderr, bytes)
71+
assert b"err msg" in result.stderr
72+
73+
def test_zero_return_code_on_success(self):
74+
result = cmd.run('python -c "import sys; sys.exit(0)"')
75+
assert result.return_code == 0
76+
77+
def test_nonzero_return_code_on_failure(self):
78+
result = cmd.run('python -c "import sys; sys.exit(42)"')
79+
assert result.return_code == 42
80+
81+
def test_env_passed_to_subprocess(self):
82+
result = cmd.run(
83+
"python -c \"import os; print(os.environ['CZ_TEST_VAR'])\"",
84+
env={"CZ_TEST_VAR": "sentinelvalue"},
85+
)
86+
assert "sentinelvalue" in result.out
87+
assert result.return_code == 0
88+
89+
def test_env_merged_with_os_environ(self, monkeypatch):
90+
monkeypatch.setenv("CZ_EXISTING_VAR", "fromenv")
91+
result = cmd.run(
92+
"python -c \"import os; print(os.environ['CZ_EXISTING_VAR'])\"",
93+
env={"CZ_EXTRA_VAR": "extra"},
94+
)
95+
assert "fromenv" in result.out
96+
97+
def test_empty_stdout_and_stderr(self):
98+
result = cmd.run('python -c "pass"')
99+
assert result.out == ""
100+
assert result.err == ""
101+
assert result.stdout == b""
102+
assert result.stderr == b""
103+
104+
def test_no_env_uses_os_environ(self, monkeypatch):
105+
monkeypatch.setenv("CZ_NO_ENV_TEST", "inherited")
106+
result = cmd.run("python -c \"import os; print(os.environ['CZ_NO_ENV_TEST'])\"")
107+
assert "inherited" in result.out
108+
109+
110+
class TestRunInteractive:
111+
def test_zero_return_code_on_success(self):
112+
return_code = cmd.run_interactive('python -c "import sys; sys.exit(0)"')
113+
assert return_code == 0
114+
115+
def test_nonzero_return_code_on_failure(self):
116+
return_code = cmd.run_interactive('python -c "import sys; sys.exit(3)"')
117+
assert return_code == 3
118+
119+
def test_env_passed_to_subprocess(self):
120+
return_code = cmd.run_interactive(
121+
"python -c \"import os, sys; sys.exit(0 if os.environ['CZ_ITEST_VAR'] == 'val' else 1)\"",
122+
env={"CZ_ITEST_VAR": "val"},
123+
)
124+
assert return_code == 0
125+
126+
def test_env_merged_with_os_environ(self, monkeypatch):
127+
monkeypatch.setenv("CZ_ITEST_EXISTING", "yes")
128+
return_code = cmd.run_interactive(
129+
"python -c \"import os, sys; sys.exit(0 if os.environ['CZ_ITEST_EXISTING'] == 'yes' else 1)\"",
130+
env={"CZ_ITEST_EXTRA": "extra"},
131+
)
132+
assert return_code == 0

0 commit comments

Comments
 (0)