okami.mom/strata

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

Niko1221Merge commit 'b8d604d28' into f-clean-1ab0fc432ed

main
4.0 KiB82 linesraw
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:
19    return 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
24    return value.as_posix() if isinstance(value, Path) else str(value)
25
26
27def run(*args, check=True):
28    return 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):
33    def setUp(self):
34        self.tmp = Path(tempfile.mkdtemp(prefix="strata-readme-"))
35        self.addCleanup(shutil.rmtree, self.tmp, True)
36        self.files = self.tmp / "files"
37        self.files.mkdir()
38        self.out = self.tmp / "README.txt"
39
40    def put(self, name, data):
41        (self.files / name).write_bytes(data)
42        return self.files / name
43
44    def test_every_hash_and_size_in_the_readme_comes_from_the_file(self):
45        app = self.put("Strata-tester-linux.AppImage", b"app image bytes " * 1000)
46        run(TEMPLATES / "README-linux.txt.in", self.out, "--appimage", app.name, "--files-dir", self.files)
47        text = self.out.read_text()
48        self.assertIn(sha(app), text)
49        self.assertNotIn("@APPIMAGE@", text)
50        for needle in ("libfuse2t64", "Ubuntu 26.04", "--browser", "--quit", "--save-logs", f"Strata {APP_VERSION}", NOTICE):
51            self.assertIn(needle, text)
52        run("--verify", self.out, "--files-dir", self.files)
53
54    def test_amd_readmes_hash_both_files(self):
55        for variant in ("rx9070", "rx7900"):
56            app, zipname = f"Strata-tester-linux-amd-{variant}.AppImage", f"Strata-Terminal-Linux-tester-amd-{variant}.zip"
57            a, z = self.put(app, b"a" * 5000), self.put(zipname, b"z" * 3000)
58            run(TEMPLATES / f"README-amd-{variant}.txt.in", self.out, "--appimage", app, "--files-dir", self.files)
59            text = self.out.read_text()
60            self.assertIn(sha(a), text)
61            self.assertIn(sha(z), text)
62            self.assertIn(f"./{app} --browser", text)
63            run("--verify", self.out, "--files-dir", self.files)
64
65    def test_a_missing_file_stops_the_readme_instead_of_leaving_a_stale_hash(self):
66        app = self.put("Strata-tester-linux-amd-rx9070.AppImage", b"a" * 5000)  # the zip is not there
67        result = run(TEMPLATES / "README-amd-rx9070.txt.in", self.out, "--appimage", app.name, "--files-dir", self.files, check=False)
68        self.assertNotEqual(result.returncode, 0)
69        self.assertIn("must not carry a hash it did not compute", result.stderr)
70        self.assertFalse(self.out.exists())
71
72    def test_verify_catches_a_stale_hash(self):
73        app = self.put("Strata-tester-linux.AppImage", b"first build")
74        run(TEMPLATES / "README-linux.txt.in", self.out, "--appimage", app.name, "--files-dir", self.files)
75        app.write_bytes(b"rebuilt")  # the file changed after the README was written
76        result = run("--verify", self.out, "--files-dir", self.files, check=False)
77        self.assertNotEqual(result.returncode, 0)
78        self.assertIn("STALE", result.stderr)
79
80
81if __name__ == "__main__":
82    unittest.main()