michael@0: #!/usr/bin/env python michael@0: # michael@0: # Copyright 2009, Google Inc. michael@0: # All rights reserved. michael@0: # michael@0: # Redistribution and use in source and binary forms, with or without michael@0: # modification, are permitted provided that the following conditions are michael@0: # met: michael@0: # michael@0: # * Redistributions of source code must retain the above copyright michael@0: # notice, this list of conditions and the following disclaimer. michael@0: # * Redistributions in binary form must reproduce the above michael@0: # copyright notice, this list of conditions and the following disclaimer michael@0: # in the documentation and/or other materials provided with the michael@0: # distribution. michael@0: # * Neither the name of Google Inc. nor the names of its michael@0: # contributors may be used to endorse or promote products derived from michael@0: # this software without specific prior written permission. michael@0: # michael@0: # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS michael@0: # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT michael@0: # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR michael@0: # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT michael@0: # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, michael@0: # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT michael@0: # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, michael@0: # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY michael@0: # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT michael@0: # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE michael@0: # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. michael@0: michael@0: """Tests the --help flag of Google C++ Testing Framework. michael@0: michael@0: SYNOPSIS michael@0: gtest_help_test.py --build_dir=BUILD/DIR michael@0: # where BUILD/DIR contains the built gtest_help_test_ file. michael@0: gtest_help_test.py michael@0: """ michael@0: michael@0: __author__ = 'wan@google.com (Zhanyong Wan)' michael@0: michael@0: import os michael@0: import re michael@0: import gtest_test_utils michael@0: michael@0: michael@0: IS_LINUX = os.name == 'posix' and os.uname()[0] == 'Linux' michael@0: IS_WINDOWS = os.name == 'nt' michael@0: michael@0: PROGRAM_PATH = gtest_test_utils.GetTestExecutablePath('gtest_help_test_') michael@0: FLAG_PREFIX = '--gtest_' michael@0: DEATH_TEST_STYLE_FLAG = FLAG_PREFIX + 'death_test_style' michael@0: STREAM_RESULT_TO_FLAG = FLAG_PREFIX + 'stream_result_to' michael@0: UNKNOWN_FLAG = FLAG_PREFIX + 'unknown_flag_for_testing' michael@0: LIST_TESTS_FLAG = FLAG_PREFIX + 'list_tests' michael@0: INCORRECT_FLAG_VARIANTS = [re.sub('^--', '-', LIST_TESTS_FLAG), michael@0: re.sub('^--', '/', LIST_TESTS_FLAG), michael@0: re.sub('_', '-', LIST_TESTS_FLAG)] michael@0: INTERNAL_FLAG_FOR_TESTING = FLAG_PREFIX + 'internal_flag_for_testing' michael@0: michael@0: SUPPORTS_DEATH_TESTS = "DeathTest" in gtest_test_utils.Subprocess( michael@0: [PROGRAM_PATH, LIST_TESTS_FLAG]).output michael@0: michael@0: # The help message must match this regex. michael@0: HELP_REGEX = re.compile( michael@0: FLAG_PREFIX + r'list_tests.*' + michael@0: FLAG_PREFIX + r'filter=.*' + michael@0: FLAG_PREFIX + r'also_run_disabled_tests.*' + michael@0: FLAG_PREFIX + r'repeat=.*' + michael@0: FLAG_PREFIX + r'shuffle.*' + michael@0: FLAG_PREFIX + r'random_seed=.*' + michael@0: FLAG_PREFIX + r'color=.*' + michael@0: FLAG_PREFIX + r'print_time.*' + michael@0: FLAG_PREFIX + r'output=.*' + michael@0: FLAG_PREFIX + r'break_on_failure.*' + michael@0: FLAG_PREFIX + r'throw_on_failure.*' + michael@0: FLAG_PREFIX + r'catch_exceptions=0.*', michael@0: re.DOTALL) michael@0: michael@0: michael@0: def RunWithFlag(flag): michael@0: """Runs gtest_help_test_ with the given flag. michael@0: michael@0: Returns: michael@0: the exit code and the text output as a tuple. michael@0: Args: michael@0: flag: the command-line flag to pass to gtest_help_test_, or None. michael@0: """ michael@0: michael@0: if flag is None: michael@0: command = [PROGRAM_PATH] michael@0: else: michael@0: command = [PROGRAM_PATH, flag] michael@0: child = gtest_test_utils.Subprocess(command) michael@0: return child.exit_code, child.output michael@0: michael@0: michael@0: class GTestHelpTest(gtest_test_utils.TestCase): michael@0: """Tests the --help flag and its equivalent forms.""" michael@0: michael@0: def TestHelpFlag(self, flag): michael@0: """Verifies correct behavior when help flag is specified. michael@0: michael@0: The right message must be printed and the tests must michael@0: skipped when the given flag is specified. michael@0: michael@0: Args: michael@0: flag: A flag to pass to the binary or None. michael@0: """ michael@0: michael@0: exit_code, output = RunWithFlag(flag) michael@0: self.assertEquals(0, exit_code) michael@0: self.assert_(HELP_REGEX.search(output), output) michael@0: michael@0: if IS_LINUX: michael@0: self.assert_(STREAM_RESULT_TO_FLAG in output, output) michael@0: else: michael@0: self.assert_(STREAM_RESULT_TO_FLAG not in output, output) michael@0: michael@0: if SUPPORTS_DEATH_TESTS and not IS_WINDOWS: michael@0: self.assert_(DEATH_TEST_STYLE_FLAG in output, output) michael@0: else: michael@0: self.assert_(DEATH_TEST_STYLE_FLAG not in output, output) michael@0: michael@0: def TestNonHelpFlag(self, flag): michael@0: """Verifies correct behavior when no help flag is specified. michael@0: michael@0: Verifies that when no help flag is specified, the tests are run michael@0: and the help message is not printed. michael@0: michael@0: Args: michael@0: flag: A flag to pass to the binary or None. michael@0: """ michael@0: michael@0: exit_code, output = RunWithFlag(flag) michael@0: self.assert_(exit_code != 0) michael@0: self.assert_(not HELP_REGEX.search(output), output) michael@0: michael@0: def testPrintsHelpWithFullFlag(self): michael@0: self.TestHelpFlag('--help') michael@0: michael@0: def testPrintsHelpWithShortFlag(self): michael@0: self.TestHelpFlag('-h') michael@0: michael@0: def testPrintsHelpWithQuestionFlag(self): michael@0: self.TestHelpFlag('-?') michael@0: michael@0: def testPrintsHelpWithWindowsStyleQuestionFlag(self): michael@0: self.TestHelpFlag('/?') michael@0: michael@0: def testPrintsHelpWithUnrecognizedGoogleTestFlag(self): michael@0: self.TestHelpFlag(UNKNOWN_FLAG) michael@0: michael@0: def testPrintsHelpWithIncorrectFlagStyle(self): michael@0: for incorrect_flag in INCORRECT_FLAG_VARIANTS: michael@0: self.TestHelpFlag(incorrect_flag) michael@0: michael@0: def testRunsTestsWithoutHelpFlag(self): michael@0: """Verifies that when no help flag is specified, the tests are run michael@0: and the help message is not printed.""" michael@0: michael@0: self.TestNonHelpFlag(None) michael@0: michael@0: def testRunsTestsWithGtestInternalFlag(self): michael@0: """Verifies that the tests are run and no help message is printed when michael@0: a flag starting with Google Test prefix and 'internal_' is supplied.""" michael@0: michael@0: self.TestNonHelpFlag(INTERNAL_FLAG_FOR_TESTING) michael@0: michael@0: michael@0: if __name__ == '__main__': michael@0: gtest_test_utils.Main()