Document, fix, and test GTEST_FLAGFILE. Fixes #2726.
This commit is contained in:
		
							parent
							
								
									23b2a3b1cf
								
							
						
					
					
						commit
						ea847bfccb
					
				| @ -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 | If an option is specified both by an environment variable and by a flag, the | ||||||
| latter takes precedence. | 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 | ### Selecting Tests | ||||||
| 
 | 
 | ||||||
| #### Listing Test Names | #### Listing Test Names | ||||||
|  | |||||||
| @ -6172,6 +6172,11 @@ static void LoadFlagsFromFile(const std::string& path) { | |||||||
| // instantiated to either char or wchar_t.
 | // instantiated to either char or wchar_t.
 | ||||||
| template <typename CharType> | template <typename CharType> | ||||||
| void ParseGoogleTestFlagsOnlyImpl(int* argc, CharType** argv) { | 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++) { |   for (int i = 1; i < *argc; i++) { | ||||||
|     const std::string arg_string = StreamableToString(argv[i]); |     const std::string arg_string = StreamableToString(argv[i]); | ||||||
|     const char* const arg = arg_string.c_str(); |     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))) { |     } else if (ParseStringFlag(arg, kFlagfileFlag, >EST_FLAG(flagfile))) { | ||||||
|       LoadFlagsFromFile(GTEST_FLAG(flagfile)); |       LoadFlagsFromFile(GTEST_FLAG(flagfile)); | ||||||
|       remove_flag = true; |       remove_flag = true; | ||||||
|  |       flagfile_used = true; | ||||||
| #endif  // GTEST_USE_OWN_FLAGFILE_FLAG_
 | #endif  // GTEST_USE_OWN_FLAGFILE_FLAG_
 | ||||||
|     } else if (arg_string == "--help" || arg_string == "-h" || |     } else if (arg_string == "--help" || arg_string == "-h" || | ||||||
|                arg_string == "-?" || arg_string == "/?" || |                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) { |   if (g_help_flag) { | ||||||
|     // We print the help here instead of in RUN_ALL_TESTS(), as the
 |     // 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
 |     // latter may not be called at all if the user is using Google
 | ||||||
|  | |||||||
| @ -77,6 +77,18 @@ def TestFlag(flag, test_val, default_val): | |||||||
|   SetEnvVar(env_var, None) |   SetEnvVar(env_var, None) | ||||||
|   AssertEq(default_val, GetFlag(flag)) |   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): | class GTestEnvVarTest(gtest_test_utils.TestCase): | ||||||
| 
 | 
 | ||||||
| @ -98,6 +110,19 @@ class GTestEnvVarTest(gtest_test_utils.TestCase): | |||||||
|       TestFlag('death_test_use_fork', '1', '0') |       TestFlag('death_test_use_fork', '1', '0') | ||||||
|       TestFlag('stack_trace_depth', '0', '100') |       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): |   def testXmlOutputFile(self): | ||||||
|     """Tests that $XML_OUTPUT_FILE affects the output flag.""" |     """Tests that $XML_OUTPUT_FILE affects the output flag.""" | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user