Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 49 additions & 32 deletions googletest/src/gtest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3154,9 +3154,8 @@ static void PrintTestPartResult(const TestPartResult& test_part_result) {
}

// class PrettyUnitTestResultPrinter
#if defined(GTEST_OS_WINDOWS) && !defined(GTEST_OS_WINDOWS_MOBILE) && \
!defined(GTEST_OS_WINDOWS_PHONE) && !defined(GTEST_OS_WINDOWS_RT) && \
!defined(GTEST_OS_WINDOWS_MINGW)
#if defined(GTEST_OS_WINDOWS) && !defined(GTEST_OS_WINDOWS_MOBILE) && \
!defined(GTEST_OS_WINDOWS_PHONE) && !defined(GTEST_OS_WINDOWS_RT)

// Returns the character attribute for the given color.
static WORD GetColorAttribute(GTestColor color) {
Expand Down Expand Up @@ -3203,7 +3202,7 @@ static WORD GetNewColor(GTestColor color, WORD old_color_attrs) {
return new_color;
}

#else
#endif // GTEST_OS_WINDOWS && !GTEST_OS_WINDOWS_MOBILE

// Returns the ANSI color code for the given color. GTestColor::kDefault is
// an invalid input.
Expand All @@ -3220,15 +3219,20 @@ static const char* GetAnsiColorCode(GTestColor color) {
}
}

#endif // GTEST_OS_WINDOWS && !GTEST_OS_WINDOWS_MOBILE
static void EmitAnsiColorOutput(GTestColor color, const char* fmt,
va_list args) {
printf("\033[0;3%sm", GetAnsiColorCode(color));
vprintf(fmt, args);
printf("\033[m"); // Resets the terminal to default.
}

// Returns true if and only if Google Test should use colors in the output.
bool ShouldUseColor(bool stdout_is_tty) {
std::string c = GTEST_FLAG_GET(color);
const char* const gtest_color = c.c_str();

if (String::CaseInsensitiveCStringEquals(gtest_color, "auto")) {
#if defined(GTEST_OS_WINDOWS) && !defined(GTEST_OS_WINDOWS_MINGW)
#if defined(GTEST_OS_WINDOWS)
// On Windows the TERM variable is usually not set, but the
// console there does support colors.
return stdout_is_tty;
Expand Down Expand Up @@ -3268,11 +3272,12 @@ static void ColoredPrintf(GTestColor color, const char* fmt, ...) {
va_list args;
va_start(args, fmt);

static const bool in_color_mode =
#if GTEST_HAS_FILE_SYSTEM
ShouldUseColor(posix::IsATTY(posix::FileNo(stdout)) != 0);
static const bool stdout_is_tty = posix::IsATTY(posix::FileNo(stdout)) != 0;
static const bool in_color_mode = ShouldUseColor(stdout_is_tty);
#else
false;
static const bool stdout_is_tty = false;
static const bool in_color_mode = false;
#endif // GTEST_HAS_FILE_SYSTEM

const bool use_color = in_color_mode && (color != GTestColor::kDefault);
Expand All @@ -3283,32 +3288,38 @@ static void ColoredPrintf(GTestColor color, const char* fmt, ...) {
return;
}

#if defined(GTEST_OS_WINDOWS) && !defined(GTEST_OS_WINDOWS_MOBILE) && \
!defined(GTEST_OS_WINDOWS_PHONE) && !defined(GTEST_OS_WINDOWS_RT) && \
!defined(GTEST_OS_WINDOWS_MINGW)
const HANDLE stdout_handle = GetStdHandle(STD_OUTPUT_HANDLE);

// Gets the current text color.
CONSOLE_SCREEN_BUFFER_INFO buffer_info;
GetConsoleScreenBufferInfo(stdout_handle, &buffer_info);
const WORD old_color_attrs = buffer_info.wAttributes;
const WORD new_color = GetNewColor(color, old_color_attrs);

// We need to flush the stream buffers into the console before each
// SetConsoleTextAttribute call lest it affect the text that is already
// printed but has not yet reached the console.
fflush(stdout);
SetConsoleTextAttribute(stdout_handle, new_color);
#if defined(GTEST_OS_WINDOWS) && !defined(GTEST_OS_WINDOWS_MOBILE) && \
!defined(GTEST_OS_WINDOWS_PHONE) && !defined(GTEST_OS_WINDOWS_RT)
#if defined(GTEST_OS_WINDOWS_MINGW)
static const bool using_mingw = AlwaysTrue();
#else
static const bool using_mingw = AlwaysFalse();
#endif
if (!using_mingw || stdout_is_tty) {
const HANDLE stdout_handle = GetStdHandle(STD_OUTPUT_HANDLE);

// Gets the current text color.
CONSOLE_SCREEN_BUFFER_INFO buffer_info;
GetConsoleScreenBufferInfo(stdout_handle, &buffer_info);
const WORD old_color_attrs = buffer_info.wAttributes;
const WORD new_color = GetNewColor(color, old_color_attrs);

// We need to flush the stream buffers into the console before each
// SetConsoleTextAttribute call lest it affect the text that is already
// printed but has not yet reached the console.
fflush(stdout);
SetConsoleTextAttribute(stdout_handle, new_color);

vprintf(fmt, args);
vprintf(fmt, args);

fflush(stdout);
// Restores the text color.
SetConsoleTextAttribute(stdout_handle, old_color_attrs);
fflush(stdout);
// Restores the text color.
SetConsoleTextAttribute(stdout_handle, old_color_attrs);
} else if (using_mingw) {
EmitAnsiColorOutput(color, fmt, args);
}
#else
printf("\033[0;3%sm", GetAnsiColorCode(color));
vprintf(fmt, args);
printf("\033[m"); // Resets the terminal to default.
EmitAnsiColorOutput(color, fmt, args);
#endif // GTEST_OS_WINDOWS && !GTEST_OS_WINDOWS_MOBILE
va_end(args);
}
Expand Down Expand Up @@ -6502,6 +6513,12 @@ static const char kColorEncodedHelpMessage[] =
" @G--" GTEST_FLAG_PREFIX_
"color=@Y(@Gyes@Y|@Gno@Y|@Gauto@Y)@D\n"
" Enable/disable colored output. The default is @Gauto@D.\n"
#if GTEST_OS_WINDOWS_MINGW
" With MinGW builds, color output will use the Windows Console API\n"
" only if stdout is a TTY, otherwise it uses ANSI escape sequences.\n"
" This means under mintty you need @G--" GTEST_FLAG_PREFIX_
"color=yes@D for color.\n"
#endif
" @G--" GTEST_FLAG_PREFIX_
"brief=1@D\n"
" Only print test failures.\n"
Expand Down