@@ -15,8 +15,8 @@ class TestStreamOutput(unittest.TestCase):
15
15
# Outputs the process stdout to sys.stdout.buffer.
16
16
@mock .patch ("subprocess.Popen" )
17
17
@mock .patch ("sys.stdout.buffer.write" )
18
- def test_outputs_stdout (self , mock_write : Mock , mock_popen : Mock ) -> None :
19
- process = mock_popen .return_value
18
+ def test_outputs_stdout (self , mock_write : Mock , mock_run : Mock ) -> None :
19
+ process = mock_run .return_value
20
20
process .stdout .read .side_effect = [b"output1" , b"output2" , b"" ]
21
21
22
22
stream_output (process )
@@ -26,8 +26,8 @@ def test_outputs_stdout(self, mock_write: Mock, mock_popen: Mock) -> None:
26
26
# Handles the process stdout stream correctly.
27
27
@mock .patch ("subprocess.Popen" )
28
28
@mock .patch ("sys.stdout.buffer.write" )
29
- def test_handles_stdout_stream (self , mock_write : Mock , mock_popen : Mock ) -> None :
30
- process = mock_popen .return_value
29
+ def test_handles_stdout_stream (self , mock_write : Mock , mock_run : Mock ) -> None :
30
+ process = mock_run .return_value
31
31
process .stdout .read .side_effect = [b"output1" , b"output2" , b"" ]
32
32
33
33
stream_output (process )
@@ -38,20 +38,25 @@ def test_handles_stdout_stream(self, mock_write: Mock, mock_popen: Mock) -> None
38
38
class TestRunCommand (unittest .TestCase ):
39
39
40
40
# Runs command with no errors and returns exit code
41
- @mock .patch ("lib.utils.stream_output" )
42
- @mock .patch ("subprocess.Popen " )
43
- def test_runs_command_no_errors (self , mock_popen : Mock , _ : Mock ) -> None :
41
+ @mock .patch ("builtins.open" , new_callable = mock . mock_open )
42
+ @mock .patch ("subprocess.run " )
43
+ def test_runs_command_no_errors (self , mock_run : Mock , mock_open : Mock ) -> None :
44
44
45
45
# Set up mock
46
- mock_process = mock_popen .return_value
46
+ mock_process = mock_run .return_value
47
47
mock_process .returncode = 0
48
48
49
49
# Call the function under test
50
50
exit_code = run_command (["ls" ])
51
51
52
52
# Assert the exit code is returned correctly
53
53
self .assertEqual (exit_code , 0 )
54
- mock_popen .assert_called_once_with (["ls" ], cwd = None , stdout = subprocess .PIPE )
54
+ # Assert the open call was made correctly
55
+ mock_open .assert_called_with ("logs/error.log" , "w" , encoding = "utf-8" )
56
+ # Assert the subprocess.run call was made correctly
57
+ mock_run .assert_called_once_with (
58
+ ["ls" ], check = True , cwd = None , stdout = mock_open .return_value , stderr = mock_open .return_value
59
+ )
55
60
56
61
57
62
if __name__ == "__main__" :
0 commit comments