"comfy/supported_models_base.py" did not exist on "394e7a416603d4f9b60e60554ac5194c58f3b359"
gtest_output_test.py 9.85 KB
Newer Older
shiqian's avatar
shiqian committed
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
#!/usr/bin/env python
#
# Copyright 2008, Google Inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
#     * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
#     * Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following disclaimer
# in the documentation and/or other materials provided with the
# distribution.
#     * Neither the name of Google Inc. nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

"""Tests the text output of Google C++ Testing Framework.

SYNOPSIS
35
36
       gtest_output_test.py --gtest_build_dir=BUILD/DIR --gengolden
         # where BUILD/DIR contains the built gtest_output_test_ file.
shiqian's avatar
shiqian committed
37
38
39
40
41
42
43
44
45
46
47
       gtest_output_test.py --gengolden
       gtest_output_test.py
"""

__author__ = 'wan@google.com (Zhanyong Wan)'

import os
import re
import string
import sys
import unittest
48
import gtest_test_utils
shiqian's avatar
shiqian committed
49
50
51
52
53
54
55
56
57
58
59
60


# The flag for generating the golden file
GENGOLDEN_FLAG = '--gengolden'

IS_WINDOWS = os.name == 'nt'

if IS_WINDOWS:
  GOLDEN_NAME = 'gtest_output_test_golden_win.txt'
else:
  GOLDEN_NAME = 'gtest_output_test_golden_lin.txt'

61
PROGRAM_PATH = gtest_test_utils.GetTestExecutablePath('gtest_output_test_')
62
63
64

# At least one command we exercise must not have the
# --gtest_internal_skip_environment_and_ad_hoc_tests flag.
65
COMMAND_LIST_TESTS = ({}, PROGRAM_PATH + ' --gtest_list_tests')
66
67
COMMAND_WITH_COLOR = ({}, PROGRAM_PATH + ' --gtest_color=yes')
COMMAND_WITH_TIME = ({}, PROGRAM_PATH + ' --gtest_print_time '
68
69
                     '--gtest_internal_skip_environment_and_ad_hoc_tests '
                     '--gtest_filter="FatalFailureTest.*:LoggingTest.*"')
70
COMMAND_WITH_DISABLED = ({}, PROGRAM_PATH + ' --gtest_also_run_disabled_tests '
71
72
                         '--gtest_internal_skip_environment_and_ad_hoc_tests '
                         '--gtest_filter="*DISABLED_*"')
73
74
75
76
COMMAND_WITH_SHARDING = ({'GTEST_SHARD_INDEX': '1', 'GTEST_TOTAL_SHARDS': '2'},
                         PROGRAM_PATH +
                         ' --gtest_internal_skip_environment_and_ad_hoc_tests '
                         ' --gtest_filter="PassingTest.*"')
shiqian's avatar
shiqian committed
77

78
GOLDEN_PATH = os.path.join(gtest_test_utils.GetSourceDir(), GOLDEN_NAME)
shiqian's avatar
shiqian committed
79

80

shiqian's avatar
shiqian committed
81
82
83
84
85
86
87
88
89
90
91
92
93
94
def ToUnixLineEnding(s):
  """Changes all Windows/Mac line endings in s to UNIX line endings."""

  return s.replace('\r\n', '\n').replace('\r', '\n')


def RemoveLocations(output):
  """Removes all file location info from a Google Test program's output.

  Args:
       output:  the output of a Google Test program.

  Returns:
       output with all file location info (in the form of
95
96
       'DIRECTORY/FILE_NAME:LINE_NUMBER: 'or
       'DIRECTORY\\FILE_NAME(LINE_NUMBER): ') replaced by
shiqian's avatar
shiqian committed
97
98
99
       'FILE_NAME:#: '.
  """

100
  return re.sub(r'.*[/\\](.+)(\:\d+|\(\d+\))\: ', r'\1:#: ', output)
