aboutsummaryrefslogtreecommitdiffstats
path: root/selftest/all_tests.py
blob: bb00793997a73a0338b813505d3d59db554b526f (plain)
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
121
122
123
124
125
126
127
128
129
#!/usr/bin/env python3

import os
import sys
import subprocess
import time
import difflib
import argparse
import re

parser = argparse.ArgumentParser()
parser.add_argument('testdir_or_test', nargs='*',
        help='subdir name or test script name')
parser.add_argument('-u', '--update', action='store_true',
        help='Update test expecations instead of verifying them')
args = parser.parse_args()

def run_test(path):
    print(path)
    p = subprocess.Popen(path, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    o,e = p.communicate()
    while True:
        retval = p.poll()
        if retval is not None:
            break;
        p.kill()
        time.sleep(.1)
    return retval, o.decode('utf-8'), e.decode('utf-8')

def udiff(expect, got, expect_path):
    expect = expect.splitlines(1)
    got =  got.splitlines(1)
    for line in difflib.unified_diff(expect, got,
                                     fromfile=expect_path, tofile='got'):
        sys.stderr.write(line)
        if not line.endswith('\n'):
            sys.stderr.write('[no-newline]\n')

def verify_output(got, expect_file, update=False):
    if os.path.isfile(expect_file):
        ign_file = expect_file + '.ign'
        if os.path.isfile(ign_file):
            with open(ign_file, 'r') as f:
                ign_rules = f.readlines()
            for ign_rule in ign_rules:
                if not ign_rule:
                    continue
                if '\t' in ign_rule:
                    ign_rule, repl = ign_rule.split('\t')
                    repl = repl.strip()
                else:
                    repl = '*'
                ir = re.compile(ign_rule)
                got = repl.join(ir.split(got))

        if update:
            with open(expect_file, 'w') as f:
                f.write(got)
            return True

        with open(expect_file, 'r') as f:
            expect = f.read()

        if expect != got:
            udiff(expect, got, expect_file)
            sys.stderr.write('output mismatch: %r\n'
                            % os.path.basename(expect_file))
            return False
    return True


script_dir = sys.path[0]

tests = []
for d in os.listdir(script_dir):
    dir_path = os.path.join(script_dir, d)
    if not os.path.isdir(dir_path):
        continue
    if not dir_path.endswith('_test'):
        continue
    for f in os.listdir(dir_path):
        file_path = os.path.join(script_dir, d, f)
        if not os.path.isfile(file_path):
            continue

        if not (file_path.endswith('_test.py') or file_path.endswith('_test.sh')):
            continue
        tests.append(file_path)

ran = []
errors = []

for test in sorted(tests):

    if args.testdir_or_test:
        if not any([t in test for t in args.testdir_or_test]):
            continue

    ran.append(test)

    success = True

    name, ext = os.path.splitext(test)
    ok_file = name + '.ok'
    err_file = name + '.err'

    rc, out, err = run_test(test)

    if rc != 0:
        sys.stderr.write('%r: returned %d\n' % (os.path.basename(test), rc))
        success = False

    if not verify_output(out, ok_file, args.update):
        success = False
    if not verify_output(err, err_file, args.update):
        success = False

    if not success:
        sys.stderr.write('\nTest failed: %r\n\n' % os.path.basename(test))
        errors.append(test)

if errors:
    print('%d of %d TESTS FAILED:\n  %s' % (len(errors), len(ran), '\n  '.join(errors)))
    exit(1)

print('%d tests ok' % len(ran))
exit(0)

# vim: expandtab tabstop=4 shiftwidth=4