From f7afa44ded44091f0b9d4ec2e6158aaef4985a33 Mon Sep 17 00:00:00 2001 From: momoyonwork Date: Sat, 25 Jan 2025 21:28:31 +0500 Subject: [PATCH] Implemented 'record' subcommand... - TODO: Take input for the test while recording and pass it while running. --- test.py | 37 +++++++++++++++++++++++++++++++++- tests/arena.code.expected | 1 + tests/arena.out.out.expected | 0 tests/logging.code.expected | 1 + tests/logging.err.out.expected | 0 tests/logging.out.out.expected | 0 tests/tmp.stderr | 0 tests/tmp.stdout | 0 8 files changed, 38 insertions(+), 1 deletion(-) delete mode 100644 tests/arena.out.out.expected delete mode 100644 tests/logging.err.out.expected delete mode 100644 tests/logging.out.out.expected delete mode 100644 tests/tmp.stderr delete mode 100644 tests/tmp.stdout diff --git a/test.py b/test.py index 0816d23..4685437 100644 --- a/test.py +++ b/test.py @@ -26,9 +26,23 @@ class Test: 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_returncode == '': + self.expected_returncode = -1 + else: + self.expected_returncode = int(self.expected_returncode) # 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 save_expected(self): + def write_expected(name: str, content: str) -> str: + f = f"{self.name}.{name}.expected" + with open(f, "w") as file: + file.write(content) + + write_expected("out", self.expected_stdout) + write_expected("err", self.expected_stderr) + write_expected("code", str(self.expected_returncode)) + def usage(program: str): print(f"Usage: {program} ") @@ -40,6 +54,7 @@ def hhelp(): help - Prints this help message. build - Builds all the tests. run - Runs all the tests. + record - Records the expected behaviour of all the tests. ''') def main(): @@ -102,7 +117,7 @@ def main(): capture_output = True, text = True) - if len(test.expected_returncode) <= 0: + if test.expected_returncode == -1: 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!") @@ -112,6 +127,26 @@ def main(): print(f"But Got: >>>{res.stdout}>>>") continue print('[SUCCESS]') + elif subcmd == "record": + for test_name in tests: + print(f'+ Recording expected behaviour for {test_name}...') + test = tests[test_name] + + prompt_msg = "Record current behaviour as the expected one? [y/N]" + ans = input(prompt_msg) + + if ans.lower() == "y": + res = subprocess.run([f"./{test_name}"], + capture_output = True, + text = True) + tests[test_name].expected_stdout = res.stdout + tests[test_name].expected_stderr = res.stderr + tests[test_name].expected_returncode = res.returncode + tests[test_name].save_expected() + print('[SUCCESS] Recorded expected behaviour') + else: + print('[SKIP]') + else: print(f"[ERROR] Invalid subcommand '{subcmd}'", file=sys.stderr) exit(1) diff --git a/tests/arena.code.expected b/tests/arena.code.expected index e69de29..c227083 100644 --- a/tests/arena.code.expected +++ b/tests/arena.code.expected @@ -0,0 +1 @@ +0 \ No newline at end of file diff --git a/tests/arena.out.out.expected b/tests/arena.out.out.expected deleted file mode 100644 index e69de29..0000000 diff --git a/tests/logging.code.expected b/tests/logging.code.expected index e69de29..c227083 100644 --- a/tests/logging.code.expected +++ b/tests/logging.code.expected @@ -0,0 +1 @@ +0 \ No newline at end of file diff --git a/tests/logging.err.out.expected b/tests/logging.err.out.expected deleted file mode 100644 index e69de29..0000000 diff --git a/tests/logging.out.out.expected b/tests/logging.out.out.expected deleted file mode 100644 index e69de29..0000000 diff --git a/tests/tmp.stderr b/tests/tmp.stderr deleted file mode 100644 index e69de29..0000000 diff --git a/tests/tmp.stdout b/tests/tmp.stdout deleted file mode 100644 index e69de29..0000000 -- 2.39.5