~mmach/netext73/mesa-haswell

« back to all changes in this revision

Viewing changes to src/compiler/glsl/glcpp/tests/glcpp_test.py

  • Committer: mmach
  • Date: 2022-09-22 19:56:13 UTC
  • Revision ID: netbit73@gmail.com-20220922195613-wtik9mmy20tmor0i
2022-09-22 21:17:09

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# encoding=utf-8
2
 
# Copyright © 2018 Intel Corporation
3
 
 
4
 
# Permission is hereby granted, free of charge, to any person obtaining a copy
5
 
# of this software and associated documentation files (the "Software"), to deal
6
 
# in the Software without restriction, including without limitation the rights
7
 
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
 
# copies of the Software, and to permit persons to whom the Software is
9
 
# furnished to do so, subject to the following conditions:
10
 
 
11
 
# The above copyright notice and this permission notice shall be included in
12
 
# all copies or substantial portions of the Software.
13
 
 
14
 
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
 
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
 
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
 
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
 
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
 
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
 
# SOFTWARE.
21
 
 
22
 
"""Run glcpp tests with various line endings."""
23
 
 
24
 
import argparse
25
 
import difflib
26
 
import errno
27
 
import io
28
 
import os
29
 
import subprocess
30
 
import sys
31
 
 
32
 
# The meson version handles windows paths better, but if it's not available
33
 
# fall back to shlex
34
 
try:
35
 
    from meson.mesonlib import split_args
36
 
except ImportError:
37
 
    from shlex import split as split_args
38
 
 
39
 
 
40
 
def arg_parser():
41
 
    parser = argparse.ArgumentParser()
42
 
    parser.add_argument('glcpp', help='Path to the he glcpp binary.')
43
 
    parser.add_argument('testdir', help='Path to tests and expected output.')
44
 
    parser.add_argument('--unix', action='store_true', help='Run tests for Unix style newlines')
45
 
    parser.add_argument('--windows', action='store_true', help='Run tests for Windows/Dos style newlines')
46
 
    parser.add_argument('--oldmac', action='store_true', help='Run tests for Old Mac (pre-OSX) style newlines')
47
 
    parser.add_argument('--bizarro', action='store_true', help='Run tests for Bizarro world style newlines')
48
 
    return parser.parse_args()
49
 
 
50
 
 
51
 
def parse_test_file(contents, nl_format):
52
 
    """Check for any special arguments and return them as a list."""
53
 
    # Disable "universal newlines" mode; we can't directly use `nl_format` as
54
 
    # the `newline` argument, because the "bizarro" test uses something Python
55
 
    # considers invalid.
56
 
    for l in contents.decode('utf-8').split(nl_format):
57
 
            if 'glcpp-args:' in l:
58
 
                return l.split('glcpp-args:')[1].strip().split()
59
 
    return []
60
 
 
61
 
 
62
 
def test_output(glcpp, contents, expfile, nl_format='\n'):
63
 
    """Test that the output of glcpp is what we expect."""
64
 
    extra_args = parse_test_file(contents, nl_format)
65
 
 
66
 
    proc = subprocess.Popen(
67
 
        glcpp + extra_args,
68
 
        stdout=subprocess.PIPE,
69
 
        stderr=subprocess.STDOUT,
70
 
        stdin=subprocess.PIPE)
71
 
    actual, _ = proc.communicate(contents)
72
 
    actual = actual.decode('utf-8')
73
 
 
74
 
    if proc.returncode == 255:
75
 
        print("Test returned general error, possibly missing linker")
76
 
        sys.exit(77)
77
 
 
78
 
    with open(expfile, 'r') as f:
79
 
        expected = f.read()
80
 
 
81
 
    # Bison 3.6 changed '$end' to 'end of file' in its error messages
82
 
    # See: https://gitlab.freedesktop.org/mesa/mesa/-/issues/3181
83
 
    actual = actual.replace('$end', 'end of file')
84
 
 
85
 
    if actual == expected:
86
 
        return (True, [])
87
 
    return (False, difflib.unified_diff(actual.splitlines(), expected.splitlines()))
88
 
 
89
 
 
90
 
def test_unix(args):
91
 
    """Test files with unix style (\n) new lines."""
92
 
    total = 0
93
 
    passed = 0
