128f6c2f2SEnji Cooper# Copyright 2021 Google Inc. All Rights Reserved.
228f6c2f2SEnji Cooper#
328f6c2f2SEnji Cooper# Redistribution and use in source and binary forms, with or without
428f6c2f2SEnji Cooper# modification, are permitted provided that the following conditions are
528f6c2f2SEnji Cooper# met:
628f6c2f2SEnji Cooper#
728f6c2f2SEnji Cooper#     * Redistributions of source code must retain the above copyright
828f6c2f2SEnji Cooper# notice, this list of conditions and the following disclaimer.
928f6c2f2SEnji Cooper#     * Redistributions in binary form must reproduce the above
1028f6c2f2SEnji Cooper# copyright notice, this list of conditions and the following disclaimer
1128f6c2f2SEnji Cooper# in the documentation and/or other materials provided with the
1228f6c2f2SEnji Cooper# distribution.
1328f6c2f2SEnji Cooper#     * Neither the name of Google Inc. nor the names of its
1428f6c2f2SEnji Cooper# contributors may be used to endorse or promote products derived from
1528f6c2f2SEnji Cooper# this software without specific prior written permission.
1628f6c2f2SEnji Cooper#
1728f6c2f2SEnji Cooper# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1828f6c2f2SEnji Cooper# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1928f6c2f2SEnji Cooper# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
2028f6c2f2SEnji Cooper# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2128f6c2f2SEnji Cooper# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2228f6c2f2SEnji Cooper# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2328f6c2f2SEnji Cooper# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2428f6c2f2SEnji Cooper# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2528f6c2f2SEnji Cooper# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2628f6c2f2SEnji Cooper# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2728f6c2f2SEnji Cooper# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2828f6c2f2SEnji Cooper"""Unit test for Google Test's global test environment behavior.
2928f6c2f2SEnji Cooper
3028f6c2f2SEnji CooperA user can specify a global test environment via
3128f6c2f2SEnji Coopertesting::AddGlobalTestEnvironment. Failures in the global environment should
3228f6c2f2SEnji Cooperresult in all unit tests being skipped.
3328f6c2f2SEnji Cooper
3428f6c2f2SEnji CooperThis script tests such functionality by invoking
3528f6c2f2SEnji Coopergoogletest-global-environment-unittest_ (a program written with Google Test).
3628f6c2f2SEnji Cooper"""
3728f6c2f2SEnji Cooper
3828f6c2f2SEnji Cooperimport re
3928f6c2f2SEnji Cooperfrom googletest.test import gtest_test_utils
4028f6c2f2SEnji Cooper
4128f6c2f2SEnji Cooper
4228f6c2f2SEnji Cooperdef RunAndReturnOutput(args=None):
4328f6c2f2SEnji Cooper  """Runs the test program and returns its output."""
4428f6c2f2SEnji Cooper
4528f6c2f2SEnji Cooper  return gtest_test_utils.Subprocess(
4628f6c2f2SEnji Cooper      [
4728f6c2f2SEnji Cooper          gtest_test_utils.GetTestExecutablePath(
4828f6c2f2SEnji Cooper              'googletest-global-environment-unittest_'
4928f6c2f2SEnji Cooper          )
5028f6c2f2SEnji Cooper      ]
5128f6c2f2SEnji Cooper      + (args or [])
5228f6c2f2SEnji Cooper  ).output
5328f6c2f2SEnji Cooper
5428f6c2f2SEnji Cooper
5528f6c2f2SEnji Cooperclass GTestGlobalEnvironmentUnitTest(gtest_test_utils.TestCase):
5628f6c2f2SEnji Cooper  """Tests global test environment failures."""
5728f6c2f2SEnji Cooper
5828f6c2f2SEnji Cooper  def testEnvironmentSetUpFails(self):
5928f6c2f2SEnji Cooper    """Tests the behavior of not specifying the fail_fast."""
6028f6c2f2SEnji Cooper
6128f6c2f2SEnji Cooper    # Run the test.
6228f6c2f2SEnji Cooper    txt = RunAndReturnOutput()
6328f6c2f2SEnji Cooper
6428f6c2f2SEnji Cooper    # We should see the text of the global environment setup error.
6528f6c2f2SEnji Cooper    self.assertIn('Canned environment setup error', txt)
6628f6c2f2SEnji Cooper
6728f6c2f2SEnji Cooper    # Our test should have been skipped due to the error, and not treated as a
6828f6c2f2SEnji Cooper    # pass.
6928f6c2f2SEnji Cooper    self.assertIn('[  SKIPPED ] 1 test', txt)
7028f6c2f2SEnji Cooper    self.assertIn('[  PASSED  ] 0 tests', txt)
7128f6c2f2SEnji Cooper
7228f6c2f2SEnji Cooper    # The test case shouldn't have been run.
7328f6c2f2SEnji Cooper    self.assertNotIn('Unexpected call', txt)
7428f6c2f2SEnji Cooper
7528f6c2f2SEnji Cooper  def testEnvironmentSetUpAndTornDownForEachRepeat(self):
7628f6c2f2SEnji Cooper    """Tests the behavior of test environments and gtest_repeat."""
7728f6c2f2SEnji Cooper
7828f6c2f2SEnji Cooper    # When --gtest_recreate_environments_when_repeating is true, the global test
7928f6c2f2SEnji Cooper    # environment should be set up and torn down for each iteration.
8028f6c2f2SEnji Cooper    txt = RunAndReturnOutput([
8128f6c2f2SEnji Cooper        '--gtest_repeat=2',
8228f6c2f2SEnji Cooper        '--gtest_recreate_environments_when_repeating=true',
8328f6c2f2SEnji Cooper    ])
8428f6c2f2SEnji Cooper
8528f6c2f2SEnji Cooper    expected_pattern = (
8628f6c2f2SEnji Cooper        '(.|\n)*'
8728f6c2f2SEnji Cooper        r'Repeating all tests \(iteration 1\)'
8828f6c2f2SEnji Cooper        '(.|\n)*'
8928f6c2f2SEnji Cooper        'Global test environment set-up.'
9028f6c2f2SEnji Cooper        '(.|\n)*'
9128f6c2f2SEnji Cooper        'SomeTest.DoesFoo'
9228f6c2f2SEnji Cooper        '(.|\n)*'
9328f6c2f2SEnji Cooper        'Global test environment tear-down'
9428f6c2f2SEnji Cooper        '(.|\n)*'
9528f6c2f2SEnji Cooper        r'Repeating all tests \(iteration 2\)'
9628f6c2f2SEnji Cooper        '(.|\n)*'
9728f6c2f2SEnji Cooper        'Global test environment set-up.'
9828f6c2f2SEnji Cooper        '(.|\n)*'
9928f6c2f2SEnji Cooper        'SomeTest.DoesFoo'
10028f6c2f2SEnji Cooper        '(.|\n)*'
10128f6c2f2SEnji Cooper        'Global test environment tear-down'
10228f6c2f2SEnji Cooper        '(.|\n)*'
10328f6c2f2SEnji Cooper    )
10428f6c2f2SEnji Cooper    self.assertRegex(txt, expected_pattern)
10528f6c2f2SEnji Cooper
10628f6c2f2SEnji Cooper  def testEnvironmentSetUpAndTornDownOnce(self):
10728f6c2f2SEnji Cooper    """Tests environment and --gtest_recreate_environments_when_repeating."""
10828f6c2f2SEnji Cooper
10928f6c2f2SEnji Cooper    # By default the environment should only be set up and torn down once, at
11028f6c2f2SEnji Cooper    # the start and end of the test respectively.
11128f6c2f2SEnji Cooper    txt = RunAndReturnOutput(
11228f6c2f2SEnji Cooper        [
11328f6c2f2SEnji Cooper            '--gtest_repeat=2',
11428f6c2f2SEnji Cooper        ]
11528f6c2f2SEnji Cooper    )
11628f6c2f2SEnji Cooper
11728f6c2f2SEnji Cooper    expected_pattern = (
11828f6c2f2SEnji Cooper        '(.|\n)*'
11928f6c2f2SEnji Cooper        r'Repeating all tests \(iteration 1\)'
12028f6c2f2SEnji Cooper        '(.|\n)*'
12128f6c2f2SEnji Cooper        'Global test environment set-up.'
12228f6c2f2SEnji Cooper        '(.|\n)*'
12328f6c2f2SEnji Cooper        'SomeTest.DoesFoo'
12428f6c2f2SEnji Cooper        '(.|\n)*'
12528f6c2f2SEnji Cooper        r'Repeating all tests \(iteration 2\)'
12628f6c2f2SEnji Cooper        '(.|\n)*'
12728f6c2f2SEnji Cooper        'SomeTest.DoesFoo'
12828f6c2f2SEnji Cooper        '(.|\n)*'
12928f6c2f2SEnji Cooper        'Global test environment tear-down'
13028f6c2f2SEnji Cooper        '(.|\n)*'
13128f6c2f2SEnji Cooper    )
13228f6c2f2SEnji Cooper    self.assertRegex(txt, expected_pattern)
13328f6c2f2SEnji Cooper
13428f6c2f2SEnji Cooper    self.assertEqual(len(re.findall('Global test environment set-up', txt)), 1)
13528f6c2f2SEnji Cooper    self.assertEqual(
13628f6c2f2SEnji Cooper        len(re.findall('Global test environment tear-down', txt)), 1
13728f6c2f2SEnji Cooper    )
13828f6c2f2SEnji Cooper
13928f6c2f2SEnji Cooper
14028f6c2f2SEnji Cooperif __name__ == '__main__':
14128f6c2f2SEnji Cooper  gtest_test_utils.Main()
142