shiqian's avatar
shiqian committed
101
102
103
104
105
106
107
108
109
110


def RemoveStackTraces(output):
  """Removes all stack traces from a Google Test program's output."""

  # *? means "find the shortest string that matches".
  return re.sub(r'Stack trace:(.|\n)*?\n\n',
                'Stack trace: (omitted)\n\n', output)


111
112
113
114
115
116
def RemoveTime(output):
  """Removes all time information from a Google Test program's output."""

  return re.sub(r'\(\d+ ms', '(? ms', output)


117
118
119
def RemoveTestCounts(output):
  """Removes test counts from a Google Test program's output."""

120
121
122
123
  output = re.sub(r'\d+ tests, listed below',
                  '? tests, listed below', output)
  output = re.sub(r'\d+ FAILED TESTS',
                  '? FAILED TESTS', output)
124
125
126
127
128
  output = re.sub(r'\d+ tests from \d+ test cases',
                  '? tests from ? test cases', output)
  return re.sub(r'\d+ tests\.', '? tests.', output)


129
130
def RemoveMatchingTests(test_output, pattern):
  """Removes typed test information from a Google Test program's output.
131

132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
  This function strips not only the beginning and the end of a test but also all
  output in between.

  Args:
    test_output:       A string containing the test output.
    pattern:           A string that matches names of test cases to remove.

  Returns:
    Contents of test_output with removed test case whose names match pattern.
  """

  test_output = re.sub(
      r'\[ RUN      \] .*%s(.|\n)*?\[(  FAILED  |       OK )\] .*%s.*\n' % (
          pattern, pattern),
      '',
      test_output)
  return re.sub(r'.*%s.*\n' % pattern, '', test_output)
149
150


shiqian's avatar
shiqian committed
151
152
153
154
155
156
def NormalizeOutput(output):
  """Normalizes output (the output of gtest_output_test_.exe)."""

  output = ToUnixLineEnding(output)
  output = RemoveLocations(output)
  output = RemoveStackTraces(output)
157
  output = RemoveTime(output)
shiqian's avatar
shiqian committed
158
159
160
  return output


161
def IterShellCommandOutput(env_cmd, stdin_string=None):
shiqian's avatar
shiqian committed
162
163
164
165
  """Runs a command in a sub-process, and iterates the lines in its STDOUT.

  Args:

166
167
168
169
170
171
    env_cmd:           The shell command. A 2-tuple where element 0 is a dict
                       of extra environment variables to set, and element 1
                       is a string with the command and any flags.
    stdin_string:      The string to be fed to the STDIN of the sub-process;
                       If None, the sub-process will inherit the STDIN
                       from the parent process.
shiqian's avatar
shiqian committed
172
173
174
  """

  # Spawns cmd in a sub-process, and gets its standard I/O file objects.
175
176
177
178
179
180
  # Set and save the environment properly.
  old_env_vars = dict(os.environ)
  os.environ.update(env_cmd[0])
  stdin_file, stdout_file = os.popen2(env_cmd[1], 'b')
  os.environ.clear()
  os.environ.update(old_env_vars)
shiqian's avatar
shiqian committed
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199

  # If the caller didn't specify a string for STDIN, gets it from the
  # parent process.
  if stdin_string is None:
    stdin_string = sys.stdin.read()

  # Feeds the STDIN string to the sub-process.
  stdin_file.write(stdin_string)
  stdin_file.close()

  while True:
    line = stdout_file.readline()
    if not line:  # EOF
      stdout_file.close()
      break

    yield line


200
def GetShellCommandOutput(env_cmd, stdin_string=None):
shiqian's avatar
shiqian committed
201
202
203
204
  """Runs a command in a sub-process, and returns its STDOUT in a string.

  Args:

205
206
207
208
209
210
    env_cmd:           The shell command. A 2-tuple where element 0 is a dict
                       of extra environment variables to set, and element 1
                       is a string with the command and any flags.
    stdin_string:      The string to be fed to the STDIN of the sub-process;
                       If None, the sub-process will inherit the STDIN
                       from the parent process.
shiqian's avatar
shiqian committed
211
212
  """

