Skip to content

Commit d483e68

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

File tree

1 file changed

+75
-0
lines changed

1 file changed

+75
-0
lines changed

tests/test_cmd.py

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,78 @@ def decode(self, encoding="utf-8", errors="strict"):
5555

5656
with pytest.raises(CharacterSetDecodeError):
5757
cmd._try_decode(_bytes())
58+
59+
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)