Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 390af24

Browse files
committed
Merge pull request #92 from pre-commit/simplify_repo_cmd_runner
Simplify Repository.cmd_runner
2 parents f9db2b5 + c5cbd47 commit 390af24

4 files changed

Lines changed: 24 additions & 46 deletions

File tree

pre_commit/commands.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -195,11 +195,7 @@ def _run_single_hook(runner, repository, hook_id, args, write, skips=set()):
195195
write(get_hook_message(_hook_msg_start(hook, args.verbose), end_len=6))
196196
sys.stdout.flush()
197197

198-
retcode, stdout, stderr = repository.run_hook(
199-
runner.cmd_runner,
200-
hook_id,
201-
filenames,
202-
)
198+
retcode, stdout, stderr = repository.run_hook(hook_id, filenames)
203199

204200
if retcode != repository.hooks[hook_id]['expected_return_value']:
205201
retcode = 1

pre_commit/prefixed_command_runner.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class PrefixedCommandRunner(object):
4040
For instance:
4141
PrefixedCommandRunner('/tmp/foo').run(['{prefix}foo.sh', 'bar', 'baz'])
4242
43-
will run ['/tmpl/foo/foo.sh', 'bar', 'baz']
43+
will run ['/tmp/foo/foo.sh', 'bar', 'baz']
4444
"""
4545
def __init__(self, prefix_dir, popen=subprocess.Popen, makedirs=os.makedirs):
4646
self.prefix_dir = prefix_dir.rstrip(os.sep) + os.sep

pre_commit/repository.py

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -43,47 +43,38 @@ def hooks(self):
4343
def manifest(self):
4444
return Manifest(self.repo_path_getter)
4545

46-
def get_cmd_runner(self, hooks_cmd_runner):
47-
# TODO: this effectively throws away the original cmd runner
48-
return PrefixedCommandRunner.from_command_runner(
49-
hooks_cmd_runner, self.repo_path_getter.repo_path,
50-
)
46+
@cached_property
47+
def cmd_runner(self):
48+
return PrefixedCommandRunner(self.repo_path_getter.repo_path)
5149

52-
def require_installed(self, cmd_runner):
50+
def require_installed(self):
5351
if self.__installed:
5452
return
5553

56-
self.install(cmd_runner)
54+
self.install()
5755
self.__installed = True
5856

59-
def install(self, cmd_runner):
60-
"""Install the hook repository.
61-
62-
Args:
63-
cmd_runner - A `PrefixedCommandRunner` bound to the hooks workspace
64-
"""
65-
repo_cmd_runner = self.get_cmd_runner(cmd_runner)
57+
def install(self):
58+
"""Install the hook repository."""
6659
for language_name in self.languages:
6760
language = languages[language_name]
6861
if (
6962
language.ENVIRONMENT_DIR is None or
70-
repo_cmd_runner.exists(language.ENVIRONMENT_DIR)
63+
self.cmd_runner.exists(language.ENVIRONMENT_DIR)
7164
):
7265
# The language is already installed
7366
continue
74-
language.install_environment(repo_cmd_runner)
67+
language.install_environment(self.cmd_runner)
7568

76-
def run_hook(self, cmd_runner, hook_id, file_args):
69+
def run_hook(self, hook_id, file_args):
7770
"""Run a hook.
7871
7972
Args:
80-
cmd_runner - A `PrefixedCommandRunner` bound to the hooks workspace
8173
hook_id - Id of the hook
8274
file_args - List of files to run
8375
"""
84-
self.require_installed(cmd_runner)
85-
repo_cmd_runner = self.get_cmd_runner(cmd_runner)
76+
self.require_installed()
8677
hook = self.hooks[hook_id]
8778
return languages[hook['language']].run_hook(
88-
repo_cmd_runner, hook, file_args,
79+
self.cmd_runner, hook, file_args,
8980
)

tests/repository_test.py

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,20 @@
55
from pre_commit.clientlib.validate_config import CONFIG_JSON_SCHEMA
66
from pre_commit.clientlib.validate_config import validate_config_extra
77
from pre_commit.jsonschema_extensions import apply_defaults
8-
from pre_commit.prefixed_command_runner import PrefixedCommandRunner
98
from pre_commit.repository import Repository
109

1110

1211
@pytest.mark.integration
1312
def test_install_python_repo_in_env(config_for_python_hooks_repo, store):
1413
repo = Repository.create(config_for_python_hooks_repo, store)
15-
repo.install(PrefixedCommandRunner(store.directory))
14+
repo.install()
1615
assert os.path.exists(os.path.join(store.directory, repo.sha, 'py_env'))
1716

1817

1918
@pytest.mark.integration
2019
def test_run_a_python_hook(config_for_python_hooks_repo, store):
2120
repo = Repository.create(config_for_python_hooks_repo, store)
22-
ret = repo.run_hook(
23-
PrefixedCommandRunner(store.directory), 'foo', ['/dev/null'],
24-
)
21+
ret = repo.run_hook('foo', ['/dev/null'])
2522

2623
assert ret[0] == 0
2724
assert ret[1] == "['/dev/null']\nHello World\n"
@@ -30,9 +27,7 @@ def test_run_a_python_hook(config_for_python_hooks_repo, store):
3027
@pytest.mark.integration
3128
def test_lots_of_files(config_for_python_hooks_repo, store):
3229
repo = Repository.create(config_for_python_hooks_repo, store)
33-
ret = repo.run_hook(
34-
PrefixedCommandRunner(store.directory), 'foo', ['/dev/null'] * 15000,
35-
)
30+
ret = repo.run_hook('foo', ['/dev/null'] * 15000)
3631

3732
assert ret[0] == 0
3833

@@ -41,9 +36,7 @@ def test_lots_of_files(config_for_python_hooks_repo, store):
4136
def test_cwd_of_hook(config_for_prints_cwd_repo, store):
4237
# Note: this doubles as a test for `system` hooks
4338
repo = Repository.create(config_for_prints_cwd_repo, store)
44-
ret = repo.run_hook(
45-
PrefixedCommandRunner(store.directory), 'prints_cwd', [],
46-
)
39+
ret = repo.run_hook('prints_cwd', [])
4740

4841
assert ret[0] == 0
4942
assert ret[1] == repo.repo_url + '\n'
@@ -56,7 +49,7 @@ def test_cwd_of_hook(config_for_prints_cwd_repo, store):
5649
@pytest.mark.integration
5750
def test_run_a_node_hook(config_for_node_hooks_repo, store):
5851
repo = Repository.create(config_for_node_hooks_repo, store)
59-
ret = repo.run_hook(PrefixedCommandRunner(store.directory), 'foo', [])
52+
ret = repo.run_hook('foo', [])
6053

6154
assert ret[0] == 0
6255
assert ret[1] == 'Hello World\n'
@@ -65,9 +58,7 @@ def test_run_a_node_hook(config_for_node_hooks_repo, store):
6558
@pytest.mark.integration
6659
def test_run_a_script_hook(config_for_script_hooks_repo, store):
6760
repo = Repository.create(config_for_script_hooks_repo, store)
68-
ret = repo.run_hook(
69-
PrefixedCommandRunner(store.directory), 'bash_hook', ['bar'],
70-
)
61+
ret = repo.run_hook('bash_hook', ['bar'])
7162

7263
assert ret[0] == 0
7364
assert ret[1] == 'bar\nHello World\n'
@@ -106,14 +97,14 @@ def test_languages(config_for_python_hooks_repo, store):
10697

10798
def test_reinstall(config_for_python_hooks_repo, store):
10899
repo = Repository.create(config_for_python_hooks_repo, store)
109-
repo.require_installed(PrefixedCommandRunner(store.directory))
100+
repo.require_installed()
110101
# Reinstall with same repo should not trigger another install
111102
# TODO: how to assert this?
112-
repo.require_installed(PrefixedCommandRunner(store.directory))
103+
repo.require_installed()
113104
# Reinstall on another run should not trigger another install
114105
# TODO: how to assert this?
115106
repo = Repository.create(config_for_python_hooks_repo, store)
116-
repo.require_installed(PrefixedCommandRunner(store.directory))
107+
repo.require_installed()
117108

118109

119110
@pytest.mark.integration
@@ -122,4 +113,4 @@ def test_really_long_file_paths(config_for_python_hooks_repo, store):
122113
local['git']['init', path]()
123114
with local.cwd(path):
124115
repo = Repository.create(config_for_python_hooks_repo, store)
125-
repo.require_installed(PrefixedCommandRunner(store.directory))
116+
repo.require_installed()

0 commit comments

Comments
 (0)