Merge pull request #602 from pre-commit/xdg_cache_dir

Adhere to XDG specification for cache dir.
This commit is contained in:
Anthony Sottile 2017-09-07 08:19:45 -07:00 committed by GitHub
commit 9ff6818270
9 changed files with 50 additions and 17 deletions

View file

@ -2,18 +2,35 @@ from __future__ import unicode_literals
import os.path
import mock
import pytest
from pre_commit.commands.clean import clean
from pre_commit.util import rmtree
def test_clean(runner_with_mocked_store):
@pytest.fixture(autouse=True)
def fake_old_dir(tempdir_factory):
fake_old_dir = tempdir_factory.get()
def _expanduser(path, *args, **kwargs):
assert path == '~/.pre-commit'
return fake_old_dir
with mock.patch.object(os.path, 'expanduser', side_effect=_expanduser):
yield fake_old_dir
def test_clean(runner_with_mocked_store, fake_old_dir):
assert os.path.exists(fake_old_dir)
assert os.path.exists(runner_with_mocked_store.store.directory)
clean(runner_with_mocked_store)
assert not os.path.exists(fake_old_dir)
assert not os.path.exists(runner_with_mocked_store.store.directory)
def test_clean_empty(runner_with_mocked_store):
"""Make sure clean succeeds when we the directory doesn't exist."""
"""Make sure clean succeeds when the directory doesn't exist."""
rmtree(runner_with_mocked_store.store.directory)
assert not os.path.exists(runner_with_mocked_store.store.directory)
clean(runner_with_mocked_store)

View file

@ -81,12 +81,12 @@ def test_log_and_exit(cap_out, mock_out_store_directory):
)
printed = cap_out.get()
log_file = os.path.join(mock_out_store_directory, 'pre-commit.log')
assert printed == (
'msg: FatalError: hai\n'
'Check the log at ~/.pre-commit/pre-commit.log\n'
'Check the log at {}\n'.format(log_file)
)
log_file = os.path.join(mock_out_store_directory, 'pre-commit.log')
assert os.path.exists(log_file)
contents = io.open(log_file).read()
assert contents == (
@ -102,6 +102,7 @@ def test_error_handler_non_ascii_exception(mock_out_store_directory):
def test_error_handler_no_tty(tempdir_factory):
pre_commit_home = tempdir_factory.get()
output = cmd_output_mocked_pre_commit_home(
sys.executable, '-c',
'from __future__ import unicode_literals\n'
@ -110,8 +111,10 @@ def test_error_handler_no_tty(tempdir_factory):
' raise ValueError("\\u2603")\n',
retcode=1,
tempdir_factory=tempdir_factory,
pre_commit_home=pre_commit_home,
)
log_file = os.path.join(pre_commit_home, 'pre-commit.log')
assert output[1].replace('\r', '') == (
'An unexpected error has occurred: ValueError: ☃\n'
'Check the log at ~/.pre-commit/pre-commit.log\n'
'Check the log at {}\n'.format(log_file)
)

View file

@ -2,6 +2,7 @@ from __future__ import absolute_import
from __future__ import unicode_literals
import argparse
import os.path
import mock
import pytest
@ -121,10 +122,11 @@ def test_expected_fatal_error_no_git_repo(
with cwd(tempdir_factory.get()):
with pytest.raises(SystemExit):
main.main([])
log_file = os.path.join(mock_out_store_directory, 'pre-commit.log')
assert cap_out.get() == (
'An error has occurred: FatalError: git failed. '
'Is it installed, and are you in a Git repository directory?\n'
'Check the log at ~/.pre-commit/pre-commit.log\n'
'Check the log at {}\n'.format(log_file)
)

View file

@ -29,7 +29,15 @@ def test_our_session_fixture_works():
def test_get_default_directory_defaults_to_home():
# Not we use the module level one which is not mocked
ret = _get_default_directory()
assert ret == os.path.join(os.path.expanduser('~'), '.pre-commit')
assert ret == os.path.join(os.path.expanduser('~/.cache'), 'pre-commit')
def test_adheres_to_xdg_specification():
with mock.patch.dict(
os.environ, {'XDG_CACHE_HOME': '/tmp/fakehome'},
):
ret = _get_default_directory()
assert ret == os.path.join('/tmp/fakehome', 'pre-commit')
def test_uses_environment_variable_when_present():