okami.mom/strata

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

Niko1221tests: the smoke test reads the engine's own words from .reason, the message is plain (X01)d64ae1b0b

main
2.8 KiB64 linesraw
1"""The engine test start with no model installed: an engine without a --version flag (it prints its options and exits 2) still counts as running."""
2
3import os
4import stat
5import sys
6import tempfile
7import unittest
8from pathlib import Path
9from types import SimpleNamespace
10
11from strata_control import updater
12
13
14def make_engine(folder: Path, body_posix: str, body_nt: str) -> Path:
15    if os.name == "nt":
16        path = folder / "strata.cmd"
17        path.write_text("@echo off\r\n" + body_nt, encoding="utf-8")
18    else:
19        path = folder / "strata"
20        path.write_text("#!/bin/sh\n" + body_posix, encoding="utf-8")
21        path.chmod(path.stat().st_mode | stat.S_IXUSR)
22    return path
23
24
25USAGE_POSIX = "echo 'strata generate [options]' >&2\nfor f in a b c d e f; do echo \"  --opt-$f N   something\" >&2; done\nexit 2\n"
26USAGE_NT = "echo strata generate [options] 1>&2\r\n" + "".join(f"echo   --opt-{c} N   something 1>&2\r\n" for c in "abcdef") + "exit /b 2\r\n"
27
28
29class BinarySmoke(unittest.TestCase):
30    def component(self, folder: Path, exe: Path):
31        return SimpleNamespace(exe=exe, root=folder, version="0.1.45")
32
33    def test_an_engine_that_lists_its_options_instead_of_answering_version_passes(self):
34        with tempfile.TemporaryDirectory() as tmp:
35            folder = Path(tmp)
36            res = updater.binary_smoke(self.component(folder, make_engine(folder, USAGE_POSIX, USAGE_NT)))
37            self.assertEqual(res["mode"], "binary-only")
38            self.assertIn("does not answer --version", res["detail"])
39            self.assertIn("listed its options", res["detail"])
40
41    def test_an_engine_that_answers_version_passes_as_before(self):
42        with tempfile.TemporaryDirectory() as tmp:
43            folder = Path(tmp)
44            res = updater.binary_smoke(self.component(folder, make_engine(folder, "echo strata 0.1.45\nexit 0\n", "echo strata 0.1.45\r\nexit /b 0\r\n")))
45            self.assertIn("--version only", res["detail"])
46
47    def test_a_real_failure_still_fails(self):
48        with tempfile.TemporaryDirectory() as tmp:
49            folder = Path(tmp)
50            exe = make_engine(folder, "echo 'No device of requested type available' >&2\nexit 250\n", "echo No device of requested type available 1>&2\r\nexit /b 250\r\n")
51            with self.assertRaises(updater.TestStartFailed) as e:
52                updater.binary_smoke(self.component(folder, exe))
53            self.assertIn("No device of requested type", e.exception.reason)
54
55    def test_exit_2_without_a_list_of_options_still_fails(self):
56        with tempfile.TemporaryDirectory() as tmp:
57            folder = Path(tmp)
58            exe = make_engine(folder, "echo 'bad usage' >&2\nexit 2\n", "echo bad usage 1>&2\r\nexit /b 2\r\n")
59            with self.assertRaises(updater.TestStartFailed):
60                updater.binary_smoke(self.component(folder, exe))
61
62
63if __name__ == "__main__":
64    unittest.main()