Skip to content

Commit b87c4fd

Browse files
committed
Remove more properties from Runner
1 parent 8f6fe8c commit b87c4fd

File tree

5 files changed

+43
-87
lines changed

5 files changed

+43
-87
lines changed

pre_commit/repository.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ def _prefix_from_deps(self, language_name, deps):
218218
else:
219219
return Prefix(self.store.make_local(deps))
220220

221-
@cached_property
221+
@property
222222
def manifest(self):
223223
raise NotImplementedError
224224

pre_commit/runner.py

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,7 @@ def create(cls, config_file):
2727
os.chdir(root)
2828
return cls(root, config_file)
2929

30-
@cached_property
31-
def git_dir(self):
32-
return git.get_git_dir(self.git_root)
33-
34-
@cached_property
30+
@property
3531
def config_file_path(self):
3632
return os.path.join(self.git_root, self.config_file)
3733

@@ -40,12 +36,4 @@ def config(self):
4036
return load_config(self.config_file_path)
4137

4238
def get_hook_path(self, hook_type):
43-
return os.path.join(self.git_dir, 'hooks', hook_type)
44-
45-
@cached_property
46-
def pre_commit_path(self):
47-
return self.get_hook_path('pre-commit')
48-
49-
@cached_property
50-
def pre_push_path(self):
51-
return self.get_hook_path('pre-push')
39+
return os.path.join(git.get_git_dir(self.git_root), 'hooks', hook_type)

pre_commit/store.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
import sqlite3
88
import tempfile
99

10-
from cached_property import cached_property
11-
1210
import pre_commit.constants as C
1311
from pre_commit import file_lock
1412
from pre_commit.util import clean_path_on_failure
@@ -173,6 +171,6 @@ def _git_cmd(*args):
173171
'local', C.LOCAL_REPO_VERSION, deps, make_local_strategy,
174172
)
175173

176-
@cached_property
174+
@property
177175
def db_path(self):
178176
return os.path.join(self.directory, 'db.db')

tests/commands/install_uninstall_test.py

Lines changed: 39 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -49,21 +49,21 @@ def test_install_pre_commit(tempdir_factory, store):
4949
path = git_dir(tempdir_factory)
5050
runner = Runner(path, C.CONFIG_FILE)
5151
assert not install(runner, store)
52-
assert os.access(runner.pre_commit_path, os.X_OK)
52+
assert os.access(os.path.join(path, '.git/hooks/pre-commit'), os.X_OK)
5353

5454
assert not install(runner, store, hook_type='pre-push')
55-
assert os.access(runner.pre_push_path, os.X_OK)
55+
assert os.access(os.path.join(path, '.git/hooks/pre-push'), os.X_OK)
5656

5757

5858
def test_install_hooks_directory_not_present(tempdir_factory, store):
5959
path = git_dir(tempdir_factory)
6060
# Simulate some git clients which don't make .git/hooks #234
61-
hooks = os.path.join(path, '.git', 'hooks')
61+
hooks = os.path.join(path, '.git/hooks')
6262
if os.path.exists(hooks): # pragma: no cover (latest git)
6363
shutil.rmtree(hooks)
6464
runner = Runner(path, C.CONFIG_FILE)
6565
install(runner, store)
66-
assert os.path.exists(runner.pre_commit_path)
66+
assert os.path.exists(os.path.join(path, '.git/hooks/pre-commit'))
6767

6868

6969
def test_install_refuses_core_hookspath(tempdir_factory, store):
@@ -80,10 +80,10 @@ def test_install_hooks_dead_symlink(
8080
): # pragma: no cover (non-windows)
8181
path = git_dir(tempdir_factory)
8282
runner = Runner(path, C.CONFIG_FILE)
83-
mkdirp(os.path.dirname(runner.pre_commit_path))
84-
os.symlink('/fake/baz', os.path.join(path, '.git', 'hooks', 'pre-commit'))
83+
mkdirp(os.path.join(path, '.git/hooks'))
84+
os.symlink('/fake/baz', os.path.join(path, '.git/hooks/pre-commit'))
8585
install(runner, store)
86-
assert os.path.exists(runner.pre_commit_path)
86+
assert os.path.exists(os.path.join(path, '.git/hooks/pre-commit'))
8787

8888

