~statik/ubuntu/maverick/protobuf/A

« back to all changes in this revision

Viewing changes to gtest/test/gtest_test_utils.py

  • Committer: Bazaar Package Importer
  • Author(s): Steve Kowalik
  • Date: 2010-02-11 11:13:19 UTC
  • mfrom: (2.2.2 squeeze)
  • Revision ID: james.westby@ubuntu.com-20100211111319-zdn8hmw0gh8s4cf8
Tags: 2.2.0a-0.1ubuntu1
* Merge from Debian testing.
* Remaining Ubuntu changes:
  - Don't use python2.4.
* Ubuntu changes dropped:
  - Disable death tests on Itanium, fixed upstream.

Show diffs side-by-side

added added

removed removed

Lines of Context:
33
33
 
34
34
__author__ = 'wan@google.com (Zhanyong Wan)'
35
35
 
 
36
import atexit
36
37
import os
 
38
import shutil
37
39
import sys
 
40
import tempfile
38
41
import unittest
 
42
_test_module = unittest
39
43
 
 
44
# Suppresses the 'Import not at the top of the file' lint complaint.
 
45
# pylint: disable-msg=C6204
40
46
try:
41
47
  import subprocess
42
48
  _SUBPROCESS_MODULE_AVAILABLE = True
43
49
except:
44
50
  import popen2
45
51
  _SUBPROCESS_MODULE_AVAILABLE = False
 
52
# pylint: enable-msg=C6204
 
53
 
46
54
 
47
55
IS_WINDOWS = os.name == 'nt'
 
56
IS_CYGWIN = os.name == 'posix' and 'CYGWIN' in os.uname()[0]
 
57
 
 
58
# Here we expose a class from a particular module, depending on the
 
59
# environment. The comment suppresses the 'Invalid variable name' lint
 
60
# complaint.
 
61
TestCase = _test_module.TestCase  # pylint: disable-msg=C6409
48
62
 
49
63
# Initially maps a flag to its default value. After
50
64
# _ParseAndStripGTestFlags() is called, maps a flag to its actual value.
56
70
def _ParseAndStripGTestFlags(argv):
57
71
  """Parses and strips Google Test flags from argv.  This is idempotent."""
58
72
 
59
 
  global _gtest_flags_are_parsed
 
73
  # Suppresses the lint complaint about a global variable since we need it
 
74
  # here to maintain module-wide state.
 
75
  global _gtest_flags_are_parsed  # pylint: disable-msg=W0603
60
76
  if _gtest_flags_are_parsed:
61
77
    return
62
78
 
103
119
  return os.path.abspath(GetFlag('gtest_build_dir'))
104
120
 
105
121
 
 
122
_temp_dir = None
 
123
 
 
124
def _RemoveTempDir():
 
125
  if _temp_dir:
 
126
    shutil.rmtree(_temp_dir, ignore_errors=True)
 
127
 
 
128
atexit.register(_RemoveTempDir)
 
129
 
 
130
 
 
131
def GetTempDir():
 
132
  """Returns a directory for temporary files."""
 
133
 
 
134
  global _temp_dir
 
135
  if not _temp_dir:
 
136
    _temp_dir = tempfile.mkdtemp()
 
137
  return _temp_dir
 
138
 
 
139
 
106
140
def GetTestExecutablePath(executable_name):
107
141
  """Returns the absolute path of the test binary given its name.
108
142
 
117
151
  """
118
152
 
119
153
  path = os.path.abspath(os.path.join(GetBuildDir(), executable_name))
120
 
  if IS_WINDOWS and not path.endswith('.exe'):
 
154
  if (IS_WINDOWS or IS_CYGWIN) and not path.endswith('.exe'):
121
155
    path += '.exe'
122
156
 
123
157
  if not os.path.exists(path):
156
190
 
157
191
 
158
192
class Subprocess:
159
 
  def __init__(self, command, working_dir=None):
 
193
  def __init__(self, command, working_dir=None, capture_stderr=True):
160
194
    """Changes into a specified directory, if provided, and executes a command.
161
195
    Restores the old directory afterwards. Execution results are returned
162
196
    via the following attributes:
169
203
                             combined in a string.
170
204
 
171
205
    Args:
172
 
      command: A command to run, in the form of sys.argv.
173
 
      working_dir: A directory to change into.
 
206
      command:        The command to run, in the form of sys.argv.
 
207
      working_dir:    The directory to change into.
 
208
      capture_stderr: Determines whether to capture stderr in the output member
 
209
                      or to discard it.
174
210
    """
175
211
 
176
212
    # The subprocess module is the preferrable way of running programs
181
217
    # functionality (Popen4) under Windows. This allows us to support Mac
182
218
    # OS X 10.4 Tiger, which has python 2.3 installed.
183
219
    if _SUBPROCESS_MODULE_AVAILABLE:
 
220
      if capture_stderr:
 
221
        stderr = subprocess.STDOUT
 
222
      else:
 
223
        stderr = subprocess.PIPE
 
224
 
184
225
      p = subprocess.Popen(command,
185
 
                           stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
 
226
                           stdout=subprocess.PIPE, stderr=stderr,
186
227
                           cwd=working_dir, universal_newlines=True)
187
228
      # communicate returns a tuple with the file obect for the child's
188
229
      # output.
193
234
      try:
194
235
        if working_dir is not None:
195
236
          os.chdir(working_dir)
196
 
        p = popen2.Popen4(command)
 
237
        if capture_stderr:
 
238
          p = popen2.Popen4(command)
 
239
        else:
 
240
          p = popen2.Popen3(command)
197
241
        p.tochild.close()
198
242
        self.output = p.fromchild.read()
199
243
        ret_code = p.wait()
223
267
  # unittest.main().  Otherwise the latter will be confused by the
224
268
  # --gtest_* flags.
225
269
  _ParseAndStripGTestFlags(sys.argv)
226
 
  unittest.main()
 
270
  _test_module.main()