okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
b0fc432ed
main
1"""The tester READMEs get their checksums from the files themselves (apps/desktop/scripts/make-tester-readme.sh).""" 2import hashlib 3import shutil 4import subprocess 5import tempfile 6import unittest 7from pathlib import Path 8 9ROOT = Path (__file__ ).resolve ().parents [2 ] 10BASH = shutil .which ("bash" )# the full path: on Windows a bare "bash" is found in System32 first, which is the WSL launcher 11SCRIPT = ROOT / "apps" / "desktop" / "scripts" / "make-tester-readme.sh" 12TEMPLATES = ROOT / "apps" / "desktop" / "tester" 13# the README names the app version, which make-tester-readme.sh reads from Cargo.toml 14APP_VERSION = next (line .split ('"' )[1 ]for line in (ROOT / "apps" / "desktop" / "src-tauri" / "Cargo.toml" ).read_text ().splitlines ()if line .startswith ("version = " )) 15NOTICE = "Strata opened in your browser because the app window couldn't start on this computer." 16 17 18def sha (path :Path )-> str : 19return hashlib .sha256 (path .read_bytes ()).hexdigest () 20 21 22def arg (value ): 23# bash on Windows (Git for Windows, MSYS2, Cygwin) reads C:/dir/file, not C:\dir\file: the backslashes would be eaten 24return value .as_posix ()if isinstance (value ,Path )else str (value ) 25 26 27def run (* args ,check = True ): 28return subprocess .run ([BASH ,arg (SCRIPT ),* map (arg ,args )],capture_output = True ,text = True ,check = check ) 29 30 31@ unittest . skipIf ( BASH is None or shutil . which ( "sha256sum" ) is None , "needs bash and sha256sum" ) 32class TesterReadmeTests (unittest .TestCase ): 33def setUp (self ): 34self .tmp = Path (tempfile .mkdtemp (prefix = "strata-readme-" )) 35self .addCleanup (shutil .rmtree ,self .tmp ,True ) 36self .files = self .tmp / "files" 37self .files .mkdir () 38self .out = self .tmp / "README.txt" 39 40def put (self ,name ,data ): 41 (self .files / name ).write_bytes (data ) 42return self .files / name 43 44def test_every_hash_and_size_in_the_readme_comes_from_the_file (self ): 45app = self .put ("Strata-tester-linux.AppImage" ,b"app image bytes " * 1000 ) 46run (TEMPLATES / "README-linux.txt.in" ,self .out ,"--appimage" ,app .name ,"--files-dir" ,self .files ) 47text = self .out .read_text () 48self .assertIn (sha (app ),text ) 49self .assertNotIn ("@APPIMAGE@" ,text ) 50for needle in ("libfuse2t64" ,"Ubuntu 26.04" ,"--browser" ,"--quit" ,"--save-logs" ,f"Strata { APP_VERSION } " ,NOTICE ): 51self .assertIn (needle ,text ) 52run ("--verify" ,self .out ,"--files-dir" ,self .files ) 53 54def test_amd_readmes_hash_both_files (self ): 55for variant in ("rx9070" ,"rx7900" ): 56app ,zipname = f"Strata-tester-linux-amd- { variant } .AppImage" ,f"Strata-Terminal-Linux-tester-amd- { variant } .zip" 57a ,z = self .put (app ,b"a" * 5000 ),self .put (zipname ,b"z" * 3000 ) 58run (TEMPLATES / f"README-amd- { variant } .txt.in" ,self .out ,"--appimage" ,app ,"--files-dir" ,self .files ) 59text = self .out .read_text () 60self .assertIn (sha (a ),text ) 61self .assertIn (sha (z ),text ) 62self .assertIn (f"./ { app } --browser" ,text ) 63run ("--verify" ,self .out ,"--files-dir" ,self .files ) 64 65def test_a_missing_file_stops_the_readme_instead_of_leaving_a_stale_hash (self ): 66app = self .put ("Strata-tester-linux-amd-rx9070.AppImage" ,b"a" * 5000 )# the zip is not there 67result = run (TEMPLATES / "README-amd-rx9070.txt.in" ,self .out ,"--appimage" ,app .name ,"--files-dir" ,self .files ,check = False ) 68self .assertNotEqual (result .returncode ,0 ) 69self .assertIn ("must not carry a hash it did not compute" ,result .stderr ) 70self .assertFalse (self .out .exists ()) 71 72def test_verify_catches_a_stale_hash (self ): 73app = self .put ("Strata-tester-linux.AppImage" ,b"first build" ) 74run (TEMPLATES / "README-linux.txt.in" ,self .out ,"--appimage" ,app .name ,"--files-dir" ,self .files ) 75app .write_bytes (b"rebuilt" )# the file changed after the README was written 76result = run ("--verify" ,self .out ,"--files-dir" ,self .files ,check = False ) 77self .assertNotEqual (result .returncode ,0 ) 78self .assertIn ("STALE" ,result .stderr ) 79 80 81if __name__ == "__main__" : 82unittest .main ()