|
| 1 | +#!/usr/bin/env python3 # pylint: disable=g-interpreter-mismatch |
| 2 | +# |
| 3 | +# Copyright 2025, Google Inc. |
| 4 | +# All rights reserved. |
| 5 | +# |
| 6 | +# Redistribution and use in source and binary forms, with or without |
| 7 | +# modification, are permitted provided that the following conditions are |
| 8 | +# met: |
| 9 | +# |
| 10 | +# * Redistributions of source code must retain the above copyright |
| 11 | +# notice, this list of conditions and the following disclaimer. |
| 12 | +# * Redistributions in binary form must reproduce the above |
| 13 | +# copyright notice, this list of conditions and the following disclaimer |
| 14 | +# in the documentation and/or other materials provided with the |
| 15 | +# distribution. |
| 16 | +# * Neither the name of Google Inc. nor the names of its |
| 17 | +# contributors may be used to endorse or promote products derived from |
| 18 | +# this software without specific prior written permission. |
| 19 | +# |
| 20 | +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 | +# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 | +# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 | +# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 | +# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 | +# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 | +# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 | +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | +"""Tests for Google Test's --gtest_fail_if_no_test_selected flag.""" |
| 32 | + |
| 33 | +from googletest.test import gtest_test_utils |
| 34 | + |
| 35 | + |
| 36 | +# The command line flag for enabling the fail-if-no-test-selected behavior. |
| 37 | +FAIL_IF_NO_TEST_SELECTED_FLAG = "gtest_fail_if_no_test_selected" |
| 38 | + |
| 39 | +# The environment variable for the test output warnings file. |
| 40 | +TEST_WARNINGS_OUTPUT_FILE = "TEST_WARNINGS_OUTPUT_FILE" |
| 41 | + |
| 42 | + |
| 43 | +class GTestFailIfNoTestSelectedTest(gtest_test_utils.TestCase): |
| 44 | + """Tests the --gtest_fail_if_no_test_selected flag.""" |
| 45 | + |
| 46 | + def Run(self, program_name, flags=None, env=None): |
| 47 | + """Run the given program with the given flag. |
| 48 | +
|
| 49 | + Args: |
| 50 | + program_name: Name of the program to run. |
| 51 | + flags: The command line flags to pass to the program, or None. |
| 52 | + env: Dictionary with environment to pass to the subprocess. |
| 53 | +
|
| 54 | + Returns: |
| 55 | + True if the program exits with code 0, false otherwise. |
| 56 | + """ |
| 57 | + |
| 58 | + exe_path = gtest_test_utils.GetTestExecutablePath(program_name) |
| 59 | + args = [exe_path] |
| 60 | + if flags is not None: |
| 61 | + args.extend(flags) |
| 62 | + process = gtest_test_utils.Subprocess(args, capture_stderr=False, env=env) |
| 63 | + return process.exited and process.exit_code == 0 |
| 64 | + |
| 65 | + def testSucceedsWhenFlagIsNotSetAndOnlyDisabledTestsPresent(self): |
| 66 | + """Tests that no test selected results in success without the flag set.""" |
| 67 | + self.assertTrue( |
| 68 | + self.Run("googletest-fail-if-no-test-linked-test-with-disabled-test_"), |
| 69 | + ) |
| 70 | + |
| 71 | + def testFailsWhenFlagIsSetAndOnlyDisabledTestsPresent(self): |
| 72 | + """Tests that no test selected results in failure with the flag set.""" |
| 73 | + self.assertFalse( |
| 74 | + self.Run( |
| 75 | + "googletest-fail-if-no-test-linked-test-with-disabled-test_", |
| 76 | + flags=[f"--{FAIL_IF_NO_TEST_SELECTED_FLAG}"], |
| 77 | + ), |
| 78 | + ) |
| 79 | + |
| 80 | + def testSucceedsWhenFlagIsSetAndEnabledTestsPresent(self): |
| 81 | + """Tests that a test running still succeeds when the flag is set.""" |
| 82 | + self.assertTrue( |
| 83 | + self.Run( |
| 84 | + "googletest-fail-if-no-test-linked-test-with-enabled-test_", |
| 85 | + flags=[f"--{FAIL_IF_NO_TEST_SELECTED_FLAG}"], |
| 86 | + ), |
| 87 | + ) |
| 88 | + |
| 89 | + |
| 90 | +if __name__ == "__main__": |
| 91 | + gtest_test_utils.Main() |
0 commit comments