8989
def test_uninstall_does_not_blow_up_when_not_there(tempdir_factory):
@@ -96,11 +96,11 @@ def test_uninstall_does_not_blow_up_when_not_there(tempdir_factory):
9696
def test_uninstall(tempdir_factory, store):
9797
path = git_dir(tempdir_factory)
9898
runner = Runner(path, C.CONFIG_FILE)
99-
assert not os.path.exists(runner.pre_commit_path)
99+
assert not os.path.exists(os.path.join(path, '.git/hooks/pre-commit'))
100100
install(runner, store)
101-
assert os.path.exists(runner.pre_commit_path)
101+
assert os.path.exists(os.path.join(path, '.git/hooks/pre-commit'))
102102
uninstall(runner)
103-
assert not os.path.exists(runner.pre_commit_path)
103+
assert not os.path.exists(os.path.join(path, '.git/hooks/pre-commit'))
104104

105105

106106
def _get_commit_output(tempdir_factory, touch_file='foo', **kwargs):
@@ -282,16 +282,19 @@ def test_failing_hooks_returns_nonzero(tempdir_factory, store):
282282
)
283283

284284

285+
def _write_legacy_hook(path):
286+
mkdirp(os.path.join(path, '.git/hooks'))
287+
with io.open(os.path.join(path, '.git/hooks/pre-commit'), 'w') as f:
288+
f.write('#!/usr/bin/env bash\necho "legacy hook"\n')
289+
make_executable(f.name)
290+
291+
285292
def test_install_existing_hooks_no_overwrite(tempdir_factory, store):
286293
path = make_consuming_repo(tempdir_factory, 'script_hooks_repo')
287294
with cwd(path):
288295
runner = Runner(path, C.CONFIG_FILE)
289296

290-
# Write out an "old" hook
291-
mkdirp(os.path.dirname(runner.pre_commit_path))
292-
with io.open(runner.pre_commit_path, 'w') as hook_file:
293-
hook_file.write('#!/usr/bin/env bash\necho "legacy hook"\n')
294-
make_executable(runner.pre_commit_path)
297+
_write_legacy_hook(path)
295298

296299
# Make sure we installed the "old" hook correctly
297300
ret, output = _get_commit_output(tempdir_factory, touch_file='baz')
@@ -313,11 +316,7 @@ def test_install_existing_hook_no_overwrite_idempotent(tempdir_factory, store):
313316
with cwd(path):
314317
runner = Runner(path, C.CONFIG_FILE)
315318

316-
# Write out an "old" hook
317-
mkdirp(os.path.dirname(runner.pre_commit_path))
318-
with io.open(runner.pre_commit_path, 'w') as hook_file:
319-
hook_file.write('#!/usr/bin/env bash\necho "legacy hook"\n')
320-
make_executable(runner.pre_commit_path)
319+
_write_legacy_hook(path)
321320

322321
# Install twice
323322
assert install(runner, store) == 0
@@ -343,10 +342,10 @@ def test_failing_existing_hook_returns_1(tempdir_factory, store):
343342
runner = Runner(path, C.CONFIG_FILE)
344343

345344
# Write out a failing "old" hook
346-
mkdirp(os.path.dirname(runner.pre_commit_path))
347-
with io.open(runner.pre_commit_path, 'w') as hook_file:
348-
hook_file.write('#!/usr/bin/env bash\necho "fail!"\nexit 1\n')
349-
make_executable(runner.pre_commit_path)
345+
mkdirp(os.path.join(path, '.git/hooks'))
346+
with io.open(os.path.join(path, '.git/hooks/pre-commit'), 'w') as f:
347+
f.write('#!/usr/bin/env bash\necho "fail!"\nexit 1\n')
348+
make_executable(f.name)
350349

351350
assert install(runner, store) == 0
352351

@@ -372,12 +371,7 @@ def test_install_overwrite(tempdir_factory, store):
372371
with cwd(path):
373372
runner = Runner(path, C.CONFIG_FILE)
374373

375-
# Write out the "old" hook
376-
mkdirp(os.path.dirname(runner.pre_commit_path))
377-
with io.open(runner.pre_commit_path, 'w') as hook_file:
378-
hook_file.write('#!/usr/bin/env bash\necho "legacy hook"\n')
379-
make_executable(runner.pre_commit_path)
380-
374+
_write_legacy_hook(path)
381375
assert install(runner, store, overwrite=True) == 0
382376

383377
ret, output = _get_commit_output(tempdir_factory)
@@ -390,11 +384,7 @@ def test_uninstall_restores_legacy_hooks(tempdir_factory, store):
390384
with cwd(path):
391385
runner = Runner(path, C.CONFIG_FILE)
392386

393-
# Write out an "old" hook
394-
mkdirp(os.path.dirname(runner.pre_commit_path))
395-
with io.open(runner.pre_commit_path, 'w') as hook_file:
396-
hook_file.write('#!/usr/bin/env bash\necho "legacy hook"\n')
397-
make_executable(runner.pre_commit_path)
387+
_write_legacy_hook(path)
398388

