From 6bfc4b2bd378940fa006bd32b9667ad4137d8f15 Mon Sep 17 00:00:00 2001 From: vladlosev Date: Thu, 22 Oct 2009 01:22:29 +0000 Subject: [PATCH] Prints help when encountering unrecognized Google Test flags. --- include/gtest/internal/gtest-port.h | 1 + src/gtest.cc | 32 +++++++++++++++++++- test/gtest_help_test.py | 46 +++++++++++++++++++++++++---- 3 files changed, 73 insertions(+), 6 deletions(-) diff --git a/include/gtest/internal/gtest-port.h b/include/gtest/internal/gtest-port.h index ee97881f..4175fcc1 100644 --- a/include/gtest/internal/gtest-port.h +++ b/include/gtest/internal/gtest-port.h @@ -169,6 +169,7 @@ #define GTEST_DEV_EMAIL_ "googletestframework@@googlegroups.com" #define GTEST_FLAG_PREFIX_ "gtest_" +#define GTEST_FLAG_PREFIX_DASH_ "gtest-" #define GTEST_FLAG_PREFIX_UPPER_ "GTEST_" #define GTEST_NAME_ "Google Test" #define GTEST_PROJECT_URL_ "http://code.google.com/p/googletest/" diff --git a/src/gtest.cc b/src/gtest.cc index 5efb5baa..ca27b313 100644 --- a/src/gtest.cc +++ b/src/gtest.cc @@ -4444,6 +4444,33 @@ bool ParseStringFlag(const char* str, const char* flag, String* value) { return true; } +// Determines whether a string pointed by *str has the prefix parameter as +// its prefix and advances it to point past the prefix if it does. +bool SkipPrefix(const char* prefix, const char** str) { + const int prefix_len = strlen(prefix); + + if (strncmp(*str, prefix, prefix_len) != 0) + return false; + + *str += prefix_len; + return true; +} + +// Determines whether a string has a prefix that Google Test uses for its +// flags, i.e., starts with GTEST_FLAG_PREFIX_ or GTEST_FLAG_PREFIX_DASH_. +// If Google Test detects that a command line flag has its prefix but is not +// recognized, it will print its help message. Flags starting with +// GTEST_INTERNAL_PREFIX_ followed by "internal_" are considered Google Test +// internal flags and do not trigger the help message. +bool HasGoogleTestFlagPrefix(const char* str) { + return (SkipPrefix("--", &str) || + SkipPrefix("-", &str) || + SkipPrefix("/", &str)) && + !SkipPrefix(GTEST_FLAG_PREFIX_ "internal_", &str) && + (SkipPrefix(GTEST_FLAG_PREFIX_, &str) || + SkipPrefix(GTEST_FLAG_PREFIX_DASH_, &str)); +} + // Prints a string containing code-encoded text. The following escape // sequences can be used in the string to control the text color: // @@ -4601,7 +4628,10 @@ void ParseGoogleTestFlagsOnlyImpl(int* argc, CharType** argv) { // an element. i--; } else if (arg_string == "--help" || arg_string == "-h" || - arg_string == "-?" || arg_string == "/?") { + arg_string == "-?" || arg_string == "/?" || + HasGoogleTestFlagPrefix(arg)) { + // Both help flag and unrecognized Google Test flags (excluding + // internal ones) trigger help display. g_help_flag = true; } } diff --git a/test/gtest_help_test.py b/test/gtest_help_test.py index 91081ad3..7883c1c5 100755 --- a/test/gtest_help_test.py +++ b/test/gtest_help_test.py @@ -50,6 +50,11 @@ PROGRAM_PATH = gtest_test_utils.GetTestExecutablePath('gtest_help_test_') FLAG_PREFIX = '--gtest_' CATCH_EXCEPTIONS_FLAG = FLAG_PREFIX + 'catch_exceptions' DEATH_TEST_STYLE_FLAG = FLAG_PREFIX + 'death_test_style' +UNKNOWN_FLAG = FLAG_PREFIX + 'unknown_flag_for_testing' +INCORRECT_FLAG_VARIANTS = [re.sub('^--', '-', DEATH_TEST_STYLE_FLAG), + re.sub('^--', '/', DEATH_TEST_STYLE_FLAG), + re.sub('_', '-', DEATH_TEST_STYLE_FLAG)] +INTERNAL_FLAG_FOR_TESTING = FLAG_PREFIX + 'internal_flag_for_testing' # The help message must match this regex. HELP_REGEX = re.compile( @@ -88,8 +93,14 @@ class GTestHelpTest(gtest_test_utils.TestCase): """Tests the --help flag and its equivalent forms.""" def TestHelpFlag(self, flag): - """Verifies that the right message is printed and the tests are - skipped when the given flag is specified.""" + """Verifies correct behavior when help flag is specified. + + The right message must be printed and the tests must + skipped when the given flag is specified. + + Args: + flag: A flag to pass to the binary or None. + """ exit_code, output = RunWithFlag(flag) self.assertEquals(0, exit_code) @@ -101,6 +112,20 @@ class GTestHelpTest(gtest_test_utils.TestCase): self.assert_(CATCH_EXCEPTIONS_FLAG not in output, output) self.assert_(DEATH_TEST_STYLE_FLAG in output, output) + def TestNonHelpFlag(self, flag): + """Verifies correct behavior when no help flag is specified. + + Verifies that when no help flag is specified, the tests are run + and the help message is not printed. + + Args: + flag: A flag to pass to the binary or None. + """ + + exit_code, output = RunWithFlag(flag) + self.assert_(exit_code != 0) + self.assert_(not HELP_REGEX.search(output), output) + def testPrintsHelpWithFullFlag(self): self.TestHelpFlag('--help') @@ -113,13 +138,24 @@ class GTestHelpTest(gtest_test_utils.TestCase): def testPrintsHelpWithWindowsStyleQuestionFlag(self): self.TestHelpFlag('/?') + def testPrintsHelpWithUnrecognizedGoogleTestFlag(self): + self.TestHelpFlag(UNKNOWN_FLAG) + + def testPrintsHelpWithIncorrectFlagStyle(self): + for incorrect_flag in INCORRECT_FLAG_VARIANTS: + self.TestHelpFlag(incorrect_flag) + def testRunsTestsWithoutHelpFlag(self): """Verifies that when no help flag is specified, the tests are run and the help message is not printed.""" - exit_code, output = RunWithFlag(None) - self.assert_(exit_code != 0) - self.assert_(not HELP_REGEX.search(output), output) + self.TestNonHelpFlag(None) + + def testRunsTestsWithGtestInternalFlag(self): + """Verifies that the tests are run and no help message is printed when + a flag starting with Google Test prefix and 'internal_' is supplied.""" + + self.TestNonHelpFlag(INTERNAL_FLAG_FOR_TESTING) if __name__ == '__main__':