okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge main: Docker commit fix069384b4f

main
3.0 KiB63 linesraw
1"""package_terminal.py must work where there is no git (the Docker build stage copies the sources without .git and the image has no git):
2the commit comes from STRATA_BUILD_COMMIT, falls back to git when it exists, and is simply left out when neither answers."""
3from __future__ import annotations
4
5import os
6import re
7import subprocess
8import sys
9import unittest
10from pathlib import Path
11from unittest import mock
12
13ROOT = Path(__file__).resolve().parents[2]
14sys.path.insert(0, str(ROOT / "packaging"))
15import package_terminal  # noqa: E402
16
17SHA = "0123456789abcdef0123456789abcdef01234567"
18
19
20class SourceCommit(unittest.TestCase):
21    def test_env_wins_and_is_lower_cased(self):
22        with mock.patch.dict(os.environ, {"STRATA_BUILD_COMMIT": " " + SHA.upper() + " "}), \
23             mock.patch.object(package_terminal.subprocess, "run", side_effect=AssertionError("git must not run")):
24            self.assertEqual(package_terminal.source_commit(), SHA)
25
26    def test_no_git_installed_gives_empty_not_a_crash(self):
27        env = {k: v for k, v in os.environ.items() if k != "STRATA_BUILD_COMMIT"}
28        with mock.patch.dict(os.environ, env, clear=True), \
29             mock.patch.object(package_terminal.subprocess, "run", side_effect=FileNotFoundError("git")):
30            self.assertEqual(package_terminal.source_commit(), "")
31
32    def test_bad_env_value_falls_back_to_git(self):
33        done = subprocess.CompletedProcess([], 0, stdout=SHA + "\n", stderr="")
34        with mock.patch.dict(os.environ, {"STRATA_BUILD_COMMIT": "not-a-sha"}), \
35             mock.patch.object(package_terminal.subprocess, "run", return_value=done):
36            self.assertEqual(package_terminal.source_commit(), SHA)
37
38    def test_not_a_checkout_gives_no_commit_file_value(self):
39        done = subprocess.CompletedProcess([], 128, stdout="", stderr="fatal: not a git repository")
40        env = {k: v for k, v in os.environ.items() if k != "STRATA_BUILD_COMMIT"}
41        with mock.patch.dict(os.environ, env, clear=True), \
42             mock.patch.object(package_terminal.subprocess, "run", return_value=done):
43            self.assertEqual(package_terminal.source_commit(), "")
44
45
46class DockerPassesTheCommit(unittest.TestCase):
47    def test_dockerfile_turns_the_build_arg_into_the_env_the_packager_reads(self):
48        text = (ROOT / "docker" / "Dockerfile").read_text(encoding="utf-8")
49        self.assertRegex(text, r"ARG STRATA_COMMIT")
50        self.assertRegex(text, r"ENV STRATA_BUILD_COMMIT=\$\{STRATA_COMMIT\}")
51        self.assertLess(text.index("ENV STRATA_BUILD_COMMIT"), text.index("RUN python3 /src/packaging/package_terminal.py"))
52
53    def test_build_sh_passes_the_commit(self):
54        text = (ROOT / "docker" / "build.sh").read_text(encoding="utf-8")
55        self.assertIn('--build-arg "STRATA_COMMIT=$commit"', text)
56
57    def test_the_packager_reads_the_same_name(self):
58        self.assertIn("STRATA_BUILD_COMMIT", (ROOT / "packaging" / "package_terminal.py").read_text(encoding="utf-8"))
59        self.assertTrue(re.fullmatch(r"[0-9a-f]{40}", SHA))
60
61
62if __name__ == "__main__":
63    unittest.main()