Wed, 31 Dec 2014 07:53:36 +0100
Correct small whitespace inconsistency, lost while renaming variables.
michael@0 | 1 | #!/usr/bin/env python |
michael@0 | 2 | # |
michael@0 | 3 | # Copyright 2009, Google Inc. |
michael@0 | 4 | # All rights reserved. |
michael@0 | 5 | # |
michael@0 | 6 | # Redistribution and use in source and binary forms, with or without |
michael@0 | 7 | # modification, are permitted provided that the following conditions are |
michael@0 | 8 | # met: |
michael@0 | 9 | # |
michael@0 | 10 | # * Redistributions of source code must retain the above copyright |
michael@0 | 11 | # notice, this list of conditions and the following disclaimer. |
michael@0 | 12 | # * Redistributions in binary form must reproduce the above |
michael@0 | 13 | # copyright notice, this list of conditions and the following disclaimer |
michael@0 | 14 | # in the documentation and/or other materials provided with the |
michael@0 | 15 | # distribution. |
michael@0 | 16 | # * Neither the name of Google Inc. nor the names of its |
michael@0 | 17 | # contributors may be used to endorse or promote products derived from |
michael@0 | 18 | # this software without specific prior written permission. |
michael@0 | 19 | # |
michael@0 | 20 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
michael@0 | 21 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
michael@0 | 22 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
michael@0 | 23 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
michael@0 | 24 | # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
michael@0 | 25 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
michael@0 | 26 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
michael@0 | 27 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
michael@0 | 28 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
michael@0 | 29 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
michael@0 | 30 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
michael@0 | 31 | |
michael@0 | 32 | """Tests the --help flag of Google C++ Testing Framework. |
michael@0 | 33 | |
michael@0 | 34 | SYNOPSIS |
michael@0 | 35 | gtest_help_test.py --build_dir=BUILD/DIR |
michael@0 | 36 | # where BUILD/DIR contains the built gtest_help_test_ file. |
michael@0 | 37 | gtest_help_test.py |
michael@0 | 38 | """ |
michael@0 | 39 | |
michael@0 | 40 | __author__ = 'wan@google.com (Zhanyong Wan)' |
michael@0 | 41 | |
michael@0 | 42 | import os |
michael@0 | 43 | import re |
michael@0 | 44 | import gtest_test_utils |
michael@0 | 45 | |
michael@0 | 46 | |
michael@0 | 47 | IS_LINUX = os.name == 'posix' and os.uname()[0] == 'Linux' |
michael@0 | 48 | IS_WINDOWS = os.name == 'nt' |
michael@0 | 49 | |
michael@0 | 50 | PROGRAM_PATH = gtest_test_utils.GetTestExecutablePath('gtest_help_test_') |
michael@0 | 51 | FLAG_PREFIX = '--gtest_' |
michael@0 | 52 | DEATH_TEST_STYLE_FLAG = FLAG_PREFIX + 'death_test_style' |
michael@0 | 53 | STREAM_RESULT_TO_FLAG = FLAG_PREFIX + 'stream_result_to' |
michael@0 | 54 | UNKNOWN_FLAG = FLAG_PREFIX + 'unknown_flag_for_testing' |
michael@0 | 55 | LIST_TESTS_FLAG = FLAG_PREFIX + 'list_tests' |
michael@0 | 56 | INCORRECT_FLAG_VARIANTS = [re.sub('^--', '-', LIST_TESTS_FLAG), |
michael@0 | 57 | re.sub('^--', '/', LIST_TESTS_FLAG), |
michael@0 | 58 | re.sub('_', '-', LIST_TESTS_FLAG)] |
michael@0 | 59 | INTERNAL_FLAG_FOR_TESTING = FLAG_PREFIX + 'internal_flag_for_testing' |
michael@0 | 60 | |
michael@0 | 61 | SUPPORTS_DEATH_TESTS = "DeathTest" in gtest_test_utils.Subprocess( |
michael@0 | 62 | [PROGRAM_PATH, LIST_TESTS_FLAG]).output |
michael@0 | 63 | |
michael@0 | 64 | # The help message must match this regex. |
michael@0 | 65 | HELP_REGEX = re.compile( |
michael@0 | 66 | FLAG_PREFIX + r'list_tests.*' + |
michael@0 | 67 | FLAG_PREFIX + r'filter=.*' + |
michael@0 | 68 | FLAG_PREFIX + r'also_run_disabled_tests.*' + |
michael@0 | 69 | FLAG_PREFIX + r'repeat=.*' + |
michael@0 | 70 | FLAG_PREFIX + r'shuffle.*' + |
michael@0 | 71 | FLAG_PREFIX + r'random_seed=.*' + |
michael@0 | 72 | FLAG_PREFIX + r'color=.*' + |
michael@0 | 73 | FLAG_PREFIX + r'print_time.*' + |
michael@0 | 74 | FLAG_PREFIX + r'output=.*' + |
michael@0 | 75 | FLAG_PREFIX + r'break_on_failure.*' + |
michael@0 | 76 | FLAG_PREFIX + r'throw_on_failure.*' + |
michael@0 | 77 | FLAG_PREFIX + r'catch_exceptions=0.*', |
michael@0 | 78 | re.DOTALL) |
michael@0 | 79 | |
michael@0 | 80 | |
michael@0 | 81 | def RunWithFlag(flag): |
michael@0 | 82 | """Runs gtest_help_test_ with the given flag. |
michael@0 | 83 | |
michael@0 | 84 | Returns: |
michael@0 | 85 | the exit code and the text output as a tuple. |
michael@0 | 86 | Args: |
michael@0 | 87 | flag: the command-line flag to pass to gtest_help_test_, or None. |
michael@0 | 88 | """ |
michael@0 | 89 | |
michael@0 | 90 | if flag is None: |
michael@0 | 91 | command = [PROGRAM_PATH] |
michael@0 | 92 | else: |
michael@0 | 93 | command = [PROGRAM_PATH, flag] |
michael@0 | 94 | child = gtest_test_utils.Subprocess(command) |
michael@0 | 95 | return child.exit_code, child.output |
michael@0 | 96 | |
michael@0 | 97 | |
michael@0 | 98 | class GTestHelpTest(gtest_test_utils.TestCase): |
michael@0 | 99 | """Tests the --help flag and its equivalent forms.""" |
michael@0 | 100 | |
michael@0 | 101 | def TestHelpFlag(self, flag): |
michael@0 | 102 | """Verifies correct behavior when help flag is specified. |
michael@0 | 103 | |
michael@0 | 104 | The right message must be printed and the tests must |
michael@0 | 105 | skipped when the given flag is specified. |
michael@0 | 106 | |
michael@0 | 107 | Args: |
michael@0 | 108 | flag: A flag to pass to the binary or None. |
michael@0 | 109 | """ |
michael@0 | 110 | |
michael@0 | 111 | exit_code, output = RunWithFlag(flag) |
michael@0 | 112 | self.assertEquals(0, exit_code) |
michael@0 | 113 | self.assert_(HELP_REGEX.search(output), output) |
michael@0 | 114 | |
michael@0 | 115 | if IS_LINUX: |
michael@0 | 116 | self.assert_(STREAM_RESULT_TO_FLAG in output, output) |
michael@0 | 117 | else: |
michael@0 | 118 | self.assert_(STREAM_RESULT_TO_FLAG not in output, output) |
michael@0 | 119 | |
michael@0 | 120 | if SUPPORTS_DEATH_TESTS and not IS_WINDOWS: |
michael@0 | 121 | self.assert_(DEATH_TEST_STYLE_FLAG in output, output) |
michael@0 | 122 | else: |
michael@0 | 123 | self.assert_(DEATH_TEST_STYLE_FLAG not in output, output) |
michael@0 | 124 | |
michael@0 | 125 | def TestNonHelpFlag(self, flag): |
michael@0 | 126 | """Verifies correct behavior when no help flag is specified. |
michael@0 | 127 | |
michael@0 | 128 | Verifies that when no help flag is specified, the tests are run |
michael@0 | 129 | and the help message is not printed. |
michael@0 | 130 | |
michael@0 | 131 | Args: |
michael@0 | 132 | flag: A flag to pass to the binary or None. |
michael@0 | 133 | """ |
michael@0 | 134 | |
michael@0 | 135 | exit_code, output = RunWithFlag(flag) |
michael@0 | 136 | self.assert_(exit_code != 0) |
michael@0 | 137 | self.assert_(not HELP_REGEX.search(output), output) |
michael@0 | 138 | |
michael@0 | 139 | def testPrintsHelpWithFullFlag(self): |
michael@0 | 140 | self.TestHelpFlag('--help') |
michael@0 | 141 | |
michael@0 | 142 | def testPrintsHelpWithShortFlag(self): |
michael@0 | 143 | self.TestHelpFlag('-h') |
michael@0 | 144 | |
michael@0 | 145 | def testPrintsHelpWithQuestionFlag(self): |
michael@0 | 146 | self.TestHelpFlag('-?') |
michael@0 | 147 | |
michael@0 | 148 | def testPrintsHelpWithWindowsStyleQuestionFlag(self): |
michael@0 | 149 | self.TestHelpFlag('/?') |
michael@0 | 150 | |
michael@0 | 151 | def testPrintsHelpWithUnrecognizedGoogleTestFlag(self): |
michael@0 | 152 | self.TestHelpFlag(UNKNOWN_FLAG) |
michael@0 | 153 | |
michael@0 | 154 | def testPrintsHelpWithIncorrectFlagStyle(self): |
michael@0 | 155 | for incorrect_flag in INCORRECT_FLAG_VARIANTS: |
michael@0 | 156 | self.TestHelpFlag(incorrect_flag) |
michael@0 | 157 | |
michael@0 | 158 | def testRunsTestsWithoutHelpFlag(self): |
michael@0 | 159 | """Verifies that when no help flag is specified, the tests are run |
michael@0 | 160 | and the help message is not printed.""" |
michael@0 | 161 | |
michael@0 | 162 | self.TestNonHelpFlag(None) |
michael@0 | 163 | |
michael@0 | 164 | def testRunsTestsWithGtestInternalFlag(self): |
michael@0 | 165 | """Verifies that the tests are run and no help message is printed when |
michael@0 | 166 | a flag starting with Google Test prefix and 'internal_' is supplied.""" |
michael@0 | 167 | |
michael@0 | 168 | self.TestNonHelpFlag(INTERNAL_FLAG_FOR_TESTING) |
michael@0 | 169 | |
michael@0 | 170 | |
michael@0 | 171 | if __name__ == '__main__': |
michael@0 | 172 | gtest_test_utils.Main() |