Files
mandy/tests/test_run_wine_tests.py
2026-11-22 08:38:34 -05:00

418 lines
20 KiB
Python

"""Tests for scripts/run_wine_tests.py: path mapping, per-test statuses and the job's exit code.
python -I tests/test_run_wine_tests.py
No Wine and no game data: `wine` is the Python interpreter and every "build artifact" is a tiny script that exits
with a chosen code, so the runner's own logic is what is under test.
"""
from __future__ import annotations
import argparse
import contextlib
import io
import json
import os
from pathlib import Path
import sys
import tempfile
import unittest
from unittest import mock
ROOT = Path(__file__).resolve().parents[1]
SCRIPTS = ROOT / "scripts"
if str(SCRIPTS) not in sys.path:
sys.path.insert(0, str(SCRIPTS))
import run_wine_tests as rwt # noqa: E402
WINDOWS_ROOT = "D:/win/root"
def ctest_entry(name, command, properties=None):
entry = {"name": name}
if command is not None:
entry["command"] = command
if properties:
entry["properties"] = [{"name": k, "value": v} for k, v in properties.items()]
return entry
class PathMappingTest(unittest.TestCase):
def test_paths_under_the_windows_checkout_are_mapped(self):
self.assertEqual(rwt.map_path("D:/win/root", WINDOWS_ROOT, "/repo"), "/repo")
self.assertEqual(rwt.map_path("D:\\win\\root\\runtime\\build", WINDOWS_ROOT, "/repo"),
os.path.join("/repo", "runtime", "build"))
self.assertEqual(rwt.map_path("d:/WIN/root/a/b.exe", WINDOWS_ROOT, "/repo"), os.path.join("/repo", "a", "b.exe"))
self.assertEqual(rwt.map_path("D:/win/root/", WINDOWS_ROOT, "/repo"), "/repo")
def test_paths_outside_the_checkout_are_not_mapped(self):
for path in ("D:/win/root2/a.exe", "C:/Windows/system32/cmd.exe", "/usr/bin/cmake", ""):
with self.subTest(path=path):
self.assertIsNone(rwt.map_path(path, WINDOWS_ROOT, "/repo"))
def test_wine_path_uses_the_z_drive(self):
self.assertEqual(rwt.wine_path("/tmp/x y"), "Z:" + os.path.abspath("/tmp/x y").replace("/", "\\"))
def test_properties_are_flattened(self):
self.assertEqual(rwt.properties(ctest_entry("t", ["x"], {"SKIP_RETURN_CODE": "77", "WILL_FAIL": "1"})),
{"SKIP_RETURN_CODE": "77", "WILL_FAIL": "1"})
self.assertEqual(rwt.properties({"name": "t"}), {})
def test_ctest_booleans_are_not_python_truthiness(self):
for value in ("1", "ON", "true", "yes", " 1 "):
with self.subTest(value=value):
self.assertTrue(rwt.truthy(value))
for value in ("0", "", None, "false", "no"):
with self.subTest(value=value):
self.assertFalse(rwt.truthy(value))
class RunTestTest(unittest.TestCase):
def setUp(self):
self._tmp = tempfile.TemporaryDirectory()
self.root = Path(self._tmp.name)
self.artifact = self.root / "artifact"
self.artifact.mkdir()
self.args = argparse.Namespace(windows_root=WINDOWS_ROOT, root=str(self.root), timeout=10.0)
self.wine = [sys.executable]
def tearDown(self):
self._tmp.cleanup()
def program(self, name, body):
path = self.artifact / name
path.write_text(body, encoding="utf-8")
return str(path)
def exiting(self, code, name=None):
return self.program(name or f"exit{code}.py", f"import sys\nsys.exit({code})\n")
def command_for(self, exe, *args):
"""The same program as ctest would list it: a path in the Windows checkout."""
parts = Path(exe).relative_to(self.root).parts
return [WINDOWS_ROOT + "/" + "/".join(parts), *args]
def run_entry(self, entry):
return rwt.run_test(entry, self.args, self.wine)
def test_a_passing_test(self):
status, detail = self.run_entry(ctest_entry("ok", self.command_for(self.exiting(0))))
self.assertEqual(status, "pass")
self.assertIn("exit 0", detail)
def test_a_failing_test_reports_the_exit_code(self):
status, detail = self.run_entry(ctest_entry("bad", self.command_for(self.exiting(3))))
self.assertEqual(status, "fail")
self.assertIn("exit 3", detail)
def test_a_test_without_a_command_fails(self):
status, detail = self.run_entry(ctest_entry("unbuilt", None))
self.assertEqual(status, "fail")
self.assertIn("no command", detail)
def test_a_test_that_runs_a_runner_program_is_skipped(self):
status, detail = self.run_entry(ctest_entry("python-test", ["/usr/bin/cmake", "-P", "x.cmake"]))
self.assertEqual(status, "skip")
self.assertIn("not a program from the build", detail)
def test_a_missing_executable_fails(self):
status, detail = self.run_entry(ctest_entry("gone", [WINDOWS_ROOT + "/artifact/gone.py"]))
self.assertEqual(status, "fail")
self.assertIn("not in the artifact", detail)
def test_will_fail_inverts_the_exit_code(self):
exe = self.exiting(2)
status, _ = self.run_entry(ctest_entry("expected", self.command_for(exe), {"WILL_FAIL": "1"}))
self.assertEqual(status, "pass")
def test_will_fail_zero_does_not_invert_the_exit_code(self):
# A JSON property value is a string; "0" must stay false.
status, detail = self.run_entry(ctest_entry("normal", self.command_for(self.exiting(0)), {"WILL_FAIL": "0"}))
self.assertEqual(status, "pass", detail)
def test_the_skip_return_code_is_a_skip(self):
status, detail = self.run_entry(
ctest_entry("skipped", self.command_for(self.exiting(77)), {"SKIP_RETURN_CODE": "77"}))
self.assertEqual(status, "skip")
self.assertIn("exit 77", detail)
def test_ctest_output_expressions_determine_the_result(self):
usage = {"PASS_REGULAR_EXPRESSION": ["alternative", "usage: gta6rt"]}
forbidden = {"FAIL_REGULAR_EXPRESSION": ["running ", r"module\(s\) mapped"]}
for code, output, props, expected in (
(2, "usage: gta6rt", usage | forbidden, "pass"),
(0, "unexpected output", usage, "fail"),
(2, "usage: gta6rt\n1 module(s) mapped", usage | forbidden, "fail"),
(0, "running tiny_game.exe", forbidden, "fail"),
(2, "unexpected output", forbidden, "fail"),
(77, "unexpected output", usage | {"SKIP_RETURN_CODE": 77}, "skip"),
(0, "running tiny_game.exe", forbidden | {"WILL_FAIL": True}, "pass"),
):
with self.subTest(code=code, output=output, props=props):
exe = self.program("regex.py", f"import sys\nprint({output!r}, file=sys.stderr)\nsys.exit({code})\n")
status, detail = self.run_entry(ctest_entry("regex", self.command_for(exe), props))
self.assertEqual(status, expected, detail)
def test_a_scalar_output_expression_is_matched_as_one_pattern(self):
required = {"PASS_REGULAR_EXPRESSION": "usage: gta6rt"}
forbidden = {"FAIL_REGULAR_EXPRESSION": r"module\(s\) mapped"}
for code, output, props, expected in (
(3, "unexpected output", required, "fail"),
(2, "usage: gta6rt", required, "pass"),
(0, "unexpected output", forbidden, "pass"),
(0, "1 module(s) mapped", forbidden, "fail"),
):
with self.subTest(output=output, props=props):
exe = self.program("regex.py", f"import sys\nprint({output!r})\nsys.exit({code})\n")
status, detail = self.run_entry(ctest_entry("regex", self.command_for(exe), props))
self.assertEqual(status, expected, detail)
def test_a_signal_termination_fails_even_with_success_overrides(self):
# The mocked negative returncode keeps the test portable across Windows and POSIX.
exe = self.exiting(0)
fake = mock.Mock(returncode=-11, stdout=b"usage: gta6rt")
for props in ({"WILL_FAIL": True}, {"PASS_REGULAR_EXPRESSION": ["usage: gta6rt"]}):
with self.subTest(props=props), mock.patch.object(rwt.subprocess, "run", return_value=fake):
entry = ctest_entry("segv", self.command_for(exe), props | {"SKIP_RETURN_CODE": 77})
status, detail = self.run_entry(entry)
self.assertEqual(status, "fail")
self.assertIn("signal 11", detail)
def test_a_timeout_is_reported_without_hanging(self):
slow = self.program("slow.py", "import time\ntime.sleep(5)\n")
self.args.timeout = 0.2
status, detail = self.run_entry(ctest_entry("slow", self.command_for(slow)))
self.assertEqual(status, "fail")
self.assertIn("timed out after 0.2 s", detail)
def test_an_unstartable_wine_fails_the_test_instead_of_aborting_the_run(self):
status, detail = rwt.run_test(ctest_entry("ok", self.command_for(self.exiting(0))), self.args,
["definitely-not-a-real-wine-xyz"])
self.assertEqual(status, "fail")
self.assertIn("could not start", detail)
def test_arguments_and_working_directory_are_mapped(self):
report = self.root / "report.json"
script = self.program("report.py", (
"import json, os, sys\n"
"json.dump({'argv': sys.argv[1:], 'cwd': os.getcwd()}, open(os.environ['REPORT_PATH'], 'w'))\n"))
(self.artifact / "sub").mkdir()
entry = ctest_entry("mapped", self.command_for(script, WINDOWS_ROOT + "/artifact/data.bin", "--flag"),
{"WORKING_DIRECTORY": WINDOWS_ROOT + "/artifact/sub"})
with mock.patch.dict(os.environ, {"REPORT_PATH": str(report)}):
status, detail = self.run_entry(entry)
self.assertEqual(status, "pass", detail)
seen = json.loads(report.read_text(encoding="utf-8"))
self.assertEqual(len(seen["argv"]), 2)
self.assertTrue(seen["argv"][0].startswith("Z:"), seen["argv"][0])
self.assertTrue(seen["argv"][0].endswith("data.bin"), seen["argv"][0])
self.assertEqual(seen["argv"][1], "--flag")
self.assertEqual(os.path.normcase(os.path.realpath(seen["cwd"])),
os.path.normcase(os.path.realpath(self.artifact / "sub")))
@unittest.skipIf(os.name == "nt", "the runner and its dosdevices links are for Linux and macOS")
class DriveMapTest(unittest.TestCase):
def setUp(self):
self._tmp = tempfile.TemporaryDirectory()
base = Path(self._tmp.name)
self.root = base / "checkout"
(self.root / "runtime" / "build" / "generated").mkdir(parents=True)
(self.root / "runtime" / "build" / "generated" / "cs.spv").write_text("spirv", encoding="utf-8")
self.prefix = base / "prefix"
(self.prefix / "dosdevices").mkdir(parents=True)
self.link = self.prefix / "dosdevices" / "d:"
def tearDown(self):
self._tmp.cleanup()
def test_the_drive_leads_to_the_checkout_and_is_removed_afterwards(self):
with rwt.DriveMap("D:/a/Leonida/Leonida", str(self.root), str(self.prefix)) as drive:
seen = self.link / "a" / "Leonida" / "Leonida" / "runtime" / "build" / "generated" / "cs.spv"
self.assertEqual(seen.read_text(encoding="utf-8"), "spirv")
self.assertIn("drive d: maps", drive.note)
scratch = drive.scratch
self.assertFalse(os.path.lexists(self.link))
self.assertFalse(os.path.exists(scratch))
self.assertTrue((self.root / "runtime" / "build" / "generated" / "cs.spv").is_file()) # links only
def test_backslashes_and_a_lower_case_drive_are_accepted(self):
with rwt.DriveMap("d:\\work\\leonida\\", str(self.root), str(self.prefix)):
self.assertTrue((self.link / "work" / "leonida" / "runtime" / "build").is_dir())
def test_a_drive_the_prefix_already_has_is_left_alone(self):
other = self.prefix / "elsewhere"
other.mkdir()
os.symlink(other, self.link)
with rwt.DriveMap("D:/a/Leonida/Leonida", str(self.root), str(self.prefix)) as drive:
self.assertIn("already set", drive.note)
self.assertEqual(os.readlink(self.link), str(other))
def test_no_prefix_or_no_drive_letter_maps_nothing(self):
with rwt.DriveMap("D:/a/b", str(self.root), str(self.prefix / "absent")) as drive:
self.assertIn("no Wine prefix", drive.note)
with rwt.DriveMap("/home/runner/work", str(self.root), str(self.prefix)) as drive:
self.assertIn("no drive letter", drive.note)
self.assertFalse(os.path.lexists(self.link))
def test_main_maps_the_drive_while_the_tests_run(self):
# The "Wine" here is the Python interpreter, so the program reads the drive through its dosdevices link, which
# is where Wine resolves D:/win/root/... too.
probe = self.root / "probe.py"
target = self.link / "win" / "root" / "runtime" / "build" / "generated" / "cs.spv"
probe.write_text(f"import sys\nsys.exit(0 if open({str(target)!r}).read() == 'spirv' else 1)\n", encoding="utf-8")
listing = self.root / "ctest.json"
listing.write_text(json.dumps({"tests": [ctest_entry("compiled_in_path", [WINDOWS_ROOT + "/probe.py"])]}),
encoding="utf-8")
argv = ["run_wine_tests.py", "--tests", str(listing), "--windows-root", WINDOWS_ROOT,
"--root", str(self.root), "--wine", sys.executable.replace(os.sep, "/")]
out = io.StringIO()
with mock.patch.dict(os.environ, {"WINEPREFIX": str(self.prefix)}), mock.patch.object(sys, "argv", argv), \
contextlib.redirect_stdout(out):
code = rwt.main()
self.assertEqual(code, 0, out.getvalue())
self.assertIn("[PASS] compiled_in_path", out.getvalue())
self.assertFalse(os.path.lexists(self.link))
class MainTest(unittest.TestCase):
def setUp(self):
self._tmp = tempfile.TemporaryDirectory()
self.root = Path(self._tmp.name)
self.artifact = self.root / "artifact"
self.artifact.mkdir()
for name, code in (("ok.py", 0), ("bad.py", 1)):
(self.artifact / name).write_text(f"import sys\nsys.exit({code})\n", encoding="utf-8")
# main() maps the checkout's drive into $WINEPREFIX: keep it away from a real prefix (~/.wine).
env = mock.patch.dict(os.environ, {"WINEPREFIX": str(self.root / "no-prefix")})
env.start()
self.addCleanup(env.stop)
def tearDown(self):
self._tmp.cleanup()
def list_tests(self, entries):
path = self.root / "ctest.json"
path.write_text(json.dumps({"tests": entries}), encoding="utf-8")
return str(path)
def command(self, name):
return [WINDOWS_ROOT + "/artifact/" + name]
def run_main(self, entries, *extra, stderr=False):
listing = self.list_tests(entries)
argv = ["run_wine_tests.py", "--tests", listing, "--windows-root", WINDOWS_ROOT,
"--root", str(self.root), "--wine", sys.executable.replace(os.sep, "/"), *extra]
out, err = io.StringIO(), io.StringIO()
with mock.patch.object(sys, "argv", argv), contextlib.redirect_stdout(out), contextlib.redirect_stderr(err):
code = rwt.main()
return code, out.getvalue(), err.getvalue()
def test_a_required_test_that_passes_exits_zero(self):
code, out, _ = self.run_main([ctest_entry("ok", self.command("ok.py"))], "--require", "ok")
self.assertEqual(code, 0)
self.assertIn("1 passed, 0 failed, 0 skipped under Wine (1 tests)", out)
def test_a_required_test_that_fails_exits_one(self):
code, out, _ = self.run_main([ctest_entry("bad", self.command("bad.py"))], "--require", "bad")
self.assertEqual(code, 1)
self.assertIn("required tests that did not pass: bad", out)
def test_a_required_test_that_is_not_listed_exits_one(self):
code, out, _ = self.run_main([ctest_entry("ok", self.command("ok.py"))], "--require", "absent")
self.assertEqual(code, 1)
self.assertIn("required test not listed", out)
def test_unrequired_failures_are_reported_but_do_not_fail_the_job(self):
code, out, _ = self.run_main([ctest_entry("bad", self.command("bad.py"))])
self.assertEqual(code, 0)
self.assertIn("[FAIL] bad", out)
def test_the_summary_table_is_appended(self):
summary = self.root / "summary.md"
code, _, _ = self.run_main([ctest_entry("ok", self.command("ok.py")), ctest_entry("bad", self.command("bad.py"))],
"--summary", str(summary), "--label", "Wine 11", "--require", "ok")
self.assertEqual(code, 0)
text = summary.read_text(encoding="utf-8")
self.assertIn("### Windows tests under Wine 11", text)
self.assertIn("| `ok` | pass | yes |", text)
self.assertIn("| `bad` | fail | |", text)
def test_a_summary_in_a_missing_folder_creates_it(self):
summary = self.root / "missing" / "sub" / "summary.md"
code, _, err = self.run_main([ctest_entry("ok", self.command("ok.py"))],
"--summary", str(summary), "--label", "Wine 12")
self.assertEqual(code, 0)
self.assertEqual(err, "")
self.assertIn("### Windows tests under Wine 12", summary.read_text(encoding="utf-8"))
def test_an_unwritable_summary_is_one_error_line(self):
blocker = self.root / "blocker"
blocker.write_text("not a folder", encoding="utf-8")
summary = blocker / "summary.md" # its parent is a file, not a folder
code, _, err = self.run_main([ctest_entry("ok", self.command("ok.py"))], "--summary", str(summary))
self.assertEqual(code, 1)
self.assertNotIn("Traceback", err)
self.assertIn("ERROR: cannot write the summary", err)
self.assertEqual(len(err.strip().splitlines()), 1, err)
def test_a_file_that_is_not_json_is_one_error_line(self):
listing = self.root / "notes.txt"
listing.write_text("this is not ctest output\n", encoding="utf-8")
argv = ["run_wine_tests.py", "--tests", str(listing), "--windows-root", WINDOWS_ROOT,
"--root", str(self.root), "--wine", sys.executable.replace(os.sep, "/")]
out, err = io.StringIO(), io.StringIO()
with mock.patch.object(sys, "argv", argv), contextlib.redirect_stdout(out), contextlib.redirect_stderr(err):
code = rwt.main()
self.assertEqual(code, 2)
self.assertIn("ERROR: cannot read", err.getvalue())
self.assertNotIn("Traceback", err.getvalue())
def test_json_that_is_not_ctest_output_is_one_error_line(self):
code, _, err = self.run_main_json([{"name": "t"}])
self.assertEqual(code, 2)
self.assertIn("not the output of ctest", err)
def run_main_json(self, document):
listing = self.root / "wrong.json"
listing.write_text(json.dumps(document), encoding="utf-8")
argv = ["run_wine_tests.py", "--tests", str(listing), "--windows-root", WINDOWS_ROOT,
"--root", str(self.root), "--wine", sys.executable.replace(os.sep, "/")]
out, err = io.StringIO(), io.StringIO()
with mock.patch.object(sys, "argv", argv), contextlib.redirect_stdout(out), contextlib.redirect_stderr(err):
code = rwt.main()
return code, out.getvalue(), err.getvalue()
def test_a_bad_timeout_is_refused_by_argparse(self):
for text in ("0", "-1", "abc", "nan", "inf", "-inf"):
with self.subTest(text=text):
with self.assertRaises(argparse.ArgumentTypeError):
rwt.positive_seconds(text)
self.assertEqual(rwt.positive_seconds("0.5"), 0.5)
def test_a_nonfinite_timeout_is_a_usage_error(self):
for text in ("nan", "inf", "-inf"):
with self.subTest(text=text):
with self.assertRaises(SystemExit) as ctx:
self.run_main([ctest_entry("ok", self.command("ok.py"))], "--timeout", text)
self.assertEqual(ctx.exception.code, 2)
def test_a_blank_wine_is_a_usage_error(self):
with self.assertRaises(SystemExit) as ctx:
self.run_main([ctest_entry("ok", self.command("ok.py"))], "--wine", "")
self.assertEqual(ctx.exception.code, 2)
def test_an_unclosed_quote_in_wine_is_a_usage_error(self):
with self.assertRaises(SystemExit) as ctx:
self.run_main([ctest_entry("ok", self.command("ok.py"))], "--wine", "wine 'unclosed")
self.assertEqual(ctx.exception.code, 2)
def test_wine_quoting_is_still_split(self):
self.assertEqual(rwt.wine_command("prog 'two words'"), ["prog", "two words"])
with self.assertRaises(argparse.ArgumentTypeError):
rwt.wine_command(" ")
if __name__ == "__main__":
unittest.main()