2018-01-09 23:26:39 +00:00
|
|
|
# encoding=utf-8
|
|
|
|
# Copyright © 2018 Intel Corporation
|
|
|
|
|
|
|
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
# of this software and associated documentation files (the "Software"), to deal
|
|
|
|
# in the Software without restriction, including without limitation the rights
|
|
|
|
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
# copies of the Software, and to permit persons to whom the Software is
|
|
|
|
# furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
# The above copyright notice and this permission notice shall be included in
|
|
|
|
# all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
# SOFTWARE.
|
|
|
|
|
|
|
|
"""Run glcpp tests with various line endings."""
|
|
|
|
|
|
|
|
import argparse
|
|
|
|
import difflib
|
2020-05-14 23:36:36 +01:00
|
|
|
import errno
|
2018-01-09 23:26:39 +00:00
|
|
|
import io
|
|
|
|
import os
|
|
|
|
import subprocess
|
|
|
|
import sys
|
|
|
|
|
2020-05-14 23:36:36 +01:00
|
|
|
# The meson version handles windows paths better, but if it's not available
|
|
|
|
# fall back to shlex
|
|
|
|
try:
|
|
|
|
from meson.mesonlib import split_args
|
|
|
|
except ImportError:
|
|
|
|
from shlex import split as split_args
|
|
|
|
|
2018-01-09 23:26:39 +00:00
|
|
|
|
|
|
|
def arg_parser():
|
|
|
|
parser = argparse.ArgumentParser()
|
|
|
|
parser.add_argument('glcpp', help='Path to the he glcpp binary.')
|
|
|
|
parser.add_argument('testdir', help='Path to tests and expected output.')
|
|
|
|
parser.add_argument('--unix', action='store_true', help='Run tests for Unix style newlines')
|
|
|
|
parser.add_argument('--windows', action='store_true', help='Run tests for Windows/Dos style newlines')
|
|
|
|
parser.add_argument('--oldmac', action='store_true', help='Run tests for Old Mac (pre-OSX) style newlines')
|
|
|
|
parser.add_argument('--bizarro', action='store_true', help='Run tests for Bizarro world style newlines')
|
|
|
|
parser.add_argument('--valgrind', action='store_true', help='Run with valgrind for errors')
|
|
|
|
return parser.parse_args()
|
|
|
|
|
|
|
|
|
2021-03-05 22:05:51 +00:00
|
|
|
def parse_test_file(contents, nl_format):
|
2018-01-09 23:26:39 +00:00
|
|
|
"""Check for any special arguments and return them as a list."""
|
python: Disable universal newlines
We are testing the behaviour of a tool, for different input files, each
one using a different newline sequence. ('\n' on UNIX, '\r\n' on
Windows, …)
Unfortunately, when opening a file in text mode, Python 3 will by
default enable the "universal newlines" mode, which means it replaces
all the known newline sequences by '\n'.
This (usually useful) behaviour breaks the tests, which are specifically
trying to handle files with newline sequences different from '\n'.
Disabling the universal newlines mode fixes the tests.
However, to keep the script compatible with both Python 2 and 3, we must
use the io.open() function instead of the open() builtin, as the latter
only knows about the `newline` argument on Python 3.
Reviewed-by: Dylan Baker <dylan@pnwbakers.com>
2018-08-17 20:32:17 +01:00
|
|
|
# Disable "universal newlines" mode; we can't directly use `nl_format` as
|
|
|
|
# the `newline` argument, because the "bizarro" test uses something Python
|
|
|
|
# considers invalid.
|
2021-03-05 22:05:51 +00:00
|
|
|
for l in contents.decode('utf-8').split(nl_format):
|
2018-01-09 23:26:39 +00:00
|
|
|
if 'glcpp-args:' in l:
|
|
|
|
return l.split('glcpp-args:')[1].strip().split()
|
|
|
|
return []
|
|
|
|
|
|
|
|
|
2021-03-05 22:05:51 +00:00
|
|
|
def test_output(glcpp, contents, expfile, nl_format='\n'):
|
2018-01-09 23:26:39 +00:00
|
|
|
"""Test that the output of glcpp is what we expect."""
|
2021-03-05 22:05:51 +00:00
|
|
|
extra_args = parse_test_file(contents, nl_format)
|
2018-01-09 23:26:39 +00:00
|
|
|
|
2021-03-05 22:05:51 +00:00
|
|
|
proc = subprocess.Popen(
|
|
|
|
glcpp + extra_args,
|
|
|
|
stdout=subprocess.PIPE,
|
|
|
|
stderr=subprocess.STDOUT,
|
|
|
|
stdin=subprocess.PIPE)
|
|
|
|
actual, _ = proc.communicate(contents)
|
|
|
|
actual = actual.decode('utf-8')
|
|
|
|
|
|
|
|
if proc.returncode == 255:
|
|
|
|
print("Test returned general error, possibly missing linker")
|
|
|
|
sys.exit(77)
|
2020-09-12 16:53:52 +01:00
|
|
|
|
2018-08-17 20:32:16 +01:00
|
|
|
with open(expfile, 'r') as f:
|
2018-01-09 23:26:39 +00:00
|
|
|
expected = f.read()
|
|
|
|
|
2020-11-17 19:58:24 +00:00
|
|
|
# Bison 3.6 changed '$end' to 'end of file' in its error messages
|
|
|
|
# See: https://gitlab.freedesktop.org/mesa/mesa/-/issues/3181
|
|
|
|
actual = actual.replace('$end', 'end of file')
|
|
|
|
|
2018-01-09 23:26:39 +00:00
|
|
|
if actual == expected:
|
|
|
|
return (True, [])
|
|
|
|
return (False, difflib.unified_diff(actual.splitlines(), expected.splitlines()))
|
|
|
|
|
|
|
|
|
|
|
|
def _valgrind(glcpp, filename):
|
|
|
|
"""Run valgrind and report any warnings."""
|
2021-03-05 22:05:51 +00:00
|
|
|
with open(filename, 'rb') as f:
|
|
|
|
contents = f.read()
|
|
|
|
extra_args = parse_test_file(contents, nl_format='\n')
|
|
|
|
|
|
|
|
proc = subprocess.Popen(
|
2021-03-05 23:24:09 +00:00
|
|
|
['valgrind', '--error-exitcode=126'] + glcpp + extra_args,
|
2021-03-05 22:05:51 +00:00
|
|
|
stdout=subprocess.DEVNULL,
|
|
|
|
stderr=subprocess.PIPE,
|
|
|
|
stdin=subprocess.PIPE)
|
|
|
|
_, errors = proc.communicate(contents)
|
2021-03-05 23:24:09 +00:00
|
|
|
if proc.returncode != 126:
|
2021-03-05 22:05:51 +00:00
|
|
|
return (True, [])
|
|
|
|
return (False, errors.decode('utf-8'))
|
2018-01-09 23:26:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_unix(args):
|
|
|
|
"""Test files with unix style (\n) new lines."""
|
|
|
|
total = 0
|
|
|
|
passed = 0
|
|
|
|
|
|
|
|
print('============= Testing for Correctness (Unix) =============')
|
|
|
|
for filename in os.listdir(args.testdir):
|
|
|
|
if not filename.endswith('.c'):
|
|
|
|
continue
|
|
|
|
|
|
|
|
print( '{}:'.format(os.path.splitext(filename)[0]), end=' ')
|
|
|
|
total += 1
|
|
|
|
|
|
|
|
testfile = os.path.join(args.testdir, filename)
|
2021-03-05 22:05:51 +00:00
|
|
|
with open(testfile, 'rb') as f:
|
|
|
|
contents = f.read()
|
|
|
|
valid, diff = test_output(args.glcpp, contents, testfile + '.expected')
|
2018-01-09 23:26:39 +00:00
|
|
|
if valid:
|
|
|
|
passed += 1
|
|
|
|
print('PASS')
|
|
|
|
else:
|
|
|
|
print('FAIL')
|
|
|
|
for l in diff:
|
|
|
|
print(l, file=sys.stderr)
|
|
|
|
|
2018-04-24 18:49:22 +01:00
|
|
|
if not total:
|
|
|
|
raise Exception('Could not find any tests.')
|
|
|
|
|
2018-01-09 23:26:39 +00:00
|
|
|
print('{}/{}'.format(passed, total), 'tests returned correct results')
|
|
|
|
return total == passed
|
|
|
|
|
|
|
|
|
|
|
|
def _replace_test(args, replace):
|
|
|
|
"""Test files with non-unix style line endings. Print your own header."""
|
|
|
|
total = 0
|
|
|
|
passed = 0
|
|
|
|
|
|
|
|
for filename in os.listdir(args.testdir):
|
|
|
|
if not filename.endswith('.c'):
|
|
|
|
continue
|
|
|
|
|
|
|
|
print( '{}:'.format(os.path.splitext(filename)[0]), end=' ')
|
|
|
|
total += 1
|
|
|
|
testfile = os.path.join(args.testdir, filename)
|
2021-03-05 22:05:51 +00:00
|
|
|
|
|
|
|
with open(testfile, 'rt') as f:
|
|
|
|
contents = f.read()
|
|
|
|
contents = contents.replace('\n', replace).encode('utf-8')
|
|
|
|
valid, diff = test_output(
|
|
|
|
args.glcpp, contents, testfile + '.expected', nl_format=replace)
|
2018-01-09 23:26:39 +00:00
|
|
|
|
|
|
|
if valid:
|
|
|
|
passed += 1
|
|
|
|
print('PASS')
|
|
|
|
else:
|
|
|
|
print('FAIL')
|
|
|
|
for l in diff:
|
|
|
|
print(l, file=sys.stderr)
|
|
|
|
|
2018-04-24 18:49:22 +01:00
|
|
|
if not total:
|
|
|
|
raise Exception('Could not find any tests.')
|
|
|
|
|
2018-01-09 23:26:39 +00:00
|
|
|
print('{}/{}'.format(passed, total), 'tests returned correct results')
|
|
|
|
return total == passed
|
|
|
|
|
|
|
|
|
|
|
|
def test_windows(args):
|
|
|
|
"""Test files with windows/dos style (\r\n) new lines."""
|
|
|
|
print('============= Testing for Correctness (Windows) =============')
|
|
|
|
return _replace_test(args, '\r\n')
|
|
|
|
|
|
|
|
|
|
|
|
def test_oldmac(args):
|
|
|
|
"""Test files with Old Mac style (\r) new lines."""
|
|
|
|
print('============= Testing for Correctness (Old Mac) =============')
|
|
|
|
return _replace_test(args, '\r')
|
|
|
|
|
|
|
|
|
|
|
|
def test_bizarro(args):
|
|
|
|
"""Test files with Bizarro world style (\n\r) new lines."""
|
|
|
|
# This is allowed by the spec, but why?
|
|
|
|
print('============= Testing for Correctness (Bizarro) =============')
|
|
|
|
return _replace_test(args, '\n\r')
|
|
|
|
|
|
|
|
|
|
|
|
def test_valgrind(args):
|
|
|
|
total = 0
|
|
|
|
passed = 0
|
|
|
|
|
|
|
|
print('============= Testing for Valgrind Warnings =============')
|
|
|
|
for filename in os.listdir(args.testdir):
|
|
|
|
if not filename.endswith('.c'):
|
|
|
|
continue
|
|
|
|
|
|
|
|
print( '{}:'.format(os.path.splitext(filename)[0]), end=' ')
|
|
|
|
total += 1
|
|
|
|
valid, log = _valgrind(args.glcpp, os.path.join(args.testdir, filename))
|
|
|
|
if valid:
|
|
|
|
passed += 1
|
|
|
|
print('PASS')
|
|
|
|
else:
|
|
|
|
print('FAIL')
|
|
|
|
print(log, file=sys.stderr)
|
|
|
|
|
2018-04-24 18:49:22 +01:00
|
|
|
if not total:
|
|
|
|
raise Exception('Could not find any tests.')
|
|
|
|
|
2018-01-09 23:26:39 +00:00
|
|
|
print('{}/{}'.format(passed, total), 'tests returned correct results')
|
|
|
|
return total == passed
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
args = arg_parser()
|
|
|
|
|
2020-05-14 23:36:36 +01:00
|
|
|
wrapper = os.environ.get('MESON_EXE_WRAPPER')
|
|
|
|
if wrapper is not None:
|
|
|
|
args.glcpp = split_args(wrapper) + [args.glcpp]
|
|
|
|
else:
|
|
|
|
args.glcpp = [args.glcpp]
|
|
|
|
|
2018-01-09 23:26:39 +00:00
|
|
|
success = True
|
2020-05-14 23:36:36 +01:00
|
|
|
try:
|
|
|
|
if args.unix:
|
|
|
|
success = success and test_unix(args)
|
|
|
|
if args.windows:
|
|
|
|
success = success and test_windows(args)
|
|
|
|
if args.oldmac:
|
|
|
|
success = success and test_oldmac(args)
|
|
|
|
if args.bizarro:
|
|
|
|
success = success and test_bizarro(args)
|
|
|
|
if args.valgrind:
|
|
|
|
success = success and test_valgrind(args)
|
|
|
|
except OSError as e:
|
|
|
|
if e.errno == errno.ENOEXEC:
|
|
|
|
print('Skipping due to inability to run host binaries.',
|
|
|
|
file=sys.stderr)
|
|
|
|
sys.exit(77)
|
|
|
|
raise
|
2018-01-09 23:26:39 +00:00
|
|
|
|
|
|
|
exit(0 if success else 1)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|