Fixes the bug where the XML output path is affected by test changing the current directory. By Stefan Weigand.

This commit is contained in:
zhanyong.wan
2009-01-26 19:21:32 +00:00
parent b593ccbbbe
commit 650d5bf3ba
6 changed files with 288 additions and 41 deletions

View File

@@ -46,8 +46,8 @@
#include <unistd.h>
#else
#include <limits.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/stat.h> // NOLINT
#include <unistd.h> // NOLINT
#endif // _WIN32_WCE or _WIN32
#ifdef GTEST_OS_WINDOWS
@@ -144,13 +144,22 @@ FilePath FilePath::MakeFileName(const FilePath& directory,
const FilePath& base_name,
int number,
const char* extension) {
FilePath dir(directory.RemoveTrailingPathSeparator());
if (number == 0) {
return FilePath(String::Format("%s%c%s.%s", dir.c_str(), kPathSeparator,
base_name.c_str(), extension));
}
return FilePath(String::Format("%s%c%s_%d.%s", dir.c_str(), kPathSeparator,
base_name.c_str(), number, extension));
const FilePath file_name(
(number == 0) ?
String::Format("%s.%s", base_name.c_str(), extension) :
String::Format("%s_%d.%s", base_name.c_str(), number, extension));
return ConcatPaths(directory, file_name);
}
// Given directory = "dir", relative_path = "test.xml", returns "dir/test.xml".
// On Windows, uses \ as the separator rather than /.
FilePath FilePath::ConcatPaths(const FilePath& directory,
const FilePath& relative_path) {
if (directory.IsEmpty())
return relative_path;
const FilePath dir(directory.RemoveTrailingPathSeparator());
return FilePath(String::Format("%s%c%s", dir.c_str(), kPathSeparator,
relative_path.c_str()));
}
// Returns true if pathname describes something findable in the file-system,
@@ -207,13 +216,26 @@ bool FilePath::DirectoryExists() const {
bool FilePath::IsRootDirectory() const {
#ifdef GTEST_OS_WINDOWS
const char* const name = pathname_.c_str();
return pathname_.GetLength() == 3 &&
// TODO(wan@google.com): on Windows a network share like
// \\server\share can be a root directory, although it cannot be the
// current directory. Handle this properly.
return pathname_.GetLength() == 3 && IsAbsolutePath();
#else
return pathname_ == kPathSeparatorString;
#endif
}
// Returns true if pathname describes an absolute path.
bool FilePath::IsAbsolutePath() const {
const char* const name = pathname_.c_str();
#ifdef GTEST_OS_WINDOWS
return pathname_.GetLength() >= 3 &&
((name[0] >= 'a' && name[0] <= 'z') ||
(name[0] >= 'A' && name[0] <= 'Z')) &&
name[1] == ':' &&
name[2] == kPathSeparator;
#else
return pathname_ == kPathSeparatorString;
return name[0] == kPathSeparator;
#endif
}

View File

@@ -799,9 +799,10 @@ class UnitTestOptions {
// Returns the output format, or "" for normal printed output.
static String GetOutputFormat();
// Returns the name of the requested output file, or the default if none
// was explicitly specified.
static String GetOutputFile();
// Returns the absolute path of the requested output file, or the
// default (test_detail.xml in the original working directory) if
// none was explicitly specified.
static String GetAbsolutePathToOutputFile();
// Functions for processing the gtest_filter flag.

View File

@@ -69,7 +69,7 @@
#include <sys/time.h> // NOLINT
// On z/OS we additionally need strings.h for strcasecmp.
#include <strings.h>
#include <strings.h> // NOLINT
#elif defined(_WIN32_WCE) // We are on Windows CE.
@@ -289,6 +289,7 @@ Mutex g_linked_ptr_mutex(Mutex::NO_CONSTRUCTOR_NEEDED_FOR_STATIC_MUTEX);
// Application pathname gotten in InitGoogleTest.
String g_executable_path;
String g_original_working_dir;
// Returns the current application's name, removing directory path if that
// is present.
@@ -319,16 +320,27 @@ String UnitTestOptions::GetOutputFormat() {
// Returns the name of the requested output file, or the default if none
// was explicitly specified.
String UnitTestOptions::GetOutputFile() {
String UnitTestOptions::GetAbsolutePathToOutputFile() {
const char* const gtest_output_flag = GTEST_FLAG(output).c_str();
if (gtest_output_flag == NULL)
return String("");
const char* const colon = strchr(gtest_output_flag, ':');
if (colon == NULL)
return String(kDefaultOutputFile);
return String(internal::FilePath::ConcatPaths(
internal::FilePath(g_original_working_dir),
internal::FilePath(kDefaultOutputFile)).ToString() );
internal::FilePath output_name(colon + 1);
if (!output_name.IsAbsolutePath())
// TODO(wan@google.com): on Windows \some\path is not an absolute
// path (as its meaning depends on the current drive), yet the
// following logic for turning it into an absolute path is wrong.
// Fix it.
output_name = internal::FilePath::ConcatPaths(
internal::FilePath(g_original_working_dir),
internal::FilePath(colon + 1));
if (!output_name.IsDirectory())
return output_name.ToString();
@@ -3675,7 +3687,7 @@ UnitTestEventListenerInterface* UnitTestImpl::result_printer() {
const String& output_format = internal::UnitTestOptions::GetOutputFormat();
if (output_format == "xml") {
repeater->AddListener(new XmlUnitTestResultPrinter(
internal::UnitTestOptions::GetOutputFile().c_str()));
internal::UnitTestOptions::GetAbsolutePathToOutputFile().c_str()));
} else if (output_format != "") {
printf("WARNING: unrecognized output format \"%s\" ignored.\n",
output_format.c_str());
@@ -3926,6 +3938,8 @@ void InitGoogleTestImpl(int* argc, CharType** argv) {
if (*argc <= 0) return;
internal::g_executable_path = internal::StreamableToString(argv[0]);
internal::g_original_working_dir =
internal::FilePath::GetCurrentDir().ToString();
#ifdef GTEST_HAS_DEATH_TEST
g_argvs.clear();