Tudor Timi | 7b8793f | 2020-01-02 19:44:10 +0100 | [diff] [blame] | 1 | import mmap |
Tudor Timi | 2c27044 | 2019-12-23 19:14:29 +0100 | [diff] [blame] | 2 | import os |
Tudor Timi | 81be42d | 2019-12-23 19:17:57 +0100 | [diff] [blame] | 3 | import pathlib |
Tudor Timi | 7b8793f | 2020-01-02 19:44:10 +0100 | [diff] [blame] | 4 | import re |
Tudor Timi | 8788ae4 | 2019-12-27 17:32:06 +0100 | [diff] [blame] | 5 | import shutil |
Tudor Timi | 2c27044 | 2019-12-23 19:14:29 +0100 | [diff] [blame] | 6 | import subprocess |
| 7 | |
| 8 | |
Tudor Timi | 81be42d | 2019-12-23 19:17:57 +0100 | [diff] [blame] | 9 | def clean_paths(rm_paths): |
| 10 | for rm_path in rm_paths: |
| 11 | for p in pathlib.Path('.').glob(rm_path): |
| 12 | p.unlink() |
| 13 | |
| 14 | |
Tudor Timi | 2c27044 | 2019-12-23 19:14:29 +0100 | [diff] [blame] | 15 | def create_unit_test(name): |
| 16 | subprocess.check_call(['create_unit_test.pl', name]) |
| 17 | |
| 18 | |
Tudor Timi | 8788ae4 | 2019-12-27 17:32:06 +0100 | [diff] [blame] | 19 | def get_simulators(): |
| 20 | result = [] |
| 21 | |
| 22 | if shutil.which('irun'): |
| 23 | result.append('irun') |
| 24 | if shutil.which('vcs'): |
| 25 | result.append('vcs') |
| 26 | if shutil.which('vlog'): |
| 27 | result.append('modelsim') |
| 28 | |
| 29 | assert result, 'None of irun, modelsim or vcs are in your path. You need at least 1 simulator to regress svunit-code!' |
| 30 | |
| 31 | return result |
| 32 | |
| 33 | |
| 34 | def get_svunit_root(): |
| 35 | return os.path.dirname(os.path.dirname(os.path.realpath(__file__))) |
| 36 | |
| 37 | |
Tudor Timi | 2c27044 | 2019-12-23 19:14:29 +0100 | [diff] [blame] | 38 | def golden_class_unit_test(FILE, MYNAME): |
| 39 | template = open('{}/test/templates/class_unit_test.gold'.format(os.environ['SVUNIT_INSTALL'])) |
| 40 | with open('{}_unit_test.gold'.format(FILE), 'w') as output: |
| 41 | for line in template: |
| 42 | output.write(line.replace('FILE', FILE).replace('MYNAME', MYNAME)) |
| 43 | |
Tudor Timi | 63b4c81 | 2019-12-25 16:34:31 +0100 | [diff] [blame] | 44 | def golden_module_unit_test(FILE, MYNAME): |
| 45 | template = open('{}/test/templates/module_unit_test.gold'.format(os.environ['SVUNIT_INSTALL'])) |
| 46 | with open('{}_unit_test.gold'.format(FILE), 'w') as output: |
| 47 | for line in template: |
| 48 | output.write(line.replace('FILE', FILE).replace('MYNAME', MYNAME)) |
| 49 | |
Tudor Timi | 4b574fc | 2019-12-25 16:38:01 +0100 | [diff] [blame] | 50 | def golden_if_unit_test(FILE, MYNAME): |
| 51 | template = open('{}/test/templates/if_unit_test.gold'.format(os.environ['SVUNIT_INSTALL'])) |
| 52 | with open('{}_unit_test.gold'.format(FILE), 'w') as output: |
| 53 | for line in template: |
| 54 | output.write(line.replace('FILE', FILE).replace('MYNAME', MYNAME)) |
| 55 | |
Tudor Timi | 2c27044 | 2019-12-23 19:14:29 +0100 | [diff] [blame] | 56 | def golden_testsuite_with_1_unittest(MYNAME): |
| 57 | template = open('{}/test/templates/testsuite_with_1_unittest.gold'.format(os.environ['SVUNIT_INSTALL'])) |
| 58 | with open('testsuite.gold', 'w') as output: |
| 59 | for line in template: |
| 60 | output.write(line.replace('MYNAME', MYNAME)) |
| 61 | |
Tudor Timi | 1544f40 | 2020-01-02 18:39:20 +0100 | [diff] [blame] | 62 | def golden_testsuite_with_2_unittests(MYNAME1, MYNAME2): |
| 63 | template = open('{}/test/templates/testsuite_with_2_unittest.gold'.format(os.environ['SVUNIT_INSTALL'])) |
| 64 | with open('testsuite.gold', 'w') as output: |
| 65 | for line in template: |
| 66 | output.write(line.replace('MYNAME1', MYNAME1).replace('MYNAME2', MYNAME2)) |
| 67 | |
Tudor Timi | 2c27044 | 2019-12-23 19:14:29 +0100 | [diff] [blame] | 68 | def golden_testrunner_with_1_testsuite(): |
| 69 | template = open('{}/test/templates/testrunner_with_1_testsuite.gold'.format(os.environ['SVUNIT_INSTALL'])) |
| 70 | with open('testrunner.gold', 'w') as output: |
| 71 | for line in template: |
| 72 | output.write(line) |
| 73 | |
Tudor Timi | 8f95e9b | 2019-12-24 14:42:04 +0100 | [diff] [blame] | 74 | def golden_testrunner_with_2_testsuites(): |
| 75 | template = open('{}/test/templates/testrunner_with_2_testsuite.gold'.format(os.environ['SVUNIT_INSTALL'])) |
| 76 | with open('testrunner.gold', 'w') as output: |
| 77 | for line in template: |
| 78 | output.write(line) |
| 79 | |
Tudor Timi | bd0006b | 2019-12-25 16:22:04 +0100 | [diff] [blame] | 80 | def golden_testrunner_with_3_testsuites(): |
| 81 | template = open('{}/test/templates/testrunner_with_3_testsuite.gold'.format(os.environ['SVUNIT_INSTALL'])) |
| 82 | with open('testrunner.gold', 'w') as output: |
| 83 | for line in template: |
| 84 | output.write(line) |
| 85 | |
Tudor Timi | 02cc3c1 | 2019-12-25 11:15:45 +0100 | [diff] [blame] | 86 | def golden_testrunner_with_4_testsuites(): |
| 87 | template = open('{}/test/templates/testrunner_with_4_testsuite.gold'.format(os.environ['SVUNIT_INSTALL'])) |
| 88 | with open('testrunner.gold', 'w') as output: |
| 89 | for line in template: |
| 90 | output.write(line) |
| 91 | |
Tudor Timi | 2c27044 | 2019-12-23 19:14:29 +0100 | [diff] [blame] | 92 | |
| 93 | def verify_file(file0, file1): |
| 94 | result = subprocess.run(['diff', '-wbB', file0, file1], stdout=subprocess.PIPE) |
| 95 | assert result.returncode in [0, 1] |
| 96 | if result.returncode == 1: |
| 97 | assert result.stdout == b'' |
| 98 | |
Tudor Timi | 4c2bea7 | 2020-01-02 18:30:10 +0100 | [diff] [blame] | 99 | def verify_testsuite(testsuite, dir=''): |
Tudor Timi | 2c27044 | 2019-12-23 19:14:29 +0100 | [diff] [blame] | 100 | PWD = '_' |
| 101 | file = open(testsuite) |
| 102 | with open('.{}'.format(testsuite), 'w') as output: |
| 103 | for line in file: |
| 104 | output.write(line.replace('PWD', "{}{}".format(PWD, dir))) |
| 105 | verify_file(output.name, '.{}{}_testsuite.sv'.format(PWD, dir)) |
| 106 | |
| 107 | def verify_testrunner(testrunner, ts0, ts1='', ts2='', ts3='', tr=''): |
| 108 | if tr == '': |
| 109 | tr = '.testrunner.sv' |
| 110 | file = open(testrunner) |
| 111 | with open('.{}'.format(testrunner), 'w') as output: |
| 112 | for line in file: |
| 113 | output.write(line.replace('TS0', ts0).replace('TS1', ts1).replace('TS2', ts2).replace('TS3', ts3)) |
| 114 | verify_file(output.name, tr) |
Tudor Timi | 7b8793f | 2020-01-02 19:44:10 +0100 | [diff] [blame] | 115 | |
| 116 | |
| 117 | def expect_testrunner_pass(logfile_path): |
| 118 | with open(logfile_path) as file, mmap.mmap(file.fileno(), 0, access=mmap.ACCESS_READ) as log: |
| 119 | return re.search(br'INFO: \[.*\]\[testrunner\]: PASSED (. of . suites passing) \[$SVUnitVersion\]', log) |
Tudor Timi | 8e4a36e | 2020-01-02 19:55:48 +0100 | [diff] [blame] | 120 | |
| 121 | def expect_string(pattern, logfile_path): |
| 122 | with open(logfile_path) as file, mmap.mmap(file.fileno(), 0, access=mmap.ACCESS_READ) as log: |
| 123 | return re.search(pattern, log) |
Tudor Timi | 80274d6 | 2020-01-02 20:04:38 +0100 | [diff] [blame] | 124 | |
| 125 | def expect_file(path): |
| 126 | return os.path.exists(path) |
| 127 | |
| 128 | def expect_file_does_contain(pattern, file_path): |
| 129 | return expect_string(pattern, file_path) |