From ea847bfccba13a0b899db543c35ee768fd76edb0 Mon Sep 17 00:00:00 2001 From: Dan Kegel Date: Mon, 24 Feb 2020 21:01:27 -0800 Subject: [PATCH] Document, fix, and test GTEST_FLAGFILE. Fixes #2726. --- googletest/docs/advanced.md | 4 ++++ googletest/src/gtest.cc | 13 +++++++++++ googletest/test/googletest-env-var-test.py | 25 ++++++++++++++++++++++ 3 files changed, 42 insertions(+) diff --git a/googletest/docs/advanced.md b/googletest/docs/advanced.md index 5677643d..364b21d2 100644 --- a/googletest/docs/advanced.md +++ b/googletest/docs/advanced.md @@ -2062,6 +2062,10 @@ with the `--help` flag. You can also use `-h`, `-?`, or `/?` for short. If an option is specified both by an environment variable and by a flag, the latter takes precedence. +If you set the `GTEST_FLAGFILE` environment variable or the `--gtest_flagfile` +flag to a filename, googletest will read more options from that file. +(This can be handy when running a test harness you'd rather not edit.) + ### Selecting Tests #### Listing Test Names diff --git a/googletest/src/gtest.cc b/googletest/src/gtest.cc index 095778e6..40fd2a38 100644 --- a/googletest/src/gtest.cc +++ b/googletest/src/gtest.cc @@ -6172,6 +6172,11 @@ static void LoadFlagsFromFile(const std::string& path) { // instantiated to either char or wchar_t. template void ParseGoogleTestFlagsOnlyImpl(int* argc, CharType** argv) { + +#if GTEST_USE_OWN_FLAGFILE_FLAG_ + bool flagfile_used = false; +#endif // GTEST_USE_OWN_FLAGFILE_FLAG_ + for (int i = 1; i < *argc; i++) { const std::string arg_string = StreamableToString(argv[i]); const char* const arg = arg_string.c_str(); @@ -6187,6 +6192,7 @@ void ParseGoogleTestFlagsOnlyImpl(int* argc, CharType** argv) { } else if (ParseStringFlag(arg, kFlagfileFlag, >EST_FLAG(flagfile))) { LoadFlagsFromFile(GTEST_FLAG(flagfile)); remove_flag = true; + flagfile_used = true; #endif // GTEST_USE_OWN_FLAGFILE_FLAG_ } else if (arg_string == "--help" || arg_string == "-h" || arg_string == "-?" || arg_string == "/?" || @@ -6214,6 +6220,13 @@ void ParseGoogleTestFlagsOnlyImpl(int* argc, CharType** argv) { } } +#if GTEST_USE_OWN_FLAGFILE_FLAG_ + // If --gtest_flagfile not given, obey GTEST_FLAGFILE if set + if (!g_help_flag && !flagfile_used && GTEST_FLAG(flagfile) != "") { + LoadFlagsFromFile(GTEST_FLAG(flagfile)); + } +#endif // GTEST_USE_OWN_FLAGFILE_FLAG_ + if (g_help_flag) { // We print the help here instead of in RUN_ALL_TESTS(), as the // latter may not be called at all if the user is using Google diff --git a/googletest/test/googletest-env-var-test.py b/googletest/test/googletest-env-var-test.py index 2f0e406a..eb4742d0 100755 --- a/googletest/test/googletest-env-var-test.py +++ b/googletest/test/googletest-env-var-test.py @@ -77,6 +77,18 @@ def TestFlag(flag, test_val, default_val): SetEnvVar(env_var, None) AssertEq(default_val, GetFlag(flag)) +def TestFlagfileEnv(flag, test_val, default_val): + """Verifies that the given flag is affected by the GTEST_FLAGFILE env var.""" + + file_path = os.path.join(gtest_test_utils.GetTempDir(), + 'flagfile.tmp') + with open(file_path, 'wb') as f: + f.write('--gtest_%s=%s\n' % (flag, test_val)) + env_var = 'GTEST_FLAGFILE' + SetEnvVar(env_var, file_path) + AssertEq(test_val, GetFlag(flag)) + SetEnvVar(env_var, None) + AssertEq(default_val, GetFlag(flag)) class GTestEnvVarTest(gtest_test_utils.TestCase): @@ -98,6 +110,19 @@ class GTestEnvVarTest(gtest_test_utils.TestCase): TestFlag('death_test_use_fork', '1', '0') TestFlag('stack_trace_depth', '0', '100') + def testFlagfileEnvAffectsFlag(self): + """Tests that flagfile environment variable should affect the corresponding flag.""" + + TestFlagfileEnv('break_on_failure', '1', '0') + TestFlagfileEnv('color', 'yes', 'auto') + TestFlagfileEnv('filter', 'FooTest.Bar', '*') + SetEnvVar('XML_OUTPUT_FILE', None) # For 'output' test + TestFlagfileEnv('output', 'xml:tmp/foo.xml', '') + TestFlagfileEnv('print_time', '0', '1') + TestFlagfileEnv('repeat', '999', '1') + TestFlagfileEnv('throw_on_failure', '1', '0') + TestFlagfileEnv('death_test_style', 'threadsafe', 'fast') + TestFlagfileEnv('catch_exceptions', '0', '1') def testXmlOutputFile(self): """Tests that $XML_OUTPUT_FILE affects the output flag."""