Tudor Timi | 2c27044 | 2019-12-23 19:14:29 +0100 | [diff] [blame] | 1 | import os |
Tudor Timi | 81be42d | 2019-12-23 19:17:57 +0100 | [diff] [blame] | 2 | import pathlib |
Tudor Timi | 8788ae4 | 2019-12-27 17:32:06 +0100 | [diff] [blame^] | 3 | import shutil |
Tudor Timi | 2c27044 | 2019-12-23 19:14:29 +0100 | [diff] [blame] | 4 | import subprocess |
| 5 | |
| 6 | |
Tudor Timi | 81be42d | 2019-12-23 19:17:57 +0100 | [diff] [blame] | 7 | def clean_paths(rm_paths): |
| 8 | for rm_path in rm_paths: |
| 9 | for p in pathlib.Path('.').glob(rm_path): |
| 10 | p.unlink() |
| 11 | |
| 12 | |
Tudor Timi | 2c27044 | 2019-12-23 19:14:29 +0100 | [diff] [blame] | 13 | def create_unit_test(name): |
| 14 | subprocess.check_call(['create_unit_test.pl', name]) |
| 15 | |
| 16 | |
Tudor Timi | 8788ae4 | 2019-12-27 17:32:06 +0100 | [diff] [blame^] | 17 | def get_simulators(): |
| 18 | result = [] |
| 19 | |
| 20 | if shutil.which('irun'): |
| 21 | result.append('irun') |
| 22 | if shutil.which('vcs'): |
| 23 | result.append('vcs') |
| 24 | if shutil.which('vlog'): |
| 25 | result.append('modelsim') |
| 26 | |
| 27 | assert result, 'None of irun, modelsim or vcs are in your path. You need at least 1 simulator to regress svunit-code!' |
| 28 | |
| 29 | return result |
| 30 | |
| 31 | |
| 32 | def get_svunit_root(): |
| 33 | return os.path.dirname(os.path.dirname(os.path.realpath(__file__))) |
| 34 | |
| 35 | |
Tudor Timi | 2c27044 | 2019-12-23 19:14:29 +0100 | [diff] [blame] | 36 | def golden_class_unit_test(FILE, MYNAME): |
| 37 | template = open('{}/test/templates/class_unit_test.gold'.format(os.environ['SVUNIT_INSTALL'])) |
| 38 | with open('{}_unit_test.gold'.format(FILE), 'w') as output: |
| 39 | for line in template: |
| 40 | output.write(line.replace('FILE', FILE).replace('MYNAME', MYNAME)) |
| 41 | |
Tudor Timi | 63b4c81 | 2019-12-25 16:34:31 +0100 | [diff] [blame] | 42 | def golden_module_unit_test(FILE, MYNAME): |
| 43 | template = open('{}/test/templates/module_unit_test.gold'.format(os.environ['SVUNIT_INSTALL'])) |
| 44 | with open('{}_unit_test.gold'.format(FILE), 'w') as output: |
| 45 | for line in template: |
| 46 | output.write(line.replace('FILE', FILE).replace('MYNAME', MYNAME)) |
| 47 | |
Tudor Timi | 4b574fc | 2019-12-25 16:38:01 +0100 | [diff] [blame] | 48 | def golden_if_unit_test(FILE, MYNAME): |
| 49 | template = open('{}/test/templates/if_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 Timi | 2c27044 | 2019-12-23 19:14:29 +0100 | [diff] [blame] | 54 | def golden_testsuite_with_1_unittest(MYNAME): |
| 55 | template = open('{}/test/templates/testsuite_with_1_unittest.gold'.format(os.environ['SVUNIT_INSTALL'])) |
| 56 | with open('testsuite.gold', 'w') as output: |
| 57 | for line in template: |
| 58 | output.write(line.replace('MYNAME', MYNAME)) |
| 59 | |
| 60 | def golden_testrunner_with_1_testsuite(): |
| 61 | template = open('{}/test/templates/testrunner_with_1_testsuite.gold'.format(os.environ['SVUNIT_INSTALL'])) |
| 62 | with open('testrunner.gold', 'w') as output: |
| 63 | for line in template: |
| 64 | output.write(line) |
| 65 | |
Tudor Timi | 8f95e9b | 2019-12-24 14:42:04 +0100 | [diff] [blame] | 66 | def golden_testrunner_with_2_testsuites(): |
| 67 | template = open('{}/test/templates/testrunner_with_2_testsuite.gold'.format(os.environ['SVUNIT_INSTALL'])) |
| 68 | with open('testrunner.gold', 'w') as output: |
| 69 | for line in template: |
| 70 | output.write(line) |
| 71 | |
Tudor Timi | bd0006b | 2019-12-25 16:22:04 +0100 | [diff] [blame] | 72 | def golden_testrunner_with_3_testsuites(): |
| 73 | template = open('{}/test/templates/testrunner_with_3_testsuite.gold'.format(os.environ['SVUNIT_INSTALL'])) |
| 74 | with open('testrunner.gold', 'w') as output: |
| 75 | for line in template: |
| 76 | output.write(line) |
| 77 | |
Tudor Timi | 02cc3c1 | 2019-12-25 11:15:45 +0100 | [diff] [blame] | 78 | def golden_testrunner_with_4_testsuites(): |
| 79 | template = open('{}/test/templates/testrunner_with_4_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 Timi | 2c27044 | 2019-12-23 19:14:29 +0100 | [diff] [blame] | 84 | |
| 85 | def verify_file(file0, file1): |
| 86 | result = subprocess.run(['diff', '-wbB', file0, file1], stdout=subprocess.PIPE) |
| 87 | assert result.returncode in [0, 1] |
| 88 | if result.returncode == 1: |
| 89 | assert result.stdout == b'' |
| 90 | |
| 91 | def verify_testsute(testsuite, dir=''): |
| 92 | PWD = '_' |
| 93 | file = open(testsuite) |
| 94 | with open('.{}'.format(testsuite), 'w') as output: |
| 95 | for line in file: |
| 96 | output.write(line.replace('PWD', "{}{}".format(PWD, dir))) |
| 97 | verify_file(output.name, '.{}{}_testsuite.sv'.format(PWD, dir)) |
| 98 | |
| 99 | def verify_testrunner(testrunner, ts0, ts1='', ts2='', ts3='', tr=''): |
| 100 | if tr == '': |
| 101 | tr = '.testrunner.sv' |
| 102 | file = open(testrunner) |
| 103 | with open('.{}'.format(testrunner), 'w') as output: |
| 104 | for line in file: |
| 105 | output.write(line.replace('TS0', ts0).replace('TS1', ts1).replace('TS2', ts2).replace('TS3', ts3)) |
| 106 | verify_file(output.name, tr) |