-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtest_git.py
164 lines (131 loc) · 4.22 KB
/
test_git.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
# SPDX-FileCopyrightText: 2022 Felix Gruber <[email protected]>
#
# SPDX-License-Identifier: GPL-3.0-or-later
from pathlib import Path
import pytest
from git import (
Conflict,
Git,
GitError,
GitEmptyCommitError,
GitMergeConflictError,
)
def test_create_git_dir(tmp_path: Path) -> None:
Git.create(tmp_path)
assert (tmp_path / '.git').exists()
def test_create_commit(tmp_path: Path) -> None:
git = Git.create(tmp_path)
my_file = tmp_path / 'a'
my_file.touch()
git.add_file(my_file)
git.commit('Add a')
def test_empty_commit_raises_error(tmp_path: Path) -> None:
git = Git.create(tmp_path)
my_file = tmp_path / 'a'
my_file.touch()
git.add_file(my_file)
git.commit('Add a')
with pytest.raises(GitEmptyCommitError):
git.commit('Empty commit.')
def test_empty_commit_in_dirty_working_dir_raises_error(tmp_path: Path) -> None:
git = Git.create(tmp_path)
my_file = tmp_path / 'a'
my_file.touch()
git.add_file(my_file)
git.commit('Add a')
(tmp_path / 'b').touch()
with pytest.raises(GitEmptyCommitError):
git.commit('Empty commit.')
def test_checkout_current_branch(tmp_path: Path) -> None:
# The master branch only gets created once we create a first commit.
git = Git.create(tmp_path)
my_file = tmp_path / 'a'
my_file.touch()
git.add_file(my_file)
git.commit('Add a')
current_branch = git.current_branch()
git.change_branch(current_branch)
assert git.current_branch() == current_branch
def test_create_branch(tmp_path: Path) -> None:
# Create_branch does not work in an empty repository.
# Hence, I create a commit on the master branch.
git = Git.create(tmp_path)
my_file = tmp_path / 'a'
my_file.touch()
git.add_file(my_file)
git.commit('Add a')
# Now create the new branch
original_branch = git.current_branch()
new_branch = 'my-new-branch'
git.create_branch(new_branch)
assert git.current_branch() == original_branch
def test_checkout_new_branch(tmp_path: Path) -> None:
# Create_branch does not work in an empty repository.
# Hence, I create a commit on the master branch.
git = Git.create(tmp_path)
my_file = tmp_path / 'a'
my_file.touch()
git.add_file(my_file)
git.commit('Add a')
new_branch = 'my-new-branch'
git.create_branch(new_branch, switch=True)
assert git.current_branch() == new_branch
def test_checkout_nonexistent_branch(tmp_path: Path) -> None:
git = Git.create(tmp_path)
with pytest.raises(GitError, match='switch to non-existent branch'):
git.change_branch('nonexistent')
def test_merge(tmp_path: Path) -> None:
git = Git.create(tmp_path)
my_file = tmp_path / 'a'
my_file.touch()
git.add_file(my_file)
git.commit('Add a')
main_branch = git.current_branch()
git.create_branch('b', switch=True)
my_file = tmp_path / 'b'
my_file.touch()
git.add_file(my_file)
git.commit('Add b')
git.change_branch(main_branch)
my_file = tmp_path / 'c'
my_file.touch()
git.add_file(my_file)
git.commit('Add c')
git.merge('b')
assert (tmp_path / 'a').exists()
assert (tmp_path / 'b').exists()
assert (tmp_path / 'c').exists()
def test_merge_conflict(tmp_path: Path) -> None:
git = Git.create(tmp_path)
my_file = tmp_path / 'a'
my_file.touch()
git.add_file(my_file)
git.commit('Add a')
main_branch = git.current_branch()
git.create_branch('bad', switch=True)
my_file = tmp_path / 'a'
with my_file.open('w') as f:
f.write('1')
git.add_file(my_file)
my_file = tmp_path / 'b'
with my_file.open('w') as f:
f.write('1')
git.add_file(my_file)
git.commit('Add b')
git.change_branch(main_branch)
my_file = tmp_path / 'a'
with my_file.open('w') as f:
f.write('2')
git.add_file(my_file)
my_file = tmp_path / 'b'
with my_file.open('w') as f:
f.write('2')
git.add_file(my_file)
git.commit('Add b')
with pytest.raises(GitMergeConflictError) as exc_info:
git.merge('bad')
conflicts = exc_info.value.conflicts
assert set(conflicts) == {
Conflict(name='a', type='content'),
Conflict(name='b', type='add/add'),
}