forked from svunit/svunit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
173 lines (133 loc) · 6.02 KB
/
utils.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
import contextlib
import mmap
import os
import pathlib
import re
import shutil
import subprocess
import warnings
import pytest
@contextlib.contextmanager
def working_directory(path):
"""Changes working directory and returns to previous on exit."""
prev_cwd = pathlib.Path.cwd()
os.chdir(path)
try:
yield
finally:
os.chdir(prev_cwd)
def all_files_in_dir(dirname):
dirpath = os.path.join(os.path.dirname(os.path.realpath(__file__)), dirname)
return pytest.mark.datafiles(
*pathlib.Path(dirpath).iterdir(),
keep_top_dir=True,
)
def all_available_simulators():
simulators = []
if shutil.which('irun'):
simulators.append('irun')
if shutil.which('xrun'):
simulators.append('xrun')
if shutil.which('vcs'):
simulators.append('vcs')
if shutil.which('vlog'):
simulators.append('modelsim')
if shutil.which('dsim'):
simulators.append('dsim')
if shutil.which('qrun'):
simulators.append('qrun')
if not simulators:
warnings.warn('None of irun, modelsim, vcs, dsim or qrun are in your path. You need at least 1 simulator to regress svunit-code!')
return pytest.mark.parametrize("simulator", simulators)
def clean_paths(rm_paths):
for rm_path in rm_paths:
for p in pathlib.Path('.').glob(rm_path):
p.unlink()
def create_unit_test(name):
subprocess.check_call(['create_unit_test.pl', name])
def get_svunit_root():
return os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
def golden_class_unit_test(FILE, MYNAME):
template = open('{}/test/templates/class_unit_test.gold'.format(os.environ['SVUNIT_INSTALL']))
with open('{}_unit_test.gold'.format(FILE), 'w') as output:
for line in template:
output.write(line.replace('FILE', FILE).replace('MYNAME', MYNAME))
def golden_module_unit_test(FILE, MYNAME):
template = open('{}/test/templates/module_unit_test.gold'.format(os.environ['SVUNIT_INSTALL']))
with open('{}_unit_test.gold'.format(FILE), 'w') as output:
for line in template:
output.write(line.replace('FILE', FILE).replace('MYNAME', MYNAME))
def golden_if_unit_test(FILE, MYNAME):
template = open('{}/test/templates/if_unit_test.gold'.format(os.environ['SVUNIT_INSTALL']))
with open('{}_unit_test.gold'.format(FILE), 'w') as output:
for line in template:
output.write(line.replace('FILE', FILE).replace('MYNAME', MYNAME))
def golden_testsuite_with_1_unittest(MYNAME):
template = open('{}/test/templates/testsuite_with_1_unittest.gold'.format(os.environ['SVUNIT_INSTALL']))
with open('testsuite.gold', 'w') as output:
for line in template:
output.write(line.replace('MYNAME', MYNAME))
def golden_testsuite_with_2_unittests(MYNAME1, MYNAME2):
template = open('{}/test/templates/testsuite_with_2_unittest.gold'.format(os.environ['SVUNIT_INSTALL']))
with open('testsuite.gold', 'w') as output:
for line in template:
output.write(line.replace('MYNAME1', MYNAME1).replace('MYNAME2', MYNAME2))
def golden_testrunner_with_1_testsuite():
template = open('{}/test/templates/testrunner_with_1_testsuite.gold'.format(os.environ['SVUNIT_INSTALL']))
with open('testrunner.gold', 'w') as output:
for line in template:
output.write(line)
def golden_testrunner_with_2_testsuites():
template = open('{}/test/templates/testrunner_with_2_testsuite.gold'.format(os.environ['SVUNIT_INSTALL']))
with open('testrunner.gold', 'w') as output:
for line in template:
output.write(line)
def golden_testrunner_with_3_testsuites():
template = open('{}/test/templates/testrunner_with_3_testsuite.gold'.format(os.environ['SVUNIT_INSTALL']))
with open('testrunner.gold', 'w') as output:
for line in template:
output.write(line)
def golden_testrunner_with_4_testsuites():
template = open('{}/test/templates/testrunner_with_4_testsuite.gold'.format(os.environ['SVUNIT_INSTALL']))
with open('testrunner.gold', 'w') as output:
for line in template:
output.write(line)
def verify_file(file0, file1):
result = subprocess.run(['diff', '-wbB', file0, file1], stdout=subprocess.PIPE)
assert result.returncode in [0, 1]
if result.returncode == 1:
assert result.stdout == b''
def verify_testsuite(testsuite, dir=''):
PWD = '_'
file = open(testsuite)
with open('.{}'.format(testsuite), 'w') as output:
for line in file:
output.write(line.replace('PWD', "{}{}".format(PWD, dir)))
verify_file(output.name, '.{}{}_testsuite.sv'.format(PWD, dir))
def verify_testrunner(testrunner, ts0, ts1='', ts2='', ts3='', tr=''):
if tr == '':
tr = '.testrunner.sv'
file = open(testrunner)
with open('.{}'.format(testrunner), 'w') as output:
for line in file:
output.write(line.replace('TS0', ts0).replace('TS1', ts1).replace('TS2', ts2).replace('TS3', ts3))
verify_file(output.name, tr)
def expect_testrunner_pass(logfile_path):
expect_string(br'INFO: \[.*\]\[testrunner\]: PASSED \(. of . suites passing\) \[.*\]', logfile_path)
def expect_testrunner_fail(logfile_path):
expect_string(br'INFO: \[.*\]\[testrunner\]: FAILED', logfile_path)
def expect_string(pattern, logfile_path):
with open(logfile_path) as file, mmap.mmap(file.fileno(), 0, access=mmap.ACCESS_READ) as log:
assert re.search(pattern, log), "\"%s\" not found at %s log file" % (pattern, logfile_path)
def expect_file(path):
return os.path.exists(path)
def expect_file_does_contain(pattern, file_path):
return expect_string(pattern, file_path)
def expect_passing_example(dir, sim, args=[]):
with dir.as_cwd():
subprocess.check_call(['runSVUnit', '-s', sim] + args)
expect_file('run.log')
expect_testrunner_pass('run.log')
def contains_pattern(pattern, logfile_path):
with open(logfile_path) as file, mmap.mmap(file.fileno(), 0, access=mmap.ACCESS_READ) as log:
return bool(re.search(pattern, log))