blob: 1a294de63345513937cf2c3528655cb12c199332 [file] [log] [blame]
Tudor Timi7b8793f2020-01-02 19:44:10 +01001import mmap
Tudor Timi2c270442019-12-23 19:14:29 +01002import os
Tudor Timi81be42d2019-12-23 19:17:57 +01003import pathlib
Tudor Timi7b8793f2020-01-02 19:44:10 +01004import re
Tudor Timi8788ae42019-12-27 17:32:06 +01005import shutil
Tudor Timi2c270442019-12-23 19:14:29 +01006import subprocess
7
Tudor Timia3e68142020-01-03 15:37:03 +01008import pytest
9
10
11def all_files_in_dir(dirname):
12 dirpath = os.path.join(os.path.dirname(os.path.realpath(__file__)), dirname)
13 return pytest.mark.datafiles(
14 *pathlib.Path(dirpath).iterdir(),
15 keep_top_dir=True,
16 )
17
Tudor Timi981c92b2020-01-03 18:12:14 +010018def all_available_simulators():
Tudor Timia0cd43a2020-01-03 18:33:11 +010019 simulators = []
20
21 if shutil.which('irun'):
22 simulators.append('irun')
23 if shutil.which('vcs'):
24 simulators.append('vcs')
25 if shutil.which('vlog'):
26 simulators.append('modelsim')
27
28 assert simulators, 'None of irun, modelsim or vcs are in your path. You need at least 1 simulator to regress svunit-code!'
29
30 return pytest.mark.parametrize("simulator", simulators)
Tudor Timi981c92b2020-01-03 18:12:14 +010031
Tudor Timi2c270442019-12-23 19:14:29 +010032
Tudor Timi81be42d2019-12-23 19:17:57 +010033def clean_paths(rm_paths):
34 for rm_path in rm_paths:
35 for p in pathlib.Path('.').glob(rm_path):
36 p.unlink()
37
38
Tudor Timi2c270442019-12-23 19:14:29 +010039def create_unit_test(name):
40 subprocess.check_call(['create_unit_test.pl', name])
41
42
Tudor Timi8788ae42019-12-27 17:32:06 +010043def get_svunit_root():
44 return os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
45
46
Tudor Timi2c270442019-12-23 19:14:29 +010047def golden_class_unit_test(FILE, MYNAME):
48 template = open('{}/test/templates/class_unit_test.gold'.format(os.environ['SVUNIT_INSTALL']))
49 with open('{}_unit_test.gold'.format(FILE), 'w') as output:
50 for line in template:
51 output.write(line.replace('FILE', FILE).replace('MYNAME', MYNAME))
52
Tudor Timi63b4c812019-12-25 16:34:31 +010053def golden_module_unit_test(FILE, MYNAME):
54 template = open('{}/test/templates/module_unit_test.gold'.format(os.environ['SVUNIT_INSTALL']))
55 with open('{}_unit_test.gold'.format(FILE), 'w') as output:
56 for line in template:
57 output.write(line.replace('FILE', FILE).replace('MYNAME', MYNAME))
58
Tudor Timi4b574fc2019-12-25 16:38:01 +010059def golden_if_unit_test(FILE, MYNAME):
60 template = open('{}/test/templates/if_unit_test.gold'.format(os.environ['SVUNIT_INSTALL']))
61 with open('{}_unit_test.gold'.format(FILE), 'w') as output:
62 for line in template:
63 output.write(line.replace('FILE', FILE).replace('MYNAME', MYNAME))
64
Tudor Timi2c270442019-12-23 19:14:29 +010065def golden_testsuite_with_1_unittest(MYNAME):
66 template = open('{}/test/templates/testsuite_with_1_unittest.gold'.format(os.environ['SVUNIT_INSTALL']))
67 with open('testsuite.gold', 'w') as output:
68 for line in template:
69 output.write(line.replace('MYNAME', MYNAME))
70
Tudor Timi1544f402020-01-02 18:39:20 +010071def golden_testsuite_with_2_unittests(MYNAME1, MYNAME2):
72 template = open('{}/test/templates/testsuite_with_2_unittest.gold'.format(os.environ['SVUNIT_INSTALL']))
73 with open('testsuite.gold', 'w') as output:
74 for line in template:
75 output.write(line.replace('MYNAME1', MYNAME1).replace('MYNAME2', MYNAME2))
76
Tudor Timi2c270442019-12-23 19:14:29 +010077def golden_testrunner_with_1_testsuite():
78 template = open('{}/test/templates/testrunner_with_1_testsuite.gold'.format(os.environ['SVUNIT_INSTALL']))
79 with open('testrunner.gold', 'w') as output:
80 for line in template:
81 output.write(line)
82
Tudor Timi8f95e9b2019-12-24 14:42:04 +010083def golden_testrunner_with_2_testsuites():
84 template = open('{}/test/templates/testrunner_with_2_testsuite.gold'.format(os.environ['SVUNIT_INSTALL']))
85 with open('testrunner.gold', 'w') as output:
86 for line in template:
87 output.write(line)
88
Tudor Timibd0006b2019-12-25 16:22:04 +010089def golden_testrunner_with_3_testsuites():
90 template = open('{}/test/templates/testrunner_with_3_testsuite.gold'.format(os.environ['SVUNIT_INSTALL']))
91 with open('testrunner.gold', 'w') as output:
92 for line in template:
93 output.write(line)
94
Tudor Timi02cc3c12019-12-25 11:15:45 +010095def golden_testrunner_with_4_testsuites():
96 template = open('{}/test/templates/testrunner_with_4_testsuite.gold'.format(os.environ['SVUNIT_INSTALL']))
97 with open('testrunner.gold', 'w') as output:
98 for line in template:
99 output.write(line)
100
Tudor Timi2c270442019-12-23 19:14:29 +0100101
102def verify_file(file0, file1):
103 result = subprocess.run(['diff', '-wbB', file0, file1], stdout=subprocess.PIPE)
104 assert result.returncode in [0, 1]
105 if result.returncode == 1:
106 assert result.stdout == b''
107
Tudor Timi4c2bea72020-01-02 18:30:10 +0100108def verify_testsuite(testsuite, dir=''):
Tudor Timi2c270442019-12-23 19:14:29 +0100109 PWD = '_'
110 file = open(testsuite)
111 with open('.{}'.format(testsuite), 'w') as output:
112 for line in file:
113 output.write(line.replace('PWD', "{}{}".format(PWD, dir)))
114 verify_file(output.name, '.{}{}_testsuite.sv'.format(PWD, dir))
115
116def verify_testrunner(testrunner, ts0, ts1='', ts2='', ts3='', tr=''):
117 if tr == '':
118 tr = '.testrunner.sv'
119 file = open(testrunner)
120 with open('.{}'.format(testrunner), 'w') as output:
121 for line in file:
122 output.write(line.replace('TS0', ts0).replace('TS1', ts1).replace('TS2', ts2).replace('TS3', ts3))
123 verify_file(output.name, tr)
Tudor Timi7b8793f2020-01-02 19:44:10 +0100124
125
126def expect_testrunner_pass(logfile_path):
127 with open(logfile_path) as file, mmap.mmap(file.fileno(), 0, access=mmap.ACCESS_READ) as log:
128 return re.search(br'INFO: \[.*\]\[testrunner\]: PASSED (. of . suites passing) \[$SVUnitVersion\]', log)
Tudor Timi8e4a36e2020-01-02 19:55:48 +0100129
Tudor Timi209d9332020-01-02 20:25:46 +0100130def expect_testrunner_fail(logfile_path):
131 with open(logfile_path) as file, mmap.mmap(file.fileno(), 0, access=mmap.ACCESS_READ) as log:
132 return re.search(br'INFO: \[.*\]\[testrunner\]: FAILED', log)
133
Tudor Timi8e4a36e2020-01-02 19:55:48 +0100134def expect_string(pattern, logfile_path):
135 with open(logfile_path) as file, mmap.mmap(file.fileno(), 0, access=mmap.ACCESS_READ) as log:
136 return re.search(pattern, log)
Tudor Timi80274d62020-01-02 20:04:38 +0100137
138def expect_file(path):
139 return os.path.exists(path)
140
141def expect_file_does_contain(pattern, file_path):
142 return expect_string(pattern, file_path)
Tudor Timida4126d2020-01-03 15:16:10 +0100143
144
Tudor Timi43e7aa92020-01-03 15:20:44 +0100145def expect_passing_example(dir, sim, args=[]):
Tudor Timida4126d2020-01-03 15:16:10 +0100146 with dir.as_cwd():
Tudor Timi43e7aa92020-01-03 15:20:44 +0100147 subprocess.check_call(['runSVUnit', '-s', sim] + args)
Tudor Timida4126d2020-01-03 15:16:10 +0100148
149 expect_file('run.log')
150 expect_testrunner_pass('run.log')