proxygen
gtest_test_utils Namespace Reference

Classes

class  Subprocess
 

Functions

def SetEnvVar (env_var, value)
 
def _ParseAndStripGTestFlags (argv)
 
def GetFlag (flag)
 
def GetSourceDir ()
 
def GetBuildDir ()
 
def _RemoveTempDir ()
 
def GetTempDir ()
 
def GetTestExecutablePath (executable_name, build_dir=None)
 
def GetExitStatus (exit_code)
 
def Main ()
 

Variables

string __author__ = 'wan@google.com (Zhanyong Wan)'
 
 _test_module = unittest
 
bool _SUBPROCESS_MODULE_AVAILABLE = True
 
string GTEST_OUTPUT_VAR_NAME = 'GTEST_OUTPUT'
 
string IS_WINDOWS = 'nt'
 
string IS_CYGWIN = 'posix'
 
string PREMATURE_EXIT_FILE_ENV_VAR = 'TEST_PREMATURE_EXIT_FILE'
 
 environ = os.environ.copy()
 
 TestCase = _test_module.TestCase
 
dictionary _flag_map
 
bool _gtest_flags_are_parsed = False
 
 _temp_dir = None
 

Function Documentation

def gtest_test_utils._ParseAndStripGTestFlags (   argv)
private
Parses and strips Google Test flags from argv.  This is idempotent.

Definition at line 86 of file gtest_test_utils.py.

Referenced by gtest_test_utils.Subprocess.__init__(), GetFlag(), and Main().

87  """Parses and strips Google Test flags from argv. This is idempotent."""
88 
89  # Suppresses the lint complaint about a global variable since we need it
90  # here to maintain module-wide state.
91  global _gtest_flags_are_parsed # pylint: disable-msg=W0603
92  if _gtest_flags_are_parsed:
93  return
94 
95  _gtest_flags_are_parsed = True
96  for flag in _flag_map:
97  # The environment variable overrides the default value.
98  if flag.upper() in os.environ:
99  _flag_map[flag] = os.environ[flag.upper()]
100 
101  # The command line flag overrides the environment variable.
102  i = 1 # Skips the program name.
103  while i < len(argv):
104  prefix = '--' + flag + '='
105  if argv[i].startswith(prefix):
106  _flag_map[flag] = argv[i][len(prefix):]
107  del argv[i]
108  break
109  else:
110  # We don't increment i in case we just found a --gtest_* flag
111  # and removed it from argv.
112  i += 1
113 
114 
def _ParseAndStripGTestFlags(argv)
def gtest_test_utils._RemoveTempDir ( )
private

Definition at line 140 of file gtest_test_utils.py.

141  if _temp_dir:
142  shutil.rmtree(_temp_dir, ignore_errors=True)
143 
144 atexit.register(_RemoveTempDir)
145 
146 
def gtest_test_utils.GetBuildDir ( )
Returns the absolute path of the directory where the test binaries are.

Definition at line 132 of file gtest_test_utils.py.

References GetFlag().

Referenced by GetTestExecutablePath().

133  """Returns the absolute path of the directory where the test binaries are."""
134 
135  return os.path.abspath(GetFlag('build_dir'))
136 
137 
def gtest_test_utils.GetExitStatus (   exit_code)
Returns the argument to exit(), or -1 if exit() wasn't called.

Args:
  exit_code: the result value of os.system(command).

Definition at line 187 of file gtest_test_utils.py.

187 def GetExitStatus(exit_code):
188  """Returns the argument to exit(), or -1 if exit() wasn't called.
189 
190  Args:
191  exit_code: the result value of os.system(command).
192  """
193 
194  if os.name == 'nt':
195  # On Windows, os.WEXITSTATUS() doesn't work and os.system() returns
196  # the argument to exit() directly.
197  return exit_code
198  else:
199  # On Unix, os.WEXITSTATUS() must be used to extract the exit status
200  # from the result of os.system().
201  if os.WIFEXITED(exit_code):
202  return os.WEXITSTATUS(exit_code)
203  else:
204  return -1
205 
206 
def GetExitStatus(exit_code)
def gtest_test_utils.GetFlag (   flag)
Returns the value of the given flag.

Definition at line 115 of file gtest_test_utils.py.

References _ParseAndStripGTestFlags().

Referenced by GetBuildDir(), and GetSourceDir().

115 def GetFlag(flag):
116  """Returns the value of the given flag."""
117 
118  # In case GetFlag() is called before Main(), we always call
119  # _ParseAndStripGTestFlags() here to make sure the --gtest_* flags
120  # are parsed.
121  _ParseAndStripGTestFlags(sys.argv)
122 
123  return _flag_map[flag]
124 
125 
def _ParseAndStripGTestFlags(argv)
def gtest_test_utils.GetSourceDir ( )
Returns the absolute path of the directory where the .py files are.

Definition at line 126 of file gtest_test_utils.py.

References GetFlag().

Referenced by gmock_test_utils.GetSourceDir(), and gtest_output_test.GTestOutputTest.testOutput().

127  """Returns the absolute path of the directory where the .py files are."""
128 
129  return os.path.abspath(GetFlag('source_dir'))
130 
131 
def gtest_test_utils.GetTestExecutablePath (   executable_name,
  build_dir = None 
)
Returns the absolute path of the test binary given its name.

The function will print a message and abort the program if the resulting file
doesn't exist.

Args:
  executable_name: name of the test binary that the test script runs.
  build_dir:       directory where to look for executables, by default
                   the result of GetBuildDir().

Returns:
  The absolute path of the test binary.

Definition at line 156 of file gtest_test_utils.py.

References GetBuildDir().

