|
1 | 1 | import os.path |
2 | 2 | from unittest import mock |
3 | 3 |
|
| 4 | +import pytest |
| 5 | + |
4 | 6 | import pre_commit.constants as C |
5 | 7 | from pre_commit.commands.init_templatedir import init_templatedir |
6 | 8 | from pre_commit.envcontext import envcontext |
@@ -90,3 +92,49 @@ def test_init_templatedir_hookspath_set(tmpdir, tempdir_factory, store): |
90 | 92 | C.CONFIG_FILE, store, target, hook_types=['pre-commit'], |
91 | 93 | ) |
92 | 94 | assert target.join('hooks/pre-commit').exists() |
| 95 | + |
| 96 | + |
| 97 | +@pytest.mark.parametrize( |
| 98 | + ('skip', 'commit_retcode', 'commit_output_snippet'), |
| 99 | + ( |
| 100 | + (True, 0, 'Skipping `pre-commit`.'), |
| 101 | + (False, 1, f'No {C.CONFIG_FILE} file was found'), |
| 102 | + ), |
| 103 | +) |
| 104 | +def test_init_templatedir_skip_on_missing_config( |
| 105 | + tmpdir, |
| 106 | + tempdir_factory, |
| 107 | + store, |
| 108 | + cap_out, |
| 109 | + skip, |
| 110 | + commit_retcode, |
| 111 | + commit_output_snippet, |
| 112 | +): |
| 113 | + target = str(tmpdir.join('tmpl')) |
| 114 | + init_git_dir = git_dir(tempdir_factory) |
| 115 | + with cwd(init_git_dir): |
| 116 | + cmd_output('git', 'config', 'init.templateDir', target) |
| 117 | + init_templatedir( |
| 118 | + C.CONFIG_FILE, |
| 119 | + store, |
| 120 | + target, |
| 121 | + hook_types=['pre-commit'], |
| 122 | + skip_on_missing_config=skip, |
| 123 | + ) |
| 124 | + |
| 125 | + lines = cap_out.get().splitlines() |
| 126 | + assert len(lines) == 1 |
| 127 | + assert lines[0].startswith('pre-commit installed at') |
| 128 | + |
| 129 | + with envcontext((('GIT_TEMPLATE_DIR', target),)): |
| 130 | + verify_git_dir = git_dir(tempdir_factory) |
| 131 | + |
| 132 | + with cwd(verify_git_dir): |
| 133 | + retcode, output = git_commit( |
| 134 | + fn=cmd_output_mocked_pre_commit_home, |
| 135 | + tempdir_factory=tempdir_factory, |
| 136 | + retcode=None, |
| 137 | + ) |
| 138 | + |
| 139 | + assert retcode == commit_retcode |
| 140 | + assert commit_output_snippet in output |
0 commit comments