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

Skip to content

Commit 3babceb

Browse files
committed
Merge pull request #195 from pre-commit/actually_print_when_installing
Actually print while installing
2 parents 8f51bf2 + 02f0a1c commit 3babceb

6 files changed

Lines changed: 42 additions & 27 deletions

File tree

pre_commit/repository.py

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import unicode_literals
22

3+
import logging
34
import shutil
45

56
from cached_property import cached_property
@@ -9,6 +10,9 @@
910
from pre_commit.prefixed_command_runner import PrefixedCommandRunner
1011

1112

13+
logger = logging.getLogger('pre_commit')
14+
15+
1216
class Repository(object):
1317
def __init__(self, repo_config, repo_path_getter):
1418
self.repo_config = repo_config
@@ -62,14 +66,28 @@ def require_installed(self):
6266

6367
def install(self):
6468
"""Install the hook repository."""
65-
for language_name, language_version in self.languages:
69+
def language_is_installed(language_name):
6670
language = languages[language_name]
67-
if (
71+
return (
6872
language.ENVIRONMENT_DIR is None or
6973
self.cmd_runner.exists(language.ENVIRONMENT_DIR, '.installed')
70-
):
71-
# The language is already installed
74+
)
75+
76+
if not all(
77+
language_is_installed(language_name)
78+
for language_name, _ in self.languages
79+
):
80+
logger.info(
81+
'Installing environment for {0}.'.format(self.repo_url)
82+
)
83+
logger.info('Once installed this environment will be reused.')
84+
logger.info('This may take a few minutes...')
85+
86+
for language_name, language_version in self.languages:
87+
language = languages[language_name]
88+
if language_is_installed(language_name):
7289
continue
90+
7391
# There's potentially incomplete cleanup from previous runs
7492
# Clean it up!
7593
if self.cmd_runner.exists(language.ENVIRONMENT_DIR):

pre_commit/store.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,7 @@ def clone(self, url, sha):
8181
if os.path.exists(sha_path):
8282
return os.readlink(sha_path)
8383

84-
logger.info('Installing environment for {0}.'.format(url))
85-
logger.info('Once installed this environment will be reused.')
86-
logger.info('This may take a few minutes...')
84+
logger.info('Initializing environment for {0}.'.format(url))
8785

8886
dir = tempfile.mkdtemp(prefix='repo', dir=self.directory)
8987
with clean_path_on_failure(dir):

tests/commands/install_uninstall_test.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,7 @@ def _get_commit_output(
128128

129129

130130
NORMAL_PRE_COMMIT_RUN = re.compile(
131-
r'^\[INFO\] Installing environment for .+\.\n'
132-
r'\[INFO\] Once installed this environment will be reused\.\n'
133-
r'\[INFO\] This may take a few minutes\.\.\.\n'
131+
r'^\[INFO\] Initializing environment for .+\.\n'
134132
r'Bash hook\.+Passed\n'
135133
r'\[master [a-f0-9]{7}\] Commit!\n' +
136134
FILES_CHANGED +
@@ -180,9 +178,7 @@ def test_environment_not_sourced(tmpdir_factory):
180178

181179

182180
FAILING_PRE_COMMIT_RUN = re.compile(
183-
r'^\[INFO\] Installing environment for .+\.\n'
184-
r'\[INFO\] Once installed this environment will be reused\.\n'
185-
r'\[INFO\] This may take a few minutes\.\.\.\n'
181+
r'^\[INFO\] Initializing environment for .+\.\n'
186182
r'Failing hook\.+Failed\n'
187183
r'hookid: failing_hook\n'
188184
r'\n'
@@ -258,9 +254,7 @@ def test_install_existing_hook_no_overwrite_idempotent(tmpdir_factory):
258254

259255
FAIL_OLD_HOOK = re.compile(
260256
r'fail!\n'
261-
r'\[INFO\] Installing environment for .+\.\n'
262-
r'\[INFO\] Once installed this environment will be reused\.\n'
263-
r'\[INFO\] This may take a few minutes\.\.\.\n'
257+
r'\[INFO\] Initializing environment for .+\.\n'
264258
r'Bash hook\.+Passed\n'
265259
)
266260

tests/conftest.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from __future__ import unicode_literals
33

44
import io
5+
import logging
56
import os
67
import os.path
78

@@ -113,3 +114,9 @@ def cmd_runner(tmpdir_factory):
113114
@pytest.yield_fixture
114115
def runner_with_mocked_store(mock_out_store_directory):
115116
yield Runner('/')
117+
118+
119+
@pytest.yield_fixture
120+
def log_info_mock():
121+
with mock.patch.object(logging.getLogger('pre_commit'), 'info') as mck:
122+
yield mck

tests/repository_test.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -254,18 +254,21 @@ def test_languages(tmpdir_factory, store):
254254
assert repo.languages == set([('python', 'default')])
255255

256256

257-
def test_reinstall(tmpdir_factory, store):
257+
def test_reinstall(tmpdir_factory, store, log_info_mock):
258258
path = make_repo(tmpdir_factory, 'python_hooks_repo')
259259
config = make_config_from_repo(path)
260260
repo = Repository.create(config, store)
261261
repo.require_installed()
262+
# We print some logging during clone (1) + install (3)
263+
assert log_info_mock.call_count == 4
264+
log_info_mock.reset_mock()
262265
# Reinstall with same repo should not trigger another install
263-
# TODO: how to assert this?
264266
repo.require_installed()
267+
assert log_info_mock.call_count == 0
265268
# Reinstall on another run should not trigger another install
266-
# TODO: how to assert this?
267269
repo = Repository.create(config, store)
268270
repo.require_installed()
271+
assert log_info_mock.call_count == 0
269272

270273

271274
def test_control_c_control_c_on_install(tmpdir_factory, store):

tests/store_test.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
from pre_commit import five
1313
from pre_commit.store import _get_default_directory
14-
from pre_commit.store import logger
1514
from pre_commit.store import Store
1615
from pre_commit.util import cmd_output
1716
from pre_commit.util import cwd
@@ -80,12 +79,6 @@ def test_does_not_recreate_if_directory_already_exists(store):
8079
assert not os.path.exists(os.path.join(store.directory, 'README'))
8180

8281

83-
@pytest.yield_fixture
84-
def log_info_mock():
85-
with mock.patch.object(logger, 'info', autospec=True) as info_mock:
86-
yield info_mock
87-
88-
8982
def test_clone(store, tmpdir_factory, log_info_mock):
9083
path = git_dir(tmpdir_factory)
9184
with cwd(path):
@@ -95,7 +88,9 @@ def test_clone(store, tmpdir_factory, log_info_mock):
9588

9689
ret = store.clone(path, sha)
9790
# Should have printed some stuff
98-
log_info_mock.assert_called_with('This may take a few minutes...')
91+
assert log_info_mock.call_args_list[0][0][0].startswith(
92+
'Initializing environment for '
93+
)
9994

10095
# Should return a directory inside of the store
10196
assert os.path.exists(ret)

0 commit comments

Comments
 (0)