summaryrefslogtreecommitdiff
path: root/tests/warn/warningtester.py
blob: 1f7f91eceeb09eced4fc4604e1216de909081c8a (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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

import os
import sys
import difflib

if sys.version_info.major < 3:
    import __builtin__ as builtins
else:
    import builtins

path = os.getenv('UNINSTALLED_INTROSPECTION_SRCDIR', None)
assert path is not None
sys.path.insert(0, path)

# Not correct, but enough to get the tests going uninstalled
builtins.__dict__['DATADIR'] = path

from giscanner.annotationparser import GtkDocCommentBlockParser
from giscanner.ast import Include, Namespace
from giscanner.introspectablepass import IntrospectablePass
from giscanner.maintransformer import MainTransformer
from giscanner.message import MessageLogger, WARNING, ERROR, FATAL
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, '..', ''))
top_srcdir = os.environ['UNINSTALLED_INTROSPECTION_SRCDIR']
top_builddir = os.environ['TOP_BUILDDIR']


class ChunkedIO(object):
    def __init__(self):
        self.buffer = []

    def write(self, s):
        self.buffer.append(s)

    def getvalue(self):
        return self.buffer


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


def _diff(a, b):
    retval = ''
    started = False

    for group in difflib.SequenceMatcher(None, a, b).get_grouped_opcodes(3):
        if not started:
            started = True
            retval += '--- expected\n'
            retval += '+++ emitted\n'

        for tag, i1, i2, j1, j2 in group:
            if tag == 'equal':
                for line in a[i1:i2]:
                    for l in line.split('\n'):
                        retval += ' ' + l + '\n'
                continue

            if tag in ('replace', 'delete'):
                for line in a[i1:i2]:
                    for l in line.split('\n'):
                        retval += '-' + l + '\n'

            if tag in ('replace', 'insert'):
                for line in b[j1:j2]:
                    for l in line.split('\n'):
                        retval += '+' + l + '\n'

    return retval


def _extract_expected(filename):
    fd = open(filename, 'rU')
    data = fd.read()
    fd.close()

    retval = []
    for line in data.split('\n'):
        if line.startswith('// EXPECT:'):
            retval.append(line[10:] + '\n')
        elif line.startswith('//+'):
            retval[-1] += line[3:] + '\n'

    return retval


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

    output = ChunkedIO()
    namespace = Namespace('Test', '1.0')
    logger = MessageLogger.get(namespace=namespace, output=output)
    logger.enable_warnings((WARNING, ERROR, FATAL))

    transformer = Transformer(namespace)
    transformer.set_include_paths([os.path.join(top_srcdir, 'gir'), top_builddir])
    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())

    cbp = GtkDocCommentBlockParser()
    blocks = cbp.parse_comment_blocks(ss.get_comments())

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

    final = IntrospectablePass(transformer, blocks)
    final.validate()

    emitted_warnings = [w[w.find(':') + 1:] for w in output.getvalue()]

    expected_warnings = _extract_expected(filename)

    sortkey = lambda x: int(x.split(':')[0])
    expected_warnings.sort(key=sortkey)
    emitted_warnings.sort(key=sortkey)

    if len(expected_warnings) != len(emitted_warnings):
        raise SystemExit("ERROR in '%s': %d warnings were emitted, "
                         "expected %d:\n%s" % (os.path.basename(filename),
                                               len(emitted_warnings),
                                               len(expected_warnings),
                                               _diff(expected_warnings, emitted_warnings)))

    for emitted_warning, expected_warning in zip(emitted_warnings, expected_warnings):
        if expected_warning != emitted_warning:
            raise SystemExit("ERROR in '%s': expected warning does not match emitted "
                             "warning:\n%s" % (filename,
                                               _diff([expected_warning], [emitted_warning])))

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