@@ -57,39 +57,122 @@ 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
60+ class TestRun :
61+ def test_run_returns_command_with_shell_false (self ):
62+ """Test that cmd.run executes a list-based command without shell."""
63+ c = cmd .run (["python" , "-c" , "print('hello')" ])
64+ assert c .return_code == 0
65+ assert "hello" in c .out
8766
88- with warnings .catch_warnings (record = True ) as w :
89- warnings .simplefilter ("always" )
90- c = cmd .run ("python -c \" print('deprecated')\" " )
67+ def test_run_shell_returns_command_with_shell_true (self ):
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+ def test_run_with_env (self ):
74+ """Test that cmd.run passes extra environment variables."""
75+ c = cmd .run (
76+ ["python" , "-c" , "import os; print(os.environ['CZ_TEST_VAR'])" ],
77+ env = {"CZ_TEST_VAR" : "test_value" },
78+ )
9179 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 )
80+ assert "test_value" in c .out
81+
82+ def test_run_with_string_emits_deprecation_warning (self ):
83+ """Test that passing a string to cmd.run() emits a DeprecationWarning."""
84+ import warnings
85+
86+ with warnings .catch_warnings (record = True ) as w :
87+ warnings .simplefilter ("always" )
88+ c = cmd .run ("python -c \" print('deprecated')\" " )
89+ assert c .return_code == 0
90+ assert "deprecated" in c .out
91+ assert len (w ) == 1
92+ assert issubclass (w [0 ].category , DeprecationWarning )
93+ assert "cmd.run()" in str (w [0 ].message )
94+
95+ def test_stdout_captured (self ):
96+ result = cmd .run (["python" , "-c" , "print('hello')" ])
97+ assert "hello" in result .out
98+ assert isinstance (result .stdout , bytes )
99+ assert b"hello" in result .stdout
100+
101+ def test_stderr_captured (self ):
102+ result = cmd .run (
103+ ["python" , "-c" , "import sys; print('err msg', file=sys.stderr)" ]
104+ )
105+ assert "err msg" in result .err
106+ assert isinstance (result .stderr , bytes )
107+ assert b"err msg" in result .stderr
108+
109+ def test_zero_return_code_on_success (self ):
110+ result = cmd .run (["python" , "-c" , "import sys; sys.exit(0)" ])
111+ assert result .return_code == 0
112+
113+ def test_nonzero_return_code_on_failure (self ):
114+ result = cmd .run (["python" , "-c" , "import sys; sys.exit(42)" ])
115+ assert result .return_code == 42
116+
117+ def test_env_passed_to_subprocess (self ):
118+ result = cmd .run (
119+ ["python" , "-c" , "import os; print(os.environ['CZ_TEST_VAR'])" ],
120+ env = {"CZ_TEST_VAR" : "sentinelvalue" },
121+ )
122+ assert "sentinelvalue" in result .out
123+ assert result .return_code == 0
124+
125+ def test_env_merged_with_os_environ (self , monkeypatch ):
126+ monkeypatch .setenv ("CZ_EXISTING_VAR" , "fromenv" )
127+ result = cmd .run (
128+ ["python" , "-c" , "import os; print(os.environ['CZ_EXISTING_VAR'])" ],
129+ env = {"CZ_EXTRA_VAR" : "extra" },
130+ )
131+ assert "fromenv" in result .out
132+
133+ def test_empty_stdout_and_stderr (self ):
134+ result = cmd .run (["python" , "-c" , '"pass"' ])
135+ assert result .out == ""
136+ assert result .err == ""
137+ assert result .stdout == b""
138+ assert result .stderr == b""
139+
140+ def test_no_env_uses_os_environ (self , monkeypatch ):
141+ monkeypatch .setenv ("CZ_NO_ENV_TEST" , "inherited" )
142+ result = cmd .run (
143+ ["python" , "-c" , "import os; print(os.environ['CZ_NO_ENV_TEST'])" ]
144+ )
145+ assert "inherited" in result .out
146+
147+
148+ class TestRunInteractive :
149+ def test_zero_return_code_on_success (self ):
150+ return_code = cmd .run_interactive (["python" , "-c" , "import sys; sys.exit(0)" ])
151+ assert return_code == 0
152+
153+ def test_nonzero_return_code_on_failure (self ):
154+ return_code = cmd .run_interactive (["python" , "-c" , "import sys; sys.exit(3)" ])
155+ assert return_code == 3
156+
157+ def test_env_passed_to_subprocess (self ):
158+ return_code = cmd .run_interactive (
159+ [
160+ "python" ,
161+ "-c" ,
162+ "import os, sys; sys.exit(0 if os.environ['CZ_ITEST_VAR'] == 'val' else 1)" ,
163+ ],
164+ env = {"CZ_ITEST_VAR" : "val" },
165+ )
166+ assert return_code == 0
167+
168+ def test_env_merged_with_os_environ (self , monkeypatch ):
169+ monkeypatch .setenv ("CZ_ITEST_EXISTING" , "yes" )
170+ return_code = cmd .run_interactive (
171+ [
172+ "python" ,
173+ "-c" ,
174+ "import os, sys; sys.exit(0 if os.environ['CZ_ITEST_EXISTING'] == 'yes' else 1)" ,
175+ ],
176+ env = {"CZ_ITEST_EXTRA" : "extra" },
177+ )
178+ assert return_code == 0
0 commit comments