clean: separate store from runner

This commit is contained in:
Anthony Sottile 2018-06-29 20:08:23 -07:00
parent 0e430be0ce
commit 6d683a5fac
4 changed files with 12 additions and 21 deletions

View file

@ -7,9 +7,9 @@ from pre_commit import output
from pre_commit.util import rmtree from pre_commit.util import rmtree
def clean(runner): def clean(store):
legacy_path = os.path.expanduser('~/.pre-commit') legacy_path = os.path.expanduser('~/.pre-commit')
for directory in (runner.store.directory, legacy_path): for directory in (store.directory, legacy_path):
if os.path.exists(directory): if os.path.exists(directory):
rmtree(directory) rmtree(directory)
output.write_line('Cleaned {}.'.format(directory)) output.write_line('Cleaned {}.'.format(directory))

View file

@ -243,7 +243,7 @@ def main(argv=None):
elif args.command == 'uninstall': elif args.command == 'uninstall':
return uninstall(runner, hook_type=args.hook_type) return uninstall(runner, hook_type=args.hook_type)
elif args.command == 'clean': elif args.command == 'clean':
return clean(runner) return clean(runner.store)
elif args.command == 'autoupdate': elif args.command == 'autoupdate':
if args.tags_only: if args.tags_only:
logger.warning('--tags-only is the default') logger.warning('--tags-only is the default')

View file

@ -6,7 +6,6 @@ import mock
import pytest import pytest
from pre_commit.commands.clean import clean from pre_commit.commands.clean import clean
from pre_commit.util import rmtree
@pytest.fixture(autouse=True) @pytest.fixture(autouse=True)
@ -21,17 +20,16 @@ def fake_old_dir(tempdir_factory):
yield fake_old_dir yield fake_old_dir
def test_clean(runner_with_mocked_store, fake_old_dir): def test_clean(store, fake_old_dir):
store.require_created()
assert os.path.exists(fake_old_dir) assert os.path.exists(fake_old_dir)
assert os.path.exists(runner_with_mocked_store.store.directory) assert os.path.exists(store.directory)
clean(runner_with_mocked_store) clean(store)
assert not os.path.exists(fake_old_dir) assert not os.path.exists(fake_old_dir)
assert not os.path.exists(runner_with_mocked_store.store.directory) assert not os.path.exists(store.directory)
def test_clean_empty(runner_with_mocked_store): def test_clean_idempotent(store):
"""Make sure clean succeeds when the directory doesn't exist.""" assert not os.path.exists(store.directory)
rmtree(runner_with_mocked_store.store.directory) clean(store)
assert not os.path.exists(runner_with_mocked_store.store.directory) assert not os.path.exists(store.directory)
clean(runner_with_mocked_store)
assert not os.path.exists(runner_with_mocked_store.store.directory)

View file

@ -11,10 +11,8 @@ import mock
import pytest import pytest
import six import six
import pre_commit.constants as C
from pre_commit import output from pre_commit import output
from pre_commit.logging_handler import add_logging_handler from pre_commit.logging_handler import add_logging_handler
from pre_commit.runner import Runner
from pre_commit.store import Store from pre_commit.store import Store
from pre_commit.util import cmd_output from pre_commit.util import cmd_output
from testing.fixtures import git_dir from testing.fixtures import git_dir
@ -151,11 +149,6 @@ def store(tempdir_factory):
yield Store(os.path.join(tempdir_factory.get(), '.pre-commit')) yield Store(os.path.join(tempdir_factory.get(), '.pre-commit'))
@pytest.fixture
def runner_with_mocked_store(mock_out_store_directory):
yield Runner('/', C.CONFIG_FILE)
@pytest.fixture @pytest.fixture
def log_info_mock(): def log_info_mock():
with mock.patch.object(logging.getLogger('pre_commit'), 'info') as mck: with mock.patch.object(logging.getLogger('pre_commit'), 'info') as mck: