-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
171 lines (135 loc) · 4.57 KB
/
test.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
"""Check all test cases and generate TEST.txt"""
from __future__ import annotations
import json
import os
import subprocess
from itertools import chain, repeat
from pathlib import Path
def test_files(directory: str) -> list[Path]:
return sorted(
[
file
for file in map(lambda file: Path(directory, file), os.listdir(directory))
if file.is_file() and file.suffix == ".snap"
]
)
def escape(input: str) -> str:
return input.encode("unicode_escape").decode("utf8")
class Test:
ParseError = str
def __init__(
self, title: str, description: str, args: str, input: list[str], expected: str
):
self.title = title
self.description = description
self.args = args
self.input = input
self.expected = expected
@staticmethod
def from_str(input: str) -> Test | ParseError:
front_matter, sep, expected = input[3:].partition("---\n")
if len(sep) == 0:
return Test.ParseError("Missing frontmatter seperator (---)")
metadata = json.loads(front_matter)
return Test(
title=metadata["title"],
description=metadata["description"],
args=metadata["args"],
input=metadata["input"],
expected=expected,
)
def to_str(self) -> str:
dict = self.__dict__.copy()
dict.pop("expected")
return f"---\n{json.dumps(dict, indent=2)}\n---\n{self.expected}"
def run_test(test: Test) -> str:
cmd = f"./tiny-calc {test.args}"
with subprocess.Popen(
cmd,
shell=True,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
text=True,
) as child:
for input in test.input:
child.stdin.write(input + "\n")
child.stdin.close()
child.wait()
return child.stdout.read()
def validate_snapshot(path: Path) -> Test:
print(f"\nRunning '{path}':")
test = Test.from_str(path.read_text())
if type(test) is Test.ParseError:
print(f"\n'{path}': Invalid test file format\n {test}\n")
exit(-1)
stdout = run_test(test)
if test.expected == stdout:
print("✔ Success")
return test
# print(f" Command: '{cmd}'")
# print(f" ReturnCode: {returncode}")
# print(f" Input: {test.input}")
# print(f" Expected: '{escape(test.expected)}'")
# print(f" Stdout: '{escape(stdout)}'")
print("✗ Failure")
new_test = Test(
title=test.title,
description=test.description,
args=test.args,
input=test.input,
expected=stdout,
)
new_path = path.with_suffix(".snap.new")
Path(new_path).write_text(new_test.to_str())
subprocess.run(
f"PAGER='' git diff --no-index {path} {new_path}",
shell=True,
)
answer = input("Accept this change? [Y/n]: ").strip().lower()
if answer == "n":
new_path.unlink()
else:
new_path.rename(path)
return new_test
def combine_input_output(test: Test) -> str:
if test.expected.find(">> ") == -1:
return test.expected
lines = test.expected.split(">> ")
input = chain(map(lambda input: input + "\n", test.input), repeat(""))
with_input = map(
lambda zipped: zipped[0] + ">> " + zipped[1],
zip(lines, input),
)
return "".join(with_input).rstrip(">> ")
def main():
with open("TEST.txt", mode="w") as test_docs:
test_docs.write(
"# Tests\n\n"
"An introduction and general documentation about the project structure is available in `README.md`.\n\n"
"This file was auto generated by the `test.py` script.\n"
"Snapshot tests can be found in the `tests` folder.\n"
"The output of a test combines `stdin`, `stdout` and `stderr` into one string.\n\n\n"
)
for path in test_files("tests"):
test = validate_snapshot(path)
description = (
f"{test.description}\n\n"
if len(test.description.strip()) > 0
else "" ""
)
inputs = ", ".join(
map(lambda input: f'"{escape(input + "\n")}"', test.input)
)
output = combine_input_output(test)
test_docs.write(
f"## {test.title}\n\n"
f"{description}"
f"- Command: tiny-calc {test.args}\n"
f"- Inputs: [{inputs}]\n"
f"- Output:\n"
"```\n"
f"{output}\n"
"```\n\n"
)
main()