#!/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" def print_header(header, width, padding): print( f"{TermFormat.BOLD}{'=' * (int(width / 3) + padding)}{header}{'=' * (int(width / 3) + padding)}{TermFormat.ENDC}" ) 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)) print_header("helloJSON TEST SUITE", name_width, 9) print() print_header("VALID FILES", name_width, 13) for vf in valid_files: with open(vf, "r") as infile: try: original = json.load(infile) except json.JSONDecodeError as e: print( f"\n{TermFormat.BOLD}{TermFormat.FAIL}ERROR:{TermFormat.ENDC} Failed to decode {str(vf)}\n{e.msg}" ) continue cmd = [hj_exec, vf] proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) proc.wait() stdout, _ = proc.communicate() try: parsed = json.loads(stdout) except: parsed = None pass match = original == parsed print( f"\n{str(vf):{name_width + 3}} {TermFormat.BOLD}{TermFormat.OKGREEN if match else TermFormat.FAIL}{'PASSED' if match else 'FAILED'}{TermFormat.ENDC}" )