summaryrefslogtreecommitdiff
path: root/tests/gi-tester
blob: fd51ab50bd53bcb38a4bd34680cc5cc369c0fdab (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
#!/usr/bin/env python3

import io
import sys
import os
import re
import difflib
import subprocess


def assert_files_no_diff(a, b, ignore=None):
    if ignore is None:
        ignore = []

    def filter_lines(lines):
        filtered = []
        for line in lines:
            do_ignore = False
            for pattern in ignore:
                if re.search(pattern, line) is not None:
                    do_ignore = True
                    break
            if not do_ignore:
                filtered.append(line)
        return filtered

    with io.open(a, encoding="utf-8") as ah, \
            io.open(b, encoding="utf-8") as bh:

        result = difflib.unified_diff(
            filter_lines(ah.readlines()),
            filter_lines(bh.readlines()),
            fromfile=a, tofile=b)

        result = "".join(result)
        if result:
            raise AssertionError(result)


def assert_dirs_no_diff(a, b, ignore=None):
    list_a = sorted(os.listdir(a))
    list_b = sorted(os.listdir(b))

    result = difflib.unified_diff(
        [l + "\n" for l in list_a],
        [l + "\n" for l in list_b],
        fromfile=a, tofile=b)
    result = "".join(result)
    if result:
        raise AssertionError(result)

    for entry in list_a:
        assert_files_no_diff(
            os.path.join(a, entry),
            os.path.join(b, entry),
            ignore=ignore)


def assert_no_diff(a, b, ignore=None):
    """
    Args:
        a (str): file or directory
        b (str): file or directory
        ignore (list): a list of regexes for lines in files to ignore
    """

    if os.path.isdir(a) != os.path.isdir(b):
        raise AssertionError

    if os.path.isdir(a):
        assert_dirs_no_diff(a, b, ignore=ignore)
    else:
        assert_files_no_diff(a, b, ignore=ignore)


def main(argv):
    if len(argv) != 2:
        raise SystemError("exactly one arg, not %r" % argv[1:])

    targetname = argv[1]
    targetbase = os.path.basename(targetname)
    srcdir = os.environ.get("srcdir", "")
    builddir = os.environ.get("builddir", "")

    if targetname.endswith(".typelib"):
        # Do nothing for typelibs, this just ensures they build as part of
        # the tests
        if not os.path.exists(targetname):
            raise SystemError("%s does not exist" % targetname)
    elif targetname.endswith(".gir"):
        # The "shared-library" field of the GIR is platform-dependent.
        # For example, on OSX, shared libraries have the extension .dylib.
        # Ignore this field when determining whether the output succeeded.
        ignore = ['shared-library=".*"$']
        expected = os.path.join(
            srcdir, os.path.splitext(targetbase)[0] + "-expected.gir")
        actual = os.path.join(builddir, targetname)
        assert_no_diff(expected, actual, ignore=ignore)
    elif targetname.rsplit("-")[-1] in ("C", "Python", "Gjs"):
        expected = os.path.join(srcdir, targetbase + "-expected")
        actual = os.path.join(builddir, targetbase)
        assert_no_diff(expected, actual, ignore=['^\s*$'])
    elif targetname.endswith("-sections.txt"):
        expected = os.path.join(
            srcdir, os.path.splitext(targetname)[0] + "-expected.txt")
        actual = os.path.join(builddir, targetname)
        assert_no_diff(expected, actual, ignore=['^\s*$'])
    elif targetname.endswith(".py"):
        env = os.environ.copy()
        env["PYTHONPATH"] = os.pathsep.join(
            filter(
                None,
                [os.environ.get("top_builddir"),
                 os.environ.get("top_srcdir"),
                 os.environ.get("PYTHONPATH")]))
        subprocess.check_call([env["PYTHON"], targetname], env=env)
    else:
        raise SystemError("Unknown file type: %s" % targetbase)


if __name__ == "__main__":
    sys.exit(main(sys.argv))