Use the Abseil flags library when Abseil is present
When built with `--define=absl=1` under Bazel, GoogleTest
flags use ABSL_FLAG instead of GoogleTest's own implementation.
There are some minor behavior differences in this mode.
The most notable difference is that unrecognized flags result
in a flag parsing error, and are not returned to the user though
a modified argc/argv, unless they appear after the positional
argument delimiter ("--").
For example, to pass a non-Abseil flag, you would have to do
./mytest --gtest_color=false -- --myflag=myvalue
The documentation at https://abseil.io/docs/cpp/guides/flags
may be helpful in understanding the behavior.
There are some other minor differences. For example,
passing --help results in the program returning 1 instead of 0.
https://github.com/google/googletest/issues/3646
PiperOrigin-RevId: 439312700
Change-Id: Id696a25f50f24a5b1785c45ca8fa59794f86fd5c
This commit is contained in:
committed by
Copybara-Service
parent
af29db7ec2
commit
25dcdc7e8b
@@ -174,6 +174,10 @@ py_test(
|
||||
name = "gtest_help_test",
|
||||
size = "small",
|
||||
srcs = ["gtest_help_test.py"],
|
||||
args = select({
|
||||
"//:has_absl": ["--has_absl_flags"],
|
||||
"//conditions:default": [],
|
||||
}),
|
||||
data = [":gtest_help_test_"],
|
||||
deps = [":gtest_test_utils"],
|
||||
)
|
||||
|
||||
@@ -39,6 +39,7 @@ SYNOPSIS
|
||||
|
||||
import os
|
||||
import re
|
||||
import sys
|
||||
from googletest.test import gtest_test_utils
|
||||
|
||||
|
||||
@@ -52,16 +53,15 @@ PROGRAM_PATH = gtest_test_utils.GetTestExecutablePath('gtest_help_test_')
|
||||
FLAG_PREFIX = '--gtest_'
|
||||
DEATH_TEST_STYLE_FLAG = FLAG_PREFIX + 'death_test_style'
|
||||
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'
|
||||
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'
|
||||
|
||||
SUPPORTS_DEATH_TESTS = "DeathTest" in gtest_test_utils.Subprocess(
|
||||
[PROGRAM_PATH, LIST_TESTS_FLAG]).output
|
||||
|
||||
HAS_ABSL_FLAGS = '--has_absl_flags' in sys.argv
|
||||
|
||||
# The help message must match this regex.
|
||||
HELP_REGEX = re.compile(
|
||||
FLAG_PREFIX + r'list_tests.*' +
|
||||
@@ -111,18 +111,37 @@ class GTestHelpTest(gtest_test_utils.TestCase):
|
||||
"""
|
||||
|
||||
exit_code, output = RunWithFlag(flag)
|
||||
self.assertEquals(0, exit_code)
|
||||
self.assert_(HELP_REGEX.search(output), output)
|
||||
if HAS_ABSL_FLAGS:
|
||||
# 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_GNUHURD or IS_GNUKFREEBSD or IS_OPENBSD:
|
||||
self.assert_(STREAM_RESULT_TO_FLAG in output, output)
|
||||
self.assertIn(STREAM_RESULT_TO_FLAG, output)
|
||||
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:
|
||||
self.assert_(DEATH_TEST_STYLE_FLAG in output, output)
|
||||
self.assertIn(DEATH_TEST_STYLE_FLAG, output)
|
||||
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):
|
||||
"""Verifies correct behavior when no help flag is specified.
|
||||
@@ -135,27 +154,21 @@ class GTestHelpTest(gtest_test_utils.TestCase):
|
||||
"""
|
||||
|
||||
exit_code, output = RunWithFlag(flag)
|
||||
self.assert_(exit_code != 0)
|
||||
self.assert_(not HELP_REGEX.search(output), output)
|
||||
self.assertNotEqual(exit_code, 0)
|
||||
self.assertFalse(HELP_REGEX.search(output), output)
|
||||
|
||||
def testPrintsHelpWithFullFlag(self):
|
||||
self.TestHelpFlag('--help')
|
||||
|
||||
def testPrintsHelpWithShortFlag(self):
|
||||
self.TestHelpFlag('-h')
|
||||
|
||||
def testPrintsHelpWithQuestionFlag(self):
|
||||
self.TestHelpFlag('-?')
|
||||
|
||||
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)
|
||||
# The behavior is slightly different when Abseil flags is
|
||||
# used. Abseil flags rejects all unknown flags, while the builtin
|
||||
# GTest flags implementation interprets an unknown flag with a
|
||||
# '--gtest_' prefix as a request for help.
|
||||
if HAS_ABSL_FLAGS:
|
||||
self.TestUnknownFlagWithAbseil(UNKNOWN_GTEST_PREFIXED_FLAG)
|
||||
else:
|
||||
self.TestHelpFlag(UNKNOWN_GTEST_PREFIXED_FLAG)
|
||||
|
||||
def testRunsTestsWithoutHelpFlag(self):
|
||||
"""Verifies that when no help flag is specified, the tests are run
|
||||
@@ -171,4 +184,6 @@ class GTestHelpTest(gtest_test_utils.TestCase):
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
if '--has_absl_flags' in sys.argv:
|
||||
sys.argv.remove('--has_absl_flags')
|
||||
gtest_test_utils.Main()
|
||||
|
||||
@@ -5770,15 +5770,6 @@ TEST_F(ParseFlagsTest, FailFast) {
|
||||
GTEST_TEST_PARSING_FLAGS_(argv, argv2, Flags::FailFast(true), false);
|
||||
}
|
||||
|
||||
// Tests parsing a bad --gtest_filter flag.
|
||||
TEST_F(ParseFlagsTest, FilterBad) {
|
||||
const char* argv[] = {"foo.exe", "--gtest_filter", nullptr};
|
||||
|
||||
const char* argv2[] = {"foo.exe", "--gtest_filter", nullptr};
|
||||
|
||||
GTEST_TEST_PARSING_FLAGS_(argv, argv2, Flags::Filter(""), true);
|
||||
}
|
||||
|
||||
// Tests parsing an empty --gtest_filter flag.
|
||||
TEST_F(ParseFlagsTest, FilterEmpty) {
|
||||
const char* argv[] = {"foo.exe", "--gtest_filter=", nullptr};
|
||||
@@ -5931,15 +5922,6 @@ TEST_F(ParseFlagsTest, ListTestsFalse_F) {
|
||||
GTEST_TEST_PARSING_FLAGS_(argv, argv2, Flags::ListTests(false), false);
|
||||
}
|
||||
|
||||
// Tests parsing --gtest_output (invalid).
|
||||
TEST_F(ParseFlagsTest, OutputEmpty) {
|
||||
const char* argv[] = {"foo.exe", "--gtest_output", nullptr};
|
||||
|
||||
const char* argv2[] = {"foo.exe", "--gtest_output", nullptr};
|
||||
|
||||
GTEST_TEST_PARSING_FLAGS_(argv, argv2, Flags(), true);
|
||||
}
|
||||
|
||||
// Tests parsing --gtest_output=xml
|
||||
TEST_F(ParseFlagsTest, OutputXml) {
|
||||
const char* argv[] = {"foo.exe", "--gtest_output=xml", nullptr};
|
||||
@@ -6179,6 +6161,58 @@ TEST_F(ParseFlagsTest, ThrowOnFailureTrue) {
|
||||
GTEST_TEST_PARSING_FLAGS_(argv, argv2, Flags::ThrowOnFailure(true), false);
|
||||
}
|
||||
|
||||
// Tests parsing a bad --gtest_filter flag.
|
||||
TEST_F(ParseFlagsTest, FilterBad) {
|
||||
const char* argv[] = {"foo.exe", "--gtest_filter", nullptr};
|
||||
|
||||
const char* argv2[] = {"foo.exe", "--gtest_filter", nullptr};
|
||||
|
||||
#if GTEST_HAS_ABSL && GTEST_HAS_DEATH_TEST
|
||||
// Invalid flag arguments are a fatal error when using the Abseil Flags.
|
||||
EXPECT_EXIT(GTEST_TEST_PARSING_FLAGS_(argv, argv2, Flags::Filter(""), true),
|
||||
testing::ExitedWithCode(1),
|
||||
"ERROR: Missing the value for the flag 'gtest_filter'");
|
||||
#elif !GTEST_HAS_ABSL
|
||||
GTEST_TEST_PARSING_FLAGS_(argv, argv2, Flags::Filter(""), true);
|
||||
#else
|
||||
static_cast<void>(argv);
|
||||
static_cast<void>(argv2);
|
||||
#endif
|
||||
}
|
||||
|
||||
// Tests parsing --gtest_output (invalid).
|
||||
TEST_F(ParseFlagsTest, OutputEmpty) {
|
||||
const char* argv[] = {"foo.exe", "--gtest_output", nullptr};
|
||||
|
||||
const char* argv2[] = {"foo.exe", "--gtest_output", nullptr};
|
||||
|
||||
#if GTEST_HAS_ABSL && GTEST_HAS_DEATH_TEST
|
||||
// Invalid flag arguments are a fatal error when using the Abseil Flags.
|
||||
EXPECT_EXIT(GTEST_TEST_PARSING_FLAGS_(argv, argv2, Flags(), true),
|
||||
testing::ExitedWithCode(1),
|
||||
"ERROR: Missing the value for the flag 'gtest_output'");
|
||||
#elif !GTEST_HAS_ABSL
|
||||
GTEST_TEST_PARSING_FLAGS_(argv, argv2, Flags(), true);
|
||||
#else
|
||||
static_cast<void>(argv);
|
||||
static_cast<void>(argv2);
|
||||
#endif
|
||||
}
|
||||
|
||||
#if GTEST_HAS_ABSL
|
||||
TEST_F(ParseFlagsTest, AbseilPositionalFlags) {
|
||||
const char* argv[] = {"foo.exe", "--gtest_throw_on_failure=1", "--",
|
||||
"--other_flag", nullptr};
|
||||
|
||||
// When using Abseil flags, it should be possible to pass flags not recognized
|
||||
// using "--" to delimit positional arguments. These flags should be returned
|
||||
// though argv.
|
||||
const char* argv2[] = {"foo.exe", "--other_flag", nullptr};
|
||||
|
||||
GTEST_TEST_PARSING_FLAGS_(argv, argv2, Flags::ThrowOnFailure(true), false);
|
||||
}
|
||||
#endif
|
||||
|
||||
#if GTEST_OS_WINDOWS
|
||||
// Tests parsing wide strings.
|
||||
TEST_F(ParseFlagsTest, WideStrings) {
|
||||
|
||||
Reference in New Issue
Block a user