[tests] verbose (-v) Flag.
authorahmedsamyh <ahmedsamyh10@gmail.com>
Sun, 26 Jan 2025 12:30:32 +0000 (17:30 +0500)
committerahmedsamyh <ahmedsamyh10@gmail.com>
Sun, 26 Jan 2025 12:30:32 +0000 (17:30 +0500)
- [tests] Much nicer output.
- [tests] Keep count and report passing tests count.

test.py
tests/slurp_file.c
tests/slurp_file.out.expected

diff --git a/test.py b/test.py
index 9fed229d9c641112d3d4dfecf8ed9e68f02726a7..98832639aba1a4ce38f727a4d56b2c514cbb4d6c 100644 (file)
--- 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:
index 2349de10c00a795ac994e8dfaac735dead80f1e7..50d4ae42da7d8990d7ff59332584ba595641b06e 100644 (file)
@@ -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;
 }
index 60e6a80601147462cc1f3486a9d64bbd14922876..148364823c270d0a076ef4abb80bc8909e92a4e4 100644 (file)
@@ -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;
 }
 '