-
Notifications
You must be signed in to change notification settings - Fork 2
/
rere.py
executable file
·165 lines (146 loc) · 6.59 KB
/
rere.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
#!/usr/bin/env python3
# Copyright 2024 Alexey Kutepov <[email protected]>
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
import sys
import subprocess
from difflib import unified_diff
from typing import List, BinaryIO, Tuple, Optional
def read_blob_field(f: BinaryIO, name: bytes) -> bytes:
line = f.readline()
field = b':b ' + name + b' '
assert line.startswith(field), field
assert line.endswith(b'\n')
size = int(line[len(field):-1])
blob = f.read(size)
assert f.read(1) == b'\n'
return blob
def read_int_field(f: BinaryIO, name: bytes) -> int:
line = f.readline()
field = b':i ' + name + b' '
assert line.startswith(field)
assert line.endswith(b'\n')
return int(line[len(field):-1])
def write_int_field(f: BinaryIO, name: bytes, value: int):
f.write(b':i %s %d\n' % (name, value))
def write_blob_field(f: BinaryIO, name: bytes, blob: bytes):
f.write(b':b %s %d\n' % (name, len(blob)))
f.write(blob)
f.write(b'\n')
def capture(shell: str) -> dict:
print(f"CAPTURING: {shell}")
process = subprocess.run(['sh', '-c', shell], capture_output = True)
return {
'shell': shell,
'returncode': process.returncode,
'stdout': process.stdout,
'stderr': process.stderr,
}
def load_list(file_path: str) -> list[str]:
with open(file_path) as f:
return [line.strip() for line in f]
def dump_snapshots(file_path: str, snapshots: list[dict]):
with open(file_path, "wb") as f:
write_int_field(f, b"count", len(snapshots))
for snapshot in snapshots:
write_blob_field(f, b"shell", bytes(snapshot['shell'], 'utf-8'))
write_int_field(f, b"returncode", snapshot['returncode'])
write_blob_field(f, b"stdout", snapshot['stdout'])
write_blob_field(f, b"stderr", snapshot['stderr'])
def load_snapshots(file_path: str) -> list[dict]:
snapshots = []
with open(file_path, "rb") as f:
count = read_int_field(f, b"count")
for _ in range(count):
shell = read_blob_field(f, b"shell")
returncode = read_int_field(f, b"returncode")
stdout = read_blob_field(f, b"stdout")
stderr = read_blob_field(f, b"stderr")
snapshot = {
"shell": shell,
"returncode": returncode,
"stdout": stdout,
"stderr": stderr,
}
snapshots.append(snapshot)
return snapshots
if __name__ == '__main__':
program_name, *argv = sys.argv
if len(argv) == 0:
print(f'Usage: {program_name} <record|replay> <test.list>')
print('ERROR: no subcommand is provided')
exit(1)
subcommand, *argv = argv
if subcommand == 'record':
if len(argv) == 0:
print(f'Usage: {program_name} {subcommand} <test.list>')
print('ERROR: no test.list is provided')
exit(1)
test_list_path, *argv = argv
snapshots = [capture(shell.strip()) for shell in load_list(test_list_path)]
dump_snapshots(f'{test_list_path}.bi', snapshots)
elif subcommand == 'replay':
if len(argv) == 0:
print(f'Usage: {program_name} {subcommand} <test.list>')
print('ERROR: no test.list is provided')
exit(1)
test_list_path, *argv = argv
shells = load_list(test_list_path)
snapshots = load_snapshots(f'{test_list_path}.bi')
if len(shells) != len(snapshots):
print(f"UNEXPECTED: Amount of shell commands in f{test_list_path}")
print(f" EXPECTED: {len(snapshots)}")
print(f" ACTUAL: {len(shells)}")
print(f"NOTE: You may want to do `{program_name} record {test_list_path}` to update {test_list_path}.bi")
exit(1)
for (shell, snapshot) in zip(shells, snapshots):
print(f"REPLAYING: {shell}")
snapshot_shell = snapshot['shell'].decode('utf-8')
if shell != snapshot_shell:
print(f"UNEXPECTED: shell command")
print(f" EXPECTED: {snapshot_shell}")
print(f" ACTUAL: {shell}")
print(f"NOTE: You may want to do `{program_name} record {test_list_path}` to update {test_list_path}.bi")
exit(1)
process = subprocess.run(['sh', '-c', shell], capture_output = True);
failed = False
if process.returncode != snapshot['returncode']:
print(f"UNEXPECTED: return code")
print(f" EXPECTED: {snapshot['returncode']}")
print(f" ACTUAL: {process.returncode}")
failed = True
if process.stdout != snapshot['stdout']:
# TODO: support binary outputs
a = snapshot['stdout'].decode('utf-8').splitlines(keepends=True)
b = process.stdout.decode('utf-8').splitlines(keepends=True)
print(f"UNEXPECTED: stdout")
for line in unified_diff(a, b, fromfile="expected", tofile="actual"):
print(line, end='')
failed = True
if process.stderr != snapshot['stderr']:
a = snapshot['stderr'].decode('utf-8').splitlines(keepends=True)
b = process.stderr.decode('utf-8').splitlines(keepends=True)
print(f"UNEXPECTED: stderr")
for line in unified_diff(a, b, fromfile="expected", tofile="actual"):
print(line, end='')
failed = True
if failed:
exit(1)
print('OK')
else:
print(f'ERROR: unknown subcommand {subcommand}');
exit(1);