1b89a7cc2SEnji Cooper#!/usr/bin/env python
2b89a7cc2SEnji Cooper#
3b89a7cc2SEnji Cooper# Copyright 2005 Google Inc. All Rights Reserved.
4b89a7cc2SEnji Cooper#
5b89a7cc2SEnji Cooper# Redistribution and use in source and binary forms, with or without
6b89a7cc2SEnji Cooper# modification, are permitted provided that the following conditions are
7b89a7cc2SEnji Cooper# met:
8b89a7cc2SEnji Cooper#
9b89a7cc2SEnji Cooper#     * Redistributions of source code must retain the above copyright
10b89a7cc2SEnji Cooper# notice, this list of conditions and the following disclaimer.
11b89a7cc2SEnji Cooper#     * Redistributions in binary form must reproduce the above
12b89a7cc2SEnji Cooper# copyright notice, this list of conditions and the following disclaimer
13b89a7cc2SEnji Cooper# in the documentation and/or other materials provided with the
14b89a7cc2SEnji Cooper# distribution.
15b89a7cc2SEnji Cooper#     * Neither the name of Google Inc. nor the names of its
16b89a7cc2SEnji Cooper# contributors may be used to endorse or promote products derived from
17b89a7cc2SEnji Cooper# this software without specific prior written permission.
18b89a7cc2SEnji Cooper#
19b89a7cc2SEnji Cooper# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20b89a7cc2SEnji Cooper# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21b89a7cc2SEnji Cooper# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22b89a7cc2SEnji Cooper# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23b89a7cc2SEnji Cooper# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24b89a7cc2SEnji Cooper# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25b89a7cc2SEnji Cooper# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26b89a7cc2SEnji Cooper# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27b89a7cc2SEnji Cooper# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28b89a7cc2SEnji Cooper# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29b89a7cc2SEnji Cooper# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30b89a7cc2SEnji Cooper
31b89a7cc2SEnji Cooper"""Unit test for Google Test test filters.
32b89a7cc2SEnji Cooper
33b89a7cc2SEnji CooperA user can specify which test(s) in a Google Test program to run via either
34b89a7cc2SEnji Cooperthe GTEST_FILTER environment variable or the --gtest_filter flag.
35b89a7cc2SEnji CooperThis script tests such functionality by invoking
36b89a7cc2SEnji Coopergoogletest-filter-unittest_ (a program written with Google Test) with different
37b89a7cc2SEnji Cooperenvironments and command line flags.
38b89a7cc2SEnji Cooper
39b89a7cc2SEnji CooperNote that test sharding may also influence which tests are filtered. Therefore,
40b89a7cc2SEnji Cooperwe test that here also.
41b89a7cc2SEnji Cooper"""
42b89a7cc2SEnji Cooper
43b89a7cc2SEnji Cooperimport os
44b89a7cc2SEnji Cooperimport re
4528f6c2f2SEnji Cooper
4628f6c2f2SEnji Coopertry:
4728f6c2f2SEnji Cooper  from sets import Set as set  # For Python 2.3 compatibility
4828f6c2f2SEnji Cooperexcept ImportError:
4928f6c2f2SEnji Cooper  pass
50b89a7cc2SEnji Cooperimport sys
5128f6c2f2SEnji Cooperfrom googletest.test import gtest_test_utils
52b89a7cc2SEnji Cooper
53b89a7cc2SEnji Cooper# Constants.
54b89a7cc2SEnji Cooper
55b89a7cc2SEnji Cooper# Checks if this platform can pass empty environment variables to child
56b89a7cc2SEnji Cooper# processes.  We set an env variable to an empty string and invoke a python
57b89a7cc2SEnji Cooper# script in a subprocess to print whether the variable is STILL in
58b89a7cc2SEnji Cooper# os.environ.  We then use 'eval' to parse the child's output so that an
59b89a7cc2SEnji Cooper# exception is thrown if the input is anything other than 'True' nor 'False'.
60b89a7cc2SEnji CooperCAN_PASS_EMPTY_ENV = False
61b89a7cc2SEnji Cooperif sys.executable:
62b89a7cc2SEnji Cooper  os.environ['EMPTY_VAR'] = ''
63b89a7cc2SEnji Cooper  child = gtest_test_utils.Subprocess(
6428f6c2f2SEnji Cooper      [sys.executable, '-c', "import os; print('EMPTY_VAR' in os.environ)"]
6528f6c2f2SEnji Cooper  )
66b89a7cc2SEnji Cooper  CAN_PASS_EMPTY_ENV = eval(child.output)
67b89a7cc2SEnji Cooper
68b89a7cc2SEnji Cooper
69b89a7cc2SEnji Cooper# Check if this platform can unset environment variables in child processes.
70b89a7cc2SEnji Cooper# We set an env variable to a non-empty string, unset it, and invoke
71b89a7cc2SEnji Cooper# a python script in a subprocess to print whether the variable
72b89a7cc2SEnji Cooper# is NO LONGER in os.environ.
73b89a7cc2SEnji Cooper# We use 'eval' to parse the child's output so that an exception
74b89a7cc2SEnji Cooper# is thrown if the input is neither 'True' nor 'False'.
75b89a7cc2SEnji CooperCAN_UNSET_ENV = False
76b89a7cc2SEnji Cooperif sys.executable:
77b89a7cc2SEnji Cooper  os.environ['UNSET_VAR'] = 'X'
78b89a7cc2SEnji Cooper  del os.environ['UNSET_VAR']
79b89a7cc2SEnji Cooper  child = gtest_test_utils.Subprocess(
8028f6c2f2SEnji Cooper      [sys.executable, '-c', "import os; print('UNSET_VAR' not in os.environ)"]
8128f6c2f2SEnji Cooper  )
82b89a7cc2SEnji Cooper  CAN_UNSET_ENV = eval(child.output)
83b89a7cc2SEnji Cooper
84b89a7cc2SEnji Cooper
85b89a7cc2SEnji Cooper# Checks if we should test with an empty filter. This doesn't
86b89a7cc2SEnji Cooper# make sense on platforms that cannot pass empty env variables (Win32)
87b89a7cc2SEnji Cooper# and on platforms that cannot unset variables (since we cannot tell
88b89a7cc2SEnji Cooper# the difference between "" and NULL -- Borland and Solaris < 5.10)
8928f6c2f2SEnji CooperCAN_TEST_EMPTY_FILTER = CAN_PASS_EMPTY_ENV and CAN_UNSET_ENV
90b89a7cc2SEnji Cooper
91b89a7cc2SEnji Cooper
92b89a7cc2SEnji Cooper# The environment variable for specifying the test filters.
93b89a7cc2SEnji CooperFILTER_ENV_VAR = 'GTEST_FILTER'
94b89a7cc2SEnji Cooper
95b89a7cc2SEnji Cooper# The environment variables for test sharding.
96b89a7cc2SEnji CooperTOTAL_SHARDS_ENV_VAR = 'GTEST_TOTAL_SHARDS'
97b89a7cc2SEnji CooperSHARD_INDEX_ENV_VAR = 'GTEST_SHARD_INDEX'
98b89a7cc2SEnji CooperSHARD_STATUS_FILE_ENV_VAR = 'GTEST_SHARD_STATUS_FILE'
99b89a7cc2SEnji Cooper
100b89a7cc2SEnji Cooper# The command line flag for specifying the test filters.
101b89a7cc2SEnji CooperFILTER_FLAG = 'gtest_filter'
102b89a7cc2SEnji Cooper
103b89a7cc2SEnji Cooper# The command line flag for including disabled tests.
104b89a7cc2SEnji CooperALSO_RUN_DISABLED_TESTS_FLAG = 'gtest_also_run_disabled_tests'
105b89a7cc2SEnji Cooper
106b89a7cc2SEnji Cooper# Command to run the googletest-filter-unittest_ program.
107b89a7cc2SEnji CooperCOMMAND = gtest_test_utils.GetTestExecutablePath('googletest-filter-unittest_')
108b89a7cc2SEnji Cooper
109b89a7cc2SEnji Cooper# Regex for determining whether parameterized tests are enabled in the binary.
110b89a7cc2SEnji CooperPARAM_TEST_REGEX = re.compile(r'/ParamTest')
111b89a7cc2SEnji Cooper
112b89a7cc2SEnji Cooper# Regex for parsing test case names from Google Test's output.
113b89a7cc2SEnji CooperTEST_CASE_REGEX = re.compile(r'^\[\-+\] \d+ tests? from (\w+(/\w+)?)')
114b89a7cc2SEnji Cooper
115b89a7cc2SEnji Cooper# Regex for parsing test names from Google Test's output.
116b89a7cc2SEnji CooperTEST_REGEX = re.compile(r'^\[\s*RUN\s*\].*\.(\w+(/\w+)?)')
117b89a7cc2SEnji Cooper
11828f6c2f2SEnji Cooper# Regex for parsing disabled banner from Google Test's output
11928f6c2f2SEnji CooperDISABLED_BANNER_REGEX = re.compile(r'^\[\s*DISABLED\s*\] (.*)')
12028f6c2f2SEnji Cooper
121b89a7cc2SEnji Cooper# The command line flag to tell Google Test to output the list of tests it
122b89a7cc2SEnji Cooper# will run.
123b89a7cc2SEnji CooperLIST_TESTS_FLAG = '--gtest_list_tests'
124b89a7cc2SEnji Cooper
125b89a7cc2SEnji Cooper# Indicates whether Google Test supports death tests.
12628f6c2f2SEnji CooperSUPPORTS_DEATH_TESTS = (
12728f6c2f2SEnji Cooper    'HasDeathTest'
12828f6c2f2SEnji Cooper    in gtest_test_utils.Subprocess([COMMAND, LIST_TESTS_FLAG]).output
12928f6c2f2SEnji Cooper)
130b89a7cc2SEnji Cooper
131b89a7cc2SEnji Cooper# Full names of all tests in googletest-filter-unittests_.
132b89a7cc2SEnji CooperPARAM_TESTS = [
133b89a7cc2SEnji Cooper    'SeqP/ParamTest.TestX/0',
134b89a7cc2SEnji Cooper    'SeqP/ParamTest.TestX/1',
135b89a7cc2SEnji Cooper    'SeqP/ParamTest.TestY/0',
136b89a7cc2SEnji Cooper    'SeqP/ParamTest.TestY/1',
137b89a7cc2SEnji Cooper    'SeqQ/ParamTest.TestX/0',
138b89a7cc2SEnji Cooper    'SeqQ/ParamTest.TestX/1',
139b89a7cc2SEnji Cooper    'SeqQ/ParamTest.TestY/0',
140b89a7cc2SEnji Cooper    'SeqQ/ParamTest.TestY/1',
141b89a7cc2SEnji Cooper]
142b89a7cc2SEnji Cooper
143b89a7cc2SEnji CooperDISABLED_TESTS = [
144b89a7cc2SEnji Cooper    'BarTest.DISABLED_TestFour',
145b89a7cc2SEnji Cooper    'BarTest.DISABLED_TestFive',
146b89a7cc2SEnji Cooper    'BazTest.DISABLED_TestC',
147b89a7cc2SEnji Cooper    'DISABLED_FoobarTest.Test1',
148b89a7cc2SEnji Cooper    'DISABLED_FoobarTest.DISABLED_Test2',
149b89a7cc2SEnji Cooper    'DISABLED_FoobarbazTest.TestA',
150b89a7cc2SEnji Cooper]
151b89a7cc2SEnji Cooper
152b89a7cc2SEnji Cooperif SUPPORTS_DEATH_TESTS:
153b89a7cc2SEnji Cooper  DEATH_TESTS = [
154b89a7cc2SEnji Cooper      'HasDeathTest.Test1',
155b89a7cc2SEnji Cooper      'HasDeathTest.Test2',
156b89a7cc2SEnji Cooper  ]
157b89a7cc2SEnji Cooperelse:
158b89a7cc2SEnji Cooper  DEATH_TESTS = []
159b89a7cc2SEnji Cooper
160b89a7cc2SEnji Cooper# All the non-disabled tests.
16128f6c2f2SEnji CooperACTIVE_TESTS = (
16228f6c2f2SEnji Cooper    [
163b89a7cc2SEnji Cooper        'FooTest.Abc',
164b89a7cc2SEnji Cooper        'FooTest.Xyz',
165b89a7cc2SEnji Cooper        'BarTest.TestOne',
166b89a7cc2SEnji Cooper        'BarTest.TestTwo',
167b89a7cc2SEnji Cooper        'BarTest.TestThree',
168b89a7cc2SEnji Cooper        'BazTest.TestOne',
169b89a7cc2SEnji Cooper        'BazTest.TestA',
170b89a7cc2SEnji Cooper        'BazTest.TestB',
17128f6c2f2SEnji Cooper    ]
17228f6c2f2SEnji Cooper    + DEATH_TESTS
17328f6c2f2SEnji Cooper    + PARAM_TESTS
17428f6c2f2SEnji Cooper)
175b89a7cc2SEnji Cooper
176b89a7cc2SEnji Cooperparam_tests_present = None
177b89a7cc2SEnji Cooper
178b89a7cc2SEnji Cooper# Utilities.
179b89a7cc2SEnji Cooper
180b89a7cc2SEnji Cooperenviron = os.environ.copy()
181b89a7cc2SEnji Cooper
182b89a7cc2SEnji Cooper
183b89a7cc2SEnji Cooperdef SetEnvVar(env_var, value):
184b89a7cc2SEnji Cooper  """Sets the env variable to 'value'; unsets it when 'value' is None."""
185b89a7cc2SEnji Cooper
186b89a7cc2SEnji Cooper  if value is not None:
187b89a7cc2SEnji Cooper    environ[env_var] = value
188b89a7cc2SEnji Cooper  elif env_var in environ:
189b89a7cc2SEnji Cooper    del environ[env_var]
190b89a7cc2SEnji Cooper
191b89a7cc2SEnji Cooper
192b89a7cc2SEnji Cooperdef RunAndReturnOutput(args=None):
193b89a7cc2SEnji Cooper  """Runs the test program and returns its output."""
194b89a7cc2SEnji Cooper
19528f6c2f2SEnji Cooper  return gtest_test_utils.Subprocess(
19628f6c2f2SEnji Cooper      [COMMAND] + (args or []), env=environ
19728f6c2f2SEnji Cooper  ).output
198b89a7cc2SEnji Cooper
199b89a7cc2SEnji Cooper
200b89a7cc2SEnji Cooperdef RunAndExtractTestList(args=None):
201b89a7cc2SEnji Cooper  """Runs the test program and returns its exit code and a list of tests run."""
202b89a7cc2SEnji Cooper
203b89a7cc2SEnji Cooper  p = gtest_test_utils.Subprocess([COMMAND] + (args or []), env=environ)
204b89a7cc2SEnji Cooper  tests_run = []
205b89a7cc2SEnji Cooper  test_case = ''
206b89a7cc2SEnji Cooper  test = ''
207b89a7cc2SEnji Cooper  for line in p.output.split('\n'):
208b89a7cc2SEnji Cooper    match = TEST_CASE_REGEX.match(line)
209b89a7cc2SEnji Cooper    if match is not None:
210b89a7cc2SEnji Cooper      test_case = match.group(1)
211b89a7cc2SEnji Cooper    else:
212b89a7cc2SEnji Cooper      match = TEST_REGEX.match(line)
213b89a7cc2SEnji Cooper      if match is not None:
214b89a7cc2SEnji Cooper        test = match.group(1)
215b89a7cc2SEnji Cooper        tests_run.append(test_case + '.' + test)
216b89a7cc2SEnji Cooper  return (tests_run, p.exit_code)
217b89a7cc2SEnji Cooper
218b89a7cc2SEnji Cooper
21928f6c2f2SEnji Cooperdef RunAndExtractDisabledBannerList(args=None):
22028f6c2f2SEnji Cooper  """Runs the test program and returns tests that printed a disabled banner."""
22128f6c2f2SEnji Cooper  p = gtest_test_utils.Subprocess([COMMAND] + (args or []), env=environ)
22228f6c2f2SEnji Cooper  banners_printed = []
22328f6c2f2SEnji Cooper  for line in p.output.split('\n'):
22428f6c2f2SEnji Cooper    match = DISABLED_BANNER_REGEX.match(line)
22528f6c2f2SEnji Cooper    if match is not None:
22628f6c2f2SEnji Cooper      banners_printed.append(match.group(1))
22728f6c2f2SEnji Cooper  return banners_printed
22828f6c2f2SEnji Cooper
22928f6c2f2SEnji Cooper
230b89a7cc2SEnji Cooperdef InvokeWithModifiedEnv(extra_env, function, *args, **kwargs):
231b89a7cc2SEnji Cooper  """Runs the given function and arguments in a modified environment."""
232b89a7cc2SEnji Cooper  try:
233b89a7cc2SEnji Cooper    original_env = environ.copy()
234b89a7cc2SEnji Cooper    environ.update(extra_env)
235b89a7cc2SEnji Cooper    return function(*args, **kwargs)
236b89a7cc2SEnji Cooper  finally:
237b89a7cc2SEnji Cooper    environ.clear()
238b89a7cc2SEnji Cooper    environ.update(original_env)
239b89a7cc2SEnji Cooper
240b89a7cc2SEnji Cooper
241b89a7cc2SEnji Cooperdef RunWithSharding(total_shards, shard_index, command):
242b89a7cc2SEnji Cooper  """Runs a test program shard and returns exit code and a list of tests run."""
243b89a7cc2SEnji Cooper
24428f6c2f2SEnji Cooper  extra_env = {
24528f6c2f2SEnji Cooper      SHARD_INDEX_ENV_VAR: str(shard_index),
24628f6c2f2SEnji Cooper      TOTAL_SHARDS_ENV_VAR: str(total_shards),
24728f6c2f2SEnji Cooper  }
248b89a7cc2SEnji Cooper  return InvokeWithModifiedEnv(extra_env, RunAndExtractTestList, command)
249b89a7cc2SEnji Cooper
25028f6c2f2SEnji Cooper
251b89a7cc2SEnji Cooper# The unit test.
252b89a7cc2SEnji Cooper
253b89a7cc2SEnji Cooper
254b89a7cc2SEnji Cooperclass GTestFilterUnitTest(gtest_test_utils.TestCase):
255b89a7cc2SEnji Cooper  """Tests the env variable or the command line flag to filter tests."""
256b89a7cc2SEnji Cooper
257b89a7cc2SEnji Cooper  # Utilities.
258b89a7cc2SEnji Cooper
259b89a7cc2SEnji Cooper  def AssertSetEqual(self, lhs, rhs):
260b89a7cc2SEnji Cooper    """Asserts that two sets are equal."""
261b89a7cc2SEnji Cooper
262b89a7cc2SEnji Cooper    for elem in lhs:
26328f6c2f2SEnji Cooper      self.assertTrue(elem in rhs, '%s in %s' % (elem, rhs))
264b89a7cc2SEnji Cooper
265b89a7cc2SEnji Cooper    for elem in rhs:
26628f6c2f2SEnji Cooper      self.assertTrue(elem in lhs, '%s in %s' % (elem, lhs))
267b89a7cc2SEnji Cooper
268b89a7cc2SEnji Cooper  def AssertPartitionIsValid(self, set_var, list_of_sets):
269b89a7cc2SEnji Cooper    """Asserts that list_of_sets is a valid partition of set_var."""
270b89a7cc2SEnji Cooper
271b89a7cc2SEnji Cooper    full_partition = []
272b89a7cc2SEnji Cooper    for slice_var in list_of_sets:
273b89a7cc2SEnji Cooper      full_partition.extend(slice_var)
274b89a7cc2SEnji Cooper    self.assertEqual(len(set_var), len(full_partition))
27528f6c2f2SEnji Cooper    self.assertEqual(set(set_var), set(full_partition))
276b89a7cc2SEnji Cooper
277b89a7cc2SEnji Cooper  def AdjustForParameterizedTests(self, tests_to_run):
278b89a7cc2SEnji Cooper    """Adjust tests_to_run in case value parameterized tests are disabled."""
279b89a7cc2SEnji Cooper
280b89a7cc2SEnji Cooper    global param_tests_present
281b89a7cc2SEnji Cooper    if not param_tests_present:
28228f6c2f2SEnji Cooper      return list(set(tests_to_run) - set(PARAM_TESTS))
283b89a7cc2SEnji Cooper    else:
284b89a7cc2SEnji Cooper      return tests_to_run
285b89a7cc2SEnji Cooper
286b89a7cc2SEnji Cooper  def RunAndVerify(self, gtest_filter, tests_to_run):
287b89a7cc2SEnji Cooper    """Checks that the binary runs correct set of tests for a given filter."""
288b89a7cc2SEnji Cooper
289b89a7cc2SEnji Cooper    tests_to_run = self.AdjustForParameterizedTests(tests_to_run)
290b89a7cc2SEnji Cooper
291b89a7cc2SEnji Cooper    # First, tests using the environment variable.
292b89a7cc2SEnji Cooper
293b89a7cc2SEnji Cooper    # Windows removes empty variables from the environment when passing it
294b89a7cc2SEnji Cooper    # to a new process.  This means it is impossible to pass an empty filter
295b89a7cc2SEnji Cooper    # into a process using the environment variable.  However, we can still
296b89a7cc2SEnji Cooper    # test the case when the variable is not supplied (i.e., gtest_filter is
297b89a7cc2SEnji Cooper    # None).
29828f6c2f2SEnji Cooper    # pylint: disable=g-explicit-bool-comparison
299b89a7cc2SEnji Cooper    if CAN_TEST_EMPTY_FILTER or gtest_filter != '':
300b89a7cc2SEnji Cooper      SetEnvVar(FILTER_ENV_VAR, gtest_filter)
301b89a7cc2SEnji Cooper      tests_run = RunAndExtractTestList()[0]
302b89a7cc2SEnji Cooper      SetEnvVar(FILTER_ENV_VAR, None)
303b89a7cc2SEnji Cooper      self.AssertSetEqual(tests_run, tests_to_run)
30428f6c2f2SEnji Cooper    # pylint: enable=g-explicit-bool-comparison
305b89a7cc2SEnji Cooper
306b89a7cc2SEnji Cooper    # Next, tests using the command line flag.
307b89a7cc2SEnji Cooper
308b89a7cc2SEnji Cooper    if gtest_filter is None:
309b89a7cc2SEnji Cooper      args = []
310b89a7cc2SEnji Cooper    else:
311b89a7cc2SEnji Cooper      args = ['--%s=%s' % (FILTER_FLAG, gtest_filter)]
312b89a7cc2SEnji Cooper
313b89a7cc2SEnji Cooper    tests_run = RunAndExtractTestList(args)[0]
314b89a7cc2SEnji Cooper    self.AssertSetEqual(tests_run, tests_to_run)
315b89a7cc2SEnji Cooper
31628f6c2f2SEnji Cooper  def RunAndVerifyWithSharding(
31728f6c2f2SEnji Cooper      self,
31828f6c2f2SEnji Cooper      gtest_filter,
31928f6c2f2SEnji Cooper      total_shards,
32028f6c2f2SEnji Cooper      tests_to_run,
32128f6c2f2SEnji Cooper      args=None,
32228f6c2f2SEnji Cooper      check_exit_0=False,
32328f6c2f2SEnji Cooper  ):
324b89a7cc2SEnji Cooper    """Checks that binary runs correct tests for the given filter and shard.
325b89a7cc2SEnji Cooper
326b89a7cc2SEnji Cooper    Runs all shards of googletest-filter-unittest_ with the given filter, and
327b89a7cc2SEnji Cooper    verifies that the right set of tests were run. The union of tests run
328b89a7cc2SEnji Cooper    on each shard should be identical to tests_to_run, without duplicates.
329b89a7cc2SEnji Cooper    If check_exit_0, .
330b89a7cc2SEnji Cooper
331b89a7cc2SEnji Cooper    Args:
332b89a7cc2SEnji Cooper      gtest_filter: A filter to apply to the tests.
333b89a7cc2SEnji Cooper      total_shards: A total number of shards to split test run into.
334b89a7cc2SEnji Cooper      tests_to_run: A set of tests expected to run.
335b89a7cc2SEnji Cooper      args: Arguments to pass to the to the test binary.
33628f6c2f2SEnji Cooper      check_exit_0: When set to a true value, make sure that all shards return
33728f6c2f2SEnji Cooper        0.
338b89a7cc2SEnji Cooper    """
339b89a7cc2SEnji Cooper
340b89a7cc2SEnji Cooper    tests_to_run = self.AdjustForParameterizedTests(tests_to_run)
341b89a7cc2SEnji Cooper
342b89a7cc2SEnji Cooper    # Windows removes empty variables from the environment when passing it
343b89a7cc2SEnji Cooper    # to a new process.  This means it is impossible to pass an empty filter
344b89a7cc2SEnji Cooper    # into a process using the environment variable.  However, we can still
345b89a7cc2SEnji Cooper    # test the case when the variable is not supplied (i.e., gtest_filter is
346b89a7cc2SEnji Cooper    # None).
34728f6c2f2SEnji Cooper    # pylint: disable=g-explicit-bool-comparison
348b89a7cc2SEnji Cooper    if CAN_TEST_EMPTY_FILTER or gtest_filter != '':
349b89a7cc2SEnji Cooper      SetEnvVar(FILTER_ENV_VAR, gtest_filter)
350b89a7cc2SEnji Cooper      partition = []
351b89a7cc2SEnji Cooper      for i in range(0, total_shards):
352b89a7cc2SEnji Cooper        (tests_run, exit_code) = RunWithSharding(total_shards, i, args)
353b89a7cc2SEnji Cooper        if check_exit_0:
354b89a7cc2SEnji Cooper          self.assertEqual(0, exit_code)
355b89a7cc2SEnji Cooper        partition.append(tests_run)
356b89a7cc2SEnji Cooper
357b89a7cc2SEnji Cooper      self.AssertPartitionIsValid(tests_to_run, partition)
358b89a7cc2SEnji Cooper      SetEnvVar(FILTER_ENV_VAR, None)
35928f6c2f2SEnji Cooper    # pylint: enable=g-explicit-bool-comparison
360b89a7cc2SEnji Cooper
361b89a7cc2SEnji Cooper  def RunAndVerifyAllowingDisabled(self, gtest_filter, tests_to_run):
362b89a7cc2SEnji Cooper    """Checks that the binary runs correct set of tests for the given filter.
363b89a7cc2SEnji Cooper
364b89a7cc2SEnji Cooper    Runs googletest-filter-unittest_ with the given filter, and enables
365b89a7cc2SEnji Cooper    disabled tests. Verifies that the right set of tests were run.
366b89a7cc2SEnji Cooper
367b89a7cc2SEnji Cooper    Args:
368b89a7cc2SEnji Cooper      gtest_filter: A filter to apply to the tests.
369b89a7cc2SEnji Cooper      tests_to_run: A set of tests expected to run.
370b89a7cc2SEnji Cooper    """
371b89a7cc2SEnji Cooper
372b89a7cc2SEnji Cooper    tests_to_run = self.AdjustForParameterizedTests(tests_to_run)
373b89a7cc2SEnji Cooper
374b89a7cc2SEnji Cooper    # Construct the command line.
375b89a7cc2SEnji Cooper    args = ['--%s' % ALSO_RUN_DISABLED_TESTS_FLAG]
376b89a7cc2SEnji Cooper    if gtest_filter is not None:
377b89a7cc2SEnji Cooper      args.append('--%s=%s' % (FILTER_FLAG, gtest_filter))
378b89a7cc2SEnji Cooper
379b89a7cc2SEnji Cooper    tests_run = RunAndExtractTestList(args)[0]
380b89a7cc2SEnji Cooper    self.AssertSetEqual(tests_run, tests_to_run)
381b89a7cc2SEnji Cooper
382b89a7cc2SEnji Cooper  def setUp(self):
383b89a7cc2SEnji Cooper    """Sets up test case.
384b89a7cc2SEnji Cooper
385b89a7cc2SEnji Cooper    Determines whether value-parameterized tests are enabled in the binary and
386b89a7cc2SEnji Cooper    sets the flags accordingly.
387b89a7cc2SEnji Cooper    """
388b89a7cc2SEnji Cooper
389b89a7cc2SEnji Cooper    global param_tests_present
390b89a7cc2SEnji Cooper    if param_tests_present is None:
39128f6c2f2SEnji Cooper      param_tests_present = (
39228f6c2f2SEnji Cooper          PARAM_TEST_REGEX.search(RunAndReturnOutput()) is not None
39328f6c2f2SEnji Cooper      )
394b89a7cc2SEnji Cooper
395b89a7cc2SEnji Cooper  def testDefaultBehavior(self):
396b89a7cc2SEnji Cooper    """Tests the behavior of not specifying the filter."""
397b89a7cc2SEnji Cooper
398b89a7cc2SEnji Cooper    self.RunAndVerify(None, ACTIVE_TESTS)
399b89a7cc2SEnji Cooper
400b89a7cc2SEnji Cooper  def testDefaultBehaviorWithShards(self):
401b89a7cc2SEnji Cooper    """Tests the behavior without the filter, with sharding enabled."""
402b89a7cc2SEnji Cooper
403b89a7cc2SEnji Cooper    self.RunAndVerifyWithSharding(None, 1, ACTIVE_TESTS)
404b89a7cc2SEnji Cooper    self.RunAndVerifyWithSharding(None, 2, ACTIVE_TESTS)
405b89a7cc2SEnji Cooper    self.RunAndVerifyWithSharding(None, len(ACTIVE_TESTS) - 1, ACTIVE_TESTS)
406b89a7cc2SEnji Cooper    self.RunAndVerifyWithSharding(None, len(ACTIVE_TESTS), ACTIVE_TESTS)
407b89a7cc2SEnji Cooper    self.RunAndVerifyWithSharding(None, len(ACTIVE_TESTS) + 1, ACTIVE_TESTS)
408b89a7cc2SEnji Cooper
409b89a7cc2SEnji Cooper  def testEmptyFilter(self):
410b89a7cc2SEnji Cooper    """Tests an empty filter."""
411b89a7cc2SEnji Cooper
412b89a7cc2SEnji Cooper    self.RunAndVerify('', [])
413b89a7cc2SEnji Cooper    self.RunAndVerifyWithSharding('', 1, [])
414b89a7cc2SEnji Cooper    self.RunAndVerifyWithSharding('', 2, [])
415b89a7cc2SEnji Cooper
416b89a7cc2SEnji Cooper  def testBadFilter(self):
417b89a7cc2SEnji Cooper    """Tests a filter that matches nothing."""
418b89a7cc2SEnji Cooper
419b89a7cc2SEnji Cooper    self.RunAndVerify('BadFilter', [])
420b89a7cc2SEnji Cooper    self.RunAndVerifyAllowingDisabled('BadFilter', [])
421b89a7cc2SEnji Cooper
422b89a7cc2SEnji Cooper  def testFullName(self):
423b89a7cc2SEnji Cooper    """Tests filtering by full name."""
424b89a7cc2SEnji Cooper
425b89a7cc2SEnji Cooper    self.RunAndVerify('FooTest.Xyz', ['FooTest.Xyz'])
426b89a7cc2SEnji Cooper    self.RunAndVerifyAllowingDisabled('FooTest.Xyz', ['FooTest.Xyz'])
427b89a7cc2SEnji Cooper    self.RunAndVerifyWithSharding('FooTest.Xyz', 5, ['FooTest.Xyz'])
428b89a7cc2SEnji Cooper
429b89a7cc2SEnji Cooper  def testUniversalFilters(self):
430b89a7cc2SEnji Cooper    """Tests filters that match everything."""
431b89a7cc2SEnji Cooper
432b89a7cc2SEnji Cooper    self.RunAndVerify('*', ACTIVE_TESTS)
433b89a7cc2SEnji Cooper    self.RunAndVerify('*.*', ACTIVE_TESTS)
434b89a7cc2SEnji Cooper    self.RunAndVerifyWithSharding('*.*', len(ACTIVE_TESTS) - 3, ACTIVE_TESTS)
435b89a7cc2SEnji Cooper    self.RunAndVerifyAllowingDisabled('*', ACTIVE_TESTS + DISABLED_TESTS)
436b89a7cc2SEnji Cooper    self.RunAndVerifyAllowingDisabled('*.*', ACTIVE_TESTS + DISABLED_TESTS)
437b89a7cc2SEnji Cooper
438b89a7cc2SEnji Cooper  def testFilterByTestCase(self):
439b89a7cc2SEnji Cooper    """Tests filtering by test case name."""
440b89a7cc2SEnji Cooper
441b89a7cc2SEnji Cooper    self.RunAndVerify('FooTest.*', ['FooTest.Abc', 'FooTest.Xyz'])
442b89a7cc2SEnji Cooper
443b89a7cc2SEnji Cooper    BAZ_TESTS = ['BazTest.TestOne', 'BazTest.TestA', 'BazTest.TestB']
444b89a7cc2SEnji Cooper    self.RunAndVerify('BazTest.*', BAZ_TESTS)
44528f6c2f2SEnji Cooper    self.RunAndVerifyAllowingDisabled(
44628f6c2f2SEnji Cooper        'BazTest.*', BAZ_TESTS + ['BazTest.DISABLED_TestC']
44728f6c2f2SEnji Cooper    )
448b89a7cc2SEnji Cooper
449b89a7cc2SEnji Cooper  def testFilterByTest(self):
450b89a7cc2SEnji Cooper    """Tests filtering by test name."""
451b89a7cc2SEnji Cooper
452b89a7cc2SEnji Cooper    self.RunAndVerify('*.TestOne', ['BarTest.TestOne', 'BazTest.TestOne'])
453b89a7cc2SEnji Cooper
454b89a7cc2SEnji Cooper  def testFilterDisabledTests(self):
455b89a7cc2SEnji Cooper    """Select only the disabled tests to run."""
456b89a7cc2SEnji Cooper
457b89a7cc2SEnji Cooper    self.RunAndVerify('DISABLED_FoobarTest.Test1', [])
45828f6c2f2SEnji Cooper    self.RunAndVerifyAllowingDisabled(
45928f6c2f2SEnji Cooper        'DISABLED_FoobarTest.Test1', ['DISABLED_FoobarTest.Test1']
46028f6c2f2SEnji Cooper    )
461b89a7cc2SEnji Cooper
462b89a7cc2SEnji Cooper    self.RunAndVerify('*DISABLED_*', [])
463b89a7cc2SEnji Cooper    self.RunAndVerifyAllowingDisabled('*DISABLED_*', DISABLED_TESTS)
464b89a7cc2SEnji Cooper
465b89a7cc2SEnji Cooper    self.RunAndVerify('*.DISABLED_*', [])
46628f6c2f2SEnji Cooper    self.RunAndVerifyAllowingDisabled(
46728f6c2f2SEnji Cooper        '*.DISABLED_*',
46828f6c2f2SEnji Cooper        [
469b89a7cc2SEnji Cooper            'BarTest.DISABLED_TestFour',
470b89a7cc2SEnji Cooper            'BarTest.DISABLED_TestFive',
471b89a7cc2SEnji Cooper            'BazTest.DISABLED_TestC',
472b89a7cc2SEnji Cooper            'DISABLED_FoobarTest.DISABLED_Test2',
47328f6c2f2SEnji Cooper        ],
47428f6c2f2SEnji Cooper    )
475b89a7cc2SEnji Cooper
476b89a7cc2SEnji Cooper    self.RunAndVerify('DISABLED_*', [])
47728f6c2f2SEnji Cooper    self.RunAndVerifyAllowingDisabled(
47828f6c2f2SEnji Cooper        'DISABLED_*',
47928f6c2f2SEnji Cooper        [
480b89a7cc2SEnji Cooper            'DISABLED_FoobarTest.Test1',
481b89a7cc2SEnji Cooper            'DISABLED_FoobarTest.DISABLED_Test2',
482b89a7cc2SEnji Cooper            'DISABLED_FoobarbazTest.TestA',
48328f6c2f2SEnji Cooper        ],
48428f6c2f2SEnji Cooper    )
485b89a7cc2SEnji Cooper
486b89a7cc2SEnji Cooper  def testWildcardInTestCaseName(self):
487b89a7cc2SEnji Cooper    """Tests using wildcard in the test case name."""
488b89a7cc2SEnji Cooper
48928f6c2f2SEnji Cooper    self.RunAndVerify(
49028f6c2f2SEnji Cooper        '*a*.*',
49128f6c2f2SEnji Cooper        [
492b89a7cc2SEnji Cooper            'BarTest.TestOne',
493b89a7cc2SEnji Cooper            'BarTest.TestTwo',
494b89a7cc2SEnji Cooper            'BarTest.TestThree',
495b89a7cc2SEnji Cooper            'BazTest.TestOne',
496b89a7cc2SEnji Cooper            'BazTest.TestA',
49728f6c2f2SEnji Cooper            'BazTest.TestB',
49828f6c2f2SEnji Cooper        ]
49928f6c2f2SEnji Cooper        + DEATH_TESTS
50028f6c2f2SEnji Cooper        + PARAM_TESTS,
50128f6c2f2SEnji Cooper    )
502b89a7cc2SEnji Cooper
503b89a7cc2SEnji Cooper  def testWildcardInTestName(self):
504b89a7cc2SEnji Cooper    """Tests using wildcard in the test name."""
505b89a7cc2SEnji Cooper
506b89a7cc2SEnji Cooper    self.RunAndVerify('*.*A*', ['FooTest.Abc', 'BazTest.TestA'])
507b89a7cc2SEnji Cooper
508b89a7cc2SEnji Cooper  def testFilterWithoutDot(self):
509b89a7cc2SEnji Cooper    """Tests a filter that has no '.' in it."""
510b89a7cc2SEnji Cooper
51128f6c2f2SEnji Cooper    self.RunAndVerify(
51228f6c2f2SEnji Cooper        '*z*',
51328f6c2f2SEnji Cooper        [
514b89a7cc2SEnji Cooper            'FooTest.Xyz',
515b89a7cc2SEnji Cooper            'BazTest.TestOne',
516b89a7cc2SEnji Cooper            'BazTest.TestA',
517b89a7cc2SEnji Cooper            'BazTest.TestB',
51828f6c2f2SEnji Cooper        ],
51928f6c2f2SEnji Cooper    )
520b89a7cc2SEnji Cooper
521b89a7cc2SEnji Cooper  def testTwoPatterns(self):
522b89a7cc2SEnji Cooper    """Tests filters that consist of two patterns."""
523b89a7cc2SEnji Cooper
52428f6c2f2SEnji Cooper    self.RunAndVerify(
52528f6c2f2SEnji Cooper        'Foo*.*:*A*',
52628f6c2f2SEnji Cooper        [
527b89a7cc2SEnji Cooper            'FooTest.Abc',
528b89a7cc2SEnji Cooper            'FooTest.Xyz',
529b89a7cc2SEnji Cooper            'BazTest.TestA',
53028f6c2f2SEnji Cooper        ],
53128f6c2f2SEnji Cooper    )
532b89a7cc2SEnji Cooper
533b89a7cc2SEnji Cooper    # An empty pattern + a non-empty one
534b89a7cc2SEnji Cooper    self.RunAndVerify(':*A*', ['FooTest.Abc', 'BazTest.TestA'])
535b89a7cc2SEnji Cooper
536b89a7cc2SEnji Cooper  def testThreePatterns(self):
537b89a7cc2SEnji Cooper    """Tests filters that consist of three patterns."""
538b89a7cc2SEnji Cooper
53928f6c2f2SEnji Cooper    self.RunAndVerify(
54028f6c2f2SEnji Cooper        '*oo*:*A*:*One',
54128f6c2f2SEnji Cooper        [
542b89a7cc2SEnji Cooper            'FooTest.Abc',
543b89a7cc2SEnji Cooper            'FooTest.Xyz',
544b89a7cc2SEnji Cooper            'BarTest.TestOne',
545b89a7cc2SEnji Cooper            'BazTest.TestOne',
546b89a7cc2SEnji Cooper            'BazTest.TestA',
54728f6c2f2SEnji Cooper        ],
54828f6c2f2SEnji Cooper    )
549b89a7cc2SEnji Cooper
550b89a7cc2SEnji Cooper    # The 2nd pattern is empty.
55128f6c2f2SEnji Cooper    self.RunAndVerify(
55228f6c2f2SEnji Cooper        '*oo*::*One',
55328f6c2f2SEnji Cooper        [
554b89a7cc2SEnji Cooper            'FooTest.Abc',
555b89a7cc2SEnji Cooper            'FooTest.Xyz',
556b89a7cc2SEnji Cooper            'BarTest.TestOne',
557b89a7cc2SEnji Cooper            'BazTest.TestOne',
55828f6c2f2SEnji Cooper        ],
55928f6c2f2SEnji Cooper    )
560b89a7cc2SEnji Cooper
561b89a7cc2SEnji Cooper    # The last 2 patterns are empty.
56228f6c2f2SEnji Cooper    self.RunAndVerify(
56328f6c2f2SEnji Cooper        '*oo*::',
56428f6c2f2SEnji Cooper        [
565b89a7cc2SEnji Cooper            'FooTest.Abc',
566b89a7cc2SEnji Cooper            'FooTest.Xyz',
56728f6c2f2SEnji Cooper        ],
56828f6c2f2SEnji Cooper    )
569b89a7cc2SEnji Cooper
570b89a7cc2SEnji Cooper  def testNegativeFilters(self):
57128f6c2f2SEnji Cooper    self.RunAndVerify(
57228f6c2f2SEnji Cooper        '*-BazTest.TestOne',
57328f6c2f2SEnji Cooper        [
574b89a7cc2SEnji Cooper            'FooTest.Abc',
575b89a7cc2SEnji Cooper            'FooTest.Xyz',
576b89a7cc2SEnji Cooper            'BarTest.TestOne',
577b89a7cc2SEnji Cooper            'BarTest.TestTwo',
578b89a7cc2SEnji Cooper            'BarTest.TestThree',
579b89a7cc2SEnji Cooper            'BazTest.TestA',
580b89a7cc2SEnji Cooper            'BazTest.TestB',
58128f6c2f2SEnji Cooper        ]
58228f6c2f2SEnji Cooper        + DEATH_TESTS
58328f6c2f2SEnji Cooper        + PARAM_TESTS,
58428f6c2f2SEnji Cooper    )
585b89a7cc2SEnji Cooper
58628f6c2f2SEnji Cooper    self.RunAndVerify(
58728f6c2f2SEnji Cooper        '*-FooTest.Abc:BazTest.*',
58828f6c2f2SEnji Cooper        [
589b89a7cc2SEnji Cooper            'FooTest.Xyz',
590b89a7cc2SEnji Cooper            'BarTest.TestOne',
591b89a7cc2SEnji Cooper            'BarTest.TestTwo',
592b89a7cc2SEnji Cooper            'BarTest.TestThree',
59328f6c2f2SEnji Cooper        ]
59428f6c2f2SEnji Cooper        + DEATH_TESTS
59528f6c2f2SEnji Cooper        + PARAM_TESTS,
59628f6c2f2SEnji Cooper    )
597b89a7cc2SEnji Cooper
59828f6c2f2SEnji Cooper    self.RunAndVerify(
59928f6c2f2SEnji Cooper        'BarTest.*-BarTest.TestOne',
60028f6c2f2SEnji Cooper        [
601b89a7cc2SEnji Cooper            'BarTest.TestTwo',
602b89a7cc2SEnji Cooper            'BarTest.TestThree',
60328f6c2f2SEnji Cooper        ],
60428f6c2f2SEnji Cooper    )
605b89a7cc2SEnji Cooper
606b89a7cc2SEnji Cooper    # Tests without leading '*'.
60728f6c2f2SEnji Cooper    self.RunAndVerify(
60828f6c2f2SEnji Cooper        '-FooTest.Abc:FooTest.Xyz:BazTest.*',
60928f6c2f2SEnji Cooper        [
610b89a7cc2SEnji Cooper            'BarTest.TestOne',
611b89a7cc2SEnji Cooper            'BarTest.TestTwo',
612b89a7cc2SEnji Cooper            'BarTest.TestThree',
61328f6c2f2SEnji Cooper        ]
61428f6c2f2SEnji Cooper        + DEATH_TESTS
61528f6c2f2SEnji Cooper        + PARAM_TESTS,
61628f6c2f2SEnji Cooper    )
617b89a7cc2SEnji Cooper
618b89a7cc2SEnji Cooper    # Value parameterized tests.
619b89a7cc2SEnji Cooper    self.RunAndVerify('*/*', PARAM_TESTS)
620b89a7cc2SEnji Cooper
621b89a7cc2SEnji Cooper    # Value parameterized tests filtering by the sequence name.
62228f6c2f2SEnji Cooper    self.RunAndVerify(
62328f6c2f2SEnji Cooper        'SeqP/*',
62428f6c2f2SEnji Cooper        [
625b89a7cc2SEnji Cooper            'SeqP/ParamTest.TestX/0',
626b89a7cc2SEnji Cooper            'SeqP/ParamTest.TestX/1',
627b89a7cc2SEnji Cooper            'SeqP/ParamTest.TestY/0',
628b89a7cc2SEnji Cooper            'SeqP/ParamTest.TestY/1',
62928f6c2f2SEnji Cooper        ],
63028f6c2f2SEnji Cooper    )
631b89a7cc2SEnji Cooper
632b89a7cc2SEnji Cooper    # Value parameterized tests filtering by the test name.
63328f6c2f2SEnji Cooper    self.RunAndVerify(
63428f6c2f2SEnji Cooper        '*/0',
63528f6c2f2SEnji Cooper        [
636b89a7cc2SEnji Cooper            'SeqP/ParamTest.TestX/0',
637b89a7cc2SEnji Cooper            'SeqP/ParamTest.TestY/0',
638b89a7cc2SEnji Cooper            'SeqQ/ParamTest.TestX/0',
639b89a7cc2SEnji Cooper            'SeqQ/ParamTest.TestY/0',
64028f6c2f2SEnji Cooper        ],
64128f6c2f2SEnji Cooper    )
642b89a7cc2SEnji Cooper
643b89a7cc2SEnji Cooper  def testFlagOverridesEnvVar(self):
644b89a7cc2SEnji Cooper    """Tests that the filter flag overrides the filtering env. variable."""
645b89a7cc2SEnji Cooper
646b89a7cc2SEnji Cooper    SetEnvVar(FILTER_ENV_VAR, 'Foo*')
647b89a7cc2SEnji Cooper    args = ['--%s=%s' % (FILTER_FLAG, '*One')]
648b89a7cc2SEnji Cooper    tests_run = RunAndExtractTestList(args)[0]
649b89a7cc2SEnji Cooper    SetEnvVar(FILTER_ENV_VAR, None)
650b89a7cc2SEnji Cooper
651b89a7cc2SEnji Cooper    self.AssertSetEqual(tests_run, ['BarTest.TestOne', 'BazTest.TestOne'])
652b89a7cc2SEnji Cooper
653b89a7cc2SEnji Cooper  def testShardStatusFileIsCreated(self):
654b89a7cc2SEnji Cooper    """Tests that the shard file is created if specified in the environment."""
655b89a7cc2SEnji Cooper
65628f6c2f2SEnji Cooper    shard_status_file = os.path.join(
65728f6c2f2SEnji Cooper        gtest_test_utils.GetTempDir(), 'shard_status_file'
65828f6c2f2SEnji Cooper    )
65928f6c2f2SEnji Cooper    self.assertTrue(not os.path.exists(shard_status_file))
660b89a7cc2SEnji Cooper
661b89a7cc2SEnji Cooper    extra_env = {SHARD_STATUS_FILE_ENV_VAR: shard_status_file}
662b89a7cc2SEnji Cooper    try:
663b89a7cc2SEnji Cooper      InvokeWithModifiedEnv(extra_env, RunAndReturnOutput)
664b89a7cc2SEnji Cooper    finally:
66528f6c2f2SEnji Cooper      self.assertTrue(os.path.exists(shard_status_file))
666b89a7cc2SEnji Cooper      os.remove(shard_status_file)
667b89a7cc2SEnji Cooper
668b89a7cc2SEnji Cooper  def testShardStatusFileIsCreatedWithListTests(self):
669b89a7cc2SEnji Cooper    """Tests that the shard file is created with the "list_tests" flag."""
670b89a7cc2SEnji Cooper
67128f6c2f2SEnji Cooper    shard_status_file = os.path.join(
67228f6c2f2SEnji Cooper        gtest_test_utils.GetTempDir(), 'shard_status_file2'
67328f6c2f2SEnji Cooper    )
67428f6c2f2SEnji Cooper    self.assertTrue(not os.path.exists(shard_status_file))
675b89a7cc2SEnji Cooper
676b89a7cc2SEnji Cooper    extra_env = {SHARD_STATUS_FILE_ENV_VAR: shard_status_file}
677b89a7cc2SEnji Cooper    try:
67828f6c2f2SEnji Cooper      output = InvokeWithModifiedEnv(
67928f6c2f2SEnji Cooper          extra_env, RunAndReturnOutput, [LIST_TESTS_FLAG]
68028f6c2f2SEnji Cooper      )
681b89a7cc2SEnji Cooper    finally:
682b89a7cc2SEnji Cooper      # This assertion ensures that Google Test enumerated the tests as
683b89a7cc2SEnji Cooper      # opposed to running them.
68428f6c2f2SEnji Cooper      self.assertTrue(
68528f6c2f2SEnji Cooper          '[==========]' not in output,
68628f6c2f2SEnji Cooper          (
687b89a7cc2SEnji Cooper              'Unexpected output during test enumeration.\n'
688b89a7cc2SEnji Cooper              'Please ensure that LIST_TESTS_FLAG is assigned the\n'
68928f6c2f2SEnji Cooper              'correct flag value for listing Google Test tests.'
69028f6c2f2SEnji Cooper          ),
69128f6c2f2SEnji Cooper      )
692b89a7cc2SEnji Cooper
69328f6c2f2SEnji Cooper      self.assertTrue(os.path.exists(shard_status_file))
694b89a7cc2SEnji Cooper      os.remove(shard_status_file)
695b89a7cc2SEnji Cooper
69628f6c2f2SEnji Cooper  def testDisabledBanner(self):
69728f6c2f2SEnji Cooper    """Tests that the disabled banner prints only tests that match filter."""
69828f6c2f2SEnji Cooper    make_filter = lambda s: ['--%s=%s' % (FILTER_FLAG, s)]
69928f6c2f2SEnji Cooper
70028f6c2f2SEnji Cooper    banners = RunAndExtractDisabledBannerList(make_filter('*'))
70128f6c2f2SEnji Cooper    self.AssertSetEqual(
70228f6c2f2SEnji Cooper        banners,
70328f6c2f2SEnji Cooper        [
70428f6c2f2SEnji Cooper            'BarTest.DISABLED_TestFour',
70528f6c2f2SEnji Cooper            'BarTest.DISABLED_TestFive',
70628f6c2f2SEnji Cooper            'BazTest.DISABLED_TestC',
70728f6c2f2SEnji Cooper        ],
70828f6c2f2SEnji Cooper    )
70928f6c2f2SEnji Cooper
71028f6c2f2SEnji Cooper    banners = RunAndExtractDisabledBannerList(make_filter('Bar*'))
71128f6c2f2SEnji Cooper    self.AssertSetEqual(
71228f6c2f2SEnji Cooper        banners, ['BarTest.DISABLED_TestFour', 'BarTest.DISABLED_TestFive']
71328f6c2f2SEnji Cooper    )
71428f6c2f2SEnji Cooper
71528f6c2f2SEnji Cooper    banners = RunAndExtractDisabledBannerList(make_filter('*-Bar*'))
71628f6c2f2SEnji Cooper    self.AssertSetEqual(banners, ['BazTest.DISABLED_TestC'])
71728f6c2f2SEnji Cooper
718b89a7cc2SEnji Cooper  if SUPPORTS_DEATH_TESTS:
71928f6c2f2SEnji Cooper
720b89a7cc2SEnji Cooper    def testShardingWorksWithDeathTests(self):
721b89a7cc2SEnji Cooper      """Tests integration with death tests and sharding."""
722b89a7cc2SEnji Cooper
723b89a7cc2SEnji Cooper      gtest_filter = 'HasDeathTest.*:SeqP/*'
724b89a7cc2SEnji Cooper      expected_tests = [
725b89a7cc2SEnji Cooper          'HasDeathTest.Test1',
726b89a7cc2SEnji Cooper          'HasDeathTest.Test2',
727b89a7cc2SEnji Cooper          'SeqP/ParamTest.TestX/0',
728b89a7cc2SEnji Cooper          'SeqP/ParamTest.TestX/1',
729b89a7cc2SEnji Cooper          'SeqP/ParamTest.TestY/0',
730b89a7cc2SEnji Cooper          'SeqP/ParamTest.TestY/1',
731b89a7cc2SEnji Cooper      ]
732b89a7cc2SEnji Cooper
73328f6c2f2SEnji Cooper      for flag in [
73428f6c2f2SEnji Cooper          '--gtest_death_test_style=threadsafe',
73528f6c2f2SEnji Cooper          '--gtest_death_test_style=fast',
73628f6c2f2SEnji Cooper      ]:
73728f6c2f2SEnji Cooper        self.RunAndVerifyWithSharding(
73828f6c2f2SEnji Cooper            gtest_filter, 3, expected_tests, check_exit_0=True, args=[flag]
73928f6c2f2SEnji Cooper        )
74028f6c2f2SEnji Cooper        self.RunAndVerifyWithSharding(
74128f6c2f2SEnji Cooper            gtest_filter, 5, expected_tests, check_exit_0=True, args=[flag]
74228f6c2f2SEnji Cooper        )
74328f6c2f2SEnji Cooper
744b89a7cc2SEnji Cooper
745b89a7cc2SEnji Cooperif __name__ == '__main__':
746b89a7cc2SEnji Cooper  gtest_test_utils.Main()
747