94
 
 
95
 
    print('============= Testing for Correctness (Unix) =============')
96
 
    for filename in os.listdir(args.testdir):
97
 
        if not filename.endswith('.c'):
98
 
            continue
99
 
 
100
 
        print(   '{}:'.format(os.path.splitext(filename)[0]), end=' ')
101
 
        total += 1
102
 
 
103
 
        testfile = os.path.join(args.testdir, filename)
104
 
        with open(testfile, 'rb') as f:
105
 
            contents = f.read()
106
 
        valid, diff = test_output(args.glcpp, contents, testfile + '.expected')
107
 
        if valid:
108
 
            passed += 1
109
 
            print('PASS')
110
 
        else:
111
 
            print('FAIL')
112
 
            for l in diff:
113
 
                print(l, file=sys.stderr)
114
 
 
115
 
    if not total:
116
 
        raise Exception('Could not find any tests.')
117
 
 
118
 
    print('{}/{}'.format(passed, total), 'tests returned correct results')
119
 
    return total == passed
120
 
 
121
 
 
122
 
def _replace_test(args, replace):
123
 
    """Test files with non-unix style line endings. Print your own header."""
124
 
    total = 0
125
 
    passed = 0
126
 
 
127
 
    for filename in os.listdir(args.testdir):
128
 
        if not filename.endswith('.c'):
129
 
            continue
130
 
 
131
 
        print(   '{}:'.format(os.path.splitext(filename)[0]), end=' ')
132
 
        total += 1
133
 
        testfile = os.path.join(args.testdir, filename)
134
 
 
135
 
        with open(testfile, 'rt') as f:
136
 
            contents = f.read()
137
 
        contents = contents.replace('\n', replace).encode('utf-8')
138
 
        valid, diff = test_output(
139
 
            args.glcpp, contents, testfile + '.expected', nl_format=replace)
140
 
 
141
 
        if valid:
142
 
            passed += 1
143
 
            print('PASS')
144
 
        else:
145
 
            print('FAIL')
146
 
            for l in diff:
147
 
                print(l, file=sys.stderr)
148
 
 
149
 
    if not total:
150
 
        raise Exception('Could not find any tests.')
151
 
 
152
 
    print('{}/{}'.format(passed, total), 'tests returned correct results')
153
 
    return total == passed
154
 
 
155
 
 
156
 
def test_windows(args):
157
 
    """Test files with windows/dos style (\r\n) new lines."""
158
 
    print('============= Testing for Correctness (Windows) =============')
159
 
    return _replace_test(args, '\r\n')
160
 
 
161
 
 
162
 
def test_oldmac(args):
163
 
    """Test files with Old Mac style (\r) new lines."""
164
 
    print('============= Testing for Correctness (Old Mac) =============')
165
 
    return _replace_test(args, '\r')
166
 
 
167
 
 
168
 
def test_bizarro(args):
169
 
    """Test files with Bizarro world style (\n\r) new lines."""
170
 
    # This is allowed by the spec, but why?
171
 
    print('============= Testing for Correctness (Bizarro) =============')
172
 
    return _replace_test(args, '\n\r')
173
 
 
174
 
 
175
 
def main():
176
 
    args = arg_parser()
177
 
 
178
 
    wrapper = os.environ.get('MESON_EXE_WRAPPER')
179
 
    if wrapper is not None:
180
 
        args.glcpp = split_args(wrapper) + [args.glcpp]
181
 
    else:
182
 
        args.glcpp = [args.glcpp]
183
 
 
184
 
    success = True
185
 
    try:
186
 
        if args.unix:
187
 
            success = success and test_unix(args)
188
 
        if args.windows:
189
 
            success = success and test_windows(args)
190
 
        if args.oldmac:
191
 
            success = success and test_oldmac(args)
192
 
        if args.bizarro:
193
 
            success = success and test_bizarro(args)
194
 
    except OSError as e:
195
 
        if e.errno == errno.ENOEXEC:
196
 
            print('Skipping due to inability to run host binaries.',
197
 
                  file=sys.stderr)
198
 
            sys.exit(77)
199
 
        raise
200
 
 
201
 
    exit(0 if success else 1)
202
 
 
203
 
 
204
 
if __name__ == '__main__':
205
 
    main()