blob: 063ffdaddf366c7a97e4b6782eebcbe88d89e35f [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 Timi2c270442019-12-23 19:14:29 +010018
Tudor Timi81be42d2019-12-23 19:17:57 +010019def clean_paths(rm_paths):
20 for rm_path in rm_paths:
21 for p in pathlib.Path('.').glob(rm_path):
22 p.unlink()
23
24
Tudor Timi2c270442019-12-23 19:14:29 +010025def create_unit_test(name):
26 subprocess.check_call(['create_unit_test.pl', name])
27
28
Tudor Timi8788ae42019-12-27 17:32:06 +010029def get_simulators():
30 result = []
31
32 if shutil.which('irun'):
33 result.append('irun')
34 if shutil.which('vcs'):
35 result.append('vcs')
36 if shutil.which('vlog'):
37 result.append('modelsim')
38
39 assert result, 'None of irun, modelsim or vcs are in your path. You need at least 1 simulator to regress svunit-code!'
40
41 return result
42
43
44def get_svunit_root():
45 return os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
46
47
Tudor Timi2c270442019-12-23 19:14:29 +010048def golden_class_unit_test(FILE, MYNAME):
49 template = open('{}/test/templates/class_unit_test.gold'.format(os.environ['SVUNIT_INSTALL']))
50 with open('{}_unit_test.gold'.format(FILE), 'w') as output:
51 for line in template:
52 output.write(line.replace('FILE', FILE).replace('MYNAME', MYNAME))
53
Tudor Timi63b4c812019-12-25 16:34:31 +010054def golden_module_unit_test(FILE, MYNAME):
55 template = open('{}/test/templates/module_unit_test.gold'.format(os.environ['SVUNIT_INSTALL']))
56 with open('{}_unit_test.gold'.format(FILE), 'w') as output:
57 for line in template:
58 output.write(line.replace('FILE', FILE).replace('MYNAME', MYNAME))
59
Tudor Timi4b574fc2019-12-25 16:38:01 +010060def golden_if_unit_test(FILE, MYNAME):
61 template = open('{}/test/templates/if_unit_test.gold'.format(os.environ['SVUNIT_INSTALL']))
62 with open('{}_unit_test.gold'.format(FILE), 'w') as output:
63 for line in template:
64 output.write(line.replace('FILE', FILE).replace('MYNAME', MYNAME))
65
Tudor Timi2c270442019-12-23 19:14:29 +010066def golden_testsuite_with_1_unittest(MYNAME):
67 template = open('{}/test/templates/testsuite_with_1_unittest.gold'.format(os.environ['SVUNIT_INSTALL']))
68 with open('testsuite.gold', 'w') as output:
69 for line in template:
70 output.write(line.replace('MYNAME', MYNAME))
71
Tudor Timi1544f402020-01-02 18:39:20 +010072def golden_testsuite_with_2_unittests(MYNAME1, MYNAME2):
73 template = open('{}/test/templates/testsuite_with_2_unittest.gold'.format(os.environ['SVUNIT_INSTALL']))
74 with open('testsuite.gold', 'w') as output:
75 for line in template:
76 output.write(line.replace('MYNAME1', MYNAME1).replace('MYNAME2', MYNAME2))
77
Tudor Timi2c270442019-12-23 19:14:29 +010078def golden_testrunner_with_1_testsuite():
79 template = open('{}/test/templates/testrunner_with_1_testsuite.gold'.format(os.environ['SVUNIT_INSTALL']))
80 with open('testrunner.gold', 'w') as output:
81 for line in template:
82 output.write(line)
83
Tudor Timi8f95e9b2019-12-24 14:42:04 +010084def golden_testrunner_with_2_testsuites():
85 template = open('{}/test/templates/testrunner_with_2_testsuite.gold'.format(os.environ['SVUNIT_INSTALL']))
86 with open('testrunner.gold', 'w') as output:
87 for line in template:
88 output.write(line)
89
Tudor Timibd0006b2019-12-25 16:22:04 +010090def golden_testrunner_with_3_testsuites():
91 template = open('{}/test/templates/testrunner_with_3_testsuite.gold'.format(os.environ['SVUNIT_INSTALL']))
92 with open('testrunner.gold', 'w') as output:
93 for line in template:
94 output.write(line)
95
Tudor Timi02cc3c12019-12-25 11:15:45 +010096def golden_testrunner_with_4_testsuites():
97 template = open('{}/test/templates/testrunner_with_4_testsuite.gold'.format(os.environ['SVUNIT_INSTALL']))
98 with open('testrunner.gold', 'w') as output:
99 for line in template:
100 output.write(line)
101
Tudor Timi2c270442019-12-23 19:14:29 +0100102
103def verify_file(file0, file1):
104 result = subprocess.run(['diff', '-wbB', file0, file1], stdout=subprocess.PIPE)
105 assert result.returncode in [0, 1]
106 if result.returncode == 1:
107 assert result.stdout == b''
108
Tudor Timi4c2bea72020-01-02 18:30:10 +0100109def verify_testsuite(testsuite, dir=''):
Tudor Timi2c270442019-12-23 19:14:29 +0100110 PWD = '_'
111 file = open(testsuite)
112 with open('.{}'.format(testsuite), 'w') as output:
113 for line in file:
114 output.write(line.replace('PWD', "{}{}".format(PWD, dir)))
115 verify_file(output.name, '.{}{}_testsuite.sv'.format(PWD, dir))
116
117def verify_testrunner(testrunner, ts0, ts1='', ts2='', ts3='', tr=''):
118 if tr == '':
119 tr = '.testrunner.sv'
120 file = open(testrunner)
121 with open('.{}'.format(testrunner), 'w') as output:
122 for line in file:
123 output.write(line.replace('TS0', ts0).replace('TS1', ts1).replace('TS2', ts2).replace('TS3', ts3))
124 verify_file(output.name, tr)
Tudor Timi7b8793f2020-01-02 19:44:10 +0100125
126
127def expect_testrunner_pass(logfile_path):
128 with open(logfile_path) as file, mmap.mmap(file.fileno(), 0, access=mmap.ACCESS_READ) as log:
129 return re.search(br'INFO: \[.*\]\[testrunner\]: PASSED (. of . suites passing) \[$SVUnitVersion\]', log)
Tudor Timi8e4a36e2020-01-02 19:55:48 +0100130
Tudor Timi209d9332020-01-02 20:25:46 +0100131def expect_testrunner_fail(logfile_path):
132 with open(logfile_path) as file, mmap.mmap(file.fileno(), 0, access=mmap.ACCESS_READ) as log:
133 return re.search(br'INFO: \[.*\]\[testrunner\]: FAILED', log)
134
Tudor Timi8e4a36e2020-01-02 19:55:48 +0100135def expect_string(pattern, logfile_path):
136 with open(logfile_path) as file, mmap.mmap(file.fileno(), 0, access=mmap.ACCESS_READ) as log:
137 return re.search(pattern, log)
Tudor Timi80274d62020-01-02 20:04:38 +0100138
139def expect_file(path):
140 return os.path.exists(path)
141
142def expect_file_does_contain(pattern, file_path):
143 return expect_string(pattern, file_path)
Tudor Timida4126d2020-01-03 15:16:10 +0100144
145
Tudor Timi43e7aa92020-01-03 15:20:44 +0100146def expect_passing_example(dir, sim, args=[]):
Tudor Timida4126d2020-01-03 15:16:10 +0100147 with dir.as_cwd():
Tudor Timi43e7aa92020-01-03 15:20:44 +0100148 subprocess.check_call(['runSVUnit', '-s', sim] + args)
Tudor Timida4126d2020-01-03 15:16:10 +0100149
150 expect_file('run.log')
151 expect_testrunner_pass('run.log')