3 # Copyright 2009, Google Inc.
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions are
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
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.
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.
32 """Tests the --help flag of Google C++ Testing and Mocking Framework.
35 gtest_help_test.py --build_dir=BUILD/DIR
36 # where BUILD/DIR contains the built gtest_help_test_ file.
43 from googletest.test import gtest_test_utils
46 IS_LINUX = os.name == 'posix' and os.uname()[0] == 'Linux'
47 IS_GNUHURD = os.name == 'posix' and os.uname()[0] == 'GNU'
48 IS_GNUKFREEBSD = os.name == 'posix' and os.uname()[0] == 'GNU/kFreeBSD'
49 IS_OPENBSD = os.name == 'posix' and os.uname()[0] == 'OpenBSD'
50 IS_WINDOWS = os.name == 'nt'
52 PROGRAM_PATH = gtest_test_utils.GetTestExecutablePath('gtest_help_test_')
53 FLAG_PREFIX = '--gtest_'
54 DEATH_TEST_STYLE_FLAG = FLAG_PREFIX + 'death_test_style'
55 STREAM_RESULT_TO_FLAG = FLAG_PREFIX + 'stream_result_to'
56 UNKNOWN_GTEST_PREFIXED_FLAG = FLAG_PREFIX + 'unknown_flag_for_testing'
57 LIST_TESTS_FLAG = FLAG_PREFIX + 'list_tests'
58 INTERNAL_FLAG_FOR_TESTING = FLAG_PREFIX + 'internal_flag_for_testing'
60 SUPPORTS_DEATH_TESTS = "DeathTest" in gtest_test_utils.Subprocess(
61 [PROGRAM_PATH, LIST_TESTS_FLAG]).output
63 HAS_ABSL_FLAGS = '--has_absl_flags' in sys.argv
65 # The help message must match this regex.
66 HELP_REGEX = re.compile(
67 FLAG_PREFIX + r'list_tests.*' +
68 FLAG_PREFIX + r'filter=.*' +
69 FLAG_PREFIX + r'also_run_disabled_tests.*' +
70 FLAG_PREFIX + r'repeat=.*' +
71 FLAG_PREFIX + r'shuffle.*' +
72 FLAG_PREFIX + r'random_seed=.*' +
73 FLAG_PREFIX + r'color=.*' +
74 FLAG_PREFIX + r'brief.*' +
75 FLAG_PREFIX + r'print_time.*' +
76 FLAG_PREFIX + r'output=.*' +
77 FLAG_PREFIX + r'break_on_failure.*' +
78 FLAG_PREFIX + r'throw_on_failure.*' +
79 FLAG_PREFIX + r'catch_exceptions=0.*',
83 def RunWithFlag(flag):
84 """Runs gtest_help_test_ with the given flag.
87 the exit code and the text output as a tuple.
89 flag: the command-line flag to pass to gtest_help_test_, or None.
93 command = [PROGRAM_PATH]
95 command = [PROGRAM_PATH, flag]
96 child = gtest_test_utils.Subprocess(command)
97 return child.exit_code, child.output
100 class GTestHelpTest(gtest_test_utils.TestCase):
101 """Tests the --help flag and its equivalent forms."""
103 def TestHelpFlag(self, flag):
104 """Verifies correct behavior when help flag is specified.
106 The right message must be printed and the tests must
107 skipped when the given flag is specified.
110 flag: A flag to pass to the binary or None.
113 exit_code, output = RunWithFlag(flag)
115 # The Abseil flags library prints the ProgramUsageMessage() with
116 # --help and returns 1.
117 self.assertEqual(1, exit_code)
119 self.assertEqual(0, exit_code)
121 self.assertTrue(HELP_REGEX.search(output), output)
123 if IS_LINUX or IS_GNUHURD or IS_GNUKFREEBSD or IS_OPENBSD:
124 self.assertIn(STREAM_RESULT_TO_FLAG, output)
126 self.assertNotIn(STREAM_RESULT_TO_FLAG, output)
128 if SUPPORTS_DEATH_TESTS and not IS_WINDOWS:
129 self.assertIn(DEATH_TEST_STYLE_FLAG, output)
131 self.assertNotIn(DEATH_TEST_STYLE_FLAG, output)
133 def TestUnknownFlagWithAbseil(self, flag):
134 """Verifies correct behavior when an unknown flag is specified.
136 The right message must be printed and the tests must
137 skipped when the given flag is specified.
140 flag: A flag to pass to the binary or None.
142 exit_code, output = RunWithFlag(flag)
143 self.assertEqual(1, exit_code)
144 self.assertIn('ERROR: Unknown command line flag', output)
146 def TestNonHelpFlag(self, flag):
147 """Verifies correct behavior when no help flag is specified.
149 Verifies that when no help flag is specified, the tests are run
150 and the help message is not printed.
153 flag: A flag to pass to the binary or None.
156 exit_code, output = RunWithFlag(flag)
157 self.assertNotEqual(exit_code, 0)
158 self.assertFalse(HELP_REGEX.search(output), output)
160 def testPrintsHelpWithFullFlag(self):
161 self.TestHelpFlag('--help')
163 def testPrintsHelpWithUnrecognizedGoogleTestFlag(self):
164 # The behavior is slightly different when Abseil flags is
165 # used. Abseil flags rejects all unknown flags, while the builtin
166 # GTest flags implementation interprets an unknown flag with a
167 # '--gtest_' prefix as a request for help.
169 self.TestUnknownFlagWithAbseil(UNKNOWN_GTEST_PREFIXED_FLAG)
171 self.TestHelpFlag(UNKNOWN_GTEST_PREFIXED_FLAG)
173 def testRunsTestsWithoutHelpFlag(self):
174 """Verifies that when no help flag is specified, the tests are run
175 and the help message is not printed."""
177 self.TestNonHelpFlag(None)
179 def testRunsTestsWithGtestInternalFlag(self):
180 """Verifies that the tests are run and no help message is printed when
181 a flag starting with Google Test prefix and 'internal_' is supplied."""
183 self.TestNonHelpFlag(INTERNAL_FLAG_FOR_TESTING)
186 if __name__ == '__main__':
187 if '--has_absl_flags' in sys.argv:
188 sys.argv.remove('--has_absl_flags')
189 gtest_test_utils.Main()