This repository has been archived by the owner on Dec 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
runtest.py
executable file
·374 lines (299 loc) · 11.7 KB
/
runtest.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
#!.venv/bin/python
# mypy: disallow_any_expr = false
from dataclasses import dataclass
import doctest
from importlib import import_module
from pathlib import Path
import subprocess
import shutil
import sys
from test.console import Console
# ======================================================================================
@dataclass
class Options:
test_runner: str
console: Console
module_name: str = ''
verbose: bool = False
def make_verbose(self) -> None:
self.verbose = True
self.console.verbose = True
def test_command(self) -> list[str]:
command = [sys.executable, self.test_runner]
if self.verbose:
command.append('-v')
return command
def run_tests(options: Options) -> int:
console = options.console
console.info("Getting started with Tsutsumu's test suite...")
console.detail(f'Running "{sys.executable}"')
console.detail(f' - Python {sys.version}')
try:
import tsutsumu
from tsutsumu.bundle import Toolbox
except ImportError:
console.error('Unable to import tsutsumu')
sys.exit(1)
console.detail(f'Testing tsutsumu {tsutsumu.__version__}')
cwd = Path('.').absolute()
tmpdir = cwd / 'tmp'
shutil.rmtree(tmpdir, ignore_errors=True)
tmpdir.mkdir()
# ----------------------------------------------------------------------------------
console.info('Running unit tests...')
for module in (
'test.cargo_version',
'test.cargo_extra',
):
console.detail(f'╭──── {module}')
subprocess.run([*options.test_command(), 'run-test-module', module], check=True)
console.detail('╰─╼')
# ----------------------------------------------------------------------------------
console.info('Testing ingestion from pyproject.toml...')
from cargo.distinfo import DistInfo
pyproject_path = Path('pyproject.toml').absolute()
distinfo = DistInfo.from_pyproject(pyproject_path)
for key, expected in (
('name', 'tsutsumu'),
('extras', ()),
('version', tsutsumu.__version__),
('summary', 'Simple, flexible module bundling for Python'),
('homepage', 'https://github.com/apparebit/tsutsumu'),
('required_python', '>=3.7'),
('required_packages', ('packaging',)),
('provenance', str(pyproject_path)),
):
actual = getattr(distinfo, key)
message = f'distinfo.{key} is {actual} instead of {expected}'
assert actual == expected, message
# ----------------------------------------------------------------------------------
console.info('Rebuilding repository bundles...')
# This bundle must be regenerated before documentation test, which uses it.
subprocess.run([
sys.executable,
'-m', 'tsutsumu',
'-o', str(cwd / 'bundles' / 'can.py'),
'spam'
],
check=True
)
console.detail('Rebuilt bundles/can.py')
subprocess.run([
sys.executable,
'-m', 'tsutsumu',
'-o', str(cwd / 'bundles' / 'bundler.py'),
'-r',
'tsutsumu', 'cargo',
],
check=True
)
console.detail('Rebuilt bundles/bundler.py')
# ----------------------------------------------------------------------------------
console.info('Running documentation tests...')
try:
sys.argv.remove('-v')
except:
pass
# Since documentation test uses bundles/can.py, we regenerate it first.
doc_failures, doc_tests = doctest.testfile(
'README.md', optionflags=doctest.REPORT_NDIFF)
if doc_failures != 0:
console.error(f'{doc_failures}/{doc_tests} documentation tests failed!')
sys.exit(1)
console.detail(f'All {doc_tests} documentation tests passed')
# ----------------------------------------------------------------------------------
console.info('Bundling Tsutsumu without and with repackaging...')
subprocess.run([
sys.executable,
'-m', 'tsutsumu',
'-o', str(tmpdir / 'bundler.py'),
'tsutsumu',
],
check=True
)
console.detail('Created tmp/bundler.py')
subprocess.run([
sys.executable,
'-m', 'tsutsumu',
'-r',
'-o', str(tmpdir / 'repackaged-bundler.py'),
'tsutsumu',
],
check=True
)
console.detail('Created tmp/repackaged-bundler.py')
# ----------------------------------------------------------------------------------
console.info('Bundling spam with regular, bundled, and repackaged Tsutsumu...')
subprocess.run([
sys.executable,
'-m', 'tsutsumu',
'-o', str(tmpdir / 'can1.py'),
'spam'
],
check=True
)
console.detail('Created tmp/can1.py with regular Tsutsumu')
subprocess.run([
sys.executable,
str(tmpdir / 'bundler.py'),
'-o', str(tmpdir / 'can2.py'),
'spam'
],
check=True
)
console.detail('Created tmp/can2.py with bundled tsutsumu')
subprocess.run([
sys.executable,
str(tmpdir / 'repackaged-bundler.py'),
'-o', str(tmpdir / 'can3.py'),
'spam'
],
check=True
)
console.detail('Created tmp/can3.py with repackaged tsutsumu')
# ----------------------------------------------------------------------------------
console.info('Comparing cans of bundled spam...')
files = [(tmpdir/ f'can{index}.py').read_bytes() for index in range(1, 4)]
mismatch = False
if files[0] != files[1]:
console.detail('tmp/can1.py and tmp/can2.py differ')
mismatch = True
if files[0] != files[2]:
console.detail('tmp/can1.py and tmp/can3.py differ')
mismatch = True
if mismatch:
console.error(
'Regular and bundled versions of Tsutsumu generated different bundles!')
sys.exit(1)
console.detail('All three cans contain the exact same spam!')
# ----------------------------------------------------------------------------------
console.info('Comparing bundled binary file to original...')
original = (cwd / 'spam' / 'bacon.jpg').read_bytes()
err_count = 0
for index in range(1, 4):
filename = f'can{index}.py'
path = tmpdir / filename
manifest = Toolbox.load_meta_data(path)[1]
kind, offset, length = manifest['spam/bacon.jpg']
if kind != 'b':
console.detail(
f'Bundled image in "tmp/{filename}" has kind "{kind}", not "b"')
err_count += 1
canned_bacon = Toolbox.load_from_bundle(path, kind, offset, length)
if original == canned_bacon:
console.detail(f'Bundled image in "tmp/{filename}" is the same as original')
else:
console.detail(f'Bundled image in "tmp/{filename}" differs from original:')
lines = Toolbox.read(path, offset, length).splitlines()
for line in lines[:3]:
console.detail(f' {line!r}')
console.detail(' ...')
err_count += 1
if err_count > 0:
console.error(f'Bundling of binary files is broken!')
raise SystemExit(1)
# ----------------------------------------------------------------------------------
console.info('Comparing repackaged Tsutsumu modules to originals...')
completion = subprocess.run([*options.test_command(), 'run-repackaged-module-test'])
if completion.returncode != 0:
console.error('Repackaged modules differ from originals!')
sys.exit(1)
# ----------------------------------------------------------------------------------
console.success('W00t! All tests passed!')
shutil.rmtree(tmpdir)
return 0
# ======================================================================================
def run_module_test(options: Options) -> int:
console = options.console
module = import_module(options.module_name)
errors = 0
for key in dir(module):
if not key.startswith('test_'):
continue
value = getattr(module, key)
if not callable(value):
continue
console.detail(f'├─ {value.__name__}')
with console.new_prefix('│ '):
try:
value(options.console)
except Exception as x:
console.exception(x)
errors += 1
return bool(errors + console.failed_assertions)
# --------------------------------------------------------------------------------------
def run_repackaged_module_test(options: Options) -> int:
console = options.console
# We cannot import Bundle and Toolbox without also importing tsutsumu's
# __init__ and bundle modules, which breaks repackage(). At the same time,
# we can still read, compile, and exec the bundle module.
# mypy: Expression type contains "Any" (has type "type[Path]") — Huh??
cwd = Path.cwd().absolute()
mod_bundle_path = cwd / 'tsutsumu' / 'bundle.py'
mod_bundle_binary = compile(
mod_bundle_path.read_bytes(),
mod_bundle_path,
'exec',
dont_inherit=True,
)
mod_bundle_bindings: dict[str, object] = dict()
exec(mod_bundle_binary, mod_bundle_bindings)
Toolbox = mod_bundle_bindings['Toolbox']
Bundle = mod_bundle_bindings['Bundle']
repackaged_path = cwd / 'tmp' / 'repackaged-bundler.py'
version, manifest = (
Toolbox.load_meta_data(repackaged_path)) # type: ignore[attr-defined]
bundle = (Bundle.install( # type: ignore[attr-defined]
repackaged_path, version, manifest))
bundle.repackage()
# Compare repackaged modules to their originals.
package_path = cwd / 'tsutsumu'
bundled_package_path = repackaged_path / 'tsutsumu'
is_different = False
for module in ('__init__.py', 'bundle.py'):
original = (package_path / module).read_bytes()
repackaged = bundle[str(bundled_package_path / module)]
if original == repackaged:
console.detail(f'Repackaged "tsutsumu/{module}" matched original')
else:
console.error(f'Repackaged "tsutsumu/{module}" did NOT match original:')
console.detail(f'{repackaged!r}')
is_different = True
return is_different
# --------------------------------------------------------------------------------------
if __name__ == '__main__':
try:
options = Options(sys.argv[0], Console(sys.stdout))
console = options.console
fn = run_tests
module = None
for arg in sys.argv[1:]:
if arg == '-v':
options.make_verbose()
elif arg == 'run-test-module':
fn = run_module_test
elif arg == 'run-repackaged-module-test':
fn = run_repackaged_module_test
elif fn == run_module_test and options.module_name == '':
options.module_name = arg
else:
raise SystemExit(f'unrecognized command line argument "{arg}"')
if fn == run_module_test and options.module_name == '':
raise SystemExit('can\'t "run-test-module" without module name')
sys.exit(fn(options))
except SystemExit as x:
if isinstance(x.args[0], str):
console.error(x.args[0])
code = 1
elif isinstance(x.args[0], int):
code = x.args[0]
sys.exit(code)
except subprocess.CalledProcessError as x:
cmd = list(x.cmd)
if (Path('.') / '.venv/bin/python').samefile(cmd[0]):
cmd[0] = 'python'
console.info(
f'command "{" ".join(cmd)}" failed with exit status {x.returncode}')
except Exception as x:
console.exception(x)
sys.exit(1)