1#!/usr/bin/env python 2# 3# Copyright 2009, 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 32"""Tests the --help flag of Google C++ Testing and Mocking Framework. 33 34SYNOPSIS 35 gtest_help_test.py --build_dir=BUILD/DIR 36 # where BUILD/DIR contains the built gtest_help_test_ file. 37 gtest_help_test.py 38""" 39 40import os 41import re 42import sys 43from googletest.test import gtest_test_utils 44 45 46FREEBSD = ('FreeBSD', 'GNU/kFreeBSD') 47NETBSD = ('NetBSD',) 48OPENBSD = ('OpenBSD',) 49 50 51def is_bsd_based_os() -> bool: 52 """Determine whether or not the OS is BSD-based.""" 53 if os.name != 'posix': 54 return False 55 56 return os.uname()[0] in (FREEBSD + NETBSD + OPENBSD) 57 58 59IS_DARWIN = os.name == 'posix' and os.uname()[0] == 'Darwin' 60IS_LINUX = os.name == 'posix' and os.uname()[0] == 'Linux' 61IS_GNUHURD = os.name == 'posix' and os.uname()[0] == 'GNU' 62IS_WINDOWS = os.name == 'nt' 63 64PROGRAM_PATH = gtest_test_utils.GetTestExecutablePath('gtest_help_test_') 65FLAG_PREFIX = '--gtest_' 66DEATH_TEST_STYLE_FLAG = FLAG_PREFIX + 'death_test_style' 67STREAM_RESULT_TO_FLAG = FLAG_PREFIX + 'stream_result_to' 68LIST_TESTS_FLAG = FLAG_PREFIX + 'list_tests' 69INTERNAL_FLAG_FOR_TESTING = FLAG_PREFIX + 'internal_flag_for_testing' 70 71SUPPORTS_DEATH_TESTS = ( 72 'DeathTest' 73 in gtest_test_utils.Subprocess([PROGRAM_PATH, LIST_TESTS_FLAG]).output 74) 75 76HAS_ABSL_FLAGS = '--has_absl_flags' in sys.argv 77 78# The help message must match this regex. 79HELP_REGEX = re.compile( 80 FLAG_PREFIX 81 + r'list_tests.*' 82 + FLAG_PREFIX 83 + r'filter=.*' 84 + FLAG_PREFIX 85 + r'also_run_disabled_tests.*' 86 + FLAG_PREFIX 87 + r'repeat=.*' 88 + FLAG_PREFIX 89 + r'shuffle.*' 90 + FLAG_PREFIX 91 + r'random_seed=.*' 92 + FLAG_PREFIX 93 + r'color=.*' 94 + FLAG_PREFIX 95 + r'brief.*' 96 + FLAG_PREFIX 97 + r'print_time.*' 98 + FLAG_PREFIX 99 + r'output=.*' 100 + FLAG_PREFIX 101 + r'break_on_failure.*' 102 + FLAG_PREFIX 103 + r'throw_on_failure.*' 104 + FLAG_PREFIX 105 + r'catch_exceptions=0.*', 106 re.DOTALL, 107) 108 109 110def run_with_flag(flag): 111 """Runs gtest_help_test_ with the given flag. 112 113 Returns: 114 the exit code and the text output as a tuple. 115 Args: 116 flag: the command-line flag to pass to gtest_help_test_, or None. 117 """ 118 119 if flag is None: 120 command = [PROGRAM_PATH] 121 else: 122 command = [PROGRAM_PATH, flag] 123 child = gtest_test_utils.Subprocess(command) 124 return child.exit_code, child.output 125 126 127class GTestHelpTest(gtest_test_utils.TestCase): 128 """Tests the --help flag and its equivalent forms.""" 129 130 def test_prints_help_with_full_flag(self): 131 """Verifies correct behavior when help flag is specified. 132 133 The right message must be printed and the tests must 134 skipped when the given flag is specified. 135 """ 136 137 exit_code, output = run_with_flag('--help') 138 if HAS_ABSL_FLAGS: 139 # The Abseil flags library prints the ProgramUsageMessage() with 140 # --help and returns 1. 141 self.assertEqual(1, exit_code) 142 else: 143 self.assertEqual(0, exit_code) 144 145 self.assertTrue(HELP_REGEX.search(output), output) 146 147 if IS_DARWIN or IS_LINUX or IS_GNUHURD or is_bsd_based_os(): 148 self.assertIn(STREAM_RESULT_TO_FLAG, output) 149 else: 150 self.assertNotIn(STREAM_RESULT_TO_FLAG, output) 151 152 if SUPPORTS_DEATH_TESTS and not IS_WINDOWS: 153 self.assertIn(DEATH_TEST_STYLE_FLAG, output) 154 else: 155 self.assertNotIn(DEATH_TEST_STYLE_FLAG, output) 156 157 def test_runs_tests_without_help_flag(self): 158 """Verifies correct behavior when no help flag is specified. 159 160 Verifies that when no help flag is specified, the tests are run 161 and the help message is not printed. 162 """ 163 164 exit_code, output = run_with_flag(None) 165 self.assertNotEqual(exit_code, 0) 166 self.assertFalse(HELP_REGEX.search(output), output) 167 168 def test_runs_tests_with_gtest_internal_flag(self): 169 """Verifies correct behavior when internal testing flag is specified. 170 171 Verifies that the tests are run and no help message is printed when 172 a flag starting with Google Test prefix and 'internal_' is supplied. 173 """ 174 175 exit_code, output = run_with_flag(INTERNAL_FLAG_FOR_TESTING) 176 self.assertNotEqual(exit_code, 0) 177 self.assertFalse(HELP_REGEX.search(output), output) 178 179 180if __name__ == '__main__': 181 if '--has_absl_flags' in sys.argv: 182 sys.argv.remove('--has_absl_flags') 183 gtest_test_utils.Main() 184