gtest_output_test.py 6.58 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
       gtest_output_test.py --gengolden
       gtest_output_test.py
"""

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

import gtest_test_utils
import os
import re
import string
import sys
import unittest


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

IS_WINDOWS = os.name == 'nt'

if IS_WINDOWS:
  PROGRAM = r'..\build.dbg8\gtest_output_test_.exe'
  GOLDEN_NAME = 'gtest_output_test_golden_win.txt'
else:
  PROGRAM = 'gtest_output_test_'
  GOLDEN_NAME = 'gtest_output_test_golden_lin.txt'

63
64
65
66
PROGRAM_PATH = os.path.join(gtest_test_utils.GetBuildDir(), PROGRAM)
COMMAND_WITH_COLOR = PROGRAM_PATH + ' --gtest_color=yes'
COMMAND_WITH_TIME = (PROGRAM_PATH + ' --gtest_print_time '
                     + '--gtest_filter="FatalFailureTest.*:LoggingTest.*"')
shiqian's avatar
shiqian committed
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84

GOLDEN_PATH = os.path.join(gtest_test_utils.GetSourceDir(),
                           GOLDEN_NAME)

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
85
86
       'DIRECTORY/FILE_NAME:LINE_NUMBER: 'or
       'DIRECTORY\\FILE_NAME(LINE_NUMBER): ') replaced by
shiqian's avatar
shiqian committed
87
88
89
       'FILE_NAME:#: '.
  """

90
  return re.sub(r'.*[/\\](.+)(\:\d+|\(\d+\))\: ', r'\1:#: ', output)
shiqian's avatar
shiqian committed
91
92
93
94
95
96
97
98
99
100


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)


101
102
103
104
105
106
def RemoveTime(output):
  """Removes all time information from a Google Test program's output."""

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


107
108
109
110
111
112
113
114
115
116
117
118
119
120
def RemoveTestCounts(output):
  """Removes test counts from a Google Test program's output."""

  output = re.sub(r'\d+ tests from \d+ test cases',
                  '? tests from ? test cases', output)
  return re.sub(r'\d+ tests\.', '? tests.', output)


def RemoveDeathTests(output):
  """Removes death test information from a Google Test program's output."""

  return re.sub(r'\n.*DeathTest.*', '', output)


shiqian's avatar
shiqian committed
121
122
123
124
125
126
def NormalizeOutput(output):
  """Normalizes output (the output of gtest_output_test_.exe)."""

  output = ToUnixLineEnding(output)
  output = RemoveLocations(output)
  output = RemoveStackTraces(output)
127
  output = RemoveTime(output)
shiqian's avatar
shiqian committed
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
  return output


def IterShellCommandOutput(cmd, stdin_string=None):
  """Runs a command in a sub-process, and iterates the lines in its STDOUT.

  Args:

    cmd:           The shell command.
    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.
  """

  # Spawns cmd in a sub-process, and gets its standard I/O file objects.
  stdin_file, stdout_file = os.popen2(cmd, 'b')

  # 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


def GetShellCommandOutput(cmd, stdin_string=None):
  """Runs a command in a sub-process, and returns its STDOUT in a string.

  Args:

    cmd:           The shell command.
    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.
  """

  lines = list(IterShellCommandOutput(cmd, stdin_string))
  return string.join(lines, '')


def GetCommandOutput(cmd):
  """Runs a command and returns its output with all file location
  info stripped off.

  Args:
    cmd:  the shell command.
  """

  # Disables exception pop-ups on Windows.
187
  os.environ['GTEST_CATCH_EXCEPTIONS'] = '1'
shiqian's avatar
shiqian committed
188
189
190
191
192
  return NormalizeOutput(GetShellCommandOutput(cmd, ''))


class GTestOutputTest(unittest.TestCase):
  def testOutput(self):
193
194
    output = (GetCommandOutput(COMMAND_WITH_COLOR) +
              GetCommandOutput(COMMAND_WITH_TIME))
shiqian's avatar
shiqian committed
195
196
197
198
    golden_file = open(GOLDEN_PATH, 'rb')
    golden = golden_file.read()
    golden_file.close()

199
200
201
202
203
    # We want the test to pass regardless of death tests being
    # supported or not.
    self.assert_(output == golden or
                 RemoveTestCounts(output) ==
                 RemoveTestCounts(RemoveDeathTests(golden)))
shiqian's avatar
shiqian committed
204
205
206
207


if __name__ == '__main__':
  if sys.argv[1:] == [GENGOLDEN_FLAG]:
208
209
    output = (GetCommandOutput(COMMAND_WITH_COLOR) +
              GetCommandOutput(COMMAND_WITH_TIME))
shiqian's avatar
shiqian committed
210
211
212
213
214
    golden_file = open(GOLDEN_PATH, 'wb')
    golden_file.write(output)
    golden_file.close()
  else:
    gtest_test_utils.Main()