forked from pre-commit/pre-commit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit_test.py
165 lines (115 loc) · 4.69 KB
/
git_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
from __future__ import absolute_import
from __future__ import unicode_literals
import os.path
import pytest
from pre_commit import git
from pre_commit.errors import FatalError
from pre_commit.util import cmd_output
from pre_commit.util import cwd
from testing.fixtures import git_dir
from testing.util import xfailif_no_symlink
def test_get_root_at_root(tempdir_factory):
path = git_dir(tempdir_factory)
with cwd(path):
assert os.path.normcase(git.get_root()) == os.path.normcase(path)
def test_get_root_deeper(tempdir_factory):
path = git_dir(tempdir_factory)
foo_path = os.path.join(path, 'foo')
os.mkdir(foo_path)
with cwd(foo_path):
assert os.path.normcase(git.get_root()) == os.path.normcase(path)
def test_get_root_not_git_dir(tempdir_factory):
with cwd(tempdir_factory.get()):
with pytest.raises(FatalError):
git.get_root()
def test_get_staged_files_deleted(tempdir_factory):
path = git_dir(tempdir_factory)
with cwd(path):
open('test', 'a').close()
cmd_output('git', 'add', 'test')
cmd_output('git', 'commit', '-m', 'foo', '--allow-empty')
cmd_output('git', 'rm', '--cached', 'test')
assert git.get_staged_files() == []
def test_is_not_in_merge_conflict(tempdir_factory):
path = git_dir(tempdir_factory)
with cwd(path):
assert git.is_in_merge_conflict() is False
def test_is_in_merge_conflict(in_merge_conflict):
assert git.is_in_merge_conflict() is True
def test_is_in_merge_conflict_submodule(in_conflicting_submodule):
assert git.is_in_merge_conflict() is True
def test_cherry_pick_conflict(in_merge_conflict):
cmd_output('git', 'merge', '--abort')
foo_ref = cmd_output('git', 'rev-parse', 'foo')[1].strip()
cmd_output('git', 'cherry-pick', foo_ref, retcode=None)
assert git.is_in_merge_conflict() is False
@pytest.fixture
def get_files_matching_func():
def get_filenames():
return (
'.pre-commit-hooks.yaml',
'pre_commit/main.py',
'pre_commit/git.py',
'im_a_file_that_doesnt_exist.py',
)
return git.get_files_matching(get_filenames)
def test_get_files_matching_base(get_files_matching_func):
ret = get_files_matching_func('', '^$')
assert ret == {
'.pre-commit-hooks.yaml',
'pre_commit/main.py',
'pre_commit/git.py',
}
@xfailif_no_symlink
def test_matches_broken_symlink(tmpdir): # pragma: no cover (non-windwos)
with tmpdir.as_cwd():
os.symlink('does-not-exist', 'link')
func = git.get_files_matching(lambda: ('link',))
assert func('', '^$') == {'link'}
def test_get_files_matching_total_match(get_files_matching_func):
ret = get_files_matching_func('^.*\\.py$', '^$')
assert ret == {'pre_commit/main.py', 'pre_commit/git.py'}
def test_does_search_instead_of_match(get_files_matching_func):
ret = get_files_matching_func('\\.yaml$', '^$')
assert ret == {'.pre-commit-hooks.yaml'}
def test_does_not_include_deleted_fileS(get_files_matching_func):
ret = get_files_matching_func('exist.py', '^$')
assert ret == set()
def test_exclude_removes_files(get_files_matching_func):
ret = get_files_matching_func('', '\\.py$')
assert ret == {'.pre-commit-hooks.yaml'}
def resolve_conflict():
with open('conflict_file', 'w') as conflicted_file:
conflicted_file.write('herp\nderp\n')
cmd_output('git', 'add', 'conflict_file')
def test_get_conflicted_files(in_merge_conflict):
resolve_conflict()
with open('other_file', 'w') as other_file:
other_file.write('oh hai')
cmd_output('git', 'add', 'other_file')
ret = set(git.get_conflicted_files())
assert ret == {'conflict_file', 'other_file'}
def test_get_conflicted_files_in_submodule(in_conflicting_submodule):
resolve_conflict()
assert set(git.get_conflicted_files()) == {'conflict_file'}
def test_get_conflicted_files_unstaged_files(in_merge_conflict):
# If they for whatever reason did pre-commit run --no-stash during a
# conflict
resolve_conflict()
# Make unstaged file.
with open('bar_only_file', 'w') as bar_only_file:
bar_only_file.write('new contents!\n')
ret = set(git.get_conflicted_files())
assert ret == {'conflict_file'}
MERGE_MSG = b"Merge branch 'foo' into bar\n\nConflicts:\n\tconflict_file\n"
OTHER_MERGE_MSG = MERGE_MSG + b'\tother_conflict_file\n'
@pytest.mark.parametrize(
('input', 'expected_output'),
(
(MERGE_MSG, ['conflict_file']),
(OTHER_MERGE_MSG, ['conflict_file', 'other_conflict_file']),
),
)
def test_parse_merge_msg_for_conflicts(input, expected_output):
ret = git.parse_merge_msg_for_conflicts(input)
assert ret == expected_output