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

Skip to content

Commit f48c0ab

Browse files
committed
Use expanduser in init-templatedir like git does
1 parent cab8036 commit f48c0ab

2 files changed

Lines changed: 19 additions & 1 deletion

File tree

pre_commit/commands/init_templatedir.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def init_templatedir(config_file, store, directory, hook_type):
1818
except CalledProcessError:
1919
configured_path = None
2020
else:
21-
configured_path = os.path.realpath(out.strip())
21+
configured_path = os.path.realpath(os.path.expanduser(out.strip()))
2222
dest = os.path.realpath(directory)
2323
if configured_path != dest:
2424
logger.warning('`init.templateDir` not set to the target directory')

tests/commands/init_templatedir_test.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
import os.path
12
import subprocess
23

4+
import mock
5+
36
import pre_commit.constants as C
47
from pre_commit.commands.init_templatedir import init_templatedir
58
from pre_commit.envcontext import envcontext
@@ -61,3 +64,18 @@ def test_init_templatedir_not_set(tmpdir, store, cap_out):
6164
assert lines[1] == (
6265
'[WARNING] `init.templateDir` not set to the target directory'
6366
)
67+
68+
69+
def test_init_templatedir_expanduser(tmpdir, tempdir_factory, store, cap_out):
70+
target = str(tmpdir.join('tmpl'))
71+
tmp_git_dir = git_dir(tempdir_factory)
72+
with cwd(tmp_git_dir):
73+
cmd_output('git', 'config', 'init.templateDir', '~/templatedir')
74+
with mock.patch.object(os.path, 'expanduser', return_value=target):
75+
init_templatedir(
76+
C.CONFIG_FILE, store, target, hook_type='pre-commit',
77+
)
78+
79+
lines = cap_out.get().splitlines()
80+
assert len(lines) == 1
81+
assert lines[0].startswith('pre-commit installed at')

0 commit comments

Comments
 (0)