okami.mom/strata

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

Niko1221Merge f-027-gh-ctl: GitHub setup, Docker, docs and bench items for 0.2.7538acd098

main
3.0 KiB74 linesraw
1"""sycl/tools/build.sh exits with the build's own status (#2990): 0 after a good build, nonzero after a failed one.
2
3It once ended with a grep pipeline, so a good build exited 1 (nothing to find) and a failed compile exited 0, and a `build.sh && next-step` chain ran
4the wrong way round. The test runs the real script with a stub cmake on PATH; no compiler, no oneAPI, no docker.
5"""
6from __future__ import annotations
7
8import os
9import shutil
10import subprocess
11import tempfile
12import unittest
13from pathlib import Path
14
15ROOT = Path(__file__).resolve().parents[2]
16SCRIPT = ROOT / "sycl" / "tools" / "build.sh"
17BASH = shutil.which("bash")      # the full path: on Windows a bare "bash" can start the WSL launcher in System32 instead of Git bash
18CYGPATH = shutil.which("cygpath")
19
20
21def sh_path(p) -> str:
22    if CYGPATH is None:
23        return str(p)
24    return subprocess.run([CYGPATH, "-u", str(p)], capture_output=True, text=True, check=True).stdout.strip()
25
26
27STUB = """#!/bin/sh
28# a stand-in for cmake: `cmake --build DIR ...` prints what ninja prints and exits with STUB_RC; any other call does nothing
29case "$1" in
30  --build)
31    echo "[1/2] Building CXX object CMakeFiles/strata.dir/main.cpp.o"
32    if [ "${STUB_RC:-0}" != 0 ]; then
33      echo "FAILED: [code=1] CMakeFiles/strata.dir/main.cpp.o"
34      echo "main.cpp:1:21: error: 'x' was not declared in this scope"
35      echo "ninja: build stopped: cannot make progress due to previous errors."
36    else
37      echo "[2/2] Linking CXX executable strata"
38    fi
39    exit "${STUB_RC:-0}" ;;
40esac
41exit 0
42"""
43
44
45@unittest.skipIf(BASH is None, "needs bash")
46class BuildScriptStatus(unittest.TestCase):
47    def run_script(self, rc: int):
48        with tempfile.TemporaryDirectory() as tmp:
49            bindir, build = Path(tmp) / "bin", Path(tmp) / "build"
50            bindir.mkdir()
51            build.mkdir()
52            (build / "build.ninja").write_text("", encoding="utf-8")          # an existing build folder: the script does not configure it
53            (bindir / "cmake").write_text(STUB, encoding="utf-8", newline="\n")
54            (bindir / "cmake").chmod(0o755)
55            env = dict(os.environ, PATH=sh_path(bindir) + os.pathsep + os.environ["PATH"], STUB_RC=str(rc), REPO=sh_path(ROOT),
56                       BUILD_DIR=sh_path(build), AOT="", I8_ESIMD="OFF")
57            done = subprocess.run([BASH, sh_path(SCRIPT), "strata"], capture_output=True, text=True, env=env, timeout=120)
58            return done
59
60    def test_a_good_build_exits_zero(self):
61        done = self.run_script(0)
62        self.assertIn("BUILD EXIT 0", done.stdout)
63        self.assertIn("errors: 0", done.stdout)
64        self.assertEqual(done.returncode, 0, done.stdout + done.stderr)
65
66    def test_a_failed_compile_exits_nonzero_with_the_builds_status(self):
67        done = self.run_script(1)
68        self.assertIn("BUILD EXIT 1", done.stdout)
69        self.assertIn("errors: 1", done.stdout)
70        self.assertEqual(done.returncode, 1, done.stdout + done.stderr)
71
72
73if __name__ == "__main__":
74    unittest.main()