summaryrefslogtreecommitdiff
path: root/tests/warn/warningtester.py
blob: 757b1d52be45cb4812da2d28500fe5823fcf5e80 (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
import difflib
import os
import os.path
import sys
from StringIO import StringIO

from giscanner.annotationparser import AnnotationParser
from giscanner.ast import Include, Namespace
from giscanner.introspectablepass import IntrospectablePass
from giscanner.maintransformer import MainTransformer
from giscanner.message import MessageLogger
from giscanner.sourcescanner import SourceScanner
from giscanner.transformer import Transformer
from giscanner.scannermain import process_packages

currentdir = os.path.dirname(os.path.abspath(sys.argv[0]))
current_name = os.path.basename(currentdir)
path = os.path.abspath(os.path.join(currentdir, '..', ''))

class Options:
    def __init__(self):
        self.cpp_includes = []
        self.cpp_defines = []
        self.cpp_undefines = []
        self.library_paths = []

def _diff(orig, new, short):
    def _tolines(s):
        return [s + '\n' for line in s.split('\n')]
    lines = difflib.unified_diff(_tolines(orig),
                                 _tolines(new))
    if not lines:
        return

    diff = False
    try:
        first = lines.next()
        diff = True
    except StopIteration:
        pass
    else:
        print 'ERROR: while comparing %s:' % (short, )
        for line in list(lines)[2:]:
            print '%s: %r' % (short, line[:-1])

    return diff

def _extract_expected(filename):
    fd = open(filename)
    data = fd.read()

    retval = []
    while data:
        pos = data.find("EXPECT:")
        data = data[pos+7:]
        end = data.find('\n')
        if end == -1:
            break
        retval.append(data[:end])
        data = data[end:]
    return sorted(retval)

def check(args):
    filename = args[0]

    output = StringIO()
    namespace = Namespace("Test", "1.0")
    logger = MessageLogger.get(namespace=namespace,
                               output=output)
    logger.enable_warnings(True)
    transformer = Transformer(namespace)
    transformer.register_include(Include.from_string("GObject-2.0"))

    ss = SourceScanner()

    options = Options()
    exit_code = process_packages(options, ['gobject-2.0'])
    if exit_code:
        sys.exit(exit_code)
    ss.set_cpp_options(options.cpp_includes,
                       options.cpp_defines,
                       options.cpp_undefines)
    ss.parse_files([filename])
    ss.parse_macros([filename])
    transformer.parse(ss.get_symbols())

    ap = AnnotationParser()
    blocks = ap.parse(ss.get_comments())

    main = MainTransformer(transformer, blocks)
    main.transform()

    final = IntrospectablePass(transformer)
    final.validate()

    raw = output.getvalue()
    if raw.endswith('\n'):
        raw = raw[:-1]
    warnings = raw.split('\n')

    failed_tests = 0
    expected_warnings = _extract_expected(filename)
    if len(expected_warnings) != len(warnings):
        raise SystemExit(
            "ERROR: expected %d warnings, but got %d: %s\n",
            len(expected_warnings), len(warnings), warnings.join('\n'))
    for warning, expected in zip(warnings, expected_warnings):
        actual = warning.split(":", 1)[1]
        if _diff(actual, expected, filename):
            raise SystemExit("ERROR: tests %r failed" % (filename, ))

sys.exit(check(sys.argv[1:]))