-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtest_git.py
199 lines (163 loc) · 6.55 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# (c) 2020-2024 Michał Górny
# SPDX-License-Identifier: GPL-2.0-or-later
""" Tests for git support. """
import os
import subprocess
import tempfile
import unittest
from pathlib import Path
from nattka.git import (git_get_toplevel, git_is_dirty, git_commit,
git_reset_changes, GitDirtyWorkTree,
GitWorkTree, GitCommitNoChanges)
class GitTests(unittest.TestCase):
def setUp(self):
self.tempdir = tempfile.TemporaryDirectory()
def tearDown(self):
self.tempdir.cleanup()
def test_git_get_toplevel(self):
""" Check whether git_get_toplevel() works correctly. """
td = Path(self.tempdir.name)
# should return None or find some containing repository
self.assertNotEqual(git_get_toplevel(Path(self.tempdir.name)),
td)
assert subprocess.Popen(['git', 'init'], cwd=td).wait() == 0
self.assertEqual(git_get_toplevel(td), td)
sd = td / 'subdir'
os.mkdir(sd)
self.assertEqual(git_get_toplevel(sd), td)
def test_git_is_dirty(self):
""" Test whether we detect dirty working tree correctly. """
td = Path(self.tempdir.name)
assert subprocess.Popen(['git', 'init'], cwd=td).wait() == 0
self.assertFalse(git_is_dirty(td))
with open(td / 'file', 'w') as f:
f.write('test\n')
assert (subprocess.Popen(['git', 'add', '-N', 'file'], cwd=td)
.wait() == 0)
self.assertTrue(git_is_dirty(td))
assert (subprocess.Popen(['git', 'add', 'file'], cwd=td)
.wait() == 0)
self.assertFalse(git_is_dirty(td))
def test_git_commit(self):
"""Test whether we commit correctly"""
td = Path(self.tempdir.name)
assert subprocess.Popen(['git', 'init'], cwd=td).wait() == 0
assert subprocess.Popen(
['git', 'config', '--local', 'user.name', 'test'],
cwd=td).wait() == 0
assert subprocess.Popen(
['git', 'config', '--local', 'user.email', '[email protected]'],
cwd=td).wait() == 0
with open(td / 'file', 'w') as f:
f.write('test\n')
assert (subprocess.Popen(['git', 'add', 'file'], cwd=td)
.wait() == 0)
self.assertNotEqual(git_commit(td, 'test commit', ['file']), '')
with open(td / 'file', 'w') as f:
f.write('other\n')
assert (subprocess.Popen(['git', 'add', 'file'], cwd=td)
.wait() == 0)
self.assertNotEqual(git_commit(td, 'next commit', ['file']), '')
s = subprocess.Popen(['git', 'log', '--format=%an\n%ae\n%s',
'--name-only', '-2'],
cwd=td,
stdout=subprocess.PIPE)
sout, _ = s.communicate()
self.assertEqual(sout.decode(),
'''test
next commit
file
test
test commit
file
''')
def test_git_commit_unstaged(self):
td = Path(self.tempdir.name)
assert subprocess.Popen(['git', 'init'], cwd=td).wait() == 0
assert subprocess.Popen(
['git', 'config', '--local', 'user.name', 'test'],
cwd=td).wait() == 0
assert subprocess.Popen(
['git', 'config', '--local', 'user.email', '[email protected]'],
cwd=td).wait() == 0
with open(td / 'file', 'w') as f:
f.write('test\n')
assert (subprocess.Popen(['git', 'add', '-N', 'file'], cwd=td)
.wait() == 0)
self.assertNotEqual(git_commit(td, 'test commit', ['file']), '')
with open(td / 'file', 'w') as f:
f.write('other\n')
self.assertNotEqual(git_commit(td, 'next commit', ['file']), '')
s = subprocess.Popen(['git', 'log', '--format=%an\n%ae\n%s',
'--name-only', '-2'],
cwd=td,
stdout=subprocess.PIPE)
sout, _ = s.communicate()
self.assertEqual(sout.decode(),
'''test
next commit
file
test
test commit
file
''')
def test_git_commit_no_changes(self):
"""Test whether we fail commit on no changes"""
td = Path(self.tempdir.name)
assert subprocess.Popen(['git', 'init'], cwd=td).wait() == 0
assert subprocess.Popen(
['git', 'config', '--local', 'user.name', 'test'],
cwd=td).wait() == 0
assert subprocess.Popen(
['git', 'config', '--local', 'user.email', '[email protected]'],
cwd=td).wait() == 0
with open(td / 'file', 'w') as f:
f.write('test\n')
f.flush()
assert (subprocess.Popen(['git', 'add', 'file'], cwd=td)
.wait() == 0)
git_commit(td, 'test commit', ['file'])
self.assertRaises(
GitCommitNoChanges,
git_commit, td, 'second attempted commit', ['file'])
def test_git_reset_changes(self):
""" Test whether we reset changes correctly. """
td = Path(self.tempdir.name)
assert subprocess.Popen(['git', 'init'], cwd=td).wait() == 0
with open(td / 'file', 'w') as f:
f.write('test\n')
f.flush()
assert (subprocess.Popen(['git', 'add', 'file'], cwd=td)
.wait() == 0)
f.write('second\n')
git_reset_changes(td)
with open(td / 'file', 'r') as f:
self.assertEqual(f.read(), 'test\n')
def test_context_manager(self):
td = Path(self.tempdir.name)
assert subprocess.Popen(['git', 'init'], cwd=td).wait() == 0
with open(td / 'file', 'w') as f:
f.write('test\n')
f.flush()
assert (subprocess.Popen(['git', 'add', 'file'], cwd=td)
.wait() == 0)
with GitWorkTree(td):
f.write('second\n')
f.flush()
with open(td / 'file', 'r') as f:
self.assertEqual(f.read(), 'test\n')
def test_context_manager_dirty(self):
td = Path(self.tempdir.name)
assert subprocess.Popen(['git', 'init'], cwd=td).wait() == 0
with open(td / 'file', 'w') as f:
f.write('test\n')
f.flush()
assert (subprocess.Popen(['git', 'add', '-N', 'file'], cwd=td)
.wait() == 0)
with self.assertRaises(GitDirtyWorkTree):
with GitWorkTree(td):
pass