2
# Copyright © 2018 Intel Corporation
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:
11
# The above copyright notice and this permission notice shall be included in
12
# all copies or substantial portions of the Software.
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
22
"""Run glcpp tests with various line endings."""
32
# The meson version handles windows paths better, but if it's not available
35
from meson.mesonlib import split_args
37
from shlex import split as split_args
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()
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
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()
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)
66
proc = subprocess.Popen(
68
stdout=subprocess.PIPE,
69
stderr=subprocess.STDOUT,
70
stdin=subprocess.PIPE)
71
actual, _ = proc.communicate(contents)
72
actual = actual.decode('utf-8')
74
if proc.returncode == 255:
75
print("Test returned general error, possibly missing linker")
78
with open(expfile, 'r') as f:
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')
85
if actual == expected:
87
return (False, difflib.unified_diff(actual.splitlines(), expected.splitlines()))
91
"""Test files with unix style (\n) new lines."""
95
print('============= Testing for Correctness (Unix) =============')
96
for filename in os.listdir(args.testdir):
97
if not filename.endswith('.c'):
100
print( '{}:'.format(os.path.splitext(filename)[0]), end=' ')
103
testfile = os.path.join(args.testdir, filename)
104
with open(testfile, 'rb') as f:
106
valid, diff = test_output(args.glcpp, contents, testfile + '.expected')
113
print(l, file=sys.stderr)
116
raise Exception('Could not find any tests.')
118
print('{}/{}'.format(passed, total), 'tests returned correct results')
119
return total == passed
122
def _replace_test(args, replace):
123
"""Test files with non-unix style line endings. Print your own header."""
127
for filename in os.listdir(args.testdir):
128
if not filename.endswith('.c'):
131
print( '{}:'.format(os.path.splitext(filename)[0]), end=' ')
133
testfile = os.path.join(args.testdir, filename)
135
with open(testfile, 'rt') as f:
137
contents = contents.replace('\n', replace).encode('utf-8')
138
valid, diff = test_output(
139
args.glcpp, contents, testfile + '.expected', nl_format=replace)
147
print(l, file=sys.stderr)
150
raise Exception('Could not find any tests.')
152
print('{}/{}'.format(passed, total), 'tests returned correct results')
153
return total == passed
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')
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')
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')
178
wrapper = os.environ.get('MESON_EXE_WRAPPER')
179
if wrapper is not None:
180
args.glcpp = split_args(wrapper) + [args.glcpp]
182
args.glcpp = [args.glcpp]
187
success = success and test_unix(args)
189
success = success and test_windows(args)
191
success = success and test_oldmac(args)
193
success = success and test_bizarro(args)
195
if e.errno == errno.ENOEXEC:
196
print('Skipping due to inability to run host binaries.',
201
exit(0 if success else 1)
204
if __name__ == '__main__':