From: Cassie Jones Date: Sun, 7 Feb 2021 08:30:08 +0000 (-0500) Subject: [tools] Add tools/test.py as the project test runner X-Git-Url: https://git.witchoflight.com/ivy/commitdiff_plain?ds=sidebyside [tools] Add tools/test.py as the project test runner --- diff --git a/README.md b/README.md index 0d66851..f9d7df2 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ developed as an educational project to learn more about compiling functional languages, and compiling without going through frameworks like LLVM that handle the backend. It currently supports x64, and requires clang as an assembler. -Setup: +## Setup Running the compiler requires the Ivy runtime be on the linker search path. Build the runtime: @@ -32,3 +32,18 @@ while simultaneously doing pretty-printing of debug output. To get pretty-printed debug output on a compiled binary, use `tools/trace.py` to run it. + +## Testing + +You can use `tools/test.py` to run all of the project tests. It will ensure +that the compiler and runtime are properly built before running any filetests. + +Filetests are written to be run by `tools/lit.py`, and checked with +`filecheck`. See filetests in the `tests/` directory for examples. + +Recommended `.git/hooks/pre-commit` script: +```bash +#!/bin/bash +set -e +tools/test.py +``` diff --git a/tools/test.py b/tools/test.py new file mode 100755 index 0000000..bf7a2fb --- /dev/null +++ b/tools/test.py @@ -0,0 +1,105 @@ +#!/usr/bin/env python3 +import argparse +import os +import subprocess +import sys +from pathlib import Path + +from fmt import blue, yellow + +ROOT = Path(__file__).parent.parent + + +def header(msg): + print(blue(msg)) + + +def label(msg): + print(yellow(msg)) + + +def cmd(*args): + print(" ".join(args)) + subprocess.run(args, check=True) + + +def main(): + parser = argparse.ArgumentParser( + description="""Run tests for the project + +If no flags are specified, run all tests. +""" + ) + parser.add_argument("--compiler", action="store_true") + parser.add_argument("--runtime", action="store_true") + parser.add_argument("--filetest", action="store_true") + parser.add_argument("--python", action="store_true") + args = parser.parse_args() + + do_all = not any(vars(args).values()) + if do_all: + for k in vars(args): + vars(args)[k] = True + + os.chdir(ROOT) + + # Building and testing the compiler + if args.compiler: + header("Compiler...") + + label("Format...") + cmd("cargo", "fmt", "--", "--check") + + label("Test...") + cmd("cargo", "test", "--quiet") + + if args.filetest: + label("Build...") + cmd("cargo", "build", "--release", "--all") + elif args.filetest: + header("Build compiler...") + cmd("cargo", "build", "--release", "--all") + + # Building and testing the runtime + os.chdir("rt") + if args.runtime: + header("Runtime...") + + label("Format...") + cmd("cargo", "fmt", "--", "--check") + + label("Test...") + cmd("cargo", "test", "--quiet") + + if args.filetest: + label("Build...") + cmd("cargo", "build", "--release", "--all") + elif args.filetest: + header("Build runtime...") + cmd("cargo", "build", "--release", "--all") + os.chdir(ROOT) + + # Filetests + if args.filetest: + header("Filetests...") + + label("Lit...") + cmd("tools/lit.py", "ivy-examples/", "test/") + + # Linting the Python scripts + if args.python: + header("Python...") + + label("Black...") + cmd("black", "test/", "--check", "--diff", "--color") + + label("isort...") + cmd("isort", "test/", "--check", "--diff", "--color") + + +if __name__ == "__main__": + try: + main() + except Exception as e: + print(e) + sys.exit(1)