399389
# Now install and uninstall pre-commit
400390
assert install(runner, store) == 0
@@ -412,17 +402,15 @@ def test_replace_old_commit_script(tempdir_factory, store):
412402
runner = Runner(path, C.CONFIG_FILE)
413403

414404
# Install a script that looks like our old script
415-
pre_commit_contents = io.open(
416-
resource_filename('hook-tmpl'),
417-
).read()
405+
pre_commit_contents = io.open(resource_filename('hook-tmpl')).read()
418406
new_contents = pre_commit_contents.replace(
419407
CURRENT_HASH, PRIOR_HASHES[-1],
420408
)
421409

422-
mkdirp(os.path.dirname(runner.pre_commit_path))
423-
with io.open(runner.pre_commit_path, 'w') as pre_commit_file:
424-
pre_commit_file.write(new_contents)
425-
make_executable(runner.pre_commit_path)
410+
mkdirp(os.path.join(path, '.git/hooks'))
411+
with io.open(os.path.join(path, '.git/hooks/pre-commit'), 'w') as f:
412+
f.write(new_contents)
413+
make_executable(f.name)
426414

427415
# Install normally
428416
assert install(runner, store) == 0
@@ -436,14 +424,14 @@ def test_uninstall_doesnt_remove_not_our_hooks(tempdir_factory):
436424
path = git_dir(tempdir_factory)
437425
with cwd(path):
438426
runner = Runner(path, C.CONFIG_FILE)
439-
mkdirp(os.path.dirname(runner.pre_commit_path))
440-
with io.open(runner.pre_commit_path, 'w') as pre_commit_file:
441-
pre_commit_file.write('#!/usr/bin/env bash\necho 1\n')
442-
make_executable(runner.pre_commit_path)
427+
mkdirp(os.path.join(path, '.git/hooks'))
428+
with io.open(os.path.join(path, '.git/hooks/pre-commit'), 'w') as f:
429+
f.write('#!/usr/bin/env bash\necho 1\n')
430+
make_executable(f.name)
443431

444432
assert uninstall(runner) == 0
445433

446-
assert os.path.exists(runner.pre_commit_path)
434+
assert os.path.exists(os.path.join(path, '.git/hooks/pre-commit'))
447435

448436

449437
PRE_INSTALLED = re.compile(
@@ -583,17 +571,16 @@ def test_pre_push_legacy(tempdir_factory, store):
583571
with cwd(path):
584572
runner = Runner(path, C.CONFIG_FILE)
585573

586-
hook_path = runner.get_hook_path('pre-push')
587-
mkdirp(os.path.dirname(hook_path))
588-
with io.open(hook_path, 'w') as hook_file:
589-
hook_file.write(
574+
mkdirp(os.path.join(path, '.git/hooks'))
575+
with io.open(os.path.join(path, '.git/hooks/pre-push'), 'w') as f:
576+
f.write(
590577
'#!/usr/bin/env bash\n'
591578
'set -eu\n'
592579
'read lr ls rr rs\n'
593580
'test -n "$lr" -a -n "$ls" -a -n "$rr" -a -n "$rs"\n'
594581
'echo legacy\n',
595582
)
596-
make_executable(hook_path)
583+
make_executable(f.name)
597584

598585
install(runner, store, hook_type='pre-push')
599586
assert _get_commit_output(tempdir_factory)[0] == 0

tests/runner_test.py

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
import pre_commit.constants as C
77
from pre_commit.runner import Runner
8-
from pre_commit.util import cmd_output
98
from testing.fixtures import git_dir
109
from testing.util import cwd
1110

@@ -43,19 +42,3 @@ def test_config_file_path():
4342
runner = Runner(os.path.join('foo', 'bar'), C.CONFIG_FILE)
4443
expected_path = os.path.join('foo', 'bar', C.CONFIG_FILE)
4544
assert runner.config_file_path == expected_path
46-
47-
48-
def test_pre_commit_path(in_tmpdir):
49-
path = os.path.join('foo', 'bar')
50-
cmd_output('git', 'init', path)
51-
runner = Runner(path, C.CONFIG_FILE)
52-
expected_path = os.path.join(path, '.git', 'hooks', 'pre-commit')
53-
assert runner.pre_commit_path == expected_path
54-
55-
56-
def test_pre_push_path(in_tmpdir):
57-
path = os.path.join('foo', 'bar')
58-
cmd_output('git', 'init', path)
59-
runner = Runner(path, C.CONFIG_FILE)
60-
expected_path = os.path.join(path, '.git', 'hooks', 'pre-push')
61-
assert runner.pre_push_path == expected_path

0 commit comments

Comments
 (0)