Referenced by gtest_xml_output_unittest.GTestXMLOutputUnitTest._GetXmlOutput(), gtest_xml_outfiles_test.GTestXMLOutFilesTest._TestOutFile(), gmock_test_utils.GetTestExecutablePath(), and gtest_xml_output_unittest.GTestXMLOutputUnitTest.testDefaultOutputFile().

156 def GetTestExecutablePath(executable_name, build_dir=None):
157  """Returns the absolute path of the test binary given its name.
158 
159  The function will print a message and abort the program if the resulting file
160  doesn't exist.
161 
162  Args:
163  executable_name: name of the test binary that the test script runs.
164  build_dir: directory where to look for executables, by default
165  the result of GetBuildDir().
166 
167  Returns:
168  The absolute path of the test binary.
169  """
170 
171  path = os.path.abspath(os.path.join(build_dir or GetBuildDir(),
172  executable_name))
173  if (IS_WINDOWS or IS_CYGWIN) and not path.endswith('.exe'):
174  path += '.exe'
175 
176  if not os.path.exists(path):
177  message = (
178  'Unable to find the test binary "%s". Please make sure to provide\n'
179  'a path to the binary via the --build_dir flag or the BUILD_DIR\n'
180  'environment variable.' % path)
181  sys.stdout.write(message)
182  sys.exit(1)
183 
184  return path
185 
186 
def GetTestExecutablePath(executable_name, build_dir=None)
def gtest_test_utils.Main ( )
Runs the unit test.

Definition at line 306 of file gtest_test_utils.py.

References _ParseAndStripGTestFlags().

Referenced by gtest_test_utils.Subprocess.__init__(), gtest_xml_outfiles_test.GTestXMLOutFilesTest._TestOutFile(), gtest_xml_output_unittest.GTestXMLOutputUnitTest._TestXmlOutput(), gmock_test_utils.Main(), gtest_color_test.GTestColorTest.testAliasesOfYesAndNo(), gtest_break_on_failure_unittest.GTestBreakOnFailureUnitTest.testCatchExceptionsDoesNotInterfere(), gtest_uninitialized_test.GTestUninitializedTest.testExitCodeAndOutput(), gtest_output_test.GTestOutputTest.testOutput(), gtest_help_test.GTestHelpTest.testRunsTestsWithGtestInternalFlag(), gtest_filter_unittest.GTestFilterUnitTest.testShardingWorksWithDeathTests(), gtest_shuffle_test.GTestShuffleUnitTest.testShuffleShardedTestsPreservesPartition(), gtest_throw_on_failure_test.ThrowOnFailureTest.testThrowOnFailureFlagOverridesEnvVar(), gtest_catch_exceptions_test.CatchCxxExceptionsTest.testUnhandledCxxExceptionsAbortTheProgram(), gtest_list_tests_unittest.GTestListTestsUnitTest.testWithFilterFlags(), and gtest_env_var_test.GTestEnvVarTest.testXmlOutputFileOverride().

306 def Main():
307  """Runs the unit test."""
308 
309  # We must call _ParseAndStripGTestFlags() before calling
310  # unittest.main(). Otherwise the latter will be confused by the
311  # --gtest_* flags.
312  _ParseAndStripGTestFlags(sys.argv)
313  # The tested binaries should not be writing XML output files unless the
314  # script explicitly instructs them to.
315  # TODO(vladl@google.com): Move this into Subprocess when we implement
316  # passing environment into it as a parameter.
317  if GTEST_OUTPUT_VAR_NAME in os.environ:
318  del os.environ[GTEST_OUTPUT_VAR_NAME]
319 
320  _test_module.main()
321 
def _ParseAndStripGTestFlags(argv)
def gtest_test_utils.SetEnvVar (   env_var,
  value 
)
Sets/unsets an environment variable to a given value.

Definition at line 65 of file gtest_test_utils.py.

65 def SetEnvVar(env_var, value):
66  """Sets/unsets an environment variable to a given value."""
67 
68  if value is not None:
69  environ[env_var] = value
70  elif env_var in environ:
71  del environ[env_var]
72 
73 
74 # Here we expose a class from a particular module, depending on the
75 # environment. The comment suppresses the 'Invalid variable name' lint
76 # complaint.
def SetEnvVar(env_var, value)

Variable Documentation

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

Definition at line 34 of file gtest_test_utils.py.

dictionary gtest_test_utils._flag_map
private
Initial value:
1 = {'source_dir': os.path.dirname(sys.argv[0]),
2  'build_dir': os.path.dirname(sys.argv[0])}

Definition at line 81 of file gtest_test_utils.py.

bool gtest_test_utils._gtest_flags_are_parsed = False
private

Definition at line 83 of file gtest_test_utils.py.

bool gtest_test_utils._SUBPROCESS_MODULE_AVAILABLE = True
private

Definition at line 48 of file gtest_test_utils.py.

gtest_test_utils._temp_dir = None
private

Definition at line 138 of file gtest_test_utils.py.

gtest_test_utils._test_module = unittest
private

Definition at line 42 of file gtest_test_utils.py.

gtest_test_utils.environ = os.environ.copy()

Definition at line 62 of file gtest_test_utils.py.

string gtest_test_utils.GTEST_OUTPUT_VAR_NAME = 'GTEST_OUTPUT'

Definition at line 54 of file gtest_test_utils.py.

string gtest_test_utils.IS_CYGWIN = 'posix'

Definition at line 57 of file gtest_test_utils.py.

string gtest_test_utils.IS_WINDOWS = 'nt'

Definition at line 56 of file gtest_test_utils.py.

string gtest_test_utils.PREMATURE_EXIT_FILE_ENV_VAR = 'TEST_PREMATURE_EXIT_FILE'

Definition at line 60 of file gtest_test_utils.py.

gtest_test_utils.TestCase = _test_module.TestCase