Skip to content

Commit

Permalink
fix bug
Browse files Browse the repository at this point in the history
  • Loading branch information
YSevenK committed Aug 6, 2024
1 parent 6191d98 commit f897245
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions test/common/test_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,63 @@ def test_run_exception(self, mock_popen):
self.stdio.verbose.assert_called_with("[local host] run cmd = [echo \"hello\"] on localhost")
self.stdio.error.assert_called_with("run cmd = [echo \"hello\"] on localhost")
self.assertIsNone(result)


@patch('subprocess.Popen')
def test_run_get_stderr_success(self, mock_popen):
# 模拟命令成功执行
mock_process = Mock()
mock_process.communicate.return_value = (b'success', b'')
mock_popen.return_value = mock_process

cmd = 'echo "hello"'
result = self.local_client.run_get_stderr(cmd)

# 验证 verbose 和 Popen 调用
self.stdio.verbose.assert_called_with("run cmd = [echo \"hello\"] on localhost")
mock_popen.assert_called_with(cmd, stderr=subprocess.STDOUT, stdout=subprocess.PIPE, shell=True, executable='/bin/bash')

# 验证结果
self.assertEqual(result, b'')


@patch('subprocess.Popen')
def test_run_get_stderr_failure(self, mock_popen):
# 模拟命令执行失败
mock_process = Mock()
mock_process.communicate.return_value = (b'', b'error')
mock_popen.return_value = mock_process

cmd = 'echo "hello"'
result = self.local_client.run_get_stderr(cmd)

# 验证 verbose 和 Popen 调用
self.stdio.verbose.assert_called_with("run cmd = [echo \"hello\"] on localhost")
mock_popen.assert_called_with(cmd, stderr=subprocess.STDOUT, stdout=subprocess.PIPE, shell=True, executable='/bin/bash')

# 验证错误处理
# 因为 stdout 和 stderr 都是 b'',stderr 应该是 b'error'
self.assertEqual(result, b'error')

# 检查 error 方法是否被调用,且调用内容是否正确
# 注意:在正常情况下 error 方法不应该被调用,只有异常情况才会被调用。
# 确保 error 方法在异常情况下被调用
self.stdio.error.assert_not_called()


@patch('subprocess.Popen')
def test_run_get_stderr_exception(self, mock_popen):
# 模拟命令执行时抛出异常
mock_popen.side_effect = Exception('Test exception')

cmd = 'echo "hello"'
result = self.local_client.run_get_stderr(cmd)

# 验证 verbose 调用和异常处理
self.stdio.verbose.assert_called_with("run cmd = [echo \"hello\"] on localhost")
self.stdio.error.assert_called_with(f"run cmd = [{cmd}] on localhost")
self.assertIsNone(result)


if __name__ == '__main__':
unittest.main()

0 comments on commit f897245

Please sign in to comment.