Add support for --gtest_flagfile
This commit is contained in:
+88
-35
@@ -295,6 +295,11 @@ GTEST_DEFINE_bool_(
|
||||
"if exceptions are enabled or exit the program with a non-zero code "
|
||||
"otherwise.");
|
||||
|
||||
GTEST_DEFINE_string_(
|
||||
flagfile,
|
||||
internal::StringFromGTestEnv("flagfile", ""),
|
||||
"This flag specifies the flagfile to read command-line flags from.");
|
||||
|
||||
namespace internal {
|
||||
|
||||
// Generates a random number from [0, range), using a Linear
|
||||
@@ -905,6 +910,23 @@ static void StreamWideCharsToMessage(const wchar_t* wstr, size_t length,
|
||||
|
||||
#endif // GTEST_HAS_STD_WSTRING || GTEST_HAS_GLOBAL_WSTRING
|
||||
|
||||
void SplitString(const ::std::string& str, char delimiter,
|
||||
::std::vector< ::std::string>* dest) {
|
||||
::std::vector< ::std::string> parsed;
|
||||
::std::string::size_type pos = 0;
|
||||
while (::testing::internal::AlwaysTrue()) {
|
||||
const ::std::string::size_type colon = str.find(delimiter, pos);
|
||||
if (colon == ::std::string::npos) {
|
||||
parsed.push_back(str.substr(pos));
|
||||
break;
|
||||
} else {
|
||||
parsed.push_back(str.substr(pos, colon - pos));
|
||||
pos = colon + 1;
|
||||
}
|
||||
}
|
||||
dest->swap(parsed);
|
||||
}
|
||||
|
||||
} // namespace internal
|
||||
|
||||
// Constructs an empty Message.
|
||||
@@ -5178,6 +5200,56 @@ static const char kColorEncodedHelpMessage[] =
|
||||
"(not one in your own code or tests), please report it to\n"
|
||||
"@G<" GTEST_DEV_EMAIL_ ">@D.\n";
|
||||
|
||||
bool ParseGoogleTestFlag(const char* const arg) {
|
||||
return ParseBoolFlag(arg, kAlsoRunDisabledTestsFlag,
|
||||
>EST_FLAG(also_run_disabled_tests)) ||
|
||||
ParseBoolFlag(arg, kBreakOnFailureFlag,
|
||||
>EST_FLAG(break_on_failure)) ||
|
||||
ParseBoolFlag(arg, kCatchExceptionsFlag,
|
||||
>EST_FLAG(catch_exceptions)) ||
|
||||
ParseStringFlag(arg, kColorFlag, >EST_FLAG(color)) ||
|
||||
ParseStringFlag(arg, kDeathTestStyleFlag,
|
||||
>EST_FLAG(death_test_style)) ||
|
||||
ParseBoolFlag(arg, kDeathTestUseFork,
|
||||
>EST_FLAG(death_test_use_fork)) ||
|
||||
ParseStringFlag(arg, kFilterFlag, >EST_FLAG(filter)) ||
|
||||
ParseStringFlag(arg, kInternalRunDeathTestFlag,
|
||||
>EST_FLAG(internal_run_death_test)) ||
|
||||
ParseBoolFlag(arg, kListTestsFlag, >EST_FLAG(list_tests)) ||
|
||||
ParseStringFlag(arg, kOutputFlag, >EST_FLAG(output)) ||
|
||||
ParseBoolFlag(arg, kPrintTimeFlag, >EST_FLAG(print_time)) ||
|
||||
ParseInt32Flag(arg, kRandomSeedFlag, >EST_FLAG(random_seed)) ||
|
||||
ParseInt32Flag(arg, kRepeatFlag, >EST_FLAG(repeat)) ||
|
||||
ParseBoolFlag(arg, kShuffleFlag, >EST_FLAG(shuffle)) ||
|
||||
ParseInt32Flag(arg, kStackTraceDepthFlag,
|
||||
>EST_FLAG(stack_trace_depth)) ||
|
||||
ParseStringFlag(arg, kStreamResultToFlag,
|
||||
>EST_FLAG(stream_result_to)) ||
|
||||
ParseBoolFlag(arg, kThrowOnFailureFlag,
|
||||
>EST_FLAG(throw_on_failure));
|
||||
}
|
||||
|
||||
void LoadFlagsFromFile(const std::string& path) {
|
||||
FILE* flagfile = posix::FOpen(path.c_str(), "r");
|
||||
if (!flagfile) {
|
||||
fprintf(stderr,
|
||||
"Unable to open file \"%s\"\n",
|
||||
GTEST_FLAG(flagfile).c_str());
|
||||
fflush(stderr);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
std::string contents(ReadEntireFile(flagfile));
|
||||
posix::FClose(flagfile);
|
||||
std::vector<std::string> lines;
|
||||
SplitString(contents, '\n', &lines);
|
||||
for (size_t i = 0; i < lines.size(); ++i) {
|
||||
if (lines[i].empty())
|
||||
continue;
|
||||
if (!ParseGoogleTestFlag(lines[i].c_str()))
|
||||
g_help_flag = true;
|
||||
}
|
||||
}
|
||||
|
||||
// Parses the command line for Google Test flags, without initializing
|
||||
// other parts of Google Test. The type parameter CharType can be
|
||||
// instantiated to either char or wchar_t.
|
||||
@@ -5191,35 +5263,22 @@ void ParseGoogleTestFlagsOnlyImpl(int* argc, CharType** argv) {
|
||||
using internal::ParseInt32Flag;
|
||||
using internal::ParseStringFlag;
|
||||
|
||||
// Do we see a Google Test flag?
|
||||
if (ParseBoolFlag(arg, kAlsoRunDisabledTestsFlag,
|
||||
>EST_FLAG(also_run_disabled_tests)) ||
|
||||
ParseBoolFlag(arg, kBreakOnFailureFlag,
|
||||
>EST_FLAG(break_on_failure)) ||
|
||||
ParseBoolFlag(arg, kCatchExceptionsFlag,
|
||||
>EST_FLAG(catch_exceptions)) ||
|
||||
ParseStringFlag(arg, kColorFlag, >EST_FLAG(color)) ||
|
||||
ParseStringFlag(arg, kDeathTestStyleFlag,
|
||||
>EST_FLAG(death_test_style)) ||
|
||||
ParseBoolFlag(arg, kDeathTestUseFork,
|
||||
>EST_FLAG(death_test_use_fork)) ||
|
||||
ParseStringFlag(arg, kFilterFlag, >EST_FLAG(filter)) ||
|
||||
ParseStringFlag(arg, kInternalRunDeathTestFlag,
|
||||
>EST_FLAG(internal_run_death_test)) ||
|
||||
ParseBoolFlag(arg, kListTestsFlag, >EST_FLAG(list_tests)) ||
|
||||
ParseStringFlag(arg, kOutputFlag, >EST_FLAG(output)) ||
|
||||
ParseBoolFlag(arg, kPrintTimeFlag, >EST_FLAG(print_time)) ||
|
||||
ParseInt32Flag(arg, kRandomSeedFlag, >EST_FLAG(random_seed)) ||
|
||||
ParseInt32Flag(arg, kRepeatFlag, >EST_FLAG(repeat)) ||
|
||||
ParseBoolFlag(arg, kShuffleFlag, >EST_FLAG(shuffle)) ||
|
||||
ParseInt32Flag(arg, kStackTraceDepthFlag,
|
||||
>EST_FLAG(stack_trace_depth)) ||
|
||||
ParseStringFlag(arg, kStreamResultToFlag,
|
||||
>EST_FLAG(stream_result_to)) ||
|
||||
ParseBoolFlag(arg, kThrowOnFailureFlag,
|
||||
>EST_FLAG(throw_on_failure))
|
||||
) {
|
||||
// Yes. Shift the remainder of the argv list left by one. Note
|
||||
bool remove_flag = false;
|
||||
if (ParseGoogleTestFlag(arg)) {
|
||||
remove_flag = true;
|
||||
} else if (ParseStringFlag(arg, kFlagfileFlag, >EST_FLAG(flagfile))) {
|
||||
LoadFlagsFromFile(GTEST_FLAG(flagfile));
|
||||
remove_flag = true;
|
||||
} else if (arg_string == "--help" || arg_string == "-h" ||
|
||||
arg_string == "-?" || arg_string == "/?" ||
|
||||
HasGoogleTestFlagPrefix(arg)) {
|
||||
// Both help flag and unrecognized Google Test flags (excluding
|
||||
// internal ones) trigger help display.
|
||||
g_help_flag = true;
|
||||
}
|
||||
|
||||
if (remove_flag) {
|
||||
// Shift the remainder of the argv list left by one. Note
|
||||
// that argv has (*argc + 1) elements, the last one always being
|
||||
// NULL. The following loop moves the trailing NULL element as
|
||||
// well.
|
||||
@@ -5233,12 +5292,6 @@ void ParseGoogleTestFlagsOnlyImpl(int* argc, CharType** argv) {
|
||||
// We also need to decrement the iterator as we just removed
|
||||
// an element.
|
||||
i--;
|
||||
} else if (arg_string == "--help" || arg_string == "-h" ||
|
||||
arg_string == "-?" || arg_string == "/?" ||
|
||||
HasGoogleTestFlagPrefix(arg)) {
|
||||
// Both help flag and unrecognized Google Test flags (excluding
|
||||
// internal ones) trigger help display.
|
||||
g_help_flag = true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user