-
Notifications
You must be signed in to change notification settings - Fork 2
/
test.py
executable file
·329 lines (278 loc) · 10.3 KB
/
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
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
#!/usr/bin/env pytest
from subprocess import Popen
from gitrevise.odb import Repository
from pathlib import Path
import pytest
import textwrap
import gitbranchstack.main as gitbranchstack
def test_create_branches(repo) -> None:
a = repo.workdir / "a"
repo.git("commit", "--allow-empty", "-m", "[a] a1")
repo.git("commit", "--allow-empty", "-m", "[b] b1")
repo.git("commit", "--allow-empty", "-m", "WIP commit")
repo.git("commit", "--allow-empty", "-m", "[a] a2")
repo.git("commit", "--allow-empty", "-m", "[a] a3")
repo.git("commit", "--allow-empty", "-m", "another WIP commit")
expected = """\
* (HEAD -> 🐬) another WIP commit
* [a] a3
* [a] a2
* WIP commit
* [b] b1
* [a] a1
* 本"""
assert expected == graph(repo)
gitbranchstack.create_branches(repo, "🐬", INITIAL_COMMIT)
expected = """\
* (HEAD -> 🐬) another WIP commit
* [a] a3
* [a] a2
* WIP commit
* [b] b1
* [a] a1
| * (a) a3
| * a2
| * a1
|/
| * (b) b1
|/
* 本"""
assert graph(repo, "a", "b") == expected
# A repeated invocation does not change anything.
gitbranchstack.create_branches(repo, "🐬", INITIAL_COMMIT)
assert graph(repo, "a", "b") == expected
# Modifying a generated branch will make us fail.
repo.git("update-ref", "refs/heads/a", "HEAD")
assert graph(repo, "a", "b") != expected
try:
gitbranchstack.create_branches(repo, "🐬", INITIAL_COMMIT)
assert (
False
), "Expect error about refusing to create branch when it was modified since the last run"
except gitbranchstack.BranchWasModifiedError:
pass
# Unless we are asked to overwrite them.
gitbranchstack.create_branches(repo, "🐬", INITIAL_COMMIT, force=True)
assert graph(repo, "a", "b") == expected
def test_create_branches_multiline_subject(repo) -> None:
repo.git("commit", "--allow-empty", "-m", "[a] multi\nline\nsubject")
repo.git("commit", "--allow-empty", "-m", "[a] more\nlines\n\nmessage\nbody")
gitbranchstack.create_branches(repo, "🐬", INITIAL_COMMIT)
assert repo.git("log", "--reverse", "--format=%B", "-2", f"a").decode() == (
"multi\nline\nsubject" + "\n" + "more\nlines\n\nmessage\nbody" + "\n"
)
def test_create_branches_ambiguous_ref(repo) -> None:
repo.git("update-ref", "clash", "HEAD")
repo.git("commit", "--allow-empty", "-m", "[clash] commit on branch")
gitbranchstack.create_branches(repo, "🐬", INITIAL_COMMIT)
expected = """\
* (HEAD -> 🐬) [clash] commit on branch
| * (clash) commit on branch
|/
* 本"""
assert graph(repo, "refs/heads/clash") == expected
def test_create_branches_stale_cache(repo) -> None:
repo.git("commit", "--allow-empty", "-m", "[lost-branch] subject")
gitbranchstack.create_branches(repo, "🐬", INITIAL_COMMIT)
expected = """\
* (HEAD -> 🐬) [lost-branch] subject
| * (lost-branch) subject
|/
* 本"""
assert graph(repo, "lost-branch") == expected
(repo.gitdir / "refs/heads/lost-branch").unlink()
gitbranchstack.create_branches(repo, "🐬", INITIAL_COMMIT)
assert graph(repo, "lost-branch") == expected
def test_create_branches_carry_over_cache(repo) -> None:
repo.git("commit", "--allow-empty", "-m", "[a] subject a")
repo.git("commit", "--allow-empty", "-m", "[b] subject b")
gitbranchstack.create_branches(repo, "🐬", INITIAL_COMMIT, branches=("b",))
gitbranchstack.create_branches(repo, "🐬", INITIAL_COMMIT, branches=("a",))
assert tuple(
line.split()[0]
for line in (repo.gitdir / "branchstack-cache").read_bytes().splitlines()
) == (
b"b",
b"a",
)
def test_create_branches_invalid_topic(repo) -> None:
try:
gitbranchstack.create_branches(
repo,
"🐬",
INITIAL_COMMIT,
branches=("invalid-topic",),
)
assert False, "Expect error about missing topic"
except gitbranchstack.TopicNotFoundError as e:
assert e.args == ("invalid-topic", INITIAL_COMMIT, "HEAD")
def test_create_branches_custom_range(repo) -> None:
repo.git("commit", "--allow-empty", "-m", "[a] subject a")
repo.git("commit", "--allow-empty", "-m", "[b] subject b")
gitbranchstack.create_branches(repo, "🐬", "HEAD~2", "HEAD~")
assert repo.git("branch", "--list", "a")
assert not repo.git("branch", "--list", "b")
def test_create_branches_keep_tags_in_dependencies(repo) -> None:
repo.git("commit", "--allow-empty", "-m", "[b] subject b")
repo.git("commit", "--allow-empty", "-m", "[a:b] subject a")
gitbranchstack.create_branches(repo, None, INITIAL_COMMIT, "HEAD", keep_tags=None)
assert (
repo.git("log", "--format=%s", f"{INITIAL_COMMIT}..a").decode()
== "subject a\n" + "subject b"
)
gitbranchstack.create_branches(
repo, None, INITIAL_COMMIT, "HEAD", keep_tags="dependencies"
)
assert (
repo.git("log", "--format=%s", f"{INITIAL_COMMIT}..a").decode()
== "subject a\n" + "[b] subject b"
)
def test_create_branches_keep_tags_in_prefixed_parents(repo) -> None:
repo.git("commit", "--allow-empty", "-m", "[b] subject b")
repo.git("commit", "--allow-empty", "-m", "[a:+b] subject a")
gitbranchstack.create_branches(repo, None, INITIAL_COMMIT, "HEAD", keep_tags=None)
assert (
repo.git("log", "--format=%s", f"{INITIAL_COMMIT}..a").decode()
== "subject a\n" + "[b] subject b"
)
def test_dwim(repo) -> None:
origin = "origin.git"
assert Popen(("git", "init", "--bare", origin)).wait() == 0
repo.git("remote", "add", "origin", origin)
repo.git("push", "origin", "🐬:🐳")
repo.git("config", "branch.🐬.remote", "origin")
repo.git("config", "branch.🐬.merge", "refs/heads/🐳")
branch, base_commit = gitbranchstack.dwim(repo)
assert branch == "🐬"
assert base_commit == "@{upstream}"
root_id = repo.git("rev-parse", INITIAL_COMMIT).decode()
assert repo.git("rev-parse", "@{upstream}").decode() == root_id
def commit(contents):
repo.git("add", write("a", contents))
repo.git("commit", "-m", contents)
commit("onto")
commit("2")
test_branch = "test-branch"
repo.git("checkout", "-b", test_branch, "HEAD~")
commit("3")
assert Popen(("git", "rebase", "🐬")).wait() != 0
branch, base_commit = gitbranchstack.dwim(repo)
assert branch == "test-branch"
assert base_commit == repo.git("rev-parse", "🐬").decode()
def test_parse_log_custom_topic_affixes(repo) -> None:
prefix = ""
suffix = ":"
repo.git("config", "branchstack.subjectPrefixPrefix", prefix.encode())
repo.git("config", "branchstack.subjectPrefixSuffix", suffix.encode())
repo.git("commit", "--allow-empty", "-m", "a: a1")
repo.git("commit", "--allow-empty", "-m", "b: b1")
repo.git("commit", "--allow-empty", "-m", "b: b2")
repo.git("commit", "--allow-empty", "-m", "a: a2")
repo.git("commit", "--allow-empty", "-m", "c:a: c1")
commit_entries, dependency_graph = gitbranchstack.parse_log(
repo, prefix, suffix, f"{INITIAL_COMMIT}..HEAD", "--reverse"
)
assert tuple((topic, message) for commit_id, topic, message in commit_entries) == (
("a", "a1"),
("b", "b1"),
("b", "b2"),
("a", "a2"),
("c", "c1"),
)
assert dependency_graph == {
"a": {},
"b": {},
"c": {"a": False},
}
def test_parse_log_forward_dependency(repo) -> None:
repo.git("commit", "--allow-empty", "-m", "[a:b] a")
repo.git("commit", "--allow-empty", "-m", "[b] b")
commit_entries, dependency_graph = gitbranchstack.parse_log(
repo, "[", "]", f"{INITIAL_COMMIT}..HEAD", "--reverse"
)
assert tuple((topic, message) for commit_id, topic, message in commit_entries) == (
("a", "a"),
("b", "b"),
)
assert dependency_graph == {
"a": {"b": False},
"b": {},
}
def test_parse_log_include_others(repo) -> None:
repo.git("commit", "--allow-empty", "-m", "a b c")
repo.git("commit", "--allow-empty", "-m", "[t] d e f")
commit_entries, dependency_graph = gitbranchstack.parse_log(
repo,
"[",
"]",
f"{INITIAL_COMMIT}..HEAD",
)
assert tuple((topic, message) for commit_id, topic, message in commit_entries) == (
("t", "d e f"),
(None, "a b c"),
)
def test_transitive_dependencies() -> None:
dep_graph = {
"a": {"c": False},
"b": {"a": False},
"c": {"b": False},
}
assert gitbranchstack.transitive_dependencies(dep_graph, ("a", False)) == {
"a": False,
"b": False,
"c": False,
}
# Taken from git-revise
@pytest.fixture(autouse=True)
def hermetic_seal(tmp_path_factory, monkeypatch):
# Lock down user git configuration
home = tmp_path_factory.mktemp("home")
xdg_config_home = home / ".config"
monkeypatch.setenv("HOME", str(home))
monkeypatch.setenv("XDG_CONFIG_HOME", str(xdg_config_home))
monkeypatch.setenv("GIT_CONFIG_NOSYSTEM", "true")
# Lock down commit/authoring time
monkeypatch.setenv("GIT_AUTHOR_DATE", "1500000000 -0500")
monkeypatch.setenv("GIT_COMMITTER_DATE", "1500000000 -0500")
# Install known configuration
gitconfig = home / ".gitconfig"
gitconfig.write_bytes(
textwrap.dedent(
"""\
[core]
eol = lf
autocrlf = false
[init]
defaultBranch = "🐬"
[user]
email = [email protected]
name = Test User
"""
).encode()
)
monkeypatch.setenv("GIT_EDITOR", "false")
# Switch into a test workdir, and init our repo
workdir = tmp_path_factory.mktemp("workdir")
monkeypatch.chdir(workdir)
assert Popen(("git", "init", "-q")).wait() == 0
assert Popen(("git", "commit", "--allow-empty", "-m", "本")).wait() == 0
INITIAL_COMMIT = ":/本"
@pytest.fixture
def repo(hermetic_seal):
with Repository() as repo:
yield repo
def graph(repo, *args) -> str:
output = repo.git(
"log",
"--graph",
"--oneline",
"--format=%d %s",
"🐬",
*args,
"--",
).decode()
return "\n".join(line.rstrip() for line in output.splitlines())
def write(filename, contents) -> str:
with open(filename, "w") as f:
f.write(contents)
return filename