| 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 |
|
| 34 |
SYNOPSIS |
| 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 |
|
| 40 |
import os |
| 41 |
import re |
| 42 |
from googletest.test import gtest_test_utils |
| 43 |
|
| 44 |
|
| 45 |
IS_LINUX = os.name == 'posix' and os.uname()[0] == 'Linux' |
| 46 |
IS_GNUHURD = os.name == 'posix' and os.uname()[0] == 'GNU' |
| 47 |
IS_GNUKFREEBSD = os.name == 'posix' and os.uname()[0] == 'GNU/kFreeBSD' |
| 48 |
IS_WINDOWS = os.name == 'nt' |
| 49 |
|
| 50 |
PROGRAM_PATH = gtest_test_utils.GetTestExecutablePath('gtest_help_test_') |
| 51 |
FLAG_PREFIX = '--gtest_' |
| 52 |
DEATH_TEST_STYLE_FLAG = FLAG_PREFIX + 'death_test_style' |
| 53 |
STREAM_RESULT_TO_FLAG = FLAG_PREFIX + 'stream_result_to' |
| 54 |
UNKNOWN_FLAG = FLAG_PREFIX + 'unknown_flag_for_testing' |
| 55 |
LIST_TESTS_FLAG = FLAG_PREFIX + 'list_tests' |
| 56 |
INCORRECT_FLAG_VARIANTS = [re.sub('^--', '-', LIST_TESTS_FLAG), |
| 57 |
re.sub('^--', '/', LIST_TESTS_FLAG), |
| 58 |
re.sub('_', '-', LIST_TESTS_FLAG)] |
| 59 |
INTERNAL_FLAG_FOR_TESTING = FLAG_PREFIX + 'internal_flag_for_testing' |
| 60 |
|
| 61 |
SUPPORTS_DEATH_TESTS = "DeathTest" in gtest_test_utils.Subprocess( |
| 62 |
[PROGRAM_PATH, LIST_TESTS_FLAG]).output |
| 63 |
|
| 64 |
# The help message must match this regex. |
| 65 |
HELP_REGEX = re.compile( |
| 66 |
FLAG_PREFIX + r'list_tests.*' + |
| 67 |
FLAG_PREFIX + r'filter=.*' + |
| 68 |
FLAG_PREFIX + r'also_run_disabled_tests.*' + |
| 69 |
FLAG_PREFIX + r'repeat=.*' + |
| 70 |
FLAG_PREFIX + r'shuffle.*' + |
| 71 |
FLAG_PREFIX + r'random_seed=.*' + |
| 72 |
FLAG_PREFIX + r'color=.*' + |
| 73 |
FLAG_PREFIX + r'brief.*' + |
| 74 |
FLAG_PREFIX + r'print_time.*' + |
| 75 |
FLAG_PREFIX + r'output=.*' + |
| 76 |
FLAG_PREFIX + r'break_on_failure.*' + |
| 77 |
FLAG_PREFIX + r'throw_on_failure.*' + |
| 78 |
FLAG_PREFIX + r'catch_exceptions=0.*', |
| 79 |
re.DOTALL) |
| 80 |
|
| 81 |
|
| 82 |
def RunWithFlag(flag): |
| 83 |
"""Runs gtest_help_test_ with the given flag. |
| 84 |
|
| 85 |
Returns: |
| 86 |
the exit code and the text output as a tuple. |
| 87 |
Args: |
| 88 |
flag: the command-line flag to pass to gtest_help_test_, or None. |
| 89 |
""" |
| 90 |
|
| 91 |
if flag is None: |
| 92 |
command = [PROGRAM_PATH] |
| 93 |
else: |
| 94 |
command = [PROGRAM_PATH, flag] |
| 95 |
child = gtest_test_utils.Subprocess(command) |
| 96 |
return child.exit_code, child.output |
| 97 |
|
| 98 |
|
| 99 |
class GTestHelpTest(gtest_test_utils.TestCase): |
| 100 |
"""Tests the --help flag and its equivalent forms.""" |
| 101 |
|
| 102 |
def TestHelpFlag(self, flag): |
| 103 |
"""Verifies correct behavior when help flag is specified. |
| 104 |
|
| 105 |
The right message must be printed and the tests must |
| 106 |
skipped when the given flag is specified. |
| 107 |
|
| 108 |
Args: |
| 109 |
flag: A flag to pass to the binary or None. |
| 110 |
""" |
| 111 |
|
| 112 |
exit_code, output = RunWithFlag(flag) |
| 113 |
self.assertEquals(0, exit_code) |
| 114 |
self.assert_(HELP_REGEX.search(output), output) |
| 115 |
|
| 116 |
if IS_LINUX or IS_GNUHURD or IS_GNUKFREEBSD: |
| 117 |
self.assert_(STREAM_RESULT_TO_FLAG in output, output) |
| 118 |
else: |
| 119 |
self.assert_(STREAM_RESULT_TO_FLAG not in output, output) |
| 120 |
|
| 121 |
if SUPPORTS_DEATH_TESTS and not IS_WINDOWS: |
| 122 |
self.assert_(DEATH_TEST_STYLE_FLAG in output, output) |
| 123 |
else: |
| 124 |
self.assert_(DEATH_TEST_STYLE_FLAG not in output, output) |
| 125 |
|
| 126 |
def TestNonHelpFlag(self, flag): |
| 127 |
"""Verifies correct behavior when no help flag is specified. |
| 128 |
|
| 129 |
Verifies that when no help flag is specified, the tests are run |
| 130 |
and the help message is not printed. |
| 131 |
|
| 132 |
Args: |
| 133 |
flag: A flag to pass to the binary or None. |
| 134 |
""" |
| 135 |
|
| 136 |
exit_code, output = RunWithFlag(flag) |
| 137 |
self.assert_(exit_code != 0) |
| 138 |
self.assert_(not HELP_REGEX.search(output), output) |
| 139 |
|
| 140 |
def testPrintsHelpWithFullFlag(self): |
| 141 |
self.TestHelpFlag('--help') |
| 142 |
|
| 143 |
def testPrintsHelpWithShortFlag(self): |
| 144 |
self.TestHelpFlag('-h') |
| 145 |
|
| 146 |
def testPrintsHelpWithQuestionFlag(self): |
| 147 |
self.TestHelpFlag('-?') |
| 148 |
|
| 149 |
def testPrintsHelpWithWindowsStyleQuestionFlag(self): |
| 150 |
self.TestHelpFlag('/?') |
| 151 |
|
| 152 |
def testPrintsHelpWithUnrecognizedGoogleTestFlag(self): |
| 153 |
self.TestHelpFlag(UNKNOWN_FLAG) |
| 154 |
|
| 155 |
def testPrintsHelpWithIncorrectFlagStyle(self): |
| 156 |
for incorrect_flag in INCORRECT_FLAG_VARIANTS: |
| 157 |
self.TestHelpFlag(incorrect_flag) |
| 158 |
|
| 159 |
def testRunsTestsWithoutHelpFlag(self): |
| 160 |
"""Verifies that when no help flag is specified, the tests are run |
| 161 |
and the help message is not printed.""" |
| 162 |
|
| 163 |
self.TestNonHelpFlag(None) |
| 164 |
|
| 165 |
def testRunsTestsWithGtestInternalFlag(self): |
| 166 |
"""Verifies that the tests are run and no help message is printed when |
| 167 |
a flag starting with Google Test prefix and 'internal_' is supplied.""" |
| 168 |
|
| 169 |
self.TestNonHelpFlag(INTERNAL_FLAG_FOR_TESTING) |
| 170 |
|
| 171 |
|
| 172 |
if __name__ == '__main__': |
| 173 |
gtest_test_utils.Main() |