|
| 1 | +import os.path |
1 | 2 | import subprocess |
2 | 3 |
|
| 4 | +import mock |
| 5 | + |
3 | 6 | import pre_commit.constants as C |
4 | 7 | from pre_commit.commands.init_templatedir import init_templatedir |
5 | 8 | from pre_commit.envcontext import envcontext |
@@ -47,3 +50,32 @@ def test_init_templatedir_already_set(tmpdir, tempdir_factory, store, cap_out): |
47 | 50 | lines = cap_out.get().splitlines() |
48 | 51 | assert len(lines) == 1 |
49 | 52 | assert lines[0].startswith('pre-commit installed at') |
| 53 | + |
| 54 | + |
| 55 | +def test_init_templatedir_not_set(tmpdir, store, cap_out): |
| 56 | + # set HOME to ignore the current `.gitconfig` |
| 57 | + with envcontext([('HOME', str(tmpdir))]): |
| 58 | + with tmpdir.join('tmpl').ensure_dir().as_cwd(): |
| 59 | + # we have not set init.templateDir so this should produce a warning |
| 60 | + init_templatedir(C.CONFIG_FILE, store, '.', hook_type='pre-commit') |
| 61 | + |
| 62 | + lines = cap_out.get().splitlines() |
| 63 | + assert len(lines) == 3 |
| 64 | + assert lines[1] == ( |
| 65 | + '[WARNING] `init.templateDir` not set to the target directory' |
| 66 | + ) |
| 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