-
Notifications
You must be signed in to change notification settings - Fork 3
/
tester.py
executable file
·118 lines (96 loc) · 3.47 KB
/
tester.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/usr/bin/env python3
import argparse
import os
import subprocess as sp
import sys
class Parser:
def __init__(self):
self.parser = argparse.ArgumentParser(prog="Corewar Tester")
self.parser.add_argument(
"-d",
"--directory",
type=str,
help='Path to the directory containing the ".cor" files',
required=True,
)
self.parser.add_argument(
"-ci",
"--exec_in_ci",
action=argparse.BooleanOptionalAction,
help="If the script is executed in a CI environment",
default=False,
)
def parse_args(self) -> dict:
return {k: v for k, v in self.parser.parse_args()._get_kwargs()}
class Tester:
directory: str
files: list[str]
failed: int
passed: int
is_ci: bool
def __init__(self, directory: str, is_ci: bool = False):
self.is_ci = is_ci
self.directory = directory
self.files = [file for file in os.listdir(directory) if file.endswith(".cor")]
self.files = [os.path.join(directory, file) for file in self.files]
self.failed = 0
self.passed = 0
def test(self) -> None:
print(f"\nTesting: 2 files\n")
for file in self.files:
for file2 in self.files:
print(f"Testing: {file} & {file2}")
self.run_test(file, file2)
print(f"\nTesting: 3 files\n")
for file in self.files:
for file2 in self.files:
for file3 in self.files:
print(f"Testing: {file} & {file2} & {file3}")
self.run_test(file, file2, file3)
if not self.is_ci:
print(f"\nTesting: 4 files\n")
for file in self.files:
for file2 in self.files:
for file3 in self.files:
for file4 in self.files:
print(f"Testing: {file} & {file2} & {file3} & {file4}")
self.run_test(file, file2, file3, file4)
print(f"Passed: {self.passed}")
print(f"Failed: {self.failed}")
def run_test(self, *files) -> None:
try:
proc = sp.Popen(
["./corewar/corewar", *files], stdout=sp.PIPE, stderr=sp.PIPE
)
stdout, stderr = proc.communicate()
rc = proc.returncode
err = stderr.decode("utf-8")
if rc != 0 or err != "":
print(
f"::error title={'-'.join(files)}::Corewar returned with error: {repr(err)}"
)
print(f"Corewar returned with error code: {rc}")
self.failed += 1
else:
buff = stdout.decode("utf-8").split("\n")
if len(buff) < 3:
self.passed += 1
return
if (
buff[-2].split("player ")[1].split("(")[0]
== buff[-3].split("player ")[1].split("(")[0]
):
self.passed += 1
else:
print(f"::error title={'-'.join(files)}::Corewar wrong winner")
self.failed += 1
except FileNotFoundError:
print("Corewar not found")
sys.exit(1)
def main():
args = Parser().parse_args()
print("Corewar Tester")
tester = Tester(args["directory"], args["exec_in_ci"])
tester.test()
if __name__ == "__main__":
main()