Skip to content

Commit 0120af5

Browse files
committed
Adhere to XDG specification for cache dir.
1 parent ef8347c commit 0120af5

File tree

9 files changed

+50
-17
lines changed

9 files changed

+50
-17
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,4 @@ after_success: coveralls
2828
cache:
2929
directories:
3030
- $HOME/.cache/pip
31-
- $HOME/.pre-commit
31+
- $HOME/.cache/pre-commit

appveyor.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,4 @@ test_script: tox
2323

2424
cache:
2525
- '%LOCALAPPDATA%\pip\cache'
26-
- '%USERPROFILE%\.pre-commit'
26+
- '%USERPROFILE%\.cache\pre-commit'

pre_commit/commands/clean.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88

99

1010
def clean(runner):
11-
if os.path.exists(runner.store.directory):
12-
rmtree(runner.store.directory)
13-
output.write_line('Cleaned {}.'.format(runner.store.directory))
11+
legacy_path = os.path.expanduser('~/.pre-commit')
12+
for directory in (runner.store.directory, legacy_path):
13+
if os.path.exists(directory):
14+
rmtree(directory)
15+
output.write_line('Cleaned {}.'.format(directory))
1416
return 0

pre_commit/error_handler.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,11 @@ def _log_and_exit(msg, exc, formatted):
2828
_to_bytes(exc), b'\n',
2929
))
3030
output.write(error_msg)
31-
output.write_line('Check the log at ~/.pre-commit/pre-commit.log')
3231
store = Store()
3332
store.require_created()
34-
with open(os.path.join(store.directory, 'pre-commit.log'), 'wb') as log:
33+
log_path = os.path.join(store.directory, 'pre-commit.log')
34+
output.write_line('Check the log at {}'.format(log_path))
35+
with open(log_path, 'wb') as log:
3536
output.write(error_msg, stream=log)
3637
output.write_line(formatted, stream=log)
3738
raise SystemExit(1)

pre_commit/store.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ def _get_default_directory():
2929
`Store.get_default_directory` can be mocked in tests and
3030
`_get_default_directory` can be tested.
3131
"""
32-
return os.environ.get(
33-
'PRE_COMMIT_HOME',
34-
os.path.join(os.path.expanduser('~'), '.pre-commit'),
32+
return os.environ.get('PRE_COMMIT_HOME') or os.path.join(
33+
os.environ.get('XDG_CACHE_HOME') or os.path.expanduser('~/.cache'),
34+
'pre-commit',
3535
)
3636

3737

tests/commands/clean_test.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,35 @@
22

33
import os.path
44

5+
import mock
6+
import pytest
7+
58
from pre_commit.commands.clean import clean
69
from pre_commit.util import rmtree
710

811

9-
def test_clean(runner_with_mocked_store):
12+
@pytest.fixture(autouse=True)
13+
def fake_old_dir(tempdir_factory):
14+
fake_old_dir = tempdir_factory.get()
15+
16+
def _expanduser(path, *args, **kwargs):
17+
assert path == '~/.pre-commit'
18+
return fake_old_dir
19+
20+
with mock.patch.object(os.path, 'expanduser', side_effect=_expanduser):
21+
yield fake_old_dir
22+
23+
24+
def test_clean(runner_with_mocked_store, fake_old_dir):
25+
assert os.path.exists(fake_old_dir)
1026
assert os.path.exists(runner_with_mocked_store.store.directory)
1127
clean(runner_with_mocked_store)
28+
assert not os.path.exists(fake_old_dir)
1229
assert not os.path.exists(runner_with_mocked_store.store.directory)
1330

1431

1532
def test_clean_empty(runner_with_mocked_store):
16-
"""Make sure clean succeeds when we the directory doesn't exist."""
33+
"""Make sure clean succeeds when the directory doesn't exist."""
1734
rmtree(runner_with_mocked_store.store.directory)
1835
assert not os.path.exists(runner_with_mocked_store.store.directory)
1936
clean(runner_with_mocked_store)

tests/error_handler_test.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,12 @@ def test_log_and_exit(cap_out, mock_out_store_directory):
8181
)
8282

8383
printed = cap_out.get()
84+
log_file = os.path.join(mock_out_store_directory, 'pre-commit.log')
8485
assert printed == (
8586
'msg: FatalError: hai\n'
86-
'Check the log at ~/.pre-commit/pre-commit.log\n'
87+
'Check the log at {}\n'.format(log_file)
8788
)
8889

89-
log_file = os.path.join(mock_out_store_directory, 'pre-commit.log')
9090
assert os.path.exists(log_file)
9191
contents = io.open(log_file).read()
9292
assert contents == (
@@ -102,6 +102,7 @@ def test_error_handler_non_ascii_exception(mock_out_store_directory):
102102

103103

104104
def test_error_handler_no_tty(tempdir_factory):
105+
pre_commit_home = tempdir_factory.get()
105106
output = cmd_output_mocked_pre_commit_home(
106107
sys.executable, '-c',
107108
'from __future__ import unicode_literals\n'
@@ -110,8 +111,10 @@ def test_error_handler_no_tty(tempdir_factory):
110111
' raise ValueError("\\u2603")\n',
111112
retcode=1,
112113
tempdir_factory=tempdir_factory,
114+
pre_commit_home=pre_commit_home,
113115
)
116+
log_file = os.path.join(pre_commit_home, 'pre-commit.log')
114117
assert output[1].replace('\r', '') == (
115118
'An unexpected error has occurred: ValueError: ☃\n'
116-
'Check the log at ~/.pre-commit/pre-commit.log\n'
119+
'Check the log at {}\n'.format(log_file)
117120
)

tests/main_test.py

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

44
import argparse
5+
import os.path
56

67
import mock
78
import pytest
@@ -121,10 +122,11 @@ def test_expected_fatal_error_no_git_repo(
121122
with cwd(tempdir_factory.get()):
122123
with pytest.raises(SystemExit):
123124
main.main([])
125+
log_file = os.path.join(mock_out_store_directory, 'pre-commit.log')
124126
assert cap_out.get() == (
125127
'An error has occurred: FatalError: git failed. '
126128
'Is it installed, and are you in a Git repository directory?\n'
127-
'Check the log at ~/.pre-commit/pre-commit.log\n'
129+
'Check the log at {}\n'.format(log_file)
128130
)
129131

130132

tests/store_test.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,15 @@ def test_our_session_fixture_works():
2929
def test_get_default_directory_defaults_to_home():
3030
# Not we use the module level one which is not mocked
3131
ret = _get_default_directory()
32-
assert ret == os.path.join(os.path.expanduser('~'), '.pre-commit')
32+
assert ret == os.path.join(os.path.expanduser('~/.cache'), 'pre-commit')
33+
34+
35+
def test_adheres_to_xdg_specification():
36+
with mock.patch.dict(
37+
os.environ, {'XDG_CACHE_HOME': '/tmp/fakehome'},
38+
):
39+
ret = _get_default_directory()
40+
assert ret == os.path.join('/tmp/fakehome', 'pre-commit')
3341

3442

3543
def test_uses_environment_variable_when_present():

0 commit comments

Comments
 (0)