Added path function to PrefixedCommandRunner

This commit is contained in:
Anthony Sottile 2014-03-29 15:47:29 -07:00
parent bd6e62e28d
commit 6f0d566199
4 changed files with 44 additions and 12 deletions

View file

@ -33,10 +33,13 @@ class PrefixedCommandRunner(object):
return proc.returncode, stdout, stderr 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 @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 """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(command_runner.path(path_end), popen=command_runner.__popen)
return cls(new_prefix, popen=command_runner.__popen)

View file

@ -5,6 +5,7 @@ import os.path
import pre_commit.constants as C import pre_commit.constants as C
from pre_commit import git from pre_commit import git
from pre_commit.clientlib.validate_config import load_config 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.repository import Repository
from pre_commit.util import cached_property from pre_commit.util import cached_property
@ -44,3 +45,7 @@ class Runner(object):
@cached_property @cached_property
def pre_commit_path(self): def pre_commit_path(self):
return os.path.join(self.git_root, '.git/hooks/pre-commit') return os.path.join(self.git_root, '.git/hooks/pre-commit')
@cached_property
def workspace_runner(self):
return PrefixedCommandRunner(C.HOOKS_WORKSPACE)

View file

@ -1,4 +1,5 @@
import os
import mock import mock
import pytest import pytest
import subprocess import subprocess
@ -55,14 +56,31 @@ def test_run_substitutes_prefix(popen_mock):
) )
@pytest.mark.parametrize(('first_prefix', 'postfix', 'expected_output'), ( PATH_TESTS = (
('foo', '', 'foo/'), ('foo', '', 'foo'),
('foo', 'bar', 'foo/bar/'), ('foo', 'bar', 'foo/bar'),
('./', 'bar', './bar/'), ('foo/bar', '../baz', 'foo/baz'),
)) ('./', 'bar', 'bar'),
def test_from_command_runner(first_prefix, postfix, expected_output): ('./', '', '.'),
first = PrefixedCommandRunner(first_prefix) )
second = PrefixedCommandRunner.from_command_runner(first, postfix)
@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 assert second.prefix_dir == expected_output

View file

@ -59,3 +59,9 @@ def test_pre_commit_path():
runner = Runner('foo/bar') runner = Runner('foo/bar')
expected_path = os.path.join('foo/bar', '.git/hooks/pre-commit') expected_path = os.path.join('foo/bar', '.git/hooks/pre-commit')
assert runner.pre_commit_path == expected_path 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 + '/'