]> www.git.momoyon.org Git - commonlib.git/commitdiff
Changed to python test script.
authormomoyonwork <ahmedsamyhwork@gmail.com>
Sat, 25 Jan 2025 16:10:32 +0000 (21:10 +0500)
committermomoyonwork <ahmedsamyhwork@gmail.com>
Sat, 25 Jan 2025 16:10:32 +0000 (21:10 +0500)
- Can build and run tests.
- TODO: record expected behaviour of tests, instead of manually doing
  so.

12 files changed:
test.py
tests/arena.code.expected [new file with mode: 0644]
tests/arena.err.expected [new file with mode: 0644]
tests/arena.out.expected [new file with mode: 0644]
tests/arena.out.out.expected [new file with mode: 0644]
tests/logging.code.expected [new file with mode: 0644]
tests/logging.err.expected [new file with mode: 0644]
tests/logging.err.out.expected [new file with mode: 0644]
tests/logging.out.expected [new file with mode: 0644]
tests/logging.out.out.expected [new file with mode: 0644]
tests/tmp.stderr [new file with mode: 0644]
tests/tmp.stdout [new file with mode: 0644]

diff --git a/test.py b/test.py
index 061d4059f1285023f71a7f5d33015996b83905e3..0816d23676bc5b7a956202664bd3e7861fc931b8 100644 (file)
--- a/test.py
+++ b/test.py
@@ -2,15 +2,40 @@ import os
 import subprocess
 import sys
 
+CC="gcc"
 TESTS_DIR="./tests/"
 
+class Test:
+    expected_stdout = None
+    expected_stderr = None
+    expected_returncode = -1
+
+    def __init__(self, name):
+        self.name = name
+
+        def read_or_create_expected_file(name: str) -> str:
+            f = f"{self.name}.{name}.expected"
+            if not os.path.exists(f):
+                with open(f, "w") as file:
+                    print(f"[INFO] Created empty {self.name}.{name}.expected")
+                return ""
+            else:
+                with open(f, "r") as file:
+                     return file.read()
+
+        self.expected_stdout = read_or_create_expected_file("out")
+        self.expected_stderr = read_or_create_expected_file("err")
+        self.expected_returncode = read_or_create_expected_file("code")
+
+        # if self.expected_stdout: print(f"{self.name}.out.expected: {self.expected_stdout}")
+        # if self.expected_stderr: print(f"{self.name}.err.expected: {self.expected_stderr}")
+
 def usage(program: str):
     print(f"Usage: {program} <subcmd>")
 
 # NOTE: We named this hhelp because help is a builtin python function
 def hhelp():
     print('''
-
     Subcommands:
         help    - Prints this help message.
         build   - Builds all the tests.
@@ -19,18 +44,77 @@ def hhelp():
 
 def main():
     og_dir = os.getcwd()
-    program = sys.argv.pop()
-
-    if len(sys.arg) <= 0:
+    program = sys.argv.pop(0)
 
+    if len(sys.argv) <= 0:
+        print("[ERROR] Please provide a subcommand!", file=sys.stderr)
         usage(program)
         hhelp()
+        exit(1)
+
+    subcmd = sys.argv.pop(0)
 
     # Change to tests dir
     os.chdir(TESTS_DIR)
 
+    tests = {}
+
     for e in os.listdir(os.getcwd()):
-        print(f"entry: {e}")
+        if not e.endswith(".c"): continue
+        base_name = e.removesuffix(".c")
+        if not tests.get(base_name):
+            tests[base_name] = Test(base_name)
+
+    # for v in tests:
+    #     print(f"{v}: {tests[v]}")
+
+    if subcmd == "help":
+        hhelp()
+        exit(0)
+    elif subcmd == "build":
+        for test_name in tests:
+            print(f'+ Building {test_name}...')
+            test = tests[test_name]
+
+            res = subprocess.run([CC, '-o', test_name, f"{test_name}.c"],
+                                 capture_output = True,
+                                 text = True)
+            if res.returncode != 0:
+                print("[FAILED] ", end='')
+                if res.stderr:
+                    print(f"{res.stderr}")
+                else:
+                    print('')
+                continue
+            else:
+                print("[SUCCESS] ", end='')
+                if res.stdout:
+                    print(f"{res.stdout}")
+                else:
+                    print('')
+
+    elif subcmd == "run":
+        for test_name in tests:
+            print(f'+ Running {test_name}...')
+            test = tests[test_name]
+
+            res = subprocess.run([f"./{test_name}"],
+                                 capture_output = True,
+                                 text = True)
+
+            if len(test.expected_returncode) <= 0:
+                print(f"[WARNING] Test doesn't have any expected returncode!")
+                print(f"[WARNING] Please record the expected behaviour of the test using the 'record' subcommand!")
+
+            if res.stdout != test.expected_stdout:
+                print('[FAILED]', file=sys.stderr)
+                print(f"Expected: >>>{test.expected_stdout}>>>")
+                print(f"But Got: >>>{res.stdout}>>>")
+                continue
+            print('[SUCCESS]')
+    else:
+        print(f"[ERROR] Invalid subcommand '{subcmd}'", file=sys.stderr)
+        exit(1)
 
 if __name__ == "__main__":
     main()
diff --git a/tests/arena.code.expected b/tests/arena.code.expected
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/tests/arena.err.expected b/tests/arena.err.expected
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/tests/arena.out.expected b/tests/arena.out.expected
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/tests/arena.out.out.expected b/tests/arena.out.out.expected
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/tests/logging.code.expected b/tests/logging.code.expected
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/tests/logging.err.expected b/tests/logging.err.expected
new file mode 100644 (file)
index 0000000..420d108
--- /dev/null
@@ -0,0 +1 @@
+[ERROR] This one is an error log
diff --git a/tests/logging.err.out.expected b/tests/logging.err.out.expected
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/tests/logging.out.expected b/tests/logging.out.expected
new file mode 100644 (file)
index 0000000..e3a0d36
--- /dev/null
@@ -0,0 +1,2 @@
+[INFO] This is an info log
+[WARNING] And this is a warning log
diff --git a/tests/logging.out.out.expected b/tests/logging.out.out.expected
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/tests/tmp.stderr b/tests/tmp.stderr
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/tests/tmp.stdout b/tests/tmp.stdout
new file mode 100644 (file)
index 0000000..e69de29