From bd6e62e28d0398a6f91c808e10cd0d8b8e11208d Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Sat, 29 Mar 2014 15:17:01 -0700 Subject: [PATCH] Add PrefixedCommandRunner --- pre_commit/prefixed_command_runner.py | 42 +++++++++++++++ tests/prefixed_command_runner_test.py | 78 +++++++++++++++++++++++++++ 2 files changed, 120 insertions(+) create mode 100644 pre_commit/prefixed_command_runner.py create mode 100644 tests/prefixed_command_runner_test.py diff --git a/pre_commit/prefixed_command_runner.py b/pre_commit/prefixed_command_runner.py new file mode 100644 index 00000000..d079d7ea --- /dev/null +++ b/pre_commit/prefixed_command_runner.py @@ -0,0 +1,42 @@ + +import os +import os.path +import subprocess + + +def _replace_cmd(cmd, **kwargs): + return [part.format(**kwargs) for part in cmd] + + +class PrefixedCommandRunner(object): + """A PrefixedCommandRunner allows you to run subprocess commands with + comand substitution. + + For instance: + PrefixedCommandRunner('/tmp/foo').run(['{prefix}foo.sh', 'bar', 'baz']) + + will run ['/tmpl/foo/foo.sh', 'bar', 'baz'] + """ + def __init__(self, prefix_dir, popen=subprocess.Popen): + self.prefix_dir = prefix_dir.rstrip(os.sep) + os.sep + self.__popen = popen + + def run(self, cmd, stdin=None): + replaced_cmd = _replace_cmd(cmd, prefix=self.prefix_dir) + proc = self.__popen( + replaced_cmd, + stdin=subprocess.PIPE, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + ) + stdout, stderr = proc.communicate(stdin) + + return proc.returncode, stdout, stderr + + @classmethod + def from_command_runner(cls, command_runner, prefix_postfix): + """Constructs a new command runner from an existing one by appending + `prefix_postfix` to the command runner's prefix directory. + """ + new_prefix = os.path.join(command_runner.prefix_dir, prefix_postfix) + return cls(new_prefix, popen=command_runner.__popen) diff --git a/tests/prefixed_command_runner_test.py b/tests/prefixed_command_runner_test.py new file mode 100644 index 00000000..33100c69 --- /dev/null +++ b/tests/prefixed_command_runner_test.py @@ -0,0 +1,78 @@ + +import mock +import pytest +import subprocess + +from pre_commit.prefixed_command_runner import _replace_cmd +from pre_commit.prefixed_command_runner import PrefixedCommandRunner + + +@pytest.fixture +def popen_mock(): + popen = mock.Mock() + popen.return_value.communicate.return_value = (mock.Mock(), mock.Mock()) + return popen + + +@pytest.mark.parametrize(('input', 'kwargs', 'expected_output'), ( + ([], {}, []), + (['foo'], {}, ['foo']), + ([], {'foo': 'bar'}, []), + (['{foo}/baz'], {'foo': 'bar'}, ['bar/baz']), + (['foo'], {'foo': 'bar'}, ['foo']), + (['foo', '{bar}'], {'bar': 'baz'}, ['foo', 'baz']), +)) +def test_replace_cmd(input, kwargs, expected_output): + ret = _replace_cmd(input, **kwargs) + assert ret == expected_output + + +@pytest.mark.parametrize(('input', 'expected_prefix'), ( + ('.', './'), + ('foo', 'foo/'), + ('bar/', 'bar/'), + ('foo/bar', 'foo/bar/'), + ('foo/bar/', 'foo/bar/'), +)) +def test_init_normalizes_path_endings(input, expected_prefix): + instance = PrefixedCommandRunner(input) + assert instance.prefix_dir == expected_prefix + + +def test_run_substitutes_prefix(popen_mock): + instance = PrefixedCommandRunner('prefix', popen=popen_mock) + ret = instance.run(['{prefix}bar', 'baz']) + popen_mock.assert_called_once_with( + ['prefix/bar', 'baz'], + stdin=subprocess.PIPE, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + ) + assert ret == ( + popen_mock.return_value.returncode, + popen_mock.return_value.communicate.return_value[0], + popen_mock.return_value.communicate.return_value[1], + ) + + +@pytest.mark.parametrize(('first_prefix', 'postfix', 'expected_output'), ( + ('foo', '', 'foo/'), + ('foo', 'bar', 'foo/bar/'), + ('./', 'bar', './bar/'), +)) +def test_from_command_runner(first_prefix, postfix, expected_output): + first = PrefixedCommandRunner(first_prefix) + second = PrefixedCommandRunner.from_command_runner(first, postfix) + assert second.prefix_dir == expected_output + + +def test_from_command_runner_preserves_popen(popen_mock): + first = PrefixedCommandRunner('foo', popen=popen_mock) + second = PrefixedCommandRunner.from_command_runner(first, 'bar') + second.run(['foo/bar/baz']) + popen_mock.assert_called_once_with( + ['foo/bar/baz'], + stdin=subprocess.PIPE, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + )