1
0
Fork 0
Code Issues Pull requests Projects Releases 2 Packages Wiki Activity Actions Pages
DocForge2/tests/test_milestone5_fresh_clone.py

110 lines
3.7 KiB
Python

from __future__ import annotations
import subprocess
import tempfile
import unittest
from pathlib import Path
from tools.milestone5_fresh_clone import (
COMMIT_PATTERN,
EXPECTED_LEGACY_TAG_COMMIT,
EXPECTED_LEGACY_TAG_OBJECT,
EXPECTED_ORIGINS,
LEGACY_MIGRATION_TAG,
PUBLIC_REPOSITORY,
ROOT,
FreshCloneError,
obtain_legacy_tag,
validate_legacy_tag,
)
class Milestone5FreshCloneTests(unittest.TestCase):
def test_public_clone_route_has_no_embedded_credentials(self) -> None:
self.assertEqual(
"https://repo.andraxion.net/administrator/DocForge2.git",
PUBLIC_REPOSITORY,
)
self.assertNotIn("@", PUBLIC_REPOSITORY)
self.assertIn(PUBLIC_REPOSITORY, EXPECTED_ORIGINS)
def test_release_commit_requires_one_full_lowercase_sha1(self) -> None:
self.assertIsNotNone(COMMIT_PATTERN.fullmatch("a" * 40))
for invalid in ("a" * 39, "A" * 40, "main", "v1.4.0", "../" + "a" * 40):
with self.subTest(invalid=invalid):
self.assertIsNone(COMMIT_PATTERN.fullmatch(invalid))
def test_frozen_legacy_tag_identity_is_exact(self) -> None:
self.assertEqual("v1.0.0", LEGACY_MIGRATION_TAG)
self.assertEqual(
EXPECTED_LEGACY_TAG_OBJECT,
subprocess.run(
["git", "rev-parse", LEGACY_MIGRATION_TAG],
cwd=ROOT,
check=True,
capture_output=True,
text=True,
).stdout.strip(),
)
self.assertEqual(
EXPECTED_LEGACY_TAG_COMMIT,
subprocess.run(
["git", "rev-parse", f"{LEGACY_MIGRATION_TAG}^{{commit}}"],
cwd=ROOT,
check=True,
capture_output=True,
text=True,
).stdout.strip(),
)
def test_tagless_clone_obtains_and_verifies_only_frozen_legacy_tag(self) -> None:
with tempfile.TemporaryDirectory() as directory_name:
clone = Path(directory_name) / "clone"
subprocess.run(
["git", "clone", "--no-tags", str(ROOT), str(clone)],
check=True,
capture_output=True,
text=True,
)
initial_tags = subprocess.run(
["git", "tag", "--list"],
cwd=clone,
check=True,
capture_output=True,
text=True,
).stdout.splitlines()
self.assertEqual([], initial_tags)
_, tag_object, tag_commit = obtain_legacy_tag(clone)
self.assertEqual(EXPECTED_LEGACY_TAG_OBJECT, tag_object)
self.assertEqual(EXPECTED_LEGACY_TAG_COMMIT, tag_commit)
fetched_tags = subprocess.run(
["git", "tag", "--list"],
cwd=clone,
check=True,
capture_output=True,
text=True,
).stdout.splitlines()
self.assertEqual([LEGACY_MIGRATION_TAG], fetched_tags)
def test_legacy_tag_validation_rejects_substitution(self) -> None:
valid = {
"object_type": "tag",
"tag_object": EXPECTED_LEGACY_TAG_OBJECT,
"tag_commit": EXPECTED_LEGACY_TAG_COMMIT,
}
invalid_cases = (
("lightweight tag", {"object_type": "commit"}),
("moved tag object", {"tag_object": "a" * 40}),
("moved tag commit", {"tag_commit": "b" * 40}),
)
for label, replacement in invalid_cases:
with self.subTest(label=label):
values = valid | replacement
with self.assertRaises(FreshCloneError):
validate_legacy_tag(**values)
if __name__ == "__main__":
unittest.main()