diff --git a/googletest/include/gtest/internal/gtest-port.h b/googletest/include/gtest/internal/gtest-port.h index 7d6e4658..236aaea0 100644 --- a/googletest/include/gtest/internal/gtest-port.h +++ b/googletest/include/gtest/internal/gtest-port.h @@ -2547,9 +2547,9 @@ bool ParseInt32(const Message& src_text, const char* str, Int32* value); bool BoolFromGTestEnv(const char* flag, bool default_val); GTEST_API_ Int32 Int32FromGTestEnv(const char* flag, Int32 default_val); const char* StringFromGTestEnv(const char* flag, const char* default_val); +std::string OutputFromGTestEnv(const char * default_val); } // namespace internal } // namespace testing #endif // GTEST_INCLUDE_GTEST_INTERNAL_GTEST_PORT_H_ - diff --git a/googletest/src/gtest-port.cc b/googletest/src/gtest-port.cc index 0162fac4..05ffca0e 100644 --- a/googletest/src/gtest-port.cc +++ b/googletest/src/gtest-port.cc @@ -1235,5 +1235,24 @@ const char* StringFromGTestEnv(const char* flag, const char* default_value) { return value == NULL ? default_value : value; } +// Reads and returns GTEST_OUTPUT and/or XML_OUTPUT_FILE environment +// variables; if neither is set, returns default value. +// XML_OUTPUT_FILE is set by the Bazel build system, and its format is +// a filename without the "xml:" prefix of GTEST_OUTPUT. +std::string OutputFromGTestEnv(const char* default_value) { +#if defined(GTEST_GET_STRING_FROM_ENV_) + return GTEST_GET_STRING_FROM_ENV_("output", default_value); +#endif // defined(GTEST_GET_STRING_FROM_ENV_) + const char* value = StringFromGTestEnv("output", NULL); + if (value) { + return value; + } + value = posix::GetEnv("XML_OUTPUT_FILE"); + if (value) { + return std::string("xml:") + value; + } + return default_value; +} + } // namespace internal } // namespace testing diff --git a/googletest/src/gtest.cc b/googletest/src/gtest.cc index 2bac245d..5509f092 100644 --- a/googletest/src/gtest.cc +++ b/googletest/src/gtest.cc @@ -237,7 +237,7 @@ GTEST_DEFINE_bool_(list_tests, false, GTEST_DEFINE_string_( output, - internal::StringFromGTestEnv("output", ""), + internal::OutputFromGTestEnv(""), "A format (currently must be \"xml\"), optionally followed " "by a colon and an output file name or directory. A directory " "is indicated by a trailing pathname separator. " diff --git a/googletest/test/gtest_env_var_test.py b/googletest/test/gtest_env_var_test.py index 1fc6ebe5..13afb2b0 100755 --- a/googletest/test/gtest_env_var_test.py +++ b/googletest/test/gtest_env_var_test.py @@ -98,6 +98,19 @@ class GTestEnvVarTest(gtest_test_utils.TestCase): TestFlag('death_test_use_fork', '1', '0') TestFlag('stack_trace_depth', '0', '100') + def testXmlOutputFile(self): + """Test that $XML_OUTPUT_FILE affects the output flag.""" + + # $XML_OUTPUT_FILE sets output flag + SetEnvVar('XML_OUTPUT_FILE', 'tmp/bar.xml') + AssertEq('xml:tmp/bar.xml', GetFlag('output')) + # $XML_OUTPUT_FILE is overridden by $GTEST_OUTPUT + SetEnvVar('GTEST_OUTPUT', 'xml:tmp/foo.xml') + AssertEq('xml:tmp/foo.xml', GetFlag('output')) + # If neither set, flag has default value + SetEnvVar('XML_OUTPUT_FILE', None) + SetEnvVar('GTEST_OUTPUT', None) + AssertEq('', GetFlag('output')) if __name__ == '__main__': gtest_test_utils.Main()