okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
069384b4f
main
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 ): 21def test_env_wins_and_is_lower_cased (self ): 22with mock .patch .dict (os .environ , {"STRATA_BUILD_COMMIT" :" " + SHA .upper ()+ " " }), \ 23mock .patch .object (package_terminal .subprocess ,"run" ,side_effect = AssertionError ("git must not run" )): 24self .assertEqual (package_terminal .source_commit (),SHA ) 25 26def test_no_git_installed_gives_empty_not_a_crash (self ): 27env = {k :v for k ,v in os .environ .items ()if k != "STRATA_BUILD_COMMIT" } 28with mock .patch .dict (os .environ ,env ,clear = True ), \ 29mock .patch .object (package_terminal .subprocess ,"run" ,side_effect = FileNotFoundError ("git" )): 30self .assertEqual (package_terminal .source_commit (),"" ) 31 32def test_bad_env_value_falls_back_to_git (self ): 33done = subprocess .CompletedProcess ([],0 ,stdout = SHA + "\n" ,stderr = "" ) 34with mock .patch .dict (os .environ , {"STRATA_BUILD_COMMIT" :"not-a-sha" }), \ 35mock .patch .object (package_terminal .subprocess ,"run" ,return_value = done ): 36self .assertEqual (package_terminal .source_commit (),SHA ) 37 38def test_not_a_checkout_gives_no_commit_file_value (self ): 39done = subprocess .CompletedProcess ([],128 ,stdout = "" ,stderr = "fatal: not a git repository" ) 40env = {k :v for k ,v in os .environ .items ()if k != "STRATA_BUILD_COMMIT" } 41with mock .patch .dict (os .environ ,env ,clear = True ), \ 42mock .patch .object (package_terminal .subprocess ,"run" ,return_value = done ): 43self .assertEqual (package_terminal .source_commit (),"" ) 44 45 46class DockerPassesTheCommit (unittest .TestCase ): 47def test_dockerfile_turns_the_build_arg_into_the_env_the_packager_reads (self ): 48text = (ROOT / "docker" / "Dockerfile" ).read_text (encoding = "utf-8" ) 49self .assertRegex (text ,r"ARG STRATA_COMMIT" ) 50self .assertRegex (text ,r"ENV STRATA_BUILD_COMMIT=\$\{STRATA_COMMIT\}" ) 51self .assertLess (text .index ("ENV STRATA_BUILD_COMMIT" ),text .index ("RUN python3 /src/packaging/package_terminal.py" )) 52 53def test_build_sh_passes_the_commit (self ): 54text = (ROOT / "docker" / "build.sh" ).read_text (encoding = "utf-8" ) 55self .assertIn ('--build-arg "STRATA_COMMIT=$commit"' ,text ) 56 57def test_the_packager_reads_the_same_name (self ): 58self .assertIn ("STRATA_BUILD_COMMIT" , (ROOT / "packaging" / "package_terminal.py" ).read_text (encoding = "utf-8" )) 59self .assertTrue (re .fullmatch (r"[0-9a-f]{40}" ,SHA )) 60 61 62if __name__ == "__main__" : 63unittest .main ()