gtest_help_test.py (googletest-release-1.11.0) | : | gtest_help_test.py (googletest-release-1.12.0) | ||
---|---|---|---|---|
skipping to change at line 42 | skipping to change at line 42 | |||
"""Tests the --help flag of Google C++ Testing and Mocking Framework. | """Tests the --help flag of Google C++ Testing and Mocking Framework. | |||
SYNOPSIS | SYNOPSIS | |||
gtest_help_test.py --build_dir=BUILD/DIR | gtest_help_test.py --build_dir=BUILD/DIR | |||
# where BUILD/DIR contains the built gtest_help_test_ file. | # where BUILD/DIR contains the built gtest_help_test_ file. | |||
gtest_help_test.py | gtest_help_test.py | |||
""" | """ | |||
import os | import os | |||
import re | import re | |||
import gtest_test_utils | import sys | |||
from googletest.test import gtest_test_utils | ||||
IS_LINUX = os.name == 'posix' and os.uname()[0] == 'Linux' | IS_LINUX = os.name == 'posix' and os.uname()[0] == 'Linux' | |||
IS_GNUHURD = os.name == 'posix' and os.uname()[0] == 'GNU' | ||||
IS_GNUKFREEBSD = os.name == 'posix' and os.uname()[0] == 'GNU/kFreeBSD' | IS_GNUKFREEBSD = os.name == 'posix' and os.uname()[0] == 'GNU/kFreeBSD' | |||
IS_OPENBSD = os.name == 'posix' and os.uname()[0] == 'OpenBSD' | ||||
IS_WINDOWS = os.name == 'nt' | IS_WINDOWS = os.name == 'nt' | |||
PROGRAM_PATH = gtest_test_utils.GetTestExecutablePath('gtest_help_test_') | PROGRAM_PATH = gtest_test_utils.GetTestExecutablePath('gtest_help_test_') | |||
FLAG_PREFIX = '--gtest_' | FLAG_PREFIX = '--gtest_' | |||
DEATH_TEST_STYLE_FLAG = FLAG_PREFIX + 'death_test_style' | DEATH_TEST_STYLE_FLAG = FLAG_PREFIX + 'death_test_style' | |||
STREAM_RESULT_TO_FLAG = FLAG_PREFIX + 'stream_result_to' | STREAM_RESULT_TO_FLAG = FLAG_PREFIX + 'stream_result_to' | |||
UNKNOWN_FLAG = FLAG_PREFIX + 'unknown_flag_for_testing' | UNKNOWN_GTEST_PREFIXED_FLAG = FLAG_PREFIX + 'unknown_flag_for_testing' | |||
LIST_TESTS_FLAG = FLAG_PREFIX + 'list_tests' | LIST_TESTS_FLAG = FLAG_PREFIX + 'list_tests' | |||
INCORRECT_FLAG_VARIANTS = [re.sub('^--', '-', LIST_TESTS_FLAG), | ||||
re.sub('^--', '/', LIST_TESTS_FLAG), | ||||
re.sub('_', '-', LIST_TESTS_FLAG)] | ||||
INTERNAL_FLAG_FOR_TESTING = FLAG_PREFIX + 'internal_flag_for_testing' | INTERNAL_FLAG_FOR_TESTING = FLAG_PREFIX + 'internal_flag_for_testing' | |||
SUPPORTS_DEATH_TESTS = "DeathTest" in gtest_test_utils.Subprocess( | SUPPORTS_DEATH_TESTS = "DeathTest" in gtest_test_utils.Subprocess( | |||
[PROGRAM_PATH, LIST_TESTS_FLAG]).output | [PROGRAM_PATH, LIST_TESTS_FLAG]).output | |||
HAS_ABSL_FLAGS = '--has_absl_flags' in sys.argv | ||||
# The help message must match this regex. | # The help message must match this regex. | |||
HELP_REGEX = re.compile( | HELP_REGEX = re.compile( | |||
FLAG_PREFIX + r'list_tests.*' + | FLAG_PREFIX + r'list_tests.*' + | |||
FLAG_PREFIX + r'filter=.*' + | FLAG_PREFIX + r'filter=.*' + | |||
FLAG_PREFIX + r'also_run_disabled_tests.*' + | FLAG_PREFIX + r'also_run_disabled_tests.*' + | |||
FLAG_PREFIX + r'repeat=.*' + | FLAG_PREFIX + r'repeat=.*' + | |||
FLAG_PREFIX + r'shuffle.*' + | FLAG_PREFIX + r'shuffle.*' + | |||
FLAG_PREFIX + r'random_seed=.*' + | FLAG_PREFIX + r'random_seed=.*' + | |||
FLAG_PREFIX + r'color=.*' + | FLAG_PREFIX + r'color=.*' + | |||
FLAG_PREFIX + r'brief.*' + | FLAG_PREFIX + r'brief.*' + | |||
skipping to change at line 109 | skipping to change at line 111 | |||
"""Verifies correct behavior when help flag is specified. | """Verifies correct behavior when help flag is specified. | |||
The right message must be printed and the tests must | The right message must be printed and the tests must | |||
skipped when the given flag is specified. | skipped when the given flag is specified. | |||
Args: | Args: | |||
flag: A flag to pass to the binary or None. | flag: A flag to pass to the binary or None. | |||
""" | """ | |||
exit_code, output = RunWithFlag(flag) | exit_code, output = RunWithFlag(flag) | |||
self.assertEquals(0, exit_code) | if HAS_ABSL_FLAGS: | |||
self.assert_(HELP_REGEX.search(output), output) | # The Abseil flags library prints the ProgramUsageMessage() with | |||
# --help and returns 1. | ||||
self.assertEqual(1, exit_code) | ||||
else: | ||||
self.assertEqual(0, exit_code) | ||||
self.assertTrue(HELP_REGEX.search(output), output) | ||||
if IS_LINUX or IS_GNUKFREEBSD: | if IS_LINUX or IS_GNUHURD or IS_GNUKFREEBSD or IS_OPENBSD: | |||
self.assert_(STREAM_RESULT_TO_FLAG in output, output) | self.assertIn(STREAM_RESULT_TO_FLAG, output) | |||
else: | else: | |||
self.assert_(STREAM_RESULT_TO_FLAG not in output, output) | self.assertNotIn(STREAM_RESULT_TO_FLAG, output) | |||
if SUPPORTS_DEATH_TESTS and not IS_WINDOWS: | if SUPPORTS_DEATH_TESTS and not IS_WINDOWS: | |||
self.assert_(DEATH_TEST_STYLE_FLAG in output, output) | self.assertIn(DEATH_TEST_STYLE_FLAG, output) | |||
else: | else: | |||
self.assert_(DEATH_TEST_STYLE_FLAG not in output, output) | self.assertNotIn(DEATH_TEST_STYLE_FLAG, output) | |||
def TestUnknownFlagWithAbseil(self, flag): | ||||
"""Verifies correct behavior when an unknown 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.assertEqual(1, exit_code) | ||||
self.assertIn('ERROR: Unknown command line flag', output) | ||||
def TestNonHelpFlag(self, flag): | def TestNonHelpFlag(self, flag): | |||
"""Verifies correct behavior when no help flag is specified. | """Verifies correct behavior when no help flag is specified. | |||
Verifies that when no help flag is specified, the tests are run | Verifies that when no help flag is specified, the tests are run | |||
and the help message is not printed. | and the help message is not printed. | |||
Args: | Args: | |||
flag: A flag to pass to the binary or None. | flag: A flag to pass to the binary or None. | |||
""" | """ | |||
exit_code, output = RunWithFlag(flag) | exit_code, output = RunWithFlag(flag) | |||
self.assert_(exit_code != 0) | self.assertNotEqual(exit_code, 0) | |||
self.assert_(not HELP_REGEX.search(output), output) | self.assertFalse(HELP_REGEX.search(output), output) | |||
def testPrintsHelpWithFullFlag(self): | def testPrintsHelpWithFullFlag(self): | |||
self.TestHelpFlag('--help') | self.TestHelpFlag('--help') | |||
def testPrintsHelpWithShortFlag(self): | ||||
self.TestHelpFlag('-h') | ||||
def testPrintsHelpWithQuestionFlag(self): | ||||
self.TestHelpFlag('-?') | ||||
def testPrintsHelpWithWindowsStyleQuestionFlag(self): | ||||
self.TestHelpFlag('/?') | ||||
def testPrintsHelpWithUnrecognizedGoogleTestFlag(self): | def testPrintsHelpWithUnrecognizedGoogleTestFlag(self): | |||
self.TestHelpFlag(UNKNOWN_FLAG) | # The behavior is slightly different when Abseil flags is | |||
# used. Abseil flags rejects all unknown flags, while the builtin | ||||
def testPrintsHelpWithIncorrectFlagStyle(self): | # GTest flags implementation interprets an unknown flag with a | |||
for incorrect_flag in INCORRECT_FLAG_VARIANTS: | # '--gtest_' prefix as a request for help. | |||
self.TestHelpFlag(incorrect_flag) | if HAS_ABSL_FLAGS: | |||
self.TestUnknownFlagWithAbseil(UNKNOWN_GTEST_PREFIXED_FLAG) | ||||
else: | ||||
self.TestHelpFlag(UNKNOWN_GTEST_PREFIXED_FLAG) | ||||
def testRunsTestsWithoutHelpFlag(self): | def testRunsTestsWithoutHelpFlag(self): | |||
"""Verifies that when no help flag is specified, the tests are run | """Verifies that when no help flag is specified, the tests are run | |||
and the help message is not printed.""" | and the help message is not printed.""" | |||
self.TestNonHelpFlag(None) | self.TestNonHelpFlag(None) | |||
def testRunsTestsWithGtestInternalFlag(self): | def testRunsTestsWithGtestInternalFlag(self): | |||
"""Verifies that the tests are run and no help message is printed when | """Verifies that the tests are run and no help message is printed when | |||
a flag starting with Google Test prefix and 'internal_' is supplied.""" | a flag starting with Google Test prefix and 'internal_' is supplied.""" | |||
self.TestNonHelpFlag(INTERNAL_FLAG_FOR_TESTING) | self.TestNonHelpFlag(INTERNAL_FLAG_FOR_TESTING) | |||
if __name__ == '__main__': | if __name__ == '__main__': | |||
if '--has_absl_flags' in sys.argv: | ||||
sys.argv.remove('--has_absl_flags') | ||||
gtest_test_utils.Main() | gtest_test_utils.Main() | |||
End of changes. 15 change blocks. | ||||
28 lines changed or deleted | 45 lines changed or added |