helloJSON/run_tests.py

51 lines
1.0 KiB
Python
Executable File

#!/bin/env python3
import json
import subprocess
from pathlib import Path
class TermFormat:
OKGREEN = "\033[92m"
FAIL = "\033[91m"
ENDC = "\033[0m"
BOLD = "\033[1m"
test_dir = Path(__file__).parent / "test_files"
hj_exec = Path(__file__).parent / "main"
valid_files = [
json_file
for json_file in test_dir.iterdir()
if json_file.is_file()
and "json" in json_file.suffix
and "invalid" not in json_file.stem
]
name_width = 0
for vf in valid_files:
if len(str(vf)) > name_width:
name_width = len(str(vf))
for vf in valid_files:
with open(vf, "r") as infile:
original = json.load(infile)
cmd = [hj_exec, vf]
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
proc.wait()
stdout, _ = proc.communicate()
parsed = json.loads(stdout)
match = original == parsed
print(
f"{str(vf):<{name_width + 3}} {TermFormat.BOLD}{TermFormat.OKGREEN if match else TermFormat.FAIL}{'PASSED' if match else 'FAILED'}{TermFormat.ENDC}"
)