-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
noxfile.py
139 lines (115 loc) · 3.43 KB
/
noxfile.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
# SPDX-License-Identifier: MIT
import contextlib
import glob
import os
import os.path
import pathlib
import pickle
import subprocess
import nox
nox.options.sessions = ['test']
nox.options.reuse_existing_virtualenvs = True
def install_dependencies(session, dependencies):
unmet = pickle.loads(
subprocess.check_output([
'python',
os.path.join('tests', 'unmet-dependencies.py'),
*dependencies,
], env=session.env, text=False)
)
if unmet:
session.install(*unmet)
else:
print('no dependencies to install')
@contextlib.contextmanager
def save_path(path):
if os.path.exists(path):
os.rename(path, f'{path}.bak')
try:
yield
finally:
os.rename(f'{path}.bak', path)
else:
yield
@contextlib.contextmanager
def cd(*path_parts):
old_cwd = os.getcwd()
os.chdir(os.path.join(*path_parts))
try:
yield
finally:
os.chdir(old_cwd)
@nox.session()
def test(session):
htmlcov_output = os.path.join(session.virtualenv.location, 'htmlcov')
htmlcov_index_output = os.path.join(htmlcov_output, 'index.html')
xmlcov_output = os.path.join(session.virtualenv.location, 'coverage.xml')
if not os.path.isdir(htmlcov_output):
os.mkdir(htmlcov_output)
install_dependencies(session, {
# build system
'ninja_syntax',
'tomli',
# tests
'pytest',
# coverage
'gcovr',
})
# cleanup .gcda (GCC coverage) files
for file in glob.glob(os.path.join('**', '*.gcda'), recursive=True):
os.remove(file)
# build testsuite
with save_path('build.ninja'):
session.run('python', 'configure.py', 'testsuite')
session.run('ninja', external=True)
out_path = os.path.abspath(os.path.join('build', 'testsuite', 'out'))
# build and install python testsuite wrapper
with cd('tests', 'wrapper'):
session.run('python', 'setup.py', 'develop', env={
'LDFLAGS': f'-L{out_path}',
})
try:
# run tests
session.run(
'python', '-m', 'pytest',
'--showlocals', '-ra', '--durations=10', '--durations-min=1.0',
'tests/', *session.posargs,
env={
'LD_LIBRARY_PATH': out_path,
}
)
finally:
# generate C coverage reports
session.run(
'gcovr', '-r', '.', '-b',
'--xml', xmlcov_output,
'--html-details', htmlcov_index_output,
)
print(f'coverage report available at: file://{os.path.abspath(htmlcov_index_output)}')
# print C coverage report
session.run('gcovr', '-r', '.', '-b')
@nox.session()
@nox.parametrize('sanitizer', ('address', 'memory', 'undefined'))
def fuzz(session, sanitizer):
install_dependencies(session, {
# build system
'ninja_syntax',
'tomli',
})
# build fuzz target
with save_path('build.ninja'):
session.run(
'python', 'configure.py',
'--compiler', 'clang',
'fuzz',
f'--engine=-fsanitize=fuzzer,{sanitizer}',
)
session.run('ninja', external=True)
fuzz_exe = pathlib.Path('build', 'fuzz', 'out', 'fuzz').absolute()
# run fuzzer
session.run(
os.fspath(fuzz_exe),
'-seed=1', '-max_total_time=60',
'-print_pcs=1', '-print_final_stats=1',
external=True,
)