]> www.git.momoyon.org Git - lang.git/commitdiff
Done TODO: option to create test file if it doesn't exist.
authorahmedsamyh <ahmedsamyh10@gmail.com>
Sat, 16 Nov 2024 09:49:00 +0000 (14:49 +0500)
committerahmedsamyh <ahmedsamyh10@gmail.com>
Sat, 16 Nov 2024 09:49:00 +0000 (14:49 +0500)
test.py

diff --git a/test.py b/test.py
index a8a78d1d8b5896922641f71ec9435a968f8de8af..ef8d0ba4fcd664e0af6eff487c0fe84bc5c88d8e 100644 (file)
--- a/test.py
+++ b/test.py
@@ -20,20 +20,27 @@ def cmd(args: [str], echo=False) -> subprocess.CompletedProcess:
         print("\"")
     return subprocess.run(args, capture_output=True, text=True)
 
+def info(msg: str):
+    print(f"INFO: {msg}")
 
 def test_source_file(filename: str):
     if not os.path.exists(filename):
         error(f"File {filename} doesn't exist!")
         exit(1)
 
-    # TODO: option to record/update test file
     testfilename: str = filename + TEST_FILE_SUFFIX
     if not os.path.exists(testfilename):
         error("Test file {testfilename} doesn't exist!")
+        ans = input("Create test file with current output? [y/N]")
+        if ans.lower() == "y" or ans.lower() == "yes":
+            output = cmd(["python", "./main.py", filename]).stdout
+            info(f"Creating test file {testfilename}")
+            with open(testfilename, 'w') as f:
+                f.write(output)
+
         exit(1)
 
     p = cmd(["python", "./main.py", filename])
-
     output = p.stdout
 
     f = open(testfilename, 'r')
@@ -44,6 +51,11 @@ def test_source_file(filename: str):
         print("Failed!")
         print(f"Expected: `{expected_output}`")
         print(f"Got:      `{output}`")
+        ans = input("\nUpdate output? [y/N]")
+        if ans.lower() == "y" or ans.lower() == "yes":
+            info(f"Updating test file {testfilename}")
+            with open(testfilename, 'w') as f:
+                f.write(output)
     else:
         print("Success!")