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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
|
#!/usr/bin/env python2
# 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."""
from __future__ import print_function
import argparse
import difflib
import io
import os
import subprocess
import sys
import tempfile
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()
def parse_test_file(filename, nl_format):
"""Check for any special arguments and return them as a list."""
with open(filename) as f:
for l in f.read().split(nl_format):
if 'glcpp-args:' in l:
return l.split('glcpp-args:')[1].strip().split()
return []
def test_output(glcpp, filename, expfile, nl_format='\n'):
"""Test that the output of glcpp is what we expect."""
extra_args = parse_test_file(filename, nl_format)
with open(filename, 'rb') as f:
proc = subprocess.Popen(
[glcpp] + extra_args,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
stdin=subprocess.PIPE)
actual, _ = proc.communicate(f.read())
with open(expfile, 'rb') as f:
expected = f.read()
if actual == expected:
return (True, [])
return (False, difflib.unified_diff(actual.splitlines(), expected.splitlines()))
def _valgrind(glcpp, filename):
"""Run valgrind and report any warnings."""
extra_args = parse_test_file(filename, nl_format='\n')
try:
fd, tmpfile = tempfile.mkstemp()
os.close(fd)
with open(filename, 'rb') as f:
proc = subprocess.Popen(
['valgrind', '--error-exitcode=31', '--log-file', tmpfile, glcpp] + extra_args,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
stdin=subprocess.PIPE)
proc.communicate(f.read())
if proc.returncode != 31:
return (True, [])
with open(tmpfile, 'rb') as f:
contents = f.read()
return (False, contents)
finally:
os.unlink(tmpfile)
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)
valid, diff = test_output(args.glcpp, testfile, testfile + '.expected')
if valid:
passed += 1
print('PASS')
else:
print('FAIL')
for l in diff:
print(l, file=sys.stderr)
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)
try:
fd, tmpfile = tempfile.mkstemp()
os.close(fd)
with io.open(testfile, 'rt') as f:
contents = f.read()
with io.open(tmpfile, 'wt') as f:
f.write(contents.replace('\n', replace))
valid, diff = test_output(
args.glcpp, tmpfile, testfile + '.expected', nl_format=replace)
finally:
os.unlink(tmpfile)
if valid:
passed += 1
print('PASS')
else:
print('FAIL')
for l in diff:
print(l, file=sys.stderr)
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)
print('{}/{}'.format(passed, total), 'tests returned correct results')
return total == passed
def main():
args = arg_parser()
success = True
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)
exit(0 if success else 1)
if __name__ == '__main__':
main()
|