File: run_unit.py

package info (click to toggle)
mpich 5.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 251,828 kB
  • sloc: ansic: 1,323,147; cpp: 82,869; f90: 72,420; javascript: 40,763; perl: 28,296; sh: 19,399; python: 16,191; xml: 14,418; makefile: 9,468; fortran: 8,046; java: 4,635; pascal: 352; asm: 324; ruby: 176; awk: 27; lisp: 19; php: 8; sed: 4
file content (120 lines) | stat: -rw-r--r-- 3,253 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import os
import sys
import re
import subprocess

class G:
    unit_name = None
    template_lines = []
    tests = []

class RE:
    m = None
    def match(pat, str, flags=0):
        RE.m = re.match(pat, str, flags)
        return RE.m

def main():
    os.chdir(sys.path[0])

    if not os.path.exists("out"):
        os.mkdir("out")

    argc = len(sys.argv)
    if argc > 1:
        args = sys.argv[1:]
    else:
        args = load_test_units()
    for a in args:
        G.unit_name = a
        G.template_lines = []
        G.tests = []
        load_unit(a + '.unit')

        os.chdir("out")
        if build_unit():
            run_tests()
        os.chdir("..")

def load_test_units():
    files = os.listdir()
    units = []
    for f in files:
        if RE.match(r'(\w+)\.unit', f):
            units.append(RE.m.group(1))
    return units

def load_unit(file_spec):
    cur_block = None
    cur_test = None

    with open(file_spec, "r") as In:
        for line in In:
            if RE.match(r'(\w+):', line):
                cur_block = RE.m.group(1)
            elif cur_block == 'template':
                if RE.match(r'\s+\[(\S+):(\w+)\]', line):
                    load_source(RE.m.group(1), RE.m.group(2))
                else:
                    # trim the leading indentation
                    G.template_lines.append(re.sub('^    ', '', line))
            elif cur_block == 'TESTS':
                if RE.match(r'\s+cmd:\s*(.+)', line):
                    cur_test = {'cmd': RE.m.group(1), 'expect': None}
                    G.tests.append(cur_test)
                elif RE.match(r'\s+expect:\s*(.*)', line):
                    cur_test['expect'] = RE.m.group(1).rstrip()

def load_source(file_src, name):
    capture = False
    # assume cwd is test/units
    with open("../../" + file_src, "r") as In:
        for line in In:
            if RE.match(r'\s*\/\*\s*START UNIT\s*-\s*(\w+)', line) and RE.m.group(1) == name:
                capture = True
            elif RE.match(r'\s*\/\*\s*END UNIT', line):
                capture = False
            elif capture:
                G.template_lines.append(line)

def build_unit():
    with open("t.c", "w") as Out:
        for line in G.template_lines:
            Out.write(line)

    if os.path.exists("t"):
        os.remove("t")

    if 'CC' in os.environ:
        cc = os.environ['CC']
    else:
        cc = 'gcc'

    os.system(cc + " -o t t.c")

    return os.path.exists("t")


def run_tests():
    errs = 0
    print("Test - %s" % G.unit_name)
    for i, test in enumerate(G.tests):
        print("  %d:" % (i + 1))
        print("    %s" % test['cmd'])
        ret = subprocess.run(test['cmd'], shell=True, encoding='ascii', capture_output=True)
        if ret.returncode:
            print("    not ok, exit code = %d" % ret.returncode)
            print(ret.stdout)
        else:
            output = ret.stdout.strip()
            print("    %s" % output)
            if test['expect'] and test['expect'] != output:
                print("    not ok, expect: %s" % test['expect'])
                errs += 1
            else:
                print("    ok");
    return errs

#---------------------------------------- 
if __name__ == "__main__":
    main()