def test_spawn_failure(command): with mock.patch('subprocess.Popen') as popen: popen.side_effect = OSError('unknown program') with pytest.raises(SystemExit) as exc: print(main(['--'] + command)) assert exc.value.code == 2 popen.assert_called_once_with(command)
def test_forward_status(status, command): process = mock.MagicMock() process.returncode = status process.wait.return_value = process.returncode with mock.patch('subprocess.Popen') as popen: popen.side_effect = [process] assert main(['--'] + command) == status popen.assert_called_once_with(command)
def test_change_working_directory(tempcwd, status, command, workdir): process = mock.MagicMock() process.returncode = status process.wait.return_value = process.returncode with mock.patch('subprocess.Popen') as popen: popen.side_effect = [process] assert main(['-w', workdir, '--'] + command) == status popen.assert_called_once_with(command, cwd=workdir)
def test_redirect_stderr(tempcwd, status, command): process = mock.MagicMock() process.returncode = status process.wait.return_value = process.returncode with mock.patch('subprocess.Popen') as popen: popen.side_effect = [process] assert main(['-e', 'foo.txt', '--'] + command) == status popen.assert_called_once_with(command, stderr=mock.ANY) assert os.path.exists('foo.txt')
def test_implicit_argv(status, command): with mock.patch('sys.argv', ['runwith', '--'] + command): process = mock.MagicMock() process.returncode = status process.wait.return_value = process.returncode with mock.patch('subprocess.Popen') as popen: popen.side_effect = [process] assert main() == status popen.assert_called_once_with(command)
def test_redirect_stdin(tempcwd, status, command): process = mock.MagicMock() process.returncode = status process.wait.return_value = process.returncode with open('foo.txt', 'wb') as stream: stream.write(b'FOO') with mock.patch('subprocess.Popen') as popen: popen.side_effect = [process] assert main(['-i', 'foo.txt', '--'] + command) == status popen.assert_called_once_with(command, stdin=mock.ANY)
def test_respect_timebox(status, command, timebox): process = mock.MagicMock() process.returncode = status process.wait.side_effect = [process.returncode] with mock.patch('subprocess.Popen') as popen: popen.side_effect = [process] assert main(['-t', timebox, '--'] + command) == status popen.assert_called_once_with(command) process.wait.assert_called_once_with() process.send_signal.assert_not_called() process.terminate.assert_not_called()
def test_exceed_timebox_and_grace_time(status, command, timebox): process = mock.MagicMock() process.returncode = status process.wait.return_value = process.returncode thread = mock.MagicMock() thread.is_alive.side_effect = [True, True] thread.join.side_effect = [None, None, None] with mock.patch('threading.Thread') as T: T.side_effect = [thread] with mock.patch('subprocess.Popen') as P: P.side_effect = [process] assert main(['-t', timebox, '-g', '2s', '--'] + command) == status P.assert_called_once_with(command) T.assert_called_once() process.send_signal.assert_called_once_with(SIGKILL) process.terminate.assert_called_once()
def test_run_without_args(): with mock.patch('subprocess.Popen') as popen: with pytest.raises(SystemExit) as exc: print(main([])) assert exc.value.code == 2 popen.assert_not_called()
# -*- coding: utf-8 -*- import sys from runwith import main if __name__ == '__main__': # pragma: no cover sys.exit(main(sys.argv[1:]))