|
@@ -1,3 +1,5 @@
|
|
|
+import subprocess
|
|
|
+
|
|
|
import pytest
|
|
|
from flexmock import flexmock
|
|
|
|
|
@@ -24,17 +26,34 @@ def test_exit_code_indicates_error_respects_exit_code_and_error_on_warnings(
|
|
|
)
|
|
|
|
|
|
|
|
|
-def output_buffer_for_process_returns_stderr_when_stdout_excluded():
|
|
|
+def test_command_for_process_converts_sequence_command_to_string():
|
|
|
+ process = flexmock(args=['foo', 'bar', 'baz'])
|
|
|
+
|
|
|
+ assert module.command_for_process(process) == 'foo bar baz'
|
|
|
+
|
|
|
+
|
|
|
+def test_command_for_process_passes_through_string_command():
|
|
|
+ process = flexmock(args='foo bar baz')
|
|
|
+
|
|
|
+ assert module.command_for_process(process) == 'foo bar baz'
|
|
|
+
|
|
|
+
|
|
|
+def test_output_buffer_for_process_returns_stderr_when_stdout_excluded():
|
|
|
stdout = flexmock()
|
|
|
- process = flexmock(stdout=stdout)
|
|
|
+ stderr = flexmock()
|
|
|
+ process = flexmock(stdout=stdout, stderr=stderr)
|
|
|
|
|
|
- module.output_buffer_for_process(process, excluded_stdouts=[flexmock(), stdout])
|
|
|
+ assert module.output_buffer_for_process(process, exclude_stdouts=[flexmock(), stdout]) == stderr
|
|
|
|
|
|
|
|
|
-def output_buffer_for_process_returns_stdout_when_not_excluded():
|
|
|
- process = flexmock(stdout=flexmock())
|
|
|
+def test_output_buffer_for_process_returns_stdout_when_not_excluded():
|
|
|
+ stdout = flexmock()
|
|
|
+ process = flexmock(stdout=stdout)
|
|
|
|
|
|
- module.output_buffer_for_process(process, excluded_stdouts=[flexmock(), flexmock()])
|
|
|
+ assert (
|
|
|
+ module.output_buffer_for_process(process, exclude_stdouts=[flexmock(), flexmock()])
|
|
|
+ == stdout
|
|
|
+ )
|
|
|
|
|
|
|
|
|
def test_execute_command_calls_full_command():
|
|
@@ -76,6 +95,20 @@ def test_execute_command_calls_full_command_with_output_file():
|
|
|
assert output is None
|
|
|
|
|
|
|
|
|
+def test_execute_command_calls_full_command_without_capturing_output():
|
|
|
+ full_command = ['foo', 'bar']
|
|
|
+ flexmock(module.os, environ={'a': 'b'})
|
|
|
+ flexmock(module.subprocess).should_receive('Popen').with_args(
|
|
|
+ full_command, stdin=None, stdout=None, stderr=None, shell=False, env=None, cwd=None
|
|
|
+ ).and_return(flexmock(wait=lambda: 0)).once()
|
|
|
+ flexmock(module).should_receive('exit_code_indicates_error').and_return(False)
|
|
|
+ flexmock(module).should_receive('log_outputs')
|
|
|
+
|
|
|
+ output = module.execute_command(full_command, output_file=module.DO_NOT_CAPTURE)
|
|
|
+
|
|
|
+ assert output is None
|
|
|
+
|
|
|
+
|
|
|
def test_execute_command_calls_full_command_with_input_file():
|
|
|
full_command = ['foo', 'bar']
|
|
|
input_file = flexmock(name='test')
|
|
@@ -189,7 +222,7 @@ def test_execute_command_captures_output_with_shell():
|
|
|
expected_output = '[]'
|
|
|
flexmock(module.os, environ={'a': 'b'})
|
|
|
flexmock(module.subprocess).should_receive('check_output').with_args(
|
|
|
- full_command, shell=True, env=None, cwd=None
|
|
|
+ 'foo bar', shell=True, env=None, cwd=None
|
|
|
).and_return(flexmock(decode=lambda: expected_output)).once()
|
|
|
|
|
|
output = module.execute_command(full_command, output_log_level=None, shell=True)
|
|
@@ -225,3 +258,168 @@ def test_execute_command_captures_output_with_working_directory():
|
|
|
)
|
|
|
|
|
|
assert output == expected_output
|
|
|
+
|
|
|
+
|
|
|
+def test_execute_command_with_processes_calls_full_command():
|
|
|
+ full_command = ['foo', 'bar']
|
|
|
+ processes = (flexmock(),)
|
|
|
+ flexmock(module.os, environ={'a': 'b'})
|
|
|
+ flexmock(module.subprocess).should_receive('Popen').with_args(
|
|
|
+ full_command,
|
|
|
+ stdin=None,
|
|
|
+ stdout=module.subprocess.PIPE,
|
|
|
+ stderr=module.subprocess.STDOUT,
|
|
|
+ shell=False,
|
|
|
+ env=None,
|
|
|
+ cwd=None,
|
|
|
+ ).and_return(flexmock(stdout=None)).once()
|
|
|
+ flexmock(module).should_receive('log_outputs')
|
|
|
+
|
|
|
+ output = module.execute_command_with_processes(full_command, processes)
|
|
|
+
|
|
|
+ assert output is None
|
|
|
+
|
|
|
+
|
|
|
+def test_execute_command_with_processes_calls_full_command_with_output_file():
|
|
|
+ full_command = ['foo', 'bar']
|
|
|
+ processes = (flexmock(),)
|
|
|
+ output_file = flexmock(name='test')
|
|
|
+ flexmock(module.os, environ={'a': 'b'})
|
|
|
+ flexmock(module.subprocess).should_receive('Popen').with_args(
|
|
|
+ full_command,
|
|
|
+ stdin=None,
|
|
|
+ stdout=output_file,
|
|
|
+ stderr=module.subprocess.PIPE,
|
|
|
+ shell=False,
|
|
|
+ env=None,
|
|
|
+ cwd=None,
|
|
|
+ ).and_return(flexmock(stderr=None)).once()
|
|
|
+ flexmock(module).should_receive('log_outputs')
|
|
|
+
|
|
|
+ output = module.execute_command_with_processes(full_command, processes, output_file=output_file)
|
|
|
+
|
|
|
+ assert output is None
|
|
|
+
|
|
|
+
|
|
|
+def test_execute_command_with_processes_calls_full_command_without_capturing_output():
|
|
|
+ full_command = ['foo', 'bar']
|
|
|
+ processes = (flexmock(),)
|
|
|
+ flexmock(module.os, environ={'a': 'b'})
|
|
|
+ flexmock(module.subprocess).should_receive('Popen').with_args(
|
|
|
+ full_command, stdin=None, stdout=None, stderr=None, shell=False, env=None, cwd=None
|
|
|
+ ).and_return(flexmock(wait=lambda: 0)).once()
|
|
|
+ flexmock(module).should_receive('exit_code_indicates_error').and_return(False)
|
|
|
+ flexmock(module).should_receive('log_outputs')
|
|
|
+
|
|
|
+ output = module.execute_command_with_processes(
|
|
|
+ full_command, processes, output_file=module.DO_NOT_CAPTURE
|
|
|
+ )
|
|
|
+
|
|
|
+ assert output is None
|
|
|
+
|
|
|
+
|
|
|
+def test_execute_command_with_processes_calls_full_command_with_input_file():
|
|
|
+ full_command = ['foo', 'bar']
|
|
|
+ processes = (flexmock(),)
|
|
|
+ input_file = flexmock(name='test')
|
|
|
+ flexmock(module.os, environ={'a': 'b'})
|
|
|
+ flexmock(module.subprocess).should_receive('Popen').with_args(
|
|
|
+ full_command,
|
|
|
+ stdin=input_file,
|
|
|
+ stdout=module.subprocess.PIPE,
|
|
|
+ stderr=module.subprocess.STDOUT,
|
|
|
+ shell=False,
|
|
|
+ env=None,
|
|
|
+ cwd=None,
|
|
|
+ ).and_return(flexmock(stdout=None)).once()
|
|
|
+ flexmock(module).should_receive('log_outputs')
|
|
|
+
|
|
|
+ output = module.execute_command_with_processes(full_command, processes, input_file=input_file)
|
|
|
+
|
|
|
+ assert output is None
|
|
|
+
|
|
|
+
|
|
|
+def test_execute_command_with_processes_calls_full_command_with_shell():
|
|
|
+ full_command = ['foo', 'bar']
|
|
|
+ processes = (flexmock(),)
|
|
|
+ flexmock(module.os, environ={'a': 'b'})
|
|
|
+ flexmock(module.subprocess).should_receive('Popen').with_args(
|
|
|
+ ' '.join(full_command),
|
|
|
+ stdin=None,
|
|
|
+ stdout=module.subprocess.PIPE,
|
|
|
+ stderr=module.subprocess.STDOUT,
|
|
|
+ shell=True,
|
|
|
+ env=None,
|
|
|
+ cwd=None,
|
|
|
+ ).and_return(flexmock(stdout=None)).once()
|
|
|
+ flexmock(module).should_receive('log_outputs')
|
|
|
+
|
|
|
+ output = module.execute_command_with_processes(full_command, processes, shell=True)
|
|
|
+
|
|
|
+ assert output is None
|
|
|
+
|
|
|
+
|
|
|
+def test_execute_command_with_processes_calls_full_command_with_extra_environment():
|
|
|
+ full_command = ['foo', 'bar']
|
|
|
+ processes = (flexmock(),)
|
|
|
+ flexmock(module.os, environ={'a': 'b'})
|
|
|
+ flexmock(module.subprocess).should_receive('Popen').with_args(
|
|
|
+ full_command,
|
|
|
+ stdin=None,
|
|
|
+ stdout=module.subprocess.PIPE,
|
|
|
+ stderr=module.subprocess.STDOUT,
|
|
|
+ shell=False,
|
|
|
+ env={'a': 'b', 'c': 'd'},
|
|
|
+ cwd=None,
|
|
|
+ ).and_return(flexmock(stdout=None)).once()
|
|
|
+ flexmock(module).should_receive('log_outputs')
|
|
|
+
|
|
|
+ output = module.execute_command_with_processes(
|
|
|
+ full_command, processes, extra_environment={'c': 'd'}
|
|
|
+ )
|
|
|
+
|
|
|
+ assert output is None
|
|
|
+
|
|
|
+
|
|
|
+def test_execute_command_with_processes_calls_full_command_with_working_directory():
|
|
|
+ full_command = ['foo', 'bar']
|
|
|
+ processes = (flexmock(),)
|
|
|
+ flexmock(module.os, environ={'a': 'b'})
|
|
|
+ flexmock(module.subprocess).should_receive('Popen').with_args(
|
|
|
+ full_command,
|
|
|
+ stdin=None,
|
|
|
+ stdout=module.subprocess.PIPE,
|
|
|
+ stderr=module.subprocess.STDOUT,
|
|
|
+ shell=False,
|
|
|
+ env=None,
|
|
|
+ cwd='/working',
|
|
|
+ ).and_return(flexmock(stdout=None)).once()
|
|
|
+ flexmock(module).should_receive('log_outputs')
|
|
|
+
|
|
|
+ output = module.execute_command_with_processes(
|
|
|
+ full_command, processes, working_directory='/working'
|
|
|
+ )
|
|
|
+
|
|
|
+ assert output is None
|
|
|
+
|
|
|
+
|
|
|
+def test_execute_command_with_processes_kills_processes_on_error():
|
|
|
+ full_command = ['foo', 'bar']
|
|
|
+ process = flexmock(stdout=flexmock(read=lambda count: None))
|
|
|
+ process.should_receive('poll')
|
|
|
+ process.should_receive('kill').once()
|
|
|
+ processes = (process,)
|
|
|
+ flexmock(module.os, environ={'a': 'b'})
|
|
|
+ flexmock(module.subprocess).should_receive('Popen').with_args(
|
|
|
+ full_command,
|
|
|
+ stdin=None,
|
|
|
+ stdout=module.subprocess.PIPE,
|
|
|
+ stderr=module.subprocess.STDOUT,
|
|
|
+ shell=False,
|
|
|
+ env=None,
|
|
|
+ cwd=None,
|
|
|
+ ).and_raise(subprocess.CalledProcessError(1, full_command, 'error')).once()
|
|
|
+ flexmock(module).should_receive('log_outputs').never()
|
|
|
+
|
|
|
+ with pytest.raises(subprocess.CalledProcessError):
|
|
|
+ module.execute_command_with_processes(full_command, processes)
|