32 """Tests the text output of Google C++ Testing Framework. 35 gtest_output_test.py --build_dir=BUILD/DIR --gengolden 36 # where BUILD/DIR contains the built gtest_output_test_ file. 37 gtest_output_test.py --gengolden 41 __author__ =
'wan@google.com (Zhanyong Wan)' 46 import gtest_test_utils
50 GENGOLDEN_FLAG =
'--gengolden' 51 CATCH_EXCEPTIONS_ENV_VAR_NAME =
'GTEST_CATCH_EXCEPTIONS' 53 IS_WINDOWS = os.name ==
'nt' 56 GOLDEN_NAME =
'gtest_output_test_golden_lin.txt' 62 COMMAND_LIST_TESTS = ({}, [PROGRAM_PATH,
'--gtest_list_tests'])
63 COMMAND_WITH_COLOR = ({}, [PROGRAM_PATH,
'--gtest_color=yes'])
64 COMMAND_WITH_TIME = ({}, [PROGRAM_PATH,
66 '--gtest_internal_skip_environment_and_ad_hoc_tests',
67 '--gtest_filter=FatalFailureTest.*:LoggingTest.*'])
68 COMMAND_WITH_DISABLED = (
70 '--gtest_also_run_disabled_tests',
71 '--gtest_internal_skip_environment_and_ad_hoc_tests',
72 '--gtest_filter=*DISABLED_*'])
73 COMMAND_WITH_SHARDING = (
74 {
'GTEST_SHARD_INDEX':
'1',
'GTEST_TOTAL_SHARDS':
'2'},
76 '--gtest_internal_skip_environment_and_ad_hoc_tests',
77 '--gtest_filter=PassingTest.*'])
83 """Changes all Windows/Mac line endings in s to UNIX line endings.""" 85 return s.replace(
'\r\n',
'\n').replace(
'\r',
'\n')
89 """Removes all file location info from a Google Test program's output. 92 test_output: the output of a Google Test program. 95 output with all file location info (in the form of 96 'DIRECTORY/FILE_NAME:LINE_NUMBER: 'or 97 'DIRECTORY\\FILE_NAME(LINE_NUMBER): ') replaced by 101 return re.sub(
r'.*[/\\](.+)(\:\d+|\(\d+\))\: ',
r'\1:#: ', test_output)
105 """Removes all stack traces from a Google Test program's output.""" 108 return re.sub(
r'Stack trace:(.|\n)*?\n\n',
109 'Stack trace: (omitted)\n\n', output)
113 """Removes all traces of stack traces from a Google Test program's output.""" 116 return re.sub(
r'Stack trace:(.|\n)*?\n\n',
'', output)
120 """Removes all time information from a Google Test program's output.""" 122 return re.sub(
r'\(\d+ ms',
'(? ms', output)
126 """Removes compiler-specific type info from Google Test program's output. 129 test_output: the output of a Google Test program. 132 output with type information normalized to canonical form. 136 return re.sub(
r'unsigned int',
'unsigned', test_output)
140 """Normalizes platform specific output details for easier comparison.""" 144 test_output = re.sub(
'\x1b\\[(0;3\d)?m',
'', test_output)
146 test_output = re.sub(
r': Failure\n',
r': error: ', test_output)
148 test_output = re.sub(
r'((\w|\.)+)\((\d+)\):',
r'\1:\3:', test_output)
154 """Removes test counts from a Google Test program's output.""" 156 output = re.sub(
r'\d+ tests?, listed below',
157 '? tests, listed below', output)
158 output = re.sub(
r'\d+ FAILED TESTS',
159 '? FAILED TESTS', output)
160 output = re.sub(
r'\d+ tests? from \d+ test cases?',
161 '? tests from ? test cases', output)
162 output = re.sub(
r'\d+ tests? from ([a-zA-Z_])',
163 r'? tests from \1', output)
164 return re.sub(
r'\d+ tests?\.',
'? tests.', output)
168 """Removes output of specified tests from a Google Test program's output. 170 This function strips not only the beginning and the end of a test but also 171 all output in between. 174 test_output: A string containing the test output. 175 pattern: A regex string that matches names of test cases or 179 Contents of test_output with tests whose names match pattern removed. 182 test_output = re.sub(
183 r'.*\[ RUN \] .*%s(.|\n)*?\[( FAILED | OK )\] .*%s.*\n' % (
187 return re.sub(
r'.*%s.*\n' % pattern,
'', test_output)
191 """Normalizes output (the output of gtest_output_test_.exe).""" 201 """Runs a command in a sub-process, and returns its output in a string. 204 env_cmd: The shell command. A 2-tuple where element 0 is a dict of extra 205 environment variables to set, and element 1 is a string with 206 the command and any flags. 209 A string with the command's combined standard and diagnostic output. 214 environ = os.environ.copy()
215 environ.update(env_cmd[0])
222 """Runs a command and returns its output with all file location 226 env_cmd: The shell command. A 2-tuple where element 0 is a dict of extra 227 environment variables to set, and element 1 is a string with 228 the command and any flags. 232 environ, cmdline = env_cmd
233 environ = dict(environ)
234 environ[CATCH_EXCEPTIONS_ENV_VAR_NAME] =
'1' 239 """Returns concatenated output from several representative commands.""" 248 SUPPORTS_DEATH_TESTS =
'DeathTest' in test_list
249 SUPPORTS_TYPED_TESTS =
'TypedTest' in test_list
250 SUPPORTS_THREADS =
'ExpectFailureWithThreadsTest' in test_list
251 SUPPORTS_STACK_TRACES =
False 253 CAN_GENERATE_GOLDEN_FILE = (SUPPORTS_DEATH_TESTS
and 254 SUPPORTS_TYPED_TESTS
and 260 if not SUPPORTS_DEATH_TESTS:
262 if not SUPPORTS_TYPED_TESTS:
266 if not SUPPORTS_THREADS:
268 'ExpectFailureWithThreadsTest')
270 'ScopedFakeTestPartResultReporterTest')
273 if not SUPPORTS_STACK_TRACES:
281 golden_file = open(GOLDEN_PATH,
'rb')
296 if CAN_GENERATE_GOLDEN_FILE:
297 self.assertEqual(normalized_golden, normalized_actual)
305 if os.getenv(
'DEBUG_GTEST_OUTPUT_TEST'):
308 '_gtest_output_test_normalized_actual.txt'),
'wb').
write(
312 '_gtest_output_test_normalized_golden.txt'),
'wb').
write(
315 self.assertEqual(normalized_golden, normalized_actual)
318 if __name__ ==
'__main__':
319 if sys.argv[1:] == [GENGOLDEN_FLAG]:
320 if CAN_GENERATE_GOLDEN_FILE:
322 golden_file = open(GOLDEN_PATH,
'wb')
323 golden_file.write(output)
327 """Unable to write a golden file when compiled in an environment 328 that does not support all the required features (death tests, typed tests, 329 and multiple threads). Please generate the golden file using a binary built 330 with those features enabled.""")
332 sys.stderr.write(message)
def RemoveStackTraceDetails(output)
def GetTestExecutablePath(executable_name, build_dir=None)
def NormalizeToCurrentPlatform(test_output)
def RemoveLocations(test_output)
def GetCommandOutput(env_cmd)
def RemoveMatchingTests(test_output, pattern)
bool write(ros_opcua_srvs::Write::Request &req, ros_opcua_srvs::Write::Response &res)
def RemoveTypeInfoDetails(test_output)
def GetOutputOfAllCommands()
def RemoveTestCounts(output)
def RemoveStackTraces(output)
def NormalizeOutput(output)
def RemoveUnsupportedTests(self, test_output)
def GetShellCommandOutput(env_cmd)