mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-02-18 16:44:42 +04:00
Resolves cwd problem
This commit is contained in:
parent
6f0d566199
commit
216b5c6ab1
13 changed files with 212 additions and 107 deletions
|
|
@ -3,6 +3,7 @@ import os
|
|||
import mock
|
||||
import pytest
|
||||
import subprocess
|
||||
from plumbum import local
|
||||
|
||||
from pre_commit.prefixed_command_runner import _replace_cmd
|
||||
from pre_commit.prefixed_command_runner import PrefixedCommandRunner
|
||||
|
|
@ -10,11 +11,16 @@ from pre_commit.prefixed_command_runner import PrefixedCommandRunner
|
|||
|
||||
@pytest.fixture
|
||||
def popen_mock():
|
||||
popen = mock.Mock()
|
||||
popen = mock.Mock(spec=subprocess.Popen)
|
||||
popen.return_value.communicate.return_value = (mock.Mock(), mock.Mock())
|
||||
return popen
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def makedirs_mock():
|
||||
return mock.Mock(spec=os.makedirs)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(('input', 'kwargs', 'expected_output'), (
|
||||
([], {}, []),
|
||||
(['foo'], {}, ['foo']),
|
||||
|
|
@ -40,9 +46,9 @@ def test_init_normalizes_path_endings(input, expected_prefix):
|
|||
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'])
|
||||
def test_run_substitutes_prefix(popen_mock, makedirs_mock):
|
||||
instance = PrefixedCommandRunner('prefix', popen=popen_mock, makedirs=makedirs_mock)
|
||||
ret = instance.run(['{prefix}bar', 'baz'], retcode=None)
|
||||
popen_mock.assert_called_once_with(
|
||||
['prefix/bar', 'baz'],
|
||||
stdin=subprocess.PIPE,
|
||||
|
|
@ -72,6 +78,12 @@ def test_path(prefix, path_end, expected_output):
|
|||
assert ret == expected_output
|
||||
|
||||
|
||||
def test_path_multiple_args():
|
||||
instance = PrefixedCommandRunner('foo')
|
||||
ret = instance.path('bar', 'baz')
|
||||
assert ret == 'foo/bar/baz'
|
||||
|
||||
|
||||
@pytest.mark.parametrize(('prefix', 'path_end', 'expected_output'),
|
||||
tuple(
|
||||
(prefix, path_end, expected_output + os.sep)
|
||||
|
|
@ -84,13 +96,41 @@ def test_from_command_runner(prefix, path_end, expected_output):
|
|||
assert second.prefix_dir == expected_output
|
||||
|
||||
|
||||
def test_from_command_runner_preserves_popen(popen_mock):
|
||||
first = PrefixedCommandRunner('foo', popen=popen_mock)
|
||||
def test_from_command_runner_preserves_popen(popen_mock, makedirs_mock):
|
||||
first = PrefixedCommandRunner('foo', popen=popen_mock, makedirs=makedirs_mock)
|
||||
second = PrefixedCommandRunner.from_command_runner(first, 'bar')
|
||||
second.run(['foo/bar/baz'])
|
||||
second.run(['foo/bar/baz'], retcode=None)
|
||||
popen_mock.assert_called_once_with(
|
||||
['foo/bar/baz'],
|
||||
stdin=subprocess.PIPE,
|
||||
stdout=subprocess.PIPE,
|
||||
stderr=subprocess.PIPE,
|
||||
)
|
||||
|
||||
|
||||
def test_create_path_if_not_exists(tmpdir):
|
||||
with local.cwd(tmpdir.strpath):
|
||||
instance = PrefixedCommandRunner('foo')
|
||||
assert not os.path.exists('foo')
|
||||
instance._create_path_if_not_exists()
|
||||
assert os.path.exists('foo')
|
||||
|
||||
|
||||
def test_exists_does_not_exist(tmpdir):
|
||||
with local.cwd(tmpdir.strpath):
|
||||
assert not PrefixedCommandRunner('.').exists('foo')
|
||||
|
||||
|
||||
def test_exists_does_exist(tmpdir):
|
||||
with local.cwd(tmpdir.strpath):
|
||||
os.mkdir('foo')
|
||||
assert PrefixedCommandRunner('.').exists('foo')
|
||||
|
||||
|
||||
def test_raises_on_error(popen_mock, makedirs_mock):
|
||||
popen_mock.return_value.returncode = 1
|
||||
with pytest.raises(subprocess.CalledProcessError):
|
||||
instance = PrefixedCommandRunner(
|
||||
'.', popen=popen_mock, makedirs=makedirs_mock,
|
||||
)
|
||||
instance.run(['foo'])
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ import pre_commit.constants as C
|
|||
from pre_commit import git
|
||||
from pre_commit.clientlib.validate_config import CONFIG_JSON_SCHEMA
|
||||
from pre_commit.clientlib.validate_config import validate_config_extra
|
||||
from pre_commit.prefixed_command_runner import PrefixedCommandRunner
|
||||
from pre_commit.repository import Repository
|
||||
|
||||
|
||||
|
|
@ -31,7 +32,7 @@ def test_create_repo_in_env(dummy_repo_config, dummy_git_repo):
|
|||
@pytest.mark.integration
|
||||
def test_install_python_repo_in_env(config_for_python_hooks_repo):
|
||||
repo = Repository(config_for_python_hooks_repo)
|
||||
repo.install()
|
||||
repo.install(PrefixedCommandRunner(C.HOOKS_WORKSPACE))
|
||||
|
||||
assert os.path.exists(
|
||||
os.path.join(
|
||||
|
|
@ -43,11 +44,13 @@ def test_install_python_repo_in_env(config_for_python_hooks_repo):
|
|||
)
|
||||
|
||||
|
||||
@pytest.mark.herpderp
|
||||
@pytest.mark.integration
|
||||
def test_run_a_python_hook(config_for_python_hooks_repo):
|
||||
repo = Repository(config_for_python_hooks_repo)
|
||||
repo.install()
|
||||
ret = repo.run_hook('foo', ['/dev/null'])
|
||||
ret = repo.run_hook(
|
||||
PrefixedCommandRunner(C.HOOKS_WORKSPACE), 'foo', ['/dev/null'],
|
||||
)
|
||||
|
||||
assert ret[0] == 0
|
||||
assert ret[1] == "['/dev/null']\nHello World\n"
|
||||
|
|
@ -56,18 +59,19 @@ def test_run_a_python_hook(config_for_python_hooks_repo):
|
|||
@pytest.mark.integration
|
||||
def test_run_a_hook_lots_of_files(config_for_python_hooks_repo):
|
||||
repo = Repository(config_for_python_hooks_repo)
|
||||
repo.install()
|
||||
ret = repo.run_hook('foo', ['/dev/null'] * 15000)
|
||||
ret = repo.run_hook(
|
||||
PrefixedCommandRunner(C.HOOKS_WORKSPACE), 'foo', ['/dev/null'] * 15000,
|
||||
)
|
||||
|
||||
assert ret[0] == 0
|
||||
|
||||
|
||||
@pytest.mark.xfail
|
||||
@pytest.mark.integration
|
||||
def test_cwd_of_hook(config_for_prints_cwd_repo):
|
||||
repo = Repository(config_for_prints_cwd_repo)
|
||||
repo.install()
|
||||
ret = repo.run_hook('prints_cwd', [])
|
||||
ret = repo.run_hook(
|
||||
PrefixedCommandRunner(C.HOOKS_WORKSPACE), 'prints_cwd', [],
|
||||
)
|
||||
|
||||
assert ret[0] == 0
|
||||
assert ret[1] == '{0}\n'.format(repo.repo_url)
|
||||
|
|
@ -80,8 +84,7 @@ def test_cwd_of_hook(config_for_prints_cwd_repo):
|
|||
@pytest.mark.integration
|
||||
def test_run_a_node_hook(config_for_node_hooks_repo):
|
||||
repo = Repository(config_for_node_hooks_repo)
|
||||
repo.install()
|
||||
ret = repo.run_hook('foo', [])
|
||||
ret = repo.run_hook(PrefixedCommandRunner(C.HOOKS_WORKSPACE), 'foo', [])
|
||||
|
||||
assert ret[0] == 0
|
||||
assert ret[1] == 'Hello World\n'
|
||||
|
|
|
|||
|
|
@ -61,7 +61,7 @@ def test_pre_commit_path():
|
|||
assert runner.pre_commit_path == expected_path
|
||||
|
||||
|
||||
def test_workspace_runneR():
|
||||
def test_cmd_runner():
|
||||
runner = Runner('foo/bar')
|
||||
ret = runner.workspace_runner
|
||||
assert ret.prefix_dir == C.HOOKS_WORKSPACE + '/'
|
||||
ret = runner.cmd_runner
|
||||
assert ret.prefix_dir == os.path.join('foo/bar', C.HOOKS_WORKSPACE) + '/'
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue