-
-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathtest_regrtest.py
131 lines (94 loc) · 3.31 KB
/
test_regrtest.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
import os
import platform
import shutil
import sys
import pytest
from python_minifier import minify
try:
import yaml
except ImportError:
pass
class Manifest(object):
"""
The test manifest for a python interpreter
:param str interpreter: The python interpreter to use
"""
def __init__(self, interpreter):
self._interpreter = interpreter
self._manifest_path = 'xtest/manifests/' + interpreter + '_test_manifest.yaml'
self._files = {}
self.load()
if interpreter == 'pypy':
self._base_path = '/usr/lib64/pypy-7.0/lib-python/2.7/test/'
elif interpreter == 'pypy3':
self._base_path = '/usr/lib64/pypy3-7.0/lib-python/3/test/'
else:
self._base_path = os.path.join('/usr/lib64', interpreter, 'test')
def load(self):
with open(self._manifest_path) as f:
self._files = yaml.safe_load(f) or {}
def __len__(self):
return sum([len(test_cases) for test_cases in self._files.values()])
def __iter__(self):
for path in sorted(self._files.keys()):
for test_case in self._files[path]:
yield Case(path, **test_case['options'])
def verify(self):
"""
Verify all test cases in the manifest pass
"""
print('1..%i' % len(self))
failed = 0
for i, test_case in enumerate(self):
try:
test_case.run_test()
print('ok %i - %s' % (i, test_case))
except Exception as e:
failed += 1
print('not ok %i - %s' % (i, test_case))
print(e)
return failed
class Case(object):
def __init__(self, test_path, **options):
self.test_path = test_path
self.options = options
def __repr__(self):
return 'Case(%r, **%r)' % (self.test_path, self.options)
def __str__(self):
return '%s with options %r' % (self.test_path, self.options)
def run_test(self):
from sh import Command, ErrorReturnCode
ErrorReturnCode.truncate_cap = 1000
def execute(python, path):
python = Command(python)
python(path)
try:
with open(self.test_path, 'rb') as f:
source = f.read()
shutil.copy(self.test_path, self.test_path + '.bak')
with open(self.test_path, 'wb') as f:
f.write(minify(source, self.test_path, **self.options).encode())
execute(sys.executable, self.test_path)
except ErrorReturnCode as e:
print(self.test_path)
print(e.stderr)
raise
finally:
shutil.copy(self.test_path + '.bak', self.test_path)
def get_active_manifest():
"""
The TestManifest for the current interpreter
"""
if platform.python_implementation() == 'CPython':
return Manifest('python%i.%i' % (sys.version_info[0], sys.version_info[1]))
else:
if sys.version_info[0] == 2:
return Manifest('pypy')
else:
return Manifest('pypy3')
manifest = get_active_manifest()
@pytest.mark.parametrize('test_case', list(manifest), ids=lambda test_case: repr(test_case))
def test_regrtest(test_case):
test_case.run_test()
if __name__ == '__main__':
sys.exit(manifest.verify())