Tidy ups of mapping code
[nativeclient.git] / tools / test_lib.py
blobbac0ba0916ad1352f76f205354a420429923ad21
1 #!/usr/bin/python
2 # Copyright 2008, Google Inc.
3 # All rights reserved.
4 #
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions are
7 # met:
8 #
9 # * Redistributions of source code must retain the above copyright
10 # notice, this list of conditions and the following disclaimer.
11 # * Redistributions in binary form must reproduce the above
12 # copyright notice, this list of conditions and the following disclaimer
13 # in the documentation and/or other materials provided with the
14 # distribution.
15 # * Neither the name of Google Inc. nor the names of its
16 # contributors may be used to endorse or promote products derived from
17 # this software without specific prior written permission.
19 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 """Testing Library For Nacl
34 """
36 import difflib
37 import re
38 import subprocess
39 import sys
40 import time
43 def RunTest(cmd):
44 """Run a test where we only care about the return code."""
45 assert type(cmd) == list
46 failed = 0
47 start = time.time()
48 try:
49 p = subprocess.Popen(cmd)
50 retcode = p.wait()
51 except OSError:
52 print 'exception: ' + str(sys.exc_info()[1])
53 retcode = 0
54 failed = 1
56 end = time.time()
57 return (end - start, retcode, failed)
60 def RunTestWithInputOutput(cmd, input_data):
61 """Run a test where we also care about stdin/stdout/stderr.
63 NOTE: this cannot handle arbitrarily long output (up to 1M)
64 """
65 assert type(cmd) == list
66 stdout = ''
67 stderr = ''
68 failed = 0
69 start = time.time()
70 try:
71 p = subprocess.Popen(cmd,
72 bufsize=1000*1000,
73 stdin=subprocess.PIPE,
74 stderr=subprocess.PIPE,
75 stdout=subprocess.PIPE)
76 p.stdin.write(input_data)
77 p.stdin.close()
78 stdout = p.stdout.read()
79 stderr = p.stderr.read()
80 retcode = p.wait()
81 except OSError, x:
82 if x.errno == 10:
83 print '@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@'
84 print 'ignoring exception', str(sys.exc_info()[1])
85 print 'return code NOT checked'
86 print 'this seems to be a windows issue'
87 print '@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@'
88 failed = 0
89 retcode = 0
90 else:
91 print 'exception: ' + str(sys.exc_info()[1])
92 retcode = 0
93 failed = 1
94 end = time.time()
95 return (end - start, retcode, failed, stdout, stderr)
98 def DiffStringsIgnoringWhiteSpace(a, b):
99 a = a.splitlines()
100 b = b.splitlines()
101 # NOTE: the whitespace stuff seems to be broken in python
102 cruncher = difflib.SequenceMatcher(lambda x: x in ' \\t', a, b)
104 for group in cruncher.get_grouped_opcodes():
105 eq = True
106 for tag, i1, i2, j1, j2 in group:
107 if tag != 'equal':
108 eq = False
109 break
110 if eq: continue
111 i1, i2, j1, j2 = group[0][1], group[-1][2], group[0][3], group[-1][4]
112 yield '@@ -%d,%d +%d,%d @@\n' % (i1+1, i2-i1, j1+1, j2-j1)
114 for tag, i1, i2, j1, j2 in group:
115 if tag == 'equal':
116 for line in a[i1:i2]:
117 yield ' [' + line + ']'
118 continue
119 if tag == 'replace' or tag == 'delete':
120 for line in a[i1:i2]:
121 yield '-[' + line + ']'
122 if tag == 'replace' or tag == 'insert':
123 for line in b[j1:j2]:
124 yield '+[' + line + ']'
127 def RegexpFilterLines(regexp, lines):
128 """Apply regexp to filter lines of text.
130 Any carriage return / newline sequence is turned into a newline.
132 Args:
133 regexp: A regular expression which may contain regexp groups, in
134 which case the line is replaced with the groups (text outside
135 the groups are omitted, useful for eliminating file names that
136 might change, etc).
138 lines: A string containing newline-separated lines of text
140 Return:
141 Filtered lines of text, newline separated.
144 result = []
145 nfa = re.compile(regexp)
146 for line in lines.split('\n'):
147 if line.endswith('\r'):
148 line = line[:-1]
149 mobj = nfa.search(line)
150 if mobj:
151 if not mobj.groups():
152 result.append(line)
153 else:
154 matched_strings = []
155 for s in mobj.groups():
156 if s is not None:
157 matched_strings.append(s)
158 result.append(''.join(matched_strings))
159 return '\n'.join(result)