From 23f0b51b6db1e6d6721554219388cb8f114f534c Mon Sep 17 00:00:00 2001 From: ahmedsamyh Date: Sun, 26 Jan 2025 17:30:32 +0500 Subject: [PATCH] [tests] verbose (-v) Flag. - [tests] Much nicer output. - [tests] Keep count and report passing tests count. --- test.py | 44 ++++++++++++++++++++++++++--------- tests/slurp_file.c | 4 +--- tests/slurp_file.out.expected | 6 ++--- 3 files changed, 36 insertions(+), 18 deletions(-) diff --git a/test.py b/test.py index 9fed229..9883263 100644 --- a/test.py +++ b/test.py @@ -59,8 +59,13 @@ def hhelp(): Flags: -h - Same as the help subcommand. + -v - Verbose output. ''') +def vlog(verbose_output, msg): + if verbose_output: + print(msg) + def main(): program = sys.argv.pop(0) @@ -70,8 +75,12 @@ def main(): hhelp() exit(1) + flags = [] + # FLAG_VALUES + verbose_output = False + subcmds = [] while len(sys.argv) > 0: @@ -88,6 +97,8 @@ def main(): if flag == 'h': hhelp() exit(0) + elif flag == 'v': + verbose_output = True else: print(f"[ERROR] Invalid flag '{flag}'", file=sys.stderr) exit(1) @@ -112,7 +123,8 @@ def main(): for subcmd in subcmds: total_tests_count = len(tests) - current_test_id = -1 + current_test_id = 0 + passing_tests_count = 0 if subcmd == "help": hhelp() @@ -120,11 +132,13 @@ def main(): elif subcmd == "build": print(f'----- [BUILD] -----') for test_name in tests: - current_test_id += 1 print(f'+ Building {test_name} [{current_test_id+1}/{total_tests_count}]...') + current_test_id += 1 test = tests[test_name] - res = subprocess.run([CC, '-o', test_name, f"{test_name}.c"], + cmd = [CC, '-o', test_name, f"{test_name}.c"] + vlog(verbose_output, f"[CMD] {cmd}") + res = subprocess.run(cmd, capture_output = True, text = True) if res.returncode != 0: @@ -135,23 +149,30 @@ def main(): print('') continue else: + passing_tests_count += 1 print("[PASS] ", end='') + o = False if res.stdout: print(f"{res.stdout}") - else: - print('') - if current_test_id == total_tests_count-1: - print("ALL TESTS BUILD") + o = True + if verbose_output and res.stderr: + print(f"{res.stderr}") + o = True + if not o: print('') + + print(f"Build {passing_tests_count}/{total_tests_count} tests") elif subcmd == "run": print(f'----- [RUN] -----') for test_name in tests: - current_test_id += 1 print(f'+ Running {test_name} [{current_test_id+1}/{total_tests_count}]...') + current_test_id += 1 test = tests[test_name] res = None try: - res = subprocess.run([f"./{test_name}"], capture_output = True, text = True) + cmd = [f"./{test_name}"] + vlog(verbose_output, f"[CMD] {cmd}") + res = subprocess.run(cmd, capture_output = True, text = True) except Exception as e: print(f"[ERROR] Failed to run ./{test_name}: {e}") continue @@ -165,9 +186,10 @@ def main(): print(f"Expected: >>>{test.expected_stdout}>>>") print(f"But Got: >>>{res.stdout}>>>") continue + passing_tests_count += 1 print('[PASS]') - if current_test_id == total_tests_count-1: - print("ALL TESTS PASS") + + print(f"PASSED {passing_tests_count}/{total_tests_count}") elif subcmd == "record": print(f'----- [RECORD] -----') for test_name in tests: diff --git a/tests/slurp_file.c b/tests/slurp_file.c index 2349de1..50d4ae4 100644 --- a/tests/slurp_file.c +++ b/tests/slurp_file.c @@ -1,4 +1,3 @@ - #define COMMONLIB_IMPLEMENTATION #include "../commonlib.h" @@ -10,7 +9,6 @@ int main(void) { c_log_info("buff: '%s'", buff); - C_FREE(buff); - + // We don't care about leaking memory since we are just exiting right away! return 0; } diff --git a/tests/slurp_file.out.expected b/tests/slurp_file.out.expected index 60e6a80..1483648 100644 --- a/tests/slurp_file.out.expected +++ b/tests/slurp_file.out.expected @@ -1,5 +1,4 @@ -[INFO] buff: ' -#define COMMONLIB_IMPLEMENTATION +[INFO] buff: '#define COMMONLIB_IMPLEMENTATION #include "../commonlib.h" int main(void) { @@ -10,8 +9,7 @@ int main(void) { c_log_info("buff: '%s'", buff); - C_FREE(buff); - + // We don't care about leaking memory since we are just exiting right away! return 0; } ' -- 2.39.5