Skip to content

Commit

Permalink
Introduces a new porting flag (GTEST_HAS_FILE_SYSTEM) to indicate whe…
Browse files Browse the repository at this point in the history
…ther a platform supports filesystem operations.

PiperOrigin-RevId: 494751986
Change-Id: I07f73bdf478a73934b8f1a69c1ab4abda1b231ae
  • Loading branch information
Abseil Team authored and Copybara-Service committed Dec 12, 2022
1 parent 516940f commit b0846aa
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 40 deletions.
5 changes: 5 additions & 0 deletions googletest/include/gtest/internal/gtest-filepath.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,14 @@
#ifndef GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_FILEPATH_H_
#define GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_FILEPATH_H_

#include "gtest/internal/gtest-port.h"
#include "gtest/internal/gtest-string.h"

GTEST_DISABLE_MSC_WARNINGS_PUSH_(4251 \
/* class A needs to have dll-interface to be used by clients of class B */)

#if GTEST_HAS_FILE_SYSTEM

namespace testing {
namespace internal {

Expand Down Expand Up @@ -217,4 +220,6 @@ class GTEST_API_ FilePath {

GTEST_DISABLE_MSC_WARNINGS_POP_() // 4251

#endif // GTEST_HAS_FILE_SYSTEM

#endif // GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_FILEPATH_H_
101 changes: 65 additions & 36 deletions googletest/include/gtest/internal/gtest-port.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@
// GTEST_HAS_STD_WSTRING - Define it to 1/0 to indicate that
// std::wstring does/doesn't work (Google Test can
// be used where std::wstring is unavailable).
// GTEST_HAS_FILE_SYSTEM - Define it to 1/0 to indicate whether or not a
// file system is/isn't available.
// GTEST_HAS_SEH - Define it to 1/0 to indicate whether the
// compiler supports Microsoft's "Structured
// Exception Handling".
Expand Down Expand Up @@ -463,6 +465,11 @@ typedef struct _RTL_CRITICAL_SECTION GTEST_CRITICAL_SECTION;

#endif // GTEST_HAS_STD_WSTRING

#ifndef GTEST_HAS_FILE_SYSTEM
// Most platforms support a file system.
#define GTEST_HAS_FILE_SYSTEM 1
#endif // GTEST_HAS_FILE_SYSTEM

// Determines whether RTTI is available.
#ifndef GTEST_HAS_RTTI
// The user didn't tell us whether RTTI is enabled, so we need to
Expand Down Expand Up @@ -580,10 +587,11 @@ typedef struct _RTL_CRITICAL_SECTION GTEST_CRITICAL_SECTION;
// output correctness and to implement death tests.
#ifndef GTEST_HAS_STREAM_REDIRECTION
// By default, we assume that stream redirection is supported on all
// platforms except known mobile / embedded ones.
// platforms except known mobile / embedded ones. Also, if the port doesn't have
// a file system, stream redirection is not supported.
#if GTEST_OS_WINDOWS_MOBILE || GTEST_OS_WINDOWS_PHONE || \
GTEST_OS_WINDOWS_RT || GTEST_OS_ESP8266 || GTEST_OS_XTENSA || \
GTEST_OS_QURT
GTEST_OS_QURT || !GTEST_HAS_FILE_SYSTEM
#define GTEST_HAS_STREAM_REDIRECTION 0
#else
#define GTEST_HAS_STREAM_REDIRECTION 1
Expand All @@ -599,7 +607,10 @@ typedef struct _RTL_CRITICAL_SECTION GTEST_CRITICAL_SECTION;
GTEST_OS_FREEBSD || GTEST_OS_NETBSD || GTEST_OS_FUCHSIA || \
GTEST_OS_DRAGONFLY || GTEST_OS_GNU_KFREEBSD || GTEST_OS_HAIKU || \
GTEST_OS_GNU_HURD)
// Death tests require a file system to work properly.
#if GTEST_HAS_FILE_SYSTEM
#define GTEST_HAS_DEATH_TEST 1
#endif // GTEST_HAS_FILE_SYSTEM
#endif

// Determines whether to support type-driven tests.
Expand Down Expand Up @@ -1953,31 +1964,12 @@ inline std::string StripTrailingSpaces(std::string str) {

namespace posix {

// Functions with a different name on Windows.

// File system porting.
#if GTEST_HAS_FILE_SYSTEM
#if GTEST_OS_WINDOWS

typedef struct _stat StatStruct;

#ifdef __BORLANDC__
inline int DoIsATTY(int fd) { return isatty(fd); }
inline int StrCaseCmp(const char* s1, const char* s2) {
return stricmp(s1, s2);
}
inline char* StrDup(const char* src) { return strdup(src); }
#else // !__BORLANDC__
#if GTEST_OS_WINDOWS_MOBILE || GTEST_OS_ZOS || GTEST_OS_IOS || \
GTEST_OS_WINDOWS_PHONE || GTEST_OS_WINDOWS_RT || defined(ESP_PLATFORM)
inline int DoIsATTY(int /* fd */) { return 0; }
#else
inline int DoIsATTY(int fd) { return _isatty(fd); }
#endif // GTEST_OS_WINDOWS_MOBILE
inline int StrCaseCmp(const char* s1, const char* s2) {
return _stricmp(s1, s2);
}
inline char* StrDup(const char* src) { return _strdup(src); }
#endif // __BORLANDC__

#if GTEST_OS_WINDOWS_MOBILE
inline int FileNo(FILE* file) { return reinterpret_cast<int>(_fileno(file)); }
// Stat(), RmDir(), and IsDir() are not needed on Windows CE at this
Expand All @@ -1993,15 +1985,10 @@ inline bool IsDir(const StatStruct& st) { return (_S_IFDIR & st.st_mode) != 0; }
typedef struct stat StatStruct;

inline int FileNo(FILE* file) { return fileno(file); }
inline int DoIsATTY(int fd) { return isatty(fd); }
inline int Stat(const char* path, StatStruct* buf) {
// stat function not implemented on ESP8266
return 0;
}
inline int StrCaseCmp(const char* s1, const char* s2) {
return strcasecmp(s1, s2);
}
inline char* StrDup(const char* src) { return strdup(src); }
inline int RmDir(const char* dir) { return rmdir(dir); }
inline bool IsDir(const StatStruct& st) { return S_ISDIR(st.st_mode); }

Expand All @@ -2010,12 +1997,7 @@ inline bool IsDir(const StatStruct& st) { return S_ISDIR(st.st_mode); }
typedef struct stat StatStruct;

inline int FileNo(FILE* file) { return fileno(file); }
inline int DoIsATTY(int fd) { return isatty(fd); }
inline int Stat(const char* path, StatStruct* buf) { return stat(path, buf); }
inline int StrCaseCmp(const char* s1, const char* s2) {
return strcasecmp(s1, s2);
}
inline char* StrDup(const char* src) { return strdup(src); }
#if GTEST_OS_QURT
// QuRT doesn't support any directory functions, including rmdir
inline int RmDir(const char*) { return 0; }
Expand All @@ -2024,6 +2006,48 @@ inline int RmDir(const char* dir) { return rmdir(dir); }
#endif
inline bool IsDir(const StatStruct& st) { return S_ISDIR(st.st_mode); }

#endif // GTEST_OS_WINDOWS
#endif // GTEST_HAS_FILE_SYSTEM

// Other functions with a different name on Windows.

#if GTEST_OS_WINDOWS

#ifdef __BORLANDC__
inline int DoIsATTY(int fd) { return isatty(fd); }
inline int StrCaseCmp(const char* s1, const char* s2) {
return stricmp(s1, s2);
}
inline char* StrDup(const char* src) { return strdup(src); }
#else // !__BORLANDC__
#if GTEST_OS_WINDOWS_MOBILE || GTEST_OS_ZOS || GTEST_OS_IOS || \
GTEST_OS_WINDOWS_PHONE || GTEST_OS_WINDOWS_RT || defined(ESP_PLATFORM)
inline int DoIsATTY(int /* fd */) { return 0; }
#else
inline int DoIsATTY(int fd) { return _isatty(fd); }
#endif // GTEST_OS_WINDOWS_MOBILE
inline int StrCaseCmp(const char* s1, const char* s2) {
return _stricmp(s1, s2);
}
inline char* StrDup(const char* src) { return _strdup(src); }
#endif // __BORLANDC__

#elif GTEST_OS_ESP8266

inline int DoIsATTY(int fd) { return isatty(fd); }
inline int StrCaseCmp(const char* s1, const char* s2) {
return strcasecmp(s1, s2);
}
inline char* StrDup(const char* src) { return strdup(src); }

#else

inline int DoIsATTY(int fd) { return isatty(fd); }
inline int StrCaseCmp(const char* s1, const char* s2) {
return strcasecmp(s1, s2);
}
inline char* StrDup(const char* src) { return strdup(src); }

#endif // GTEST_OS_WINDOWS

inline int IsATTY(int fd) {
Expand All @@ -2044,7 +2068,7 @@ GTEST_DISABLE_MSC_DEPRECATED_PUSH_()
// ChDir(), FReopen(), FDOpen(), Read(), Write(), Close(), and
// StrError() aren't needed on Windows CE at this time and thus not
// defined there.

#if GTEST_HAS_FILE_SYSTEM
#if !GTEST_OS_WINDOWS_MOBILE && !GTEST_OS_WINDOWS_PHONE && \
!GTEST_OS_WINDOWS_RT && !GTEST_OS_ESP8266 && !GTEST_OS_XTENSA && \
!GTEST_OS_QURT
Expand All @@ -2066,7 +2090,7 @@ inline FILE* FReopen(const char* path, const char* mode, FILE* stream) {
return freopen(path, mode, stream);
}
inline FILE* FDOpen(int fd, const char* mode) { return fdopen(fd, mode); }
#endif
#endif // !GTEST_OS_WINDOWS_MOBILE && !GTEST_OS_QURT
inline int FClose(FILE* fp) { return fclose(fp); }
#if !GTEST_OS_WINDOWS_MOBILE && !GTEST_OS_QURT
inline int Read(int fd, void* buf, unsigned int count) {
Expand All @@ -2076,8 +2100,13 @@ inline int Write(int fd, const void* buf, unsigned int count) {
return static_cast<int>(write(fd, buf, count));
}
inline int Close(int fd) { return close(fd); }
#endif // !GTEST_OS_WINDOWS_MOBILE && !GTEST_OS_QURT
#endif // GTEST_HAS_FILE_SYSTEM

#if !GTEST_OS_WINDOWS_MOBILE && !GTEST_OS_QURT
inline const char* StrError(int errnum) { return strerror(errnum); }
#endif
#endif // !GTEST_OS_WINDOWS_MOBILE && !GTEST_OS_QURT

inline const char* GetEnv(const char* name) {
#if GTEST_OS_WINDOWS_MOBILE || GTEST_OS_WINDOWS_PHONE || \
GTEST_OS_WINDOWS_RT || GTEST_OS_ESP8266 || GTEST_OS_XTENSA || \
Expand Down
4 changes: 4 additions & 0 deletions googletest/src/gtest-filepath.cc
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@
#define GTEST_PATH_MAX_ _POSIX_PATH_MAX
#endif // GTEST_OS_WINDOWS

#if GTEST_HAS_FILE_SYSTEM

namespace testing {
namespace internal {

Expand Down Expand Up @@ -404,3 +406,5 @@ void FilePath::Normalize() {

} // namespace internal
} // namespace testing

#endif // GTEST_HAS_FILE_SYSTEM
4 changes: 4 additions & 0 deletions googletest/src/gtest-internal-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -396,9 +396,11 @@ class GTEST_API_ UnitTestOptions {
static bool MatchesFilter(const std::string& name, const char* filter);
};

#if GTEST_HAS_FILE_SYSTEM
// Returns the current application's name, removing directory path if that
// is present. Used by UnitTestOptions::GetOutputFile.
GTEST_API_ FilePath GetCurrentExecutableName();
#endif // GTEST_HAS_FILE_SYSTEM

// The role interface for getting the OS stack trace as a string.
class OsStackTraceGetterInterface {
Expand Down Expand Up @@ -840,9 +842,11 @@ class GTEST_API_ UnitTestImpl {
// The UnitTest object that owns this implementation object.
UnitTest* const parent_;

#if GTEST_HAS_FILE_SYSTEM
// The working directory when the first TEST() or TEST_F() was
// executed.
internal::FilePath original_working_dir_;
#endif // GTEST_HAS_FILE_SYSTEM

// The default test part result reporters.
DefaultGlobalTestPartResultReporter default_global_test_part_result_reporter_;
Expand Down
Loading

0 comments on commit b0846aa

Please sign in to comment.