Every platform-independent stdlib/extension release now ships the same reviewed file set as both a deterministic tar.gz (Linux/POSIX convention) and a deterministic zip (Windows convention) with per-format sha256 sidecars. The zip uses fixed DOS timestamps, create_system=3, regular-file 0644 attributes, UTF-8 name flags and deterministic deflate; both archives are double-built, verified as identical on disk to the exact commit blobs, and cross-checked against each other. Existing releases stay immutable; the rule applies to new versions. Tests updated for the dual-format artifact contract. The website projection is extended in a follow-up commit to classify .zip as an archive.
357 lines
16 KiB
Python
357 lines
16 KiB
Python
"""Contract tests for the single component-release implementation (marqdo#96 M1).
|
|
|
|
The kit is exercised through its real packaging path with temporary Git
|
|
repositories. Network paths are replaced by fakes; the deterministic archive,
|
|
evidence schema and publish transaction ordering are real code.
|
|
"""
|
|
|
|
import importlib.util
|
|
import io
|
|
import json
|
|
import os
|
|
import subprocess
|
|
import sys
|
|
import tarfile
|
|
import tempfile
|
|
import unittest
|
|
from pathlib import Path
|
|
from unittest.mock import patch
|
|
|
|
ROOT = Path(__file__).resolve().parents[2]
|
|
SCRIPT = ROOT / "scripts" / "component-release" / "release.py"
|
|
SPEC = importlib.util.spec_from_file_location("component_release", SCRIPT)
|
|
kit = importlib.util.module_from_spec(SPEC)
|
|
sys.modules["component_release"] = kit
|
|
SPEC.loader.exec_module(kit)
|
|
|
|
GIT_ENV = {
|
|
"GIT_AUTHOR_NAME": "test",
|
|
"GIT_AUTHOR_EMAIL": "test@example.invalid",
|
|
"GIT_COMMITTER_NAME": "test",
|
|
"GIT_COMMITTER_EMAIL": "test@example.invalid",
|
|
}
|
|
|
|
|
|
def git(cwd, *args):
|
|
env = os.environ.copy()
|
|
env.update(GIT_ENV)
|
|
result = subprocess.run(["git", "-C", str(cwd), *args], capture_output=True, env=env)
|
|
if result.returncode != 0:
|
|
raise AssertionError(result.stderr.decode(errors="replace"))
|
|
return result.stdout.decode().strip()
|
|
|
|
|
|
def write(path, data):
|
|
path.parent.mkdir(parents=True, exist_ok=True)
|
|
path.write_bytes(data if isinstance(data, bytes) else data.encode())
|
|
|
|
|
|
def repository(tmp: Path) -> str:
|
|
repo = tmp / "repo"
|
|
repo.mkdir()
|
|
git(repo, "init", "-q")
|
|
write(repo / "modules" / "text.mq.md", "# text\n")
|
|
write(repo / "modules" / "text" / "utf8.mq.md", "# utf8\n")
|
|
write(repo / "tests" / "text.mq.md", "# dev-only test\n")
|
|
write(repo / "README.md", "# component\n")
|
|
write(repo / "scripts" / "test.sh", "#!/bin/sh\n")
|
|
git(repo, "add", "-A")
|
|
git(repo, "commit", "-q", "-m", "fixture")
|
|
return git(repo, "rev-parse", "HEAD")
|
|
|
|
|
|
class PackageTests(unittest.TestCase):
|
|
def setUp(self):
|
|
self._tmp = tempfile.TemporaryDirectory()
|
|
self.tmp = Path(self._tmp.name)
|
|
self.repo = self.tmp / "repo"
|
|
self.sha = repository(self.tmp)
|
|
|
|
def tearDown(self):
|
|
self._tmp.cleanup()
|
|
|
|
def package(self, out: str, allow=("modules", "README.md")):
|
|
target = self.tmp / out
|
|
target.mkdir()
|
|
return kit.package_assets(self.repo, self.sha, "cflmy/marqdo-std-text",
|
|
"std-text", "0.1.0", "stdlib", list(allow), target)
|
|
|
|
@staticmethod
|
|
def artifact(result, extension):
|
|
return next(item for item in result["artifacts"] if item["format"] == extension)
|
|
|
|
def test_same_commit_packages_identical_bytes(self):
|
|
first = self.package("a")
|
|
second = self.package("b")
|
|
self.assertEqual([item["format"] for item in first["artifacts"]], ["tar.gz", "zip"])
|
|
for extension in ("tar.gz", "zip"):
|
|
left = self.artifact(first, extension)
|
|
right = self.artifact(second, extension)
|
|
self.assertEqual(left["sha256"], right["sha256"])
|
|
self.assertEqual(left["path"].read_bytes(), right["path"].read_bytes())
|
|
self.assertEqual(left["checksum"].read_text(),
|
|
f"{left['sha256']} {left['name']}\n")
|
|
self.assertEqual(self.artifact(first, "tar.gz")["name"],
|
|
"marqdo-std-text-0.1.0-source.tar.gz")
|
|
self.assertEqual(self.artifact(first, "zip")["name"],
|
|
"marqdo-std-text-0.1.0-source.zip")
|
|
|
|
def test_archive_contains_only_released_paths(self):
|
|
result = self.package("out")
|
|
targz = self.artifact(result, "tar.gz")
|
|
with tarfile.open(targz["path"], "r:gz") as archive:
|
|
names = sorted(member.name for member in archive.getmembers())
|
|
self.assertEqual(names, [
|
|
"marqdo-std-text-0.1.0/README.md",
|
|
"marqdo-std-text-0.1.0/modules/text.mq.md",
|
|
"marqdo-std-text-0.1.0/modules/text/utf8.mq.md",
|
|
])
|
|
self.assertEqual(result["files"], 3)
|
|
self.assertNotIn("tests/text.mq.md", " ".join(names))
|
|
self.assertNotIn(".git", " ".join(names))
|
|
import zipfile
|
|
with zipfile.ZipFile(self.artifact(result, "zip")["path"]) as archive:
|
|
zipped = sorted(info.filename for info in archive.infolist())
|
|
self.assertEqual(zipped, names)
|
|
|
|
def test_archive_ignores_git_export_attributes(self):
|
|
write(self.repo / ".gitattributes", "modules export-ignore\n")
|
|
git(self.repo, "add", "-A")
|
|
git(self.repo, "commit", "-q", "-m", "attributes")
|
|
sha = git(self.repo, "rev-parse", "HEAD")
|
|
out = self.tmp / "attrout"
|
|
out.mkdir()
|
|
result = kit.package_assets(self.repo, sha, "cflmy/marqdo-std-text",
|
|
"std-text", "0.1.0", "stdlib",
|
|
["modules", "README.md"], out)
|
|
with tarfile.open(self.artifact(result, "tar.gz")["path"], "r:gz") as archive:
|
|
names = sorted(member.name for member in archive.getmembers())
|
|
self.assertIn("marqdo-std-text-0.1.0/modules/text/utf8.mq.md", names)
|
|
import zipfile
|
|
with zipfile.ZipFile(self.artifact(result, "zip")["path"]) as archive:
|
|
self.assertIn("marqdo-std-text-0.1.0/modules/text/utf8.mq.md",
|
|
archive.namelist())
|
|
|
|
def test_refuses_case_and_nfc_collisions(self):
|
|
write(self.repo / "modules" / "Case.mq.md", "a")
|
|
write(self.repo / "modules" / "case.mq.md", "b")
|
|
git(self.repo, "add", "-A")
|
|
git(self.repo, "commit", "-q", "-m", "collision")
|
|
sha = git(self.repo, "rev-parse", "HEAD")
|
|
with self.assertRaises(kit.ReleaseError):
|
|
kit.released_files(self.repo, sha, ["modules"])
|
|
|
|
def test_refuses_backslash_member(self):
|
|
payload = io.BytesIO()
|
|
with tarfile.open(fileobj=payload, mode="w:gz") as archive:
|
|
info = tarfile.TarInfo("prefix/modules/..\\..\\escape.mq.md")
|
|
body = b"x"
|
|
info.size = len(body)
|
|
archive.addfile(info, io.BytesIO(body))
|
|
with self.assertRaises(kit.ReleaseError):
|
|
kit.check_archive(payload.getvalue(), "prefix", [("modules/a.mq.md", b"x")])
|
|
|
|
def test_refuses_unsafe_or_empty_paths(self):
|
|
with self.assertRaises(kit.ReleaseError):
|
|
self.package("bad", allow=("../escape",))
|
|
with self.assertRaises(kit.ReleaseError):
|
|
self.package("bad2", allow=("missing",))
|
|
|
|
def test_refuses_archive_member_outside_released_tree(self):
|
|
payload = io.BytesIO()
|
|
with tarfile.open(fileobj=payload, mode="w:gz") as archive:
|
|
info = tarfile.TarInfo("prefix/tests/secret.mq.md")
|
|
body = b"x"
|
|
info.size = len(body)
|
|
archive.addfile(info, io.BytesIO(body))
|
|
with self.assertRaises(kit.ReleaseError):
|
|
kit.check_archive(payload.getvalue(), "prefix", [("modules/a.mq.md", b"x")])
|
|
|
|
|
|
class IdentityTests(unittest.TestCase):
|
|
def test_version_and_channel(self):
|
|
self.assertEqual(kit.validate_version("v0.1.0"), "0.1.0")
|
|
self.assertEqual(kit.validate_version("v0.1.0-rc.1"), "0.1.0-rc.1")
|
|
for tag in ("0.1.0", "v1", "v1.2", "v01.2.3", "v1.2.3-"):
|
|
with self.assertRaises(kit.ReleaseError):
|
|
kit.validate_version(tag)
|
|
|
|
def test_dependency_spec(self):
|
|
self.assertEqual(
|
|
kit.parse_dependency("id=cli,source_sha=" + "a" * 40),
|
|
{"id": "cli", "source_sha": "a" * 40},
|
|
)
|
|
self.assertEqual(
|
|
kit.parse_dependency("id=cli,source_sha=" + "a" * 40 + ",asset_sha256=" + "b" * 64),
|
|
{"id": "cli", "source_sha": "a" * 40, "asset_sha256": "b" * 64},
|
|
)
|
|
for raw in ("cli", "id=cli", "id=cli,source_sha=" + "A" * 40,
|
|
"id=cli,source_sha=" + "a" * 40 + ",asset_sha256=short",
|
|
"id=cli,id=cli,source_sha=" + "a" * 40,
|
|
"id=cli,source_sha=" + "a" * 40 + ",unknown=1"):
|
|
with self.assertRaises(kit.ReleaseError):
|
|
kit.parse_dependency(raw)
|
|
|
|
|
|
class EvidenceTests(unittest.TestCase):
|
|
ASSETS = [{"name": "a.tar.gz", "size": 3, "sha256": "a" * 64}]
|
|
|
|
def evidence(self, **overrides):
|
|
data = dict(
|
|
repository="cflmy/marqdo-std-text",
|
|
component_id="std-text",
|
|
tag="v0.1.0",
|
|
source_sha="b" * 40,
|
|
ci={"pipeline_number": 1, "url": "u", "source_sha": "b" * 40},
|
|
assets=self.ASSETS,
|
|
dependencies=[{"id": "cli", "source_sha": "c" * 40}],
|
|
)
|
|
data.update(overrides)
|
|
return kit.build_evidence(**data)
|
|
|
|
def test_shape(self):
|
|
evidence = self.evidence()
|
|
self.assertEqual(evidence["assets"], [
|
|
{"name": "a.tar.gz", "size": 3, "target": "source", "sha256": "a" * 64}])
|
|
self.assertEqual(evidence["dependencies"], [{"id": "cli", "source_sha": "c" * 40}])
|
|
|
|
def test_guards(self):
|
|
with self.assertRaises(kit.ReleaseError):
|
|
self.evidence(assets=[{"name": "release-evidence.json", "size": 3,
|
|
"sha256": "a" * 64, "target": "source"}])
|
|
with self.assertRaises(kit.ReleaseError):
|
|
self.evidence(dependencies=[{"id": "std-text", "source_sha": "c" * 40}])
|
|
with self.assertRaises(kit.ReleaseError):
|
|
self.evidence(dependencies=[{"id": "cli", "source_sha": "c" * 40},
|
|
{"id": "cli", "source_sha": "d" * 40}])
|
|
with self.assertRaises(kit.ReleaseError):
|
|
self.evidence(assets=[])
|
|
|
|
|
|
class PublishTransactionTests(unittest.TestCase):
|
|
def setUp(self):
|
|
self._tmp = tempfile.TemporaryDirectory()
|
|
self.tmp = Path(self._tmp.name)
|
|
self.archive = self.tmp / "a.tar.gz"
|
|
self.archive.write_bytes(b"payload")
|
|
self.checksum = self.tmp / "a.tar.gz.sha256"
|
|
self.checksum.write_text("digest a.tar.gz\n")
|
|
self.zip = self.tmp / "a.zip"
|
|
self.zip.write_bytes(b"zippayload")
|
|
self.zip_checksum = self.tmp / "a.zip.sha256"
|
|
self.zip_checksum.write_text("digest a.zip\n")
|
|
self.index = self.tmp / "release-evidence.json"
|
|
self.index.write_text("{}\n")
|
|
self.assets = [self.archive, self.checksum, self.zip, self.zip_checksum, self.index]
|
|
self.release = {"id": 7, "tag_name": "v0.1.0", "draft": False, "prerelease": False,
|
|
"html_url": "https://git.cflmy.cn/cflmy/marqdo-std-text/releases/tag/v0.1.0"}
|
|
|
|
def tearDown(self):
|
|
self._tmp.cleanup()
|
|
|
|
def publish(self):
|
|
return kit.publish_release(
|
|
repository="cflmy/marqdo-std-text", tag="v0.1.0",
|
|
source_sha="a" * 40, body="body", prerelease=False,
|
|
assets=self.assets, token="token")
|
|
|
|
def test_identical_published_release_is_idempotent(self):
|
|
with patch.object(kit, "find_release", return_value=self.release), \
|
|
patch.object(kit, "published_matches", return_value=True):
|
|
result = self.publish()
|
|
self.assertEqual(result["status"], "idempotent")
|
|
self.assertEqual(result["release_id"], 7)
|
|
|
|
def test_different_published_bytes_are_refused(self):
|
|
with patch.object(kit, "find_release", return_value=self.release), \
|
|
patch.object(kit, "published_matches", return_value=False):
|
|
with self.assertRaises(kit.ReleaseError):
|
|
self.publish()
|
|
|
|
def test_evidence_is_uploaded_last_and_published(self):
|
|
uploaded = []
|
|
|
|
def fake_upload(repository, release_id, path, token):
|
|
uploaded.append(path.name)
|
|
return {"name": path.name}
|
|
|
|
with patch.object(kit, "find_release", return_value=None), \
|
|
patch.object(kit, "create_draft", return_value={"id": 8}), \
|
|
patch.object(kit, "read_json", return_value=[]), \
|
|
patch.object(kit, "upload_asset", side_effect=fake_upload), \
|
|
patch.object(kit, "published_matches", return_value=True), \
|
|
patch.object(kit, "patch_release",
|
|
return_value={"id": 8, "draft": False}), \
|
|
patch.object(kit, "find_release", side_effect=[None, self.release]):
|
|
result = self.publish()
|
|
self.assertEqual(result["status"], "published")
|
|
self.assertEqual(uploaded, ["a.tar.gz", "a.tar.gz.sha256", "a.zip",
|
|
"a.zip.sha256", "release-evidence.json"])
|
|
|
|
|
|
class CiDiscoveryTests(unittest.TestCase):
|
|
SHA = "a" * 40
|
|
|
|
def listing(self, **overrides):
|
|
pipeline = {"number": 12, "commit": self.SHA, "status": "success", "event": "push"}
|
|
pipeline.update(overrides)
|
|
return [pipeline]
|
|
|
|
def test_selects_lowest_successful_pipeline_deterministically(self):
|
|
lane = [{"number": 41, "commit": self.SHA, "status": "success", "event": "manual"},
|
|
{"number": 39, "commit": self.SHA, "status": "success", "event": "push"},
|
|
{"number": 40, "commit": self.SHA, "status": "success", "event": "push"}]
|
|
detail = {"number": 39, "commit": self.SHA, "status": "success", "workflows": [
|
|
{"name": "ci", "state": "success",
|
|
"children": [{"type": "commands", "state": "success"}]}]}
|
|
with patch.object(kit, "ci_repository", return_value=8), \
|
|
patch.object(kit, "read_json", side_effect=[lane, detail]):
|
|
evidence = kit.discover_ci("cflmy/marqdo-std-text", self.SHA, "ci", "")
|
|
self.assertEqual(evidence["pipeline_number"], 39)
|
|
|
|
def test_requires_successful_same_sha_ci(self):
|
|
with patch.object(kit, "ci_repository", return_value=8), \
|
|
patch.object(kit, "read_json", return_value=self.listing()):
|
|
with self.assertRaises(kit.ReleaseError):
|
|
kit.discover_ci("cflmy/marqdo-std-text", "b" * 40, "ci", "")
|
|
with patch.object(kit, "ci_repository", return_value=8), \
|
|
patch.object(kit, "read_json", return_value=self.listing(event="tag")):
|
|
with self.assertRaises(kit.ReleaseError):
|
|
kit.discover_ci("cflmy/marqdo-std-text", self.SHA, "ci", "")
|
|
|
|
def test_successful_ci_evidence(self):
|
|
detail = {"number": 12, "commit": self.SHA, "status": "success", "workflows": [
|
|
{"name": "ci", "state": "success",
|
|
"children": [{"type": "commands", "state": "success"}]}]}
|
|
with patch.object(kit, "ci_repository", return_value=8), \
|
|
patch.object(kit, "read_json", side_effect=[self.listing(), detail]):
|
|
evidence = kit.discover_ci("cflmy/marqdo-std-text", self.SHA, "ci", "")
|
|
self.assertEqual(evidence, {
|
|
"pipeline_number": 12,
|
|
"url": f"{kit.CI}/repos/8/pipeline/12",
|
|
"source_sha": self.SHA,
|
|
})
|
|
|
|
def test_workflow_must_be_successful(self):
|
|
detail = {"number": 12, "commit": self.SHA, "status": "success", "workflows": [
|
|
{"name": "ci", "state": "failure",
|
|
"children": [{"type": "commands", "state": "failure"}]}]}
|
|
with patch.object(kit, "ci_repository", return_value=8), \
|
|
patch.object(kit, "read_json", side_effect=[self.listing(), detail]):
|
|
with self.assertRaises(kit.ReleaseError):
|
|
kit.discover_ci("cflmy/marqdo-std-text", self.SHA, "ci", "")
|
|
|
|
|
|
class RemoteTagTests(unittest.TestCase):
|
|
def test_remote_tag_identity(self):
|
|
body = json.dumps({"name": "v0.1.0", "commit": {"sha": "b" * 40}}).encode()
|
|
with patch.object(kit, "api_request", return_value=(200, body, {})):
|
|
self.assertEqual(kit.remote_tag_commit("cflmy/marqdo-std-text", "v0.1.0", "t"),
|
|
"b" * 40)
|
|
with patch.object(kit, "api_request", return_value=(404, b"{}", {})):
|
|
self.assertIsNone(kit.remote_tag_commit("cflmy/marqdo-std-text", "v0.1.0", "t"))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|