blob: e7cac8e79dbbf411b3d7ffdeb4d73403852b31d2 [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():
19 return pytest.mark.parametrize("simulator", get_simulators())
20
Tudor Timi2c270442019-12-23 19:14:29 +010021
Tudor Timi81be42d2019-12-23 19:17:57 +010022def clean_paths(rm_paths):
23 for rm_path in rm_paths:
24 for p in pathlib.Path('.').glob(rm_path):
25 p.unlink()
26
27
Tudor Timi2c270442019-12-23 19:14:29 +010028def create_unit_test(name):
29 subprocess.check_call(['create_unit_test.pl', name])
30
31
Tudor Timi8788ae42019-12-27 17:32:06 +010032def get_simulators():
33 result = []
34
35 if shutil.which('irun'):
36 result.append('irun')
37 if shutil.which('vcs'):
38 result.append('vcs')
39 if shutil.which('vlog'):
40 result.append('modelsim')
41
42 assert result, 'None of irun, modelsim or vcs are in your path. You need at least 1 simulator to regress svunit-code!'
43
44 return result
45
46
47def get_svunit_root():
48 return os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
49
50
Tudor Timi2c270442019-12-23 19:14:29 +010051def golden_class_unit_test(FILE, MYNAME):
52 template = open('{}/test/templates/class_unit_test.gold'.format(os.environ['SVUNIT_INSTALL']))
53 with open('{}_unit_test.gold'.format(FILE), 'w') as output:
54 for line in template:
55 output.write(line.replace('FILE', FILE).replace('MYNAME', MYNAME))
56
Tudor Timi63b4c812019-12-25 16:34:31 +010057def golden_module_unit_test(FILE, MYNAME):
58 template = open('{}/test/templates/module_unit_test.gold'.format(os.environ['SVUNIT_INSTALL']))
59 with open('{}_unit_test.gold'.format(FILE), 'w') as output:
60 for line in template:
61 output.write(line.replace('FILE', FILE).replace('MYNAME', MYNAME))
62
Tudor Timi4b574fc2019-12-25 16:38:01 +010063def golden_if_unit_test(FILE, MYNAME):
64 template = open('{}/test/templates/if_unit_test.gold'.format(os.environ['SVUNIT_INSTALL']))
65 with open('{}_unit_test.gold'.format(FILE), 'w') as output:
66 for line in template:
67 output.write(line.replace('FILE', FILE).replace('MYNAME', MYNAME))
68
Tudor Timi2c270442019-12-23 19:14:29 +010069def golden_testsuite_with_1_unittest(MYNAME):
70 template = open('{}/test/templates/testsuite_with_1_unittest.gold'.format(os.environ['SVUNIT_INSTALL']))
71 with open('testsuite.gold', 'w') as output:
72 for line in template:
73 output.write(line.replace('MYNAME', MYNAME))
74
Tudor Timi1544f402020-01-02 18:39:20 +010075def golden_testsuite_with_2_unittests(MYNAME1, MYNAME2):
76 template = open('{}/test/templates/testsuite_with_2_unittest.gold'.format(os.environ['SVUNIT_INSTALL']))
77 with open('testsuite.gold', 'w') as output:
78 for line in template:
79 output.write(line.replace('MYNAME1', MYNAME1).replace('MYNAME2', MYNAME2))
80
Tudor Timi2c270442019-12-23 19:14:29 +010081def golden_testrunner_with_1_testsuite():
82 template = open('{}/test/templates/testrunner_with_1_testsuite.gold'.format(os.environ['SVUNIT_INSTALL']))
83 with open('testrunner.gold', 'w') as output:
84 for line in template:
85 output.write(line)
86
Tudor Timi8f95e9b2019-12-24 14:42:04 +010087def golden_testrunner_with_2_testsuites():
88 template = open('{}/test/templates/testrunner_with_2_testsuite.gold'.format(os.environ['SVUNIT_INSTALL']))
89 with open('testrunner.gold', 'w') as output:
90 for line in template:
91 output.write(line)
92
Tudor Timibd0006b2019-12-25 16:22:04 +010093def golden_testrunner_with_3_testsuites():
94 template = open('{}/test/templates/testrunner_with_3_testsuite.gold'.format(os.environ['SVUNIT_INSTALL']))
95 with open('testrunner.gold', 'w') as output:
96 for line in template:
97 output.write(line)
98
Tudor Timi02cc3c12019-12-25 11:15:45 +010099def golden_testrunner_with_4_testsuites():
100 template = open('{}/test/templates/testrunner_with_4_testsuite.gold'.format(os.environ['SVUNIT_INSTALL']))
101 with open('testrunner.gold', 'w') as output:
102 for line in template:
103 output.write(line)
104
Tudor Timi2c270442019-12-23 19:14:29 +0100105
106def verify_file(file0, file1):
107 result = subprocess.run(['diff', '-wbB', file0, file1], stdout=subprocess.PIPE)
108 assert result.returncode in [0, 1]
109 if result.returncode == 1:
110 assert result.stdout == b''
111
Tudor Timi4c2bea72020-01-02 18:30:10 +0100112def verify_testsuite(testsuite, dir=''):
Tudor Timi2c270442019-12-23 19:14:29 +0100113 PWD = '_'
114 file = open(testsuite)
115 with open('.{}'.format(testsuite), 'w') as output:
116 for line in file:
117 output.write(line.replace('PWD', "{}{}".format(PWD, dir)))
118 verify_file(output.name, '.{}{}_testsuite.sv'.format(PWD, dir))
119
120def verify_testrunner(testrunner, ts0, ts1='', ts2='', ts3='', tr=''):
121 if tr == '':
122 tr = '.testrunner.sv'
123 file = open(testrunner)
124 with open('.{}'.format(testrunner), 'w') as output:
125 for line in file:
126 output.write(line.replace('TS0', ts0).replace('TS1', ts1).replace('TS2', ts2).replace('TS3', ts3))
127 verify_file(output.name, tr)
Tudor Timi7b8793f2020-01-02 19:44:10 +0100128
129
130def expect_testrunner_pass(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\]: PASSED (. of . suites passing) \[$SVUnitVersion\]', log)
Tudor Timi8e4a36e2020-01-02 19:55:48 +0100133
Tudor Timi209d9332020-01-02 20:25:46 +0100134def expect_testrunner_fail(logfile_path):
135 with open(logfile_path) as file, mmap.mmap(file.fileno(), 0, access=mmap.ACCESS_READ) as log:
136 return re.search(br'INFO: \[.*\]\[testrunner\]: FAILED', log)
137
Tudor Timi8e4a36e2020-01-02 19:55:48 +0100138def expect_string(pattern, logfile_path):
139 with open(logfile_path) as file, mmap.mmap(file.fileno(), 0, access=mmap.ACCESS_READ) as log:
140 return re.search(pattern, log)
Tudor Timi80274d62020-01-02 20:04:38 +0100141
142def expect_file(path):
143 return os.path.exists(path)
144
145def expect_file_does_contain(pattern, file_path):
146 return expect_string(pattern, file_path)
Tudor Timida4126d2020-01-03 15:16:10 +0100147
148
Tudor Timi43e7aa92020-01-03 15:20:44 +0100149def expect_passing_example(dir, sim, args=[]):
Tudor Timida4126d2020-01-03 15:16:10 +0100150 with dir.as_cwd():
Tudor Timi43e7aa92020-01-03 15:20:44 +0100151 subprocess.check_call(['runSVUnit', '-s', sim] + args)
Tudor Timida4126d2020-01-03 15:16:10 +0100152
153 expect_file('run.log')
154 expect_testrunner_pass('run.log')