#!/usr/bin/env python3 import sys import io import contextlib from pathlib import Path sys.path.insert(0, str(Path.cwd())) from gx_cli.main import main def test_compile_missing_source(): """Test compile with missing source file""" f = io.StringIO() with contextlib.redirect_stderr(f): exit_code = main(["compile", "/tmp/nonexistent_file_12345.py"]) if exit_code == 0: print("FAIL: should return non-zero for missing source") return False if exit_code != 1: print(f"FAIL: expected exit code 1, got {exit_code}") return False output = f.getvalue() if "not found" not in output.lower(): print("FAIL: error message should mention 'not found'") return False print("PASS: Compile handles missing source file") return True def test_run_missing_gx(): """Test run with missing .gx file""" f = io.StringIO() with contextlib.redirect_stderr(f): exit_code = main(["run", "/tmp/nonexistent_file_12345.gx"]) if exit_code == 0: print("FAIL: should return non-zero for missing .gx") return False if exit_code != 1: print(f"FAIL: expected exit code 1, got {exit_code}") return False print("PASS: Run handles missing .gx file") return True def test_inspect_missing_gx(): """Test inspect with missing .gx file""" f = io.StringIO() with contextlib.redirect_stderr(f): exit_code = main(["inspect", "/tmp/nonexistent_file_12345.gx"]) if exit_code == 0: print("FAIL: should return non-zero for missing .gx") return False if exit_code != 1: print(f"FAIL: expected exit code 1, got {exit_code}") return False print("PASS: Inspect handles missing .gx file") return True def test_summary_missing_gx(): """Test summary with missing .gx file""" f = io.StringIO() with contextlib.redirect_stderr(f): exit_code = main(["summary", "/tmp/nonexistent_file_12345.gx"]) if exit_code == 0: print("FAIL: should return non-zero for missing .gx") return False if exit_code != 1: print(f"FAIL: expected exit code 1, got {exit_code}") return False print("PASS: Summary handles missing .gx file") return True def test_no_command(): """Test with no command provided""" f = io.StringIO() with contextlib.redirect_stdout(f): exit_code = main([]) if exit_code == 0: print("FAIL: should return non-zero with no command") return False if exit_code != 1: print(f"FAIL: expected exit code 1, got {exit_code}") return False output = f.getvalue() if "usage" not in output.lower() and "gx" not in output.lower(): print("FAIL: help/usage should be shown") return False print("PASS: Handles no command gracefully") return True def main_test(): print("[TEST SUITE] test_errors.py") print() tests = [ ("Compile missing source", test_compile_missing_source), ("Run missing .gx", test_run_missing_gx), ("Inspect missing .gx", test_inspect_missing_gx), ("Summary missing .gx", test_summary_missing_gx), ("No command", test_no_command), ] passed = 0 failed = 0 for name, test_func in tests: print(f"Running: {name}...", end=" ") try: if test_func(): passed += 1 else: failed += 1 except Exception as e: print(f"FAIL: {e}") failed += 1 print() print(f"Results: {passed} passed, {failed} failed") return 0 if failed == 0 else 1 if __name__ == "__main__": sys.exit(main_test())