|
| 1 | +import subprocess |
| 2 | + |
| 3 | +import pre_commit.constants as C |
| 4 | +from pre_commit.commands.init_templatedir import init_templatedir |
| 5 | +from pre_commit.envcontext import envcontext |
| 6 | +from pre_commit.util import cmd_output |
| 7 | +from testing.fixtures import git_dir |
| 8 | +from testing.fixtures import make_consuming_repo |
| 9 | +from testing.util import cmd_output_mocked_pre_commit_home |
| 10 | +from testing.util import cwd |
| 11 | +from testing.util import git_commit |
| 12 | + |
| 13 | + |
| 14 | +def test_init_templatedir(tmpdir, tempdir_factory, store, cap_out): |
| 15 | + target = str(tmpdir.join('tmpl')) |
| 16 | + init_templatedir(C.CONFIG_FILE, store, target, hook_type='pre-commit') |
| 17 | + lines = cap_out.get().splitlines() |
| 18 | + assert lines[0].startswith('pre-commit installed at ') |
| 19 | + assert lines[1] == ( |
| 20 | + '[WARNING] `init.templateDir` not set to the target directory' |
| 21 | + ) |
| 22 | + assert lines[2].startswith( |
| 23 | + '[WARNING] maybe `git config --global init.templateDir', |
| 24 | + ) |
| 25 | + |
| 26 | + with envcontext([('GIT_TEMPLATE_DIR', target)]): |
| 27 | + path = make_consuming_repo(tempdir_factory, 'script_hooks_repo') |
| 28 | + |
| 29 | + with cwd(path): |
| 30 | + retcode, output, _ = git_commit( |
| 31 | + fn=cmd_output_mocked_pre_commit_home, |
| 32 | + tempdir_factory=tempdir_factory, |
| 33 | + # git commit puts pre-commit to stderr |
| 34 | + stderr=subprocess.STDOUT, |
| 35 | + ) |
| 36 | + assert retcode == 0 |
| 37 | + assert 'Bash hook....' in output |
| 38 | + |
| 39 | + |
| 40 | +def test_init_templatedir_already_set(tmpdir, tempdir_factory, store, cap_out): |
| 41 | + target = str(tmpdir.join('tmpl')) |
| 42 | + tmp_git_dir = git_dir(tempdir_factory) |
| 43 | + with cwd(tmp_git_dir): |
| 44 | + cmd_output('git', 'config', 'init.templateDir', target) |
| 45 | + init_templatedir(C.CONFIG_FILE, store, target, hook_type='pre-commit') |
| 46 | + |
| 47 | + lines = cap_out.get().splitlines() |
| 48 | + assert len(lines) == 1 |
| 49 | + assert lines[0].startswith('pre-commit installed at') |
0 commit comments