213
  lines = list(IterShellCommandOutput(env_cmd, stdin_string))
shiqian's avatar
shiqian committed
214
215
216
  return string.join(lines, '')


217
def GetCommandOutput(env_cmd):
shiqian's avatar
shiqian committed
218
219
220
221
  """Runs a command and returns its output with all file location
  info stripped off.

  Args:
222
223
224
    env_cmd:  The shell command. A 2-tuple where element 0 is a dict of extra
              environment variables to set, and element 1 is a string with
              the command and any flags.
shiqian's avatar
shiqian committed
225
226
227
  """

  # Disables exception pop-ups on Windows.
228
  os.environ['GTEST_CATCH_EXCEPTIONS'] = '1'
229
230
231
232
233
234
235
236
237
238
  return NormalizeOutput(GetShellCommandOutput(env_cmd, ''))


def GetOutputOfAllCommands():
  """Returns concatenated output from several representative commands."""

  return (GetCommandOutput(COMMAND_WITH_COLOR) +
          GetCommandOutput(COMMAND_WITH_TIME) +
          GetCommandOutput(COMMAND_WITH_DISABLED) +
          GetCommandOutput(COMMAND_WITH_SHARDING))
shiqian's avatar
shiqian committed
239
240


241
242
243
244
245
test_list = GetShellCommandOutput(COMMAND_LIST_TESTS, '')
SUPPORTS_DEATH_TESTS = 'DeathTest' in test_list
SUPPORTS_TYPED_TESTS = 'TypedTest' in test_list


shiqian's avatar
shiqian committed
246
class GTestOutputTest(unittest.TestCase):
247
248
249
250
251
252
253
  def RemoveUnsupportedTests(self, test_output):
    if not SUPPORTS_DEATH_TESTS:
      test_output = RemoveMatchingTests(test_output, 'DeathTest')
    if not SUPPORTS_TYPED_TESTS:
      test_output = RemoveMatchingTests(test_output, 'TypedTest')
    return test_output

shiqian's avatar
shiqian committed
254
  def testOutput(self):
255
    output = GetOutputOfAllCommands()
256

shiqian's avatar
shiqian committed
257
    golden_file = open(GOLDEN_PATH, 'rb')
258
259
260
261
262
    # A mis-configured source control system can cause \r appear in EOL
    # sequences when we read the golden file irrespective of an operating
    # system used. Therefore, we need to strip those \r's from newlines
    # unconditionally.
    golden = ToUnixLineEnding(golden_file.read())
shiqian's avatar
shiqian committed
263
264
    golden_file.close()

265
266
    # We want the test to pass regardless of death tests being
    # supported or not.
267
268
269
270
271
    if SUPPORTS_DEATH_TESTS and SUPPORTS_TYPED_TESTS:
      self.assert_(golden == output)
    else:
      self.assert_(RemoveTestCounts(self.RemoveUnsupportedTests(golden)) ==
                   RemoveTestCounts(output))
shiqian's avatar
shiqian committed
272
273
274
275


if __name__ == '__main__':
  if sys.argv[1:] == [GENGOLDEN_FLAG]:
276
277
278
279
280
281
282
283
284
285
    if SUPPORTS_DEATH_TESTS and SUPPORTS_TYPED_TESTS:
      output = GetOutputOfAllCommands()
      golden_file = open(GOLDEN_PATH, 'wb')
      golden_file.write(output)
      golden_file.close()
    else:
      print >> sys.stderr, ('Unable to write a golden file when compiled in an '
                            'environment that does not support death tests and '
                            'typed tests. Are you using VC 7.1?')
      sys.exit(1)
shiqian's avatar
shiqian committed
286
287
  else:
    gtest_test_utils.Main()