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 24import argparse 25import difflib 26import errno 27import io 28import os 29import subprocess 30import sys 31 32# The meson version handles windows paths better, but if it's not available 33# fall back to shlex 34try: 35 from meson.mesonlib import split_args 36except ImportError: 37 from shlex import split as split_args 38 39 40def 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 51def 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 62def 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, 'rb') as f: 79 expected = f.read().decode('utf-8') 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 # Bison 3.6 changed '$end' to 'end of file' in its error messages 86 # See: https://gitlab.freedesktop.org/mesa/mesa/-/issues/3181 87 actual = actual.replace('$end', 'end of file') 88 89 if actual == expected: 90 return (True, []) 91 return (False, difflib.unified_diff(actual.splitlines(), expected.splitlines())) 92 93 94def test_unix(args): 95 """Test files with unix style (\n) new lines.""" 96 total = 0 97 passed = 0 98 99 print('============= Testing for Correctness (Unix) =============') 100 for filename in os.listdir(args.testdir): 101 if not filename.endswith('.c'): 102 continue 103 104 print( '{}:'.format(os.path.splitext(filename)[0]), end=' ') 105 total += 1 106 107 testfile = os.path.join(args.testdir, filename) 108 with open(testfile, 'rb') as f: 109 contents = f.read() 110 valid, diff = test_output(args.glcpp, contents, testfile + '.expected') 111 if valid: 112 passed += 1 113 print('PASS') 114 else: 115 print('FAIL') 116 for l in diff: 117 print(l, file=sys.stderr) 118 119 if not total: 120 raise Exception('Could not find any tests.') 121 122 print('{}/{}'.format(passed, total), 'tests returned correct results') 123 return total == passed 124 125 126def _replace_test(args, replace): 127 """Test files with non-unix style line endings. Print your own header.""" 128 total = 0 129 passed = 0 130 131 for filename in os.listdir(args.testdir): 132 if not filename.endswith('.c'): 133 continue 134 135 print( '{}:'.format(os.path.splitext(filename)[0]), end=' ') 136 total += 1 137 testfile = os.path.join(args.testdir, filename) 138 139 with open(testfile, 'rt') as f: 140 contents = f.read() 141 contents = contents.replace('\n', replace).encode('utf-8') 142 valid, diff = test_output( 143 args.glcpp, contents, testfile + '.expected', nl_format=replace) 144 145 if valid: 146 passed += 1 147 print('PASS') 148 else: 149 print('FAIL') 150 for l in diff: 151 print(l, file=sys.stderr) 152 153 if not total: 154 raise Exception('Could not find any tests.') 155 156 print('{}/{}'.format(passed, total), 'tests returned correct results') 157 return total == passed 158 159 160def test_windows(args): 161 """Test files with windows/dos style (\r\n) new lines.""" 162 print('============= Testing for Correctness (Windows) =============') 163 return _replace_test(args, '\r\n') 164 165 166def test_oldmac(args): 167 """Test files with Old Mac style (\r) new lines.""" 168 print('============= Testing for Correctness (Old Mac) =============') 169 return _replace_test(args, '\r') 170 171 172def test_bizarro(args): 173 """Test files with Bizarro world style (\n\r) new lines.""" 174 # This is allowed by the spec, but why? 175 print('============= Testing for Correctness (Bizarro) =============') 176 return _replace_test(args, '\n\r') 177 178 179def main(): 180 args = arg_parser() 181 182 wrapper = os.environ.get('MESON_EXE_WRAPPER') 183 if wrapper is not None: 184 args.glcpp = split_args(wrapper) + [args.glcpp] 185 else: 186 args.glcpp = [args.glcpp] 187 188 success = True 189 try: 190 if args.unix: 191 success = success and test_unix(args) 192 if args.windows: 193 success = success and test_windows(args) 194 if args.oldmac: 195 success = success and test_oldmac(args) 196 if args.bizarro: 197 success = success and test_bizarro(args) 198 except OSError as e: 199 if e.errno == errno.ENOEXEC: 200 print('Skipping due to inability to run host binaries.', 201 file=sys.stderr) 202 sys.exit(77) 203 raise 204 205 exit(0 if success else 1) 206 207 208if __name__ == '__main__': 209 main() 210