run_test.py 2.21 KB
Newer Older
Wenzel Jakob's avatar
Wenzel Jakob committed
1
2
3
4
import sys
import os
import re
import subprocess
5
import difflib
Wenzel Jakob's avatar
Wenzel Jakob committed
6
7
8

remove_unicode_marker = re.compile(r'u(\'[^\']*\')')
remove_long_marker    = re.compile(r'([0-9])L')
Wenzel Jakob's avatar
Wenzel Jakob committed
9
remove_hex            = re.compile(r'0x[0-9a-fA-F]+')
Wenzel Jakob's avatar
Wenzel Jakob committed
10
11
12
13
14
15
16
17
shorten_floats        = re.compile(r'([1-9][0-9]*\.[0-9]{4})[0-9]*')

def sanitize(lines):
    lines = lines.split('\n')
    for i in range(len(lines)):
        line = lines[i]
        if line.startswith(" |"):
            line = ""
18
19
20
21
        if line.startswith("### "):
            # Constructor/destructor output.  Useful for example, but unreliable across compilers;
            # testing of proper construction/destruction occurs with ConstructorStats mechanism instead
            line = ""
Wenzel Jakob's avatar
Wenzel Jakob committed
22
23
        line = remove_unicode_marker.sub(r'\1', line)
        line = remove_long_marker.sub(r'\1', line)
24
        line = remove_hex.sub(r'0', line)
Wenzel Jakob's avatar
Wenzel Jakob committed
25
26
27
        line = shorten_floats.sub(r'\1', line)
        line = line.replace('__builtin__', 'builtins')
        line = line.replace('example.', '')
28
        line = line.replace('unicode', 'str')
29
        line = line.replace('ExampleWithEnum.EMode', 'EMode')
30
        line = line.replace('example.EMode', 'EMode')
Wenzel Jakob's avatar
Wenzel Jakob committed
31
32
33
34
        line = line.replace('method of builtins.PyCapsule instance', '')
        line = line.strip()
        lines[i] = line

35
    return '\n'.join(sorted([l for l in lines if l != ""]))
Wenzel Jakob's avatar
Wenzel Jakob committed
36
37
38
39
40

path = os.path.dirname(__file__)
if path != '':
    os.chdir(path)

41
if len(sys.argv) < 2:
42
    print("Syntax: %s <test name>" % sys.argv[0])
43
44
    exit(0)

Wenzel Jakob's avatar
Wenzel Jakob committed
45
name = sys.argv[1]
46
try:
47
    output_bytes = subprocess.check_output([sys.executable, "-u", name + ".py"],
48
                                           stderr=subprocess.STDOUT)
49
50
51
52
53
except subprocess.CalledProcessError as exc:
    print('Test `{}` failed:\n{}\n'.format(name, '-' * 50))
    print(exc.output.decode())
    print('-' * 50)
    sys.exit(1)
Wenzel Jakob's avatar
Wenzel Jakob committed
54

Wenzel Jakob's avatar
Wenzel Jakob committed
55
56
output    = sanitize(output_bytes.decode('utf-8'))
reference = sanitize(open(name + '.ref', 'r').read())
Wenzel Jakob's avatar
Wenzel Jakob committed
57

58
if output == reference:
Wenzel Jakob's avatar
Wenzel Jakob committed
59
60
61
62
    print('Test "%s" succeeded.' % name)
    exit(0)
else:
    print('Test "%s" FAILED!' % name)
63
64
    print('--- output')
    print('+++ reference')
Wenzel Jakob's avatar
Wenzel Jakob committed
65
66
    print(''.join(difflib.ndiff(output.splitlines(True),
                                reference.splitlines(True))))
Wenzel Jakob's avatar
Wenzel Jakob committed
67
    exit(-1)