diff options
| author | Paul Oliver <contact@pauloliver.dev> | 2026-07-31 00:06:20 +0200 |
|---|---|---|
| committer | Paul Oliver <contact@pauloliver.dev> | 2026-07-31 00:11:33 +0200 |
| commit | 8a4563d6d0c9e81fdaa42f6f760cae2b49cad431 (patch) | |
| tree | 868a54949ed79b3c1e585365f4f3385bb6bee12f /salis.py | |
| parent | f2c34b1d2c18270a0327fb4896fa307fae098770 (diff) | |
Adds unit test framework
Diffstat (limited to 'salis.py')
| -rwxr-xr-x | salis.py | 20 |
1 files changed, 18 insertions, 2 deletions
@@ -7,6 +7,7 @@ # index: # [section] imports # [section] argument parser +# [section] unit tests # [section] build class # [section] logger # [section] options dict formatter @@ -24,9 +25,9 @@ import json import os import random import shutil -import signal import subprocess import sys +import unittest from argparse import ArgumentDefaultsHelpFormatter, ArgumentParser, ArgumentTypeError, RawTextHelpFormatter from tempfile import TemporaryDirectory @@ -44,6 +45,7 @@ formatter_class = lambda prog: ArgumentDefaultsHelpFormatter(max_help_position=4 new = sub_parsers.add_parser("new", formatter_class=formatter_class, help="create new simulation") load = sub_parsers.add_parser("load", formatter_class=formatter_class, help="load saved simulation") +test = sub_parsers.add_parser("test", formatter_class=formatter_class, help="run unit tests") def seed(i): ival = int(i, 0) @@ -83,6 +85,7 @@ options = ( ("m", "mvec-pow", (new,), fmt_id, {"metavar": "POW", "help": "memory core size exponent; size = 2^{POW}", "default": 20, "required": False, "type": pos}), ("n", "name", (new, load), fmt_id, {"metavar": "NAME", "help": "name of new or loaded simulation", "default": "def.sim", "required": False, "type": str}), ("o", "optimized", (new, load), fmt_id, {"action": "store_true", "help": "build with optimizations", "required": False}), + ("p", "pattern", (test,), fmt_id, {"metavar": "PATTERN", "help": "pattern for unit test discovery", "default": "*", "required": False, "type": str}), ("p", "pre-cmd", (new, load), fmt_id, {"metavar": "CMD", "help": "shell command with which to wrap call to executable; e.g. gdb, time, valgrind, etc.", "required": False, "type": str}), ("s", "seed", (new,), fmt_hex, {"metavar": "SEED", "help": "seed value for new simulation; a value of 0 disables cosmic rays; a value of -1 creates a random seed", "default": 0, "required": False, "type": seed}), ("T", "keep-temp-dir", (new, load), fmt_id, {"action": "store_true", "help": "keep temporary directory on exit", "required": False}), @@ -97,6 +100,16 @@ options = ( args = parser.parse_args() # ------------------------------------------------------------------------------ +# [section] unit tests +# ------------------------------------------------------------------------------ +if args.command == "test": + loader = unittest.TestLoader() + loader.testNamePatterns = [args.pattern] + runner = unittest.TextTestRunner(verbosity=2) + result = runner.run(loader.discover("test")) + exit(0 if not result.errors and not result.failures else 1) + +# ------------------------------------------------------------------------------ # [section] build class # ------------------------------------------------------------------------------ class Build: @@ -143,7 +156,7 @@ class Build: # When a signal is received (e.g. SIGINT), is propagates through the entire process group. # Handle signal on the interpreter and allow the simulator to shut down gracefully. try: - signal.pause() + proc.wait() except KeyboardInterrupt: proc.wait() @@ -316,6 +329,9 @@ ui_path = f"{args.ui_path}/{args.ui}" ui_flags = [f"-Iarch/{args.vm_arch}", "-Icore", f"arch/{args.vm_arch}/arch.c", arch_inst_path, "core/compress.c", "core/logger.c", "core/salis.c", "core/sql.c"] ui_defines = defines + logger_defines +if not os.path.isdir(ui_path): + raise RuntimeError(f"UI not found at '{ui_path}'") + with open(f"{ui_path}/links.json", "r") as f: ui_links = json.load(f) |
