Make posix::FileNo available under !GTEST_HAS_FILE_SYSTEM

This enables automatic color output without filesystem support, as that only requires testing to see if the output is a terminal.

Fixes: #4439
PiperOrigin-RevId: 598929397
Change-Id: Idca7490e6e090951a78cd1cdd710f41d756a68b4
This commit is contained in:
Abseil Team 2024-01-16 12:55:45 -08:00 committed by Copybara-Service
parent cfe5076a8b
commit f6f920d2ba
2 changed files with 11 additions and 6 deletions

View File

@ -2005,7 +2005,9 @@ inline std::string StripTrailingSpaces(std::string str) {
namespace posix { namespace posix {
// File system porting. // File system porting.
#if GTEST_HAS_FILE_SYSTEM // Note: Not every I/O-related function is related to file systems, so don't
// just disable all of them here. For example, fileno() and isatty(), etc. must
// always be available in order to detect if a pipe points to a terminal.
#ifdef GTEST_OS_WINDOWS #ifdef GTEST_OS_WINDOWS
typedef struct _stat StatStruct; typedef struct _stat StatStruct;
@ -2016,27 +2018,32 @@ inline int FileNo(FILE* file) { return reinterpret_cast<int>(_fileno(file)); }
// time and thus not defined there. // time and thus not defined there.
#else #else
inline int FileNo(FILE* file) { return _fileno(file); } inline int FileNo(FILE* file) { return _fileno(file); }
#if GTEST_HAS_FILE_SYSTEM
inline int Stat(const char* path, StatStruct* buf) { return _stat(path, buf); } inline int Stat(const char* path, StatStruct* buf) { return _stat(path, buf); }
inline int RmDir(const char* dir) { return _rmdir(dir); } inline int RmDir(const char* dir) { return _rmdir(dir); }
inline bool IsDir(const StatStruct& st) { return (_S_IFDIR & st.st_mode) != 0; } inline bool IsDir(const StatStruct& st) { return (_S_IFDIR & st.st_mode) != 0; }
#endif
#endif // GTEST_OS_WINDOWS_MOBILE #endif // GTEST_OS_WINDOWS_MOBILE
#elif defined(GTEST_OS_ESP8266) #elif defined(GTEST_OS_ESP8266)
typedef struct stat StatStruct; typedef struct stat StatStruct;
inline int FileNo(FILE* file) { return fileno(file); } inline int FileNo(FILE* file) { return fileno(file); }
#if GTEST_HAS_FILE_SYSTEM
inline int Stat(const char* path, StatStruct* buf) { inline int Stat(const char* path, StatStruct* buf) {
// stat function not implemented on ESP8266 // stat function not implemented on ESP8266
return 0; return 0;
} }
inline int RmDir(const char* dir) { return rmdir(dir); } inline int RmDir(const char* dir) { return rmdir(dir); }
inline bool IsDir(const StatStruct& st) { return S_ISDIR(st.st_mode); } inline bool IsDir(const StatStruct& st) { return S_ISDIR(st.st_mode); }
#endif
#else #else
typedef struct stat StatStruct; typedef struct stat StatStruct;
inline int FileNo(FILE* file) { return fileno(file); } inline int FileNo(FILE* file) { return fileno(file); }
#if GTEST_HAS_FILE_SYSTEM
inline int Stat(const char* path, StatStruct* buf) { return stat(path, buf); } inline int Stat(const char* path, StatStruct* buf) { return stat(path, buf); }
#ifdef GTEST_OS_QURT #ifdef GTEST_OS_QURT
// QuRT doesn't support any directory functions, including rmdir // QuRT doesn't support any directory functions, including rmdir
@ -2045,9 +2052,9 @@ inline int RmDir(const char*) { return 0; }
inline int RmDir(const char* dir) { return rmdir(dir); } inline int RmDir(const char* dir) { return rmdir(dir); }
#endif #endif
inline bool IsDir(const StatStruct& st) { return S_ISDIR(st.st_mode); } inline bool IsDir(const StatStruct& st) { return S_ISDIR(st.st_mode); }
#endif
#endif // GTEST_OS_WINDOWS #endif // GTEST_OS_WINDOWS
#endif // GTEST_HAS_FILE_SYSTEM
// Other functions with a different name on Windows. // Other functions with a different name on Windows.

View File

@ -3288,11 +3288,9 @@ static void ColoredPrintf(GTestColor color, const char* fmt, ...) {
va_start(args, fmt); va_start(args, fmt);
static const bool in_color_mode = static const bool in_color_mode =
#if GTEST_HAS_FILE_SYSTEM // We don't condition this on GTEST_HAS_FILE_SYSTEM because we still need
// to be able to detect terminal I/O regardless.
ShouldUseColor(posix::IsATTY(posix::FileNo(stdout)) != 0); ShouldUseColor(posix::IsATTY(posix::FileNo(stdout)) != 0);
#else
false;
#endif // GTEST_HAS_FILE_SYSTEM
const bool use_color = in_color_mode && (color != GTestColor::kDefault); const bool use_color = in_color_mode && (color != GTestColor::kDefault);