2 # Copyright 2008, Google Inc.
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions are
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
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
44 """Run a test where we only care about the return code."""
45 assert type(cmd
) == list
49 p
= subprocess
.Popen(cmd
)
52 print 'exception: ' + str(sys
.exc_info()[1])
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)
65 assert type(cmd
) == list
71 p
= subprocess
.Popen(cmd
,
73 stdin
=subprocess
.PIPE
,
74 stderr
=subprocess
.PIPE
,
75 stdout
=subprocess
.PIPE
)
76 p
.stdin
.write(input_data
)
78 stdout
= p
.stdout
.read()
79 stderr
= p
.stderr
.read()
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 '@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@'
91 print 'exception: ' + str(sys
.exc_info()[1])
95 return (end
- start
, retcode
, failed
, stdout
, stderr
)
98 def DiffStringsIgnoringWhiteSpace(a
, b
):
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():
106 for tag
, i1
, i2
, j1
, j2
in group
:
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
:
116 for line
in a
[i1
:i2
]:
117 yield ' [' + line
+ ']'
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.
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
138 lines: A string containing newline-separated lines of text
141 Filtered lines of text, newline separated.
145 nfa
= re
.compile(regexp
)
146 for line
in lines
.split('\n'):
147 if line
.endswith('\r'):
149 mobj
= nfa
.search(line
)
151 if not mobj
.groups():
155 for s
in mobj
.groups():
157 matched_strings
.append(s
)
158 result
.append(''.join(matched_strings
))
159 return '\n'.join(result
)