blob: 9bf6563ddaa0e016761b0c179c9587bfe98b5ec5 [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')
Tudor Timi98550e52020-01-03 18:43:35 +010023 if shutil.which('xrun'):
24 simulators.append('xrun')
Tudor Timia0cd43a2020-01-03 18:33:11 +010025 if shutil.which('vcs'):
26 simulators.append('vcs')
27 if shutil.which('vlog'):
28 simulators.append('modelsim')
29
30 assert simulators, 'None of irun, modelsim or vcs are in your path. You need at least 1 simulator to regress svunit-code!'
31
32 return pytest.mark.parametrize("simulator", simulators)
Tudor Timi981c92b2020-01-03 18:12:14 +010033
Tudor Timi2c270442019-12-23 19:14:29 +010034
Tudor Timi81be42d2019-12-23 19:17:57 +010035def clean_paths(rm_paths):
36 for rm_path in rm_paths:
37 for p in pathlib.Path('.').glob(rm_path):
38 p.unlink()
39
40
Tudor Timi2c270442019-12-23 19:14:29 +010041def create_unit_test(name):
42 subprocess.check_call(['create_unit_test.pl', name])
43
44
Tudor Timi8788ae42019-12-27 17:32:06 +010045def get_svunit_root():
46 return os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
47
48
Tudor Timi2c270442019-12-23 19:14:29 +010049def golden_class_unit_test(FILE, MYNAME):
50 template = open('{}/test/templates/class_unit_test.gold'.format(os.environ['SVUNIT_INSTALL']))
51 with open('{}_unit_test.gold'.format(FILE), 'w') as output:
52 for line in template:
53 output.write(line.replace('FILE', FILE).replace('MYNAME', MYNAME))
54
Tudor Timi63b4c812019-12-25 16:34:31 +010055def golden_module_unit_test(FILE, MYNAME):
56 template = open('{}/test/templates/module_unit_test.gold'.format(os.environ['SVUNIT_INSTALL']))
57 with open('{}_unit_test.gold'.format(FILE), 'w') as output:
58 for line in template:
59 output.write(line.replace('FILE', FILE).replace('MYNAME', MYNAME))
60
Tudor Timi4b574fc2019-12-25 16:38:01 +010061def golden_if_unit_test(FILE, MYNAME):
62 template = open('{}/test/templates/if_unit_test.gold'.format(os.environ['SVUNIT_INSTALL']))
63 with open('{}_unit_test.gold'.format(FILE), 'w') as output:
64 for line in template:
65 output.write(line.replace('FILE', FILE).replace('MYNAME', MYNAME))
66
Tudor Timi2c270442019-12-23 19:14:29 +010067def golden_testsuite_with_1_unittest(MYNAME):
68 template = open('{}/test/templates/testsuite_with_1_unittest.gold'.format(os.environ['SVUNIT_INSTALL']))
69 with open('testsuite.gold', 'w') as output:
70 for line in template:
71 output.write(line.replace('MYNAME', MYNAME))
72
Tudor Timi1544f402020-01-02 18:39:20 +010073def golden_testsuite_with_2_unittests(MYNAME1, MYNAME2):
74 template = open('{}/test/templates/testsuite_with_2_unittest.gold'.format(os.environ['SVUNIT_INSTALL']))
75 with open('testsuite.gold', 'w') as output:
76 for line in template:
77 output.write(line.replace('MYNAME1', MYNAME1).replace('MYNAME2', MYNAME2))
78
Tudor Timi2c270442019-12-23 19:14:29 +010079def golden_testrunner_with_1_testsuite():
80 template = open('{}/test/templates/testrunner_with_1_testsuite.gold'.format(os.environ['SVUNIT_INSTALL']))
81 with open('testrunner.gold', 'w') as output:
82 for line in template:
83 output.write(line)
84
Tudor Timi8f95e9b2019-12-24 14:42:04 +010085def golden_testrunner_with_2_testsuites():
86 template = open('{}/test/templates/testrunner_with_2_testsuite.gold'.format(os.environ['SVUNIT_INSTALL']))
87 with open('testrunner.gold', 'w') as output:
88 for line in template:
89 output.write(line)
90
Tudor Timibd0006b2019-12-25 16:22:04 +010091def golden_testrunner_with_3_testsuites():
92 template = open('{}/test/templates/testrunner_with_3_testsuite.gold'.format(os.environ['SVUNIT_INSTALL']))
93 with open('testrunner.gold', 'w') as output:
94 for line in template:
95 output.write(line)
96
Tudor Timi02cc3c12019-12-25 11:15:45 +010097def golden_testrunner_with_4_testsuites():
98 template = open('{}/test/templates/testrunner_with_4_testsuite.gold'.format(os.environ['SVUNIT_INSTALL']))
99 with open('testrunner.gold', 'w') as output:
100 for line in template:
101 output.write(line)
102
Tudor Timi2c270442019-12-23 19:14:29 +0100103
104def verify_file(file0, file1):
105 result = subprocess.run(['diff', '-wbB', file0, file1], stdout=subprocess.PIPE)
106 assert result.returncode in [0, 1]
107 if result.returncode == 1:
108 assert result.stdout == b''
109
Tudor Timi4c2bea72020-01-02 18:30:10 +0100110def verify_testsuite(testsuite, dir=''):
Tudor Timi2c270442019-12-23 19:14:29 +0100111 PWD = '_'
112 file = open(testsuite)
113 with open('.{}'.format(testsuite), 'w') as output:
114 for line in file:
115 output.write(line.replace('PWD', "{}{}".format(PWD, dir)))
116 verify_file(output.name, '.{}{}_testsuite.sv'.format(PWD, dir))
117
118def verify_testrunner(testrunner, ts0, ts1='', ts2='', ts3='', tr=''):
119 if tr == '':
120 tr = '.testrunner.sv'
121 file = open(testrunner)
122 with open('.{}'.format(testrunner), 'w') as output:
123 for line in file:
124 output.write(line.replace('TS0', ts0).replace('TS1', ts1).replace('TS2', ts2).replace('TS3', ts3))
125 verify_file(output.name, tr)
Tudor Timi7b8793f2020-01-02 19:44:10 +0100126
127
128def expect_testrunner_pass(logfile_path):
129 with open(logfile_path) as file, mmap.mmap(file.fileno(), 0, access=mmap.ACCESS_READ) as log:
130 return re.search(br'INFO: \[.*\]\[testrunner\]: PASSED (. of . suites passing) \[$SVUnitVersion\]', log)
Tudor Timi8e4a36e2020-01-02 19:55:48 +0100131
Tudor Timi209d9332020-01-02 20:25:46 +0100132def expect_testrunner_fail(logfile_path):
133 with open(logfile_path) as file, mmap.mmap(file.fileno(), 0, access=mmap.ACCESS_READ) as log:
134 return re.search(br'INFO: \[.*\]\[testrunner\]: FAILED', log)
135
Tudor Timi8e4a36e2020-01-02 19:55:48 +0100136def expect_string(pattern, logfile_path):
137 with open(logfile_path) as file, mmap.mmap(file.fileno(), 0, access=mmap.ACCESS_READ) as log:
138 return re.search(pattern, log)
Tudor Timi80274d62020-01-02 20:04:38 +0100139
140def expect_file(path):
141 return os.path.exists(path)
142
143def expect_file_does_contain(pattern, file_path):
144 return expect_string(pattern, file_path)
Tudor Timida4126d2020-01-03 15:16:10 +0100145
146
Tudor Timi43e7aa92020-01-03 15:20:44 +0100147def expect_passing_example(dir, sim, args=[]):
Tudor Timida4126d2020-01-03 15:16:10 +0100148 with dir.as_cwd():
Tudor Timi43e7aa92020-01-03 15:20:44 +0100149 subprocess.check_call(['runSVUnit', '-s', sim] + args)
Tudor Timida4126d2020-01-03 15:16:10 +0100150
151 expect_file('run.log')
152 expect_testrunner_pass('run.log')