|
1 | 1 |
|
| 2 | +import jsonschema |
2 | 3 | import os |
3 | 4 | import os.path |
4 | 5 | import pkg_resources |
| 6 | +import pytest |
| 7 | +import shutil |
5 | 8 | import stat |
| 9 | +from plumbum import local |
6 | 10 |
|
| 11 | +from pre_commit import git |
| 12 | +from pre_commit.clientlib.validate_config import CONFIG_JSON_SCHEMA |
| 13 | +from pre_commit.clientlib.validate_config import validate_config_extra |
7 | 14 | from pre_commit.commands import install |
| 15 | +from pre_commit.commands import RepositoryCannotBeUpdatedError |
8 | 16 | from pre_commit.commands import uninstall |
| 17 | +from pre_commit.commands import _update_repository |
| 18 | +from pre_commit.ordereddict import OrderedDict |
9 | 19 | from pre_commit.runner import Runner |
| 20 | +from testing.auto_namedtuple import auto_namedtuple |
| 21 | +from testing.util import get_resource_path |
10 | 22 |
|
11 | 23 |
|
12 | 24 | def test_install_pre_commit(empty_git_dir): |
@@ -35,3 +47,70 @@ def test_uninstall(empty_git_dir): |
35 | 47 | assert os.path.exists(runner.pre_commit_path) |
36 | 48 | uninstall(runner) |
37 | 49 | assert not os.path.exists(runner.pre_commit_path) |
| 50 | + |
| 51 | + |
| 52 | +@pytest.yield_fixture |
| 53 | +def up_to_date_repo(python_hooks_repo): |
| 54 | + config = OrderedDict(( |
| 55 | + ('repo', python_hooks_repo), |
| 56 | + ('sha', git.get_head_sha(python_hooks_repo)), |
| 57 | + ('hooks', [{'id': 'foo', 'files': ''}]), |
| 58 | + )) |
| 59 | + jsonschema.validate([config], CONFIG_JSON_SCHEMA) |
| 60 | + validate_config_extra([config]) |
| 61 | + yield auto_namedtuple( |
| 62 | + repo_config=config, |
| 63 | + python_hooks_repo=python_hooks_repo, |
| 64 | + ) |
| 65 | + |
| 66 | + |
| 67 | +def test_up_to_date_repo(up_to_date_repo): |
| 68 | + input_sha = up_to_date_repo.repo_config['sha'] |
| 69 | + ret = _update_repository(up_to_date_repo.repo_config) |
| 70 | + assert ret['sha'] == input_sha |
| 71 | + |
| 72 | + |
| 73 | +@pytest.yield_fixture |
| 74 | +def out_of_date_repo(python_hooks_repo): |
| 75 | + config = OrderedDict(( |
| 76 | + ('repo', python_hooks_repo), |
| 77 | + ('sha', git.get_head_sha(python_hooks_repo)), |
| 78 | + ('hooks', [{'id': 'foo', 'files': ''}]), |
| 79 | + )) |
| 80 | + jsonschema.validate([config], CONFIG_JSON_SCHEMA) |
| 81 | + validate_config_extra([config]) |
| 82 | + local['git']['commit', '--allow-empty', '-m', 'foo']() |
| 83 | + head_sha = git.get_head_sha(python_hooks_repo) |
| 84 | + yield auto_namedtuple( |
| 85 | + repo_config=config, |
| 86 | + head_sha=head_sha, |
| 87 | + python_hooks_repo=python_hooks_repo, |
| 88 | + ) |
| 89 | + |
| 90 | + |
| 91 | +def test_out_of_date_repo(out_of_date_repo): |
| 92 | + ret = _update_repository(out_of_date_repo.repo_config) |
| 93 | + assert ret['sha'] == out_of_date_repo.head_sha |
| 94 | + |
| 95 | + |
| 96 | +@pytest.yield_fixture |
| 97 | +def hook_disappearing_repo(python_hooks_repo): |
| 98 | + config = OrderedDict(( |
| 99 | + ('repo', python_hooks_repo), |
| 100 | + ('sha', git.get_head_sha(python_hooks_repo)), |
| 101 | + ('hooks', [{'id': 'foo', 'files': ''}]), |
| 102 | + )) |
| 103 | + jsonschema.validate([config], CONFIG_JSON_SCHEMA) |
| 104 | + validate_config_extra([config]) |
| 105 | + shutil.copy(get_resource_path('manifest_without_foo.yaml'), 'manifest.yaml') |
| 106 | + local['git']['add', '.']() |
| 107 | + local['git']['commit', '-m', 'Remove foo']() |
| 108 | + yield auto_namedtuple( |
| 109 | + repo_config=config, |
| 110 | + python_hooks_repo=python_hooks_repo, |
| 111 | + ) |
| 112 | + |
| 113 | + |
| 114 | +def test_hook_disppearing_repo_raises(hook_disappearing_repo): |
| 115 | + with pytest.raises(RepositoryCannotBeUpdatedError): |
| 116 | + _update_repository(hook_disappearing_repo.repo_config) |
0 commit comments