Debian Codegraph @

tests/integration/test_dunamai.py

raw
1 import datetime as dt
2 import os
3 import shutil
4 import sys
5 import time
6 from contextlib import contextmanager
7 from pathlib import Path
8 from typing import Callable, Iterator, List, Optional
9
10 import pytest
11
12 from dunamai import Version, Vcs, Concern, _get_git_version, _run_cmd
13
14
15 def avoid_identical_ref_timestamps() -> None:
16 time.sleep(1.2)
17
18
19 def lacks_git_version(version: List[int]) -> bool:
20 if shutil.which("git") is None:
21 return True
22 return _get_git_version() < version
23
24
25 REPO = Path(__file__).parent.parent.parent
26
27
28 @contextmanager
29 def chdir(where: Path) -> Iterator[None]:
30 start = Path.cwd()
31 os.chdir(str(where))
32 try:
33 yield
34 finally:
35 os.chdir(str(start))
36
37
38 def is_git_legacy() -> bool:
39 version = _get_git_version()
40 return version < [2, 7]
41
42
43 def set_missing_env(key: str, value: str, alts: Optional[List[str]] = None) -> None:
44 if alts is None:
45 alts = []
46
47 for k in [key, *alts]:
48 if os.environ.get(k) is not None:
49 return
50
51 os.environ[key] = value
52
53
54 def make_run_callback(where: Path) -> Callable:
55 def inner(command, expected_code: int = 0, env: Optional[dict] = None):
56 _, out = _run_cmd(command, where=where, codes=[expected_code], env=env)
57 return out
58
59 return inner
60
61
62 def make_from_callback(function: Callable, clear: bool = True, chronological: bool = True) -> Callable:
63 def inner(*args, fresh: bool = False, **kwargs):
64 version = function(*args, **kwargs)
65 if fresh:
66 assert version.commit is None
67 assert version.timestamp is None
68 else:
69 assert isinstance(version.commit, str)
70 assert len(version.commit) > 0
71
72 if chronological:
73 assert isinstance(version.timestamp, dt.datetime)
74 now = dt.datetime.utcnow().replace(tzinfo=dt.timezone.utc)
75 delta = dt.timedelta(minutes=1)
76 assert now - delta <= version.timestamp <= now + delta
77 if clear:
78 version.commit = None
79 version.timestamp = None
80 return version
81
82 return inner
83
84
85 from_any_vcs = make_from_callback(Version.from_any_vcs)
86 from_any_vcs_unmocked = make_from_callback(Version.from_any_vcs, clear=False)
87 from_explicit_vcs = make_from_callback(Version.from_vcs)
88
89
90 @pytest.mark.skipif(shutil.which("git") is None, reason="Requires Git")
91 def test__version__from_git__with_annotated_tags(tmp_path) -> None:
92 run = make_run_callback(tmp_path)
93 from_vcs = make_from_callback(Version.from_git)
94 b = "master"
95 legacy = is_git_legacy()
96
97 with chdir(tmp_path):
98 run("git init")
99 try:
100 # Compatibility for newer Git versions:
101 run("git branch -m master")
102 except Exception:
103 pass
104 assert from_vcs(fresh=True) == Version("0.0.0", distance=0, dirty=True, branch=b)
105 assert from_vcs(fresh=True).vcs == Vcs.Git
106
107 # Additional one-off check not in other VCS integration tests:
108 # strict mode requires there to be a tag
109 with pytest.raises(RuntimeError):
110 from_vcs(strict=True)
111
112 (tmp_path / "foo.txt").write_text("hi")
113 assert from_vcs(fresh=True) == Version("0.0.0", distance=0, dirty=True, branch=b)
114 assert from_vcs(fresh=True).concerns == set()
115
116 run("git add .")
117 run('git commit --no-gpg-sign -m "Initial commit"')
118 assert from_vcs() == Version("0.0.0", distance=1, dirty=False, branch=b)
119
120 # Detect dirty if untracked files
121 (tmp_path / "bar.txt").write_text("bye")
122 assert from_vcs() == Version("0.0.0", distance=1, dirty=True, branch=b)
123 assert from_vcs(ignore_untracked=True) == Version("0.0.0", distance=1, dirty=False, branch=b)
124
125 # Once the untracked file is removed we are no longer dirty
126 (tmp_path / "bar.txt").unlink()
127 assert from_vcs() == Version("0.0.0", distance=1, dirty=False, branch=b)
128
129 # Additional one-off check not in other VCS integration tests:
130 # when the only tag in the repository does not match the pattern.
131 run("git tag --no-sign other -m Annotated")
132 assert from_vcs() == Version("0.0.0", distance=1, dirty=False, branch=b)
133 with pytest.raises(ValueError):
134 from_vcs(strict=True)
135
136 avoid_identical_ref_timestamps()
137 run("git tag --no-sign v0.1.0 -m Annotated")
138 assert from_vcs() == Version("0.1.0", dirty=False, branch=b)
139 assert from_vcs(latest_tag=True) == Version("0.1.0", dirty=False, branch=b)
140 assert run("dunamai from git") == "0.1.0"
141 assert run("dunamai from any") == "0.1.0"
142
143 # Additional one-off checks not in other VCS integration tests:
144 assert run(r'dunamai from any --pattern "(?P<base>\d\.\d\.\d)"') == "0.1.0"
145 run(r'dunamai from any --pattern "(\d\.\d\.\d)"', 1)
146 assert run('dunamai from any --format "v{base}"') == "v0.1.0"
147 assert run('dunamai from any --style "semver"') == "0.1.0"
148 assert (
149 run('dunamai from any --format "v{base}" --style "semver"', 1)
150 == "Version 'v0.1.0' does not conform to the Semantic Versioning style"
151 )
152 assert run("dunamai from any --latest-tag") == "0.1.0"
153 assert from_explicit_vcs(Vcs.Any) == Version("0.1.0", dirty=False, branch=b)
154 assert from_explicit_vcs(Vcs.Git) == Version("0.1.0", dirty=False, branch=b)
155 assert run("dunamai from any --bump") == "0.1.0"
156 assert run('dunamai from git --format "{commit}"') != run('dunamai from git --format "{commit}" --full-commit')
157 assert run('dunamai from any --format "{commit}"') != run('dunamai from any --format "{commit}" --full-commit')
158
159 if not legacy:
160 # Verify tags with '/' work
161 run("git tag --no-sign test/v0.1.0")
162 assert run(r'dunamai from any --pattern "^test/v(?P<base>\d\.\d\.\d)"') == "0.1.0"
163 assert run('dunamai from any --pattern-prefix "test/"') == "0.1.0"
164
165 (tmp_path / "foo.txt").write_text("bye")
166 assert from_vcs() == Version("0.1.0", dirty=True, branch=b)
167
168 run("git add .")
169 run('git commit --no-gpg-sign -m "Second"')
170 assert from_vcs() == Version("0.1.0", distance=1, dirty=False, branch=b)
171 assert from_any_vcs() == Version("0.1.0", distance=1, dirty=False, branch=b)
172
173 run("git tag --no-sign unmatched -m Annotated")
174 assert from_vcs() == Version("0.1.0", distance=1, dirty=False, branch=b)
175 with pytest.raises(ValueError):
176 from_vcs(latest_tag=True)
177
178 # check that we find the expected tag that has the most recent tag creation time
179 if legacy:
180 run("git tag --no-sign -d unmatched")
181 run("git tag --no-sign v0.2.0 -m Annotated")
182 if not legacy:
183 avoid_identical_ref_timestamps()
184 run("git tag --no-sign v0.2.0b1 -m Annotated")
185 avoid_identical_ref_timestamps()
186 run("git tag --no-sign v0.2.0 -m Annotated")
187 avoid_identical_ref_timestamps()
188 run("git tag --no-sign v0.1.1 HEAD~1 -m Annotated")
189 assert from_vcs() == Version("0.2.0", dirty=False, branch=b)
190 assert from_vcs(latest_tag=True) == Version("0.2.0", dirty=False, branch=b)
191
192 # Check handling with identical tag and branch names:
193 run("git checkout -b v0.2.0")
194 assert from_vcs() == Version("0.2.0", dirty=False, branch="heads/v0.2.0")
195 assert from_vcs(latest_tag=True) == Version("0.2.0", dirty=False, branch="heads/v0.2.0")
196
197 if not legacy:
198 run("git checkout v0.1.0")
199 assert from_vcs() == Version("0.1.1", dirty=False)
200 assert from_vcs(latest_tag=True) == Version("0.1.1", dirty=False)
201
202 # Additional one-off check not in other VCS integration tests:
203 run("git checkout master")
204 (tmp_path / "foo.txt").write_text("third")
205 run("git add .")
206 run('git commit --no-gpg-sign -m "Third"')
207 # bumping:
208 commit = run('dunamai from any --format "{commit}"')
209 assert run("dunamai from any --bump") == "0.2.1.dev1+{}".format(commit)
210 if not legacy:
211 # tag with pre-release segment.
212 run("git tag --no-sign v0.2.1b3 -m Annotated")
213 assert from_vcs() == Version("0.2.1", stage=("b", 3), dirty=False, branch=b)
214
215 if not legacy:
216 # Additional one-off check: tag containing comma.
217 (tmp_path / "foo.txt").write_text("fourth")
218 run("git add .")
219 run('git commit --no-gpg-sign -m "Fourth"')
220 run("git tag --no-sign v0.3.0+a,b -m Annotated")
221 assert from_vcs() == Version("0.3.0", dirty=False, tagged_metadata="a,b", branch=b)
222
223 # Checking `highest_tag`
224 run("git checkout master")
225 run("git tag --no-sign v0.2.9 -m Annotated")
226 assert from_vcs(latest_tag=True) == Version("0.2.9", dirty=False, branch=b)
227 assert from_vcs(highest_tag=True) == Version("0.3.0", dirty=False, tagged_metadata="a,b", branch=b)
228
229 if not legacy:
230 assert from_vcs(path=tmp_path) == Version("0.2.9", dirty=False, branch=b)
231
232
233 @pytest.mark.skipif(shutil.which("git") is None, reason="Requires Git")
234 def test__version__from_git__with_lightweight_tags(tmp_path) -> None:
235 run = make_run_callback(tmp_path)
236 from_vcs = make_from_callback(Version.from_git)
237 b = "master"
238 legacy = is_git_legacy()
239
240 with chdir(tmp_path):
241 run("git init")
242 (tmp_path / "foo.txt").write_text("hi")
243 run("git add .")
244 run('git commit --no-gpg-sign -m "Initial commit"')
245
246 run("git tag --no-sign v0.1.0")
247 assert from_vcs() == Version("0.1.0", dirty=False, branch=b)
248 assert from_vcs(latest_tag=True) == Version("0.1.0", dirty=False, branch=b)
249 assert run("dunamai from git") == "0.1.0"
250 assert run("dunamai from any") == "0.1.0"
251
252 (tmp_path / "foo.txt").write_text("bye")
253 run("git add .")
254 avoid_identical_ref_timestamps()
255 run('git commit --no-gpg-sign -m "Second"')
256 assert from_vcs() == Version("0.1.0", distance=1, dirty=False, branch=b)
257 assert from_any_vcs() == Version("0.1.0", distance=1, dirty=False, branch=b)
258
259 (tmp_path / "foo.txt").write_text("again")
260 run("git add .")
261 avoid_identical_ref_timestamps()
262 run('git commit --no-gpg-sign -m "Third"')
263 assert from_vcs() == Version("0.1.0", distance=2, dirty=False, branch=b)
264 assert from_any_vcs() == Version("0.1.0", distance=2, dirty=False, branch=b)
265
266 run("git tag --no-sign v0.2.0")
267 run("git tag --no-sign v0.1.1 HEAD~1")
268 assert from_vcs() == Version("0.2.0", dirty=False, branch=b)
269 assert from_vcs(latest_tag=True) == Version("0.2.0", dirty=False, branch=b)
270
271 if not legacy:
272 run("git checkout v0.1.1")
273 assert from_vcs() == Version("0.1.1", dirty=False)
274 assert from_vcs(latest_tag=True) == Version("0.1.1", dirty=False)
275
276 # Checking `highest_tag`
277 run("git checkout master")
278 run("git tag --no-sign v0.1.2")
279 assert from_vcs(highest_tag=True) == Version("0.2.0", dirty=False, branch=b)
280
281
282 @pytest.mark.skipif(shutil.which("git") is None, reason="Requires Git")
283 def test__version__from_git__with_mixed_tags(tmp_path) -> None:
284 run = make_run_callback(tmp_path)
285 from_vcs = make_from_callback(Version.from_git)
286 b = "master"
287
288 with chdir(tmp_path):
289 run("git init")
290 (tmp_path / "foo.txt").write_text("hi")
291 run("git add .")
292 run('git commit --no-gpg-sign -m "Initial commit"')
293
294 run("git tag --no-sign v0.1.0")
295 (tmp_path / "foo.txt").write_text("hi 2")
296 run("git add .")
297 avoid_identical_ref_timestamps()
298 run('git commit --no-gpg-sign -m "Second"')
299
300 run('git tag v0.2.0 -m "Annotated"')
301 assert from_vcs() == Version("0.2.0", dirty=False, branch=b)
302 assert from_vcs(latest_tag=True) == Version("0.2.0", dirty=False, branch=b)
303
304 (tmp_path / "foo.txt").write_text("hi 3")
305 run("git add .")
306 avoid_identical_ref_timestamps()
307 run('git commit --no-gpg-sign -m "Third"')
308
309 run("git tag --no-sign v0.3.0")
310 assert from_vcs() == Version("0.3.0", dirty=False, branch=b)
311 assert from_vcs(latest_tag=True) == Version("0.3.0", dirty=False, branch=b)
312
313
314 @pytest.mark.skipif(shutil.which("git") is None, reason="Requires Git")
315 def test__version__from_git__with_nonchronological_commits(tmp_path) -> None:
316 run = make_run_callback(tmp_path)
317 from_vcs = make_from_callback(Version.from_git, chronological=False)
318 b = "master"
319 legacy = is_git_legacy()
320
321 with chdir(tmp_path):
322 run("git init")
323 (tmp_path / "foo.txt").write_text("hi")
324 run("git add .")
325 run(
326 'git commit --no-gpg-sign -m "Initial commit"',
327 env={
328 "GIT_COMMITTER_DATE": "2000-01-02T01:00:00",
329 "GIT_AUTHOR_DATE": "2000-01-02T01:00:00",
330 **os.environ,
331 },
332 )
333
334 run("git tag --no-sign v0.1.0")
335 (tmp_path / "foo.txt").write_text("hi 2")
336 run("git add .")
337 avoid_identical_ref_timestamps()
338 run(
339 'git commit --no-gpg-sign -m "Second"',
340 env={
341 "GIT_COMMITTER_DATE": "2000-01-01T01:00:00",
342 "GIT_AUTHOR_DATE": "2000-01-01T01:00:00",
343 **os.environ,
344 },
345 )
346
347 run("git tag --no-sign v0.2.0")
348 if legacy:
349 assert from_vcs() == Version("0.1.0", distance=1, dirty=False, branch=b)
350 else:
351 assert from_vcs() == Version("0.2.0", dirty=False, branch=b)
352
353
354 @pytest.mark.skipif(shutil.which("git") is None, reason="Requires Git")
355 @pytest.mark.skipif(is_git_legacy(), reason="Requires non-legacy Git")
356 def test__version__from_git__gitflow(tmp_path) -> None:
357 run = make_run_callback(tmp_path)
358 from_vcs = make_from_callback(Version.from_git)
359 b = "master"
360 b2 = "develop"
361
362 with chdir(tmp_path):
363 run("git init")
364 (tmp_path / "foo.txt").write_text("hi")
365 run("git add .")
366 run("git commit --no-gpg-sign -m Initial")
367 run("git tag --no-sign v0.1.0 -m Release")
368
369 run("git checkout -b develop")
370 (tmp_path / "foo.txt").write_text("second")
371 run("git add .")
372 run("git commit --no-gpg-sign -m Second")
373
374 run("git checkout -b release/v0.2.0")
375 (tmp_path / "foo.txt").write_text("bugfix")
376 run("git add .")
377 run("git commit --no-gpg-sign -m Bugfix")
378
379 run("git checkout develop")
380 run("git merge --no-gpg-sign --no-ff release/v0.2.0")
381
382 run("git checkout master")
383 run("git merge --no-gpg-sign --no-ff release/v0.2.0")
384
385 run("git tag --no-sign v0.2.0 -m Release")
386 assert from_vcs() == Version("0.2.0", dirty=False, branch=b)
387
388 run("git checkout develop")
389 assert from_vcs() == Version("0.1.0", distance=3, dirty=False, branch=b2)
390 assert run('dunamai from any --format "{base}+{distance}"') == "0.1.0+3"
391 assert from_vcs(tag_branch="master") == Version("0.2.0", distance=1, dirty=False, branch=b2)
392 assert run('dunamai from any --tag-branch master --format "{base}+{distance}"') == "0.2.0+1"
393 assert run('dunamai from git --tag-branch master --format "{base}+{distance}"') == "0.2.0+1"
394
395 (tmp_path / "foo.txt").write_text("feature")
396 run("git add .")
397 run("git commit --no-gpg-sign -m Feature")
398 assert from_vcs() == Version("0.1.0", distance=4, dirty=False, branch=b2)
399 assert from_vcs(tag_branch="master") == Version("0.2.0", distance=2, dirty=False, branch=b2)
400
401 run("git checkout master")
402 assert from_vcs() == Version("0.2.0", dirty=False, branch=b)
403 assert from_vcs(tag_branch="master") == Version("0.2.0", dirty=False, branch=b)
404 assert from_vcs(tag_branch="develop") == Version("0.1.0", distance=3, dirty=False, branch=b)
405
406
407 def test__version__from_git__archival_untagged() -> None:
408 with chdir(REPO / "tests" / "archival" / "git-untagged"):
409 detected = Version.from_git()
410 assert detected == Version(
411 "0.0.0",
412 branch="master",
413 commit="8fe614d",
414 timestamp=dt.datetime(2022, 11, 6, 23, 7, 50, tzinfo=dt.timezone.utc),
415 )
416 assert detected._matched_tag is None
417 assert detected._newer_unmatched_tags is None
418
419 assert Version.from_any_vcs() == detected
420
421 assert Version.from_git(full_commit=True).commit == "8fe614dbf9e767e70442ab8f56e99bd08d7e782d"
422
423 with pytest.raises(RuntimeError):
424 Version.from_git(strict=True)
425
426
427 def test__version__from_git__archival_tagged() -> None:
428 with chdir(REPO / "tests" / "archival" / "git-tagged"):
429 detected = Version.from_git()
430 assert detected == Version(
431 "0.1.0",
432 branch="master",
433 dirty=False,
434 distance=0,
435 commit="8fe614d",
436 timestamp=dt.datetime(2022, 11, 6, 23, 7, 50, tzinfo=dt.timezone.utc),
437 )
438 assert detected._matched_tag == "v0.1.0"
439 assert detected._newer_unmatched_tags == []
440
441
442 def test__version__from_git__archival_tagged_post() -> None:
443 with chdir(REPO / "tests" / "archival" / "git-tagged-post"):
444 detected = Version.from_git()
445 assert detected == Version(
446 "0.1.0",
447 branch="master",
448 dirty=False,
449 distance=1,
450 commit="1b57ff7",
451 timestamp=dt.datetime(2022, 11, 6, 23, 16, 59, tzinfo=dt.timezone.utc),
452 )
453 assert detected._matched_tag == "v0.1.0"
454 assert detected._newer_unmatched_tags == []
455
456
457 @pytest.mark.skipif(shutil.which("git") is None, reason="Requires Git")
458 def test__version__from_git__shallow(tmp_path) -> None:
459 run = make_run_callback(tmp_path)
460
461 with chdir(tmp_path):
462 run("git clone --depth 1 https://github.com/mtkennerly/dunamai.git .")
463 assert Version.from_git().concerns == {Concern.ShallowRepository}
464
465 with pytest.raises(RuntimeError):
466 Version.from_git(strict=True)
467
468
469 @pytest.mark.skipif(shutil.which("git") is None, reason="Requires Git")
470 @pytest.mark.skipif(is_git_legacy(), reason="Requires non-legacy Git")
471 def test__version__from_git__trace_env_var(tmp_path) -> None:
472 run = make_run_callback(tmp_path)
473 env = {**os.environ, "GIT_TRACE": "1"}
474
475 with chdir(tmp_path):
476 run("git init")
477 (tmp_path / "foo.txt").write_text("hi")
478 run("git add .")
479 run("git commit --no-gpg-sign -m Initial")
480 run("git tag --no-sign v0.1.0 -m Release")
481 assert run("dunamai from git", env=env) == "0.1.0"
482
483
484 @pytest.mark.skipif(lacks_git_version([2, 27]), reason="Requires Git 2.27+")
485 def test__version__from_git__exclude_decoration(tmp_path) -> None:
486 run = make_run_callback(tmp_path)
487 from_vcs = make_from_callback(Version.from_git)
488 b = "master"
489
490 with chdir(tmp_path):
491 run("git init")
492 (tmp_path / "foo.txt").write_text("hi")
493 run("git add .")
494 run("git commit --no-gpg-sign -m Initial")
495 run("git tag --no-sign v0.1.0 -m Release")
496 run("git config log.excludeDecoration refs/tags/")
497
498 assert from_vcs() == Version("0.1.0", dirty=False, branch=b)
499
500
501 # Older versions of Git fail with code 128:
502 # "fatal: missing object 0000000000000000000000000000000000000000 for refs/tags/bad.txt"
503 @pytest.mark.skipif(lacks_git_version([2, 7]), reason="Requires Git 2.7+")
504 def test__version__from_git__broken_ref(tmp_path) -> None:
505 run = make_run_callback(tmp_path)
506 from_vcs = make_from_callback(Version.from_git)
507 b = "master"
508
509 with chdir(tmp_path):
510 run("git init")
511 (tmp_path / "foo.txt").write_text("hi")
512 run("git add .")
513 run("git commit --no-gpg-sign -m Initial")
514 run("git tag --no-sign v0.1.0 -m Release")
515 (tmp_path / ".git/refs/tags/bad.txt").touch()
516
517 assert from_vcs() == Version("0.1.0", dirty=False, branch=b)
518
519
520 @pytest.mark.skipif(shutil.which("git") is None, reason="Requires Git")
521 @pytest.mark.skipif(is_git_legacy(), reason="Requires non-legacy Git")
522 def test__version__from_git__initial_commit_empty_and_tagged(tmp_path) -> None:
523 run = make_run_callback(tmp_path)
524
525 with chdir(tmp_path):
526 run("git init")
527 run("git commit --no-gpg-sign --allow-empty -m Initial")
528 run("git tag --no-sign v0.1.0 -m Release")
529 assert run("dunamai from git") == "0.1.0"
530
531 run("git commit --no-gpg-sign --allow-empty -m Second")
532 assert run("dunamai from git").startswith("0.1.0.post1.dev0+")
533
534 run("git tag --no-sign v0.2.0 -m Release")
535 assert run("dunamai from git") == "0.2.0"
536
537 (tmp_path / "foo.txt").write_text("hi")
538 run("git add .")
539 run("git commit --no-gpg-sign -m Third")
540 run("git tag --no-sign v0.3.0 -m Release")
541 assert run("dunamai from git") == "0.3.0"
542
543
544 @pytest.mark.skipif(shutil.which("git") is None, reason="Requires Git")
545 def test__version__not_a_repository(tmp_path) -> None:
546 run = make_run_callback(tmp_path)
547
548 with chdir(tmp_path):
549 assert run("dunamai from git", 1) == "This does not appear to be a Git project"
550
551
552 @pytest.mark.skipif(shutil.which("hg") is None, reason="Requires Mercurial")
553 def test__version__from_mercurial(tmp_path) -> None:
554 run = make_run_callback(tmp_path)
555 from_vcs = make_from_callback(Version.from_mercurial)
556 b = "default"
557
558 with chdir(tmp_path):
559 run("hg init")
560 assert from_vcs(fresh=True) == Version("0.0.0", distance=0, dirty=False, branch=b)
561 assert from_vcs(fresh=True).vcs == Vcs.Mercurial
562
563 (tmp_path / "foo.txt").write_text("hi")
564 assert from_vcs(fresh=True) == Version("0.0.0", distance=0, dirty=True, branch=b)
565
566 run("hg add .")
567 run('hg commit -m "Initial commit"')
568 assert from_vcs() == Version("0.0.0", distance=1, dirty=False, branch=b)
569 assert run('dunamai from mercurial --format "{commit}"') != run(
570 'dunamai from mercurial --format "{commit}" --full-commit'
571 )
572 assert run('dunamai from any --format "{commit}"') != run('dunamai from any --format "{commit}" --full-commit')
573
574 run("hg tag v0.1.0")
575 assert from_vcs() == Version("0.1.0", dirty=False, branch=b)
576 assert from_vcs(latest_tag=True) == Version("0.1.0", dirty=False, branch=b)
577 assert run("dunamai from mercurial") == "0.1.0"
578 assert run("dunamai from any") == "0.1.0"
579
580 (tmp_path / "foo.txt").write_text("bye")
581 assert from_vcs() == Version("0.1.0", dirty=True, branch=b)
582
583 run("hg add .")
584 run('hg commit -m "Second"')
585 assert from_vcs() == Version("0.1.0", distance=1, dirty=False, branch=b)
586 assert from_any_vcs() == Version("0.1.0", distance=1, dirty=False, branch=b)
587
588 run("hg tag unmatched")
589 assert from_vcs() == Version("0.1.0", distance=2, dirty=False, branch=b)
590 with pytest.raises(ValueError):
591 from_vcs(latest_tag=True)
592
593 run("hg tag v0.2.0")
594 assert from_vcs() == Version("0.2.0", dirty=False, branch=b)
595 assert from_vcs(latest_tag=True) == Version("0.2.0", dirty=False, branch=b)
596
597 run('hg tag v0.1.1 -r "tag(v0.1.0)"')
598 assert from_vcs() == Version("0.2.0", distance=1, dirty=False, branch=b)
599 assert from_vcs(latest_tag=True) == Version("0.2.0", distance=1, dirty=False, branch=b)
600
601 run("hg checkout v0.1.0")
602 assert from_vcs() == Version("0.1.0", dirty=False, branch=b)
603 assert from_vcs(latest_tag=True) == Version("0.1.0", dirty=False, branch=b)
604
605 assert from_vcs(path=tmp_path) == Version("0.1.0", dirty=False, branch=b)
606
607
608 def test__version__from_mercurial__archival_untagged() -> None:
609 with chdir(REPO / "tests" / "archival" / "hg-untagged"):
610 detected = Version.from_mercurial()
611 assert detected == Version(
612 "0.0.0",
613 branch="default",
614 commit="25e474af1332ed4fff9351c70ef8f36352c013f2",
615 )
616 assert detected._matched_tag is None
617 assert detected._newer_unmatched_tags is None
618
619 assert Version.from_any_vcs() == detected
620
621 with pytest.raises(RuntimeError):
622 Version.from_mercurial(strict=True)
623
624
625 def test__version__from_mercurial__archival_tagged() -> None:
626 with chdir(REPO / "tests" / "archival" / "hg-tagged"):
627 detected = Version.from_mercurial()
628 assert detected == Version(
629 "0.1.1",
630 branch="default",
631 commit="cf36273384e558411364a3a973aaa0cc08e48aea",
632 )
633 assert detected._matched_tag == "v0.1.1"
634 assert detected._newer_unmatched_tags == ["foo bar"]
635
636
637 @pytest.mark.skipif(shutil.which("darcs") is None, reason="Requires Darcs")
638 def test__version__from_darcs(tmp_path) -> None:
639 run = make_run_callback(tmp_path)
640 from_vcs = make_from_callback(Version.from_darcs)
641
642 with chdir(tmp_path):
643 run("darcs init")
644 assert from_vcs(fresh=True) == Version("0.0.0", distance=0, dirty=False)
645 assert from_vcs(fresh=True).vcs == Vcs.Darcs
646
647 (tmp_path / "foo.txt").write_text("hi")
648 assert from_vcs(fresh=True) == Version("0.0.0", distance=0, dirty=True)
649
650 run("darcs add foo.txt")
651 run('darcs record -am "Initial commit"')
652 assert from_vcs() == Version("0.0.0", distance=1, dirty=False)
653
654 run("darcs tag v0.1.0")
655 assert from_vcs() == Version("0.1.0", dirty=False)
656 assert from_vcs(latest_tag=True) == Version("0.1.0", dirty=False)
657 assert run("dunamai from darcs") == "0.1.0"
658 assert run("dunamai from any") == "0.1.0"
659
660 (tmp_path / "foo.txt").write_text("bye")
661 assert from_vcs() == Version("0.1.0", dirty=True)
662
663 run('darcs record -am "Second"')
664 assert from_vcs() == Version("0.1.0", distance=1, dirty=False)
665 assert from_any_vcs() == Version("0.1.0", distance=1, dirty=False)
666
667 run("darcs tag unmatched")
668 assert from_vcs() == Version("0.1.0", distance=2, dirty=False)
669 with pytest.raises(ValueError):
670 from_vcs(latest_tag=True)
671
672 run("darcs tag v0.2.0")
673 assert from_vcs() == Version("0.2.0", dirty=False)
674
675 run("darcs obliterate --all --last 3")
676 assert from_vcs() == Version("0.1.0", dirty=False)
677
678 assert from_vcs(path=tmp_path) == Version("0.1.0", dirty=False)
679
680
681 @pytest.mark.skipif(None in [shutil.which("svn"), shutil.which("svnadmin")], reason="Requires Subversion")
682 def test__version__from_subversion(tmp_path) -> None:
683 vcs = tmp_path / "dunamai-svn"
684 vcs.mkdir()
685 run = make_run_callback(vcs)
686 from_vcs = make_from_callback(Version.from_subversion, clear=False)
687
688 vcs_srv = tmp_path / "dunamai-svn-srv"
689 vcs_srv.mkdir()
690 run_srv = make_run_callback(vcs_srv)
691 vcs_srv_uri = vcs_srv.as_uri()
692
693 with chdir(vcs_srv):
694 run_srv("svnadmin create .")
695
696 with chdir(vcs):
697 run('svn checkout "{}" .'.format(vcs_srv_uri))
698 assert from_vcs(fresh=True) == Version("0.0.0", distance=0, dirty=False)
699 assert from_vcs(fresh=True).vcs == Vcs.Subversion
700
701 run("svn mkdir trunk tags")
702
703 # No tags yet, so version should be 0.0.0.
704 assert from_vcs(fresh=True) == Version("0.0.0", distance=0, dirty=True)
705
706 run("svn add --force .")
707 run('svn commit -m "Initial commit"') # commit 1
708 run("svn update")
709
710 # A single commit, but still no tags. Version should be 0.0.0.
711 assert from_vcs() == Version("0.0.0", distance=1, commit="1", dirty=False)
712
713 with chdir(vcs / "trunk"):
714 # ^-- Make sure things work when we're in trunk, too.
715 Path("foo.txt").write_text("hi")
716 run("svn add --force .")
717 run('svn commit -m "Initial foo.txt commit"') # commit 2
718 run("svn update")
719
720 # Two commits, but still no tag. Version should still be 0.0.0.
721 assert from_vcs() == Version("0.0.0", distance=2, commit="2", dirty=False)
722
723 run('svn copy {0}/trunk {0}/tags/v0.1.0 -m "Tag 1"'.format(vcs_srv_uri)) # commit 3 and first tag!
724 run("svn update")
725
726 # 3 commits, one tag (v.0.1.0), version should be 0.1.0.
727 assert from_vcs() == Version("0.1.0", commit="3", dirty=False)
728 assert run("dunamai from subversion") == "0.1.0"
729 assert run("dunamai from any") == "0.1.0"
730
731 # Dirty the working directory. Make sure we identify it as such.
732 (vcs / "trunk" / "foo.txt").write_text("bye")
733 assert from_vcs() == Version("0.1.0", commit="3", dirty=True)
734
735 # Fourth commit, still just one tag. Version should be 0.1.0, and dirty flag
736 # should be reset.
737 run('svn commit -m "Fourth"') # commit 4
738 run("svn update")
739 assert from_vcs() == Version("0.1.0", distance=1, commit="4", dirty=False)
740 assert from_any_vcs_unmocked() == Version("0.1.0", distance=1, commit="4", dirty=False)
741
742 # Ensure we get the tag based on the highest commit, not necessarily
743 # just the newest tag.
744 run('svn copy {0}/trunk {0}/tags/v0.2.0 -m "Tag 2"'.format(vcs_srv_uri)) # commit 5
745 run('svn copy {0}/trunk {0}/tags/v0.1.1 -r 1 -m "Tag 3"'.format(vcs_srv_uri)) # commit 6
746 run("svn update")
747 assert from_vcs() == Version("0.2.0", distance=1, commit="6", dirty=False)
748 assert from_vcs(latest_tag=True) == Version("0.2.0", distance=1, commit="6", dirty=False)
749
750 run('svn copy {0}/trunk {0}/tags/unmatched -m "Tag 4"'.format(vcs_srv_uri)) # commit 7
751 run("svn update")
752 assert from_vcs() == Version("0.2.0", distance=2, commit="7", dirty=False)
753 with pytest.raises(ValueError):
754 from_vcs(latest_tag=True)
755
756 # Checkout an earlier commit. Commit 2 occurred before the first tag
757 # (v0.1.0, commit #3), so version should be 0.0.0.
758 run("svn update -r 2")
759 assert from_vcs() == Version("0.0.0", distance=2, commit="2", dirty=False)
760 assert from_vcs(latest_tag=True) == Version("0.0.0", distance=2, commit="2", dirty=False)
761
762 with chdir(vcs / "trunk"):
763 # Do this in trunk, to make sure things still work there.
764 # Checkout an earlier commit. Commit 3 was first tag (v0.1.0, commit
765 # #3), so version should be 0.1.0.
766 run("svn update -r 3")
767 assert from_vcs() == Version("0.1.0", distance=0, commit="3", dirty=False)
768 assert from_vcs(latest_tag=True) == Version("0.1.0", distance=0, commit="3", dirty=False)
769
770 assert from_vcs(path=vcs) == Version("0.1.0", distance=0, commit="3", dirty=False)
771
772
773 @pytest.mark.skipif(shutil.which("bzr") is None, reason="Requires Bazaar")
774 def test__version__from_bazaar(tmp_path) -> None:
775 vcs = tmp_path / "dunamai-bzr"
776 vcs.mkdir()
777 run = make_run_callback(vcs)
778 from_vcs = make_from_callback(Version.from_bazaar, clear=False)
779 b = "dunamai-bzr"
780
781 with chdir(vcs):
782 run("bzr init")
783 assert from_vcs(fresh=True) == Version("0.0.0", distance=0, dirty=False)
784 assert from_vcs(fresh=True).vcs == Vcs.Bazaar
785
786 (vcs / "foo.txt").write_text("hi")
787 assert from_vcs(fresh=True) == Version("0.0.0", distance=0, dirty=True)
788
789 run("bzr add .")
790 run('bzr commit -m "Initial commit"')
791 assert from_vcs() == Version("0.0.0", distance=1, commit="1", dirty=False, branch=b)
792
793 run("bzr tag v0.1.0")
794 assert from_vcs() == Version("0.1.0", commit="1", dirty=False, branch=b)
795 assert from_vcs(latest_tag=True) == Version("0.1.0", commit="1", dirty=False, branch=b)
796 assert run("dunamai from bazaar") == "0.1.0"
797 assert run("dunamai from any") == "0.1.0"
798
799 (vcs / "foo.txt").write_text("bye")
800 assert from_vcs() == Version("0.1.0", commit="1", dirty=True, branch=b)
801
802 run("bzr add .")
803 run('bzr commit -m "Second"')
804 assert from_vcs() == Version("0.1.0", distance=1, commit="2", dirty=False, branch=b)
805 assert from_any_vcs_unmocked() == Version("0.1.0", distance=1, commit="2", dirty=False, branch=b)
806
807 run("bzr tag unmatched")
808 assert from_vcs() == Version("0.1.0", distance=1, commit="2", dirty=False, branch=b)
809 with pytest.raises(ValueError):
810 from_vcs(latest_tag=True)
811
812 run("bzr tag v0.2.0")
813 run("bzr tag v0.1.1 -r v0.1.0")
814 assert from_vcs() == Version("0.2.0", commit="2", dirty=False, branch=b)
815 assert from_vcs(latest_tag=True) == Version("0.2.0", commit="2", dirty=False, branch=b)
816
817 run("bzr checkout . old -r v0.1.0")
818
819 with chdir(vcs / "old"):
820 assert from_vcs() == Version("0.1.1", commit="1", dirty=False, branch=b)
821 assert from_vcs(latest_tag=True) == Version("0.1.1", commit="1", dirty=False, branch=b)
822
823 with chdir(vcs):
824 shutil.rmtree("old")
825 run("bzr nick renamed")
826 assert from_vcs() == Version("0.2.0", commit="2", dirty=False, branch=b)
827 (vcs / "foo.txt").write_text("branched")
828 run("bzr add .")
829 run('bzr commit -m "branched"')
830 assert from_vcs() == Version("0.2.0", distance=1, commit="3", dirty=False, branch="renamed")
831 run("bzr tag v0.2.1")
832 assert from_vcs() == Version("0.2.1", commit="3", dirty=False, branch="renamed")
833
834 assert from_vcs(path=vcs) == Version("0.2.1", commit="3", dirty=False, branch="renamed")
835
836
837 @pytest.mark.skipif(shutil.which("fossil") is None, reason="Requires Fossil")
838 def test__version__from_fossil(tmp_path) -> None:
839 run = make_run_callback(tmp_path)
840 from_vcs = make_from_callback(Version.from_fossil)
841 b = "trunk"
842
843 if sys.platform != "win32":
844 set_missing_env("FOSSIL_HOME", str(REPO / "tests"), ["HOME", "XDG_CONFIG_HOME"])
845 set_missing_env("USER", "dunamai")
846
847 with chdir(tmp_path):
848 run("fossil init repo")
849 run("fossil open repo --force")
850 assert from_vcs() == Version("0.0.0", distance=0, dirty=False, branch=b)
851 assert from_vcs().vcs == Vcs.Fossil
852
853 (tmp_path / "foo.txt").write_text("hi")
854 assert from_vcs() == Version("0.0.0", distance=0, dirty=True, branch=b)
855
856 run("fossil add .")
857 run('fossil commit -m "Initial commit"')
858 assert from_vcs() == Version("0.0.0", distance=1, dirty=False, branch=b)
859
860 run("fossil tag add v0.1.0 trunk")
861 assert from_vcs() == Version("0.1.0", dirty=False, branch=b)
862 assert from_vcs(latest_tag=True) == Version("0.1.0", dirty=False, branch=b)
863 assert run("dunamai from fossil") == "0.1.0"
864 assert run("dunamai from any") == "0.1.0"
865
866 (tmp_path / "foo.txt").write_text("bye")
867 assert from_vcs() == Version("0.1.0", dirty=True, branch=b)
868
869 run("fossil add .")
870 run('fossil commit -m "Second"')
871 assert from_vcs() == Version("0.1.0", distance=1, dirty=False, branch=b)
872 assert from_any_vcs() == Version("0.1.0", distance=1, dirty=False, branch=b)
873
874 run("fossil tag add unmatched trunk")
875 assert from_vcs() == Version("0.1.0", distance=1, dirty=False, branch=b)
876 with pytest.raises(ValueError):
877 from_vcs(latest_tag=True)
878
879 (tmp_path / "foo.txt").write_text("third")
880 run("fossil add .")
881 run("fossil commit --tag v0.2.0 -m 'Third'")
882 assert from_vcs() == Version("0.2.0", dirty=False, branch=b)
883 assert from_vcs(latest_tag=True) == Version("0.2.0", dirty=False, branch=b)
884
885 run("fossil tag add v0.1.1 v0.1.0")
886 assert from_vcs() == Version("0.2.0", dirty=False, branch=b)
887 assert from_vcs(latest_tag=True) == Version("0.2.0", dirty=False, branch=b)
888
889 run("fossil checkout v0.1.0")
890 assert from_vcs() == Version("0.1.1", dirty=False, branch=b)
891 assert from_vcs(latest_tag=True) == Version("0.1.1", dirty=False, branch=b)
892
893 assert from_vcs(path=tmp_path) == Version("0.1.1", dirty=False, branch=b)
894
895
896 @pytest.mark.skipif(shutil.which("pijul") is None, reason="Requires Pijul")
897 def test__version__from_pijul(tmp_path) -> None:
898 run = make_run_callback(tmp_path)
899 from_vcs = make_from_callback(Version.from_pijul)
900 b = "main"
901
902 with chdir(tmp_path):
903 run("pijul init")
904 assert from_vcs(fresh=True) == Version("0.0.0", distance=0, dirty=False, branch=b)
905 assert from_vcs(fresh=True).vcs == Vcs.Pijul
906
907 (tmp_path / "foo.txt").write_text("hi")
908 assert from_vcs(fresh=True) == Version("0.0.0", distance=0, dirty=False, branch=b)
909
910 run("pijul add foo.txt")
911 assert from_vcs(fresh=True) == Version("0.0.0", distance=0, dirty=True, branch=b)
912 run('pijul record . -am "Initial commit"')
913 assert from_vcs() == Version("0.0.0", distance=1, dirty=False, branch=b)
914
915 run("pijul tag create -m v0.1.0")
916 assert from_vcs() == Version("0.1.0", dirty=False, branch=b)
917 assert from_vcs(latest_tag=True) == Version("0.1.0", dirty=False, branch=b)
918 assert run("dunamai from pijul") == "0.1.0"
919 assert run("dunamai from any") == "0.1.0"
920
921 (tmp_path / "foo.txt").write_text("bye")
922 assert from_vcs() == Version("0.1.0", dirty=True, branch=b)
923
924 run('pijul record . -am "Second"')
925 assert from_vcs() == Version("0.1.0", distance=1, dirty=False, branch=b)
926 assert from_any_vcs() == Version("0.1.0", distance=1, dirty=False, branch=b)
927
928 run("pijul tag create -m unmatched")
929 assert from_vcs() == Version("0.1.0", distance=1, dirty=False, branch=b)
930 with pytest.raises(ValueError):
931 from_vcs(latest_tag=True)
932
933 assert from_vcs(path=tmp_path) == Version("0.1.0", distance=1, dirty=False, branch=b)