diff --git a/pre_commit/prefixed_command_runner.py b/pre_commit/prefixed_command_runner.py index d079d7ea..0b3012c1 100644 --- a/pre_commit/prefixed_command_runner.py +++ b/pre_commit/prefixed_command_runner.py @@ -33,10 +33,13 @@ class PrefixedCommandRunner(object): return proc.returncode, stdout, stderr + def path(self, path_end): + path = os.path.join(self.prefix_dir, path_end) + return os.path.normpath(path) + @classmethod - def from_command_runner(cls, command_runner, prefix_postfix): + def from_command_runner(cls, command_runner, path_end): """Constructs a new command runner from an existing one by appending - `prefix_postfix` to the command runner's prefix directory. + `path_end` 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) + return cls(command_runner.path(path_end), popen=command_runner.__popen) diff --git a/pre_commit/runner.py b/pre_commit/runner.py index 0c17cd76..b7263356 100644 --- a/pre_commit/runner.py +++ b/pre_commit/runner.py @@ -5,6 +5,7 @@ import os.path import pre_commit.constants as C from pre_commit import git from pre_commit.clientlib.validate_config import load_config +from pre_commit.prefixed_command_runner import PrefixedCommandRunner from pre_commit.repository import Repository from pre_commit.util import cached_property @@ -44,3 +45,7 @@ class Runner(object): @cached_property def pre_commit_path(self): return os.path.join(self.git_root, '.git/hooks/pre-commit') + + @cached_property + def workspace_runner(self): + return PrefixedCommandRunner(C.HOOKS_WORKSPACE) diff --git a/tests/prefixed_command_runner_test.py b/tests/prefixed_command_runner_test.py index 33100c69..630d8a4a 100644 --- a/tests/prefixed_command_runner_test.py +++ b/tests/prefixed_command_runner_test.py @@ -1,4 +1,5 @@ +import os import mock import pytest import subprocess @@ -55,14 +56,31 @@ def test_run_substitutes_prefix(popen_mock): ) -@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) +PATH_TESTS = ( + ('foo', '', 'foo'), + ('foo', 'bar', 'foo/bar'), + ('foo/bar', '../baz', 'foo/baz'), + ('./', 'bar', 'bar'), + ('./', '', '.'), +) + + +@pytest.mark.parametrize(('prefix', 'path_end', 'expected_output'), PATH_TESTS) +def test_path(prefix, path_end, expected_output): + instance = PrefixedCommandRunner(prefix) + ret = instance.path(path_end) + assert ret == expected_output + + +@pytest.mark.parametrize(('prefix', 'path_end', 'expected_output'), + tuple( + (prefix, path_end, expected_output + os.sep) + for prefix, path_end, expected_output in PATH_TESTS + ), +) +def test_from_command_runner(prefix, path_end, expected_output): + first = PrefixedCommandRunner(prefix) + second = PrefixedCommandRunner.from_command_runner(first, path_end) assert second.prefix_dir == expected_output diff --git a/tests/runner_test.py b/tests/runner_test.py index 167b7bb6..beac918e 100644 --- a/tests/runner_test.py +++ b/tests/runner_test.py @@ -59,3 +59,9 @@ def test_pre_commit_path(): runner = Runner('foo/bar') expected_path = os.path.join('foo/bar', '.git/hooks/pre-commit') assert runner.pre_commit_path == expected_path + + +def test_workspace_runneR(): + runner = Runner('foo/bar') + ret = runner.workspace_runner + assert ret.prefix_dir == C.HOOKS_WORKSPACE + '/'