Skip to content

Commit b0846aa

Browse files
Abseil Teamcopybara-github
Abseil Team
authored andcommitted
Introduces a new porting flag (GTEST_HAS_FILE_SYSTEM) to indicate whether a platform supports filesystem operations.
PiperOrigin-RevId: 494751986 Change-Id: I07f73bdf478a73934b8f1a69c1ab4abda1b231ae
1 parent 516940f commit b0846aa

File tree

5 files changed

+115
-40
lines changed

5 files changed

+115
-40
lines changed

googletest/include/gtest/internal/gtest-filepath.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,14 @@
4242
#ifndef GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_FILEPATH_H_
4343
#define GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_FILEPATH_H_
4444

45+
#include "gtest/internal/gtest-port.h"
4546
#include "gtest/internal/gtest-string.h"
4647

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

51+
#if GTEST_HAS_FILE_SYSTEM
52+
5053
namespace testing {
5154
namespace internal {
5255

@@ -217,4 +220,6 @@ class GTEST_API_ FilePath {
217220

218221
GTEST_DISABLE_MSC_WARNINGS_POP_() // 4251
219222

223+
#endif // GTEST_HAS_FILE_SYSTEM
224+
220225
#endif // GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_FILEPATH_H_

googletest/include/gtest/internal/gtest-port.h

Lines changed: 65 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@
8383
// GTEST_HAS_STD_WSTRING - Define it to 1/0 to indicate that
8484
// std::wstring does/doesn't work (Google Test can
8585
// be used where std::wstring is unavailable).
86+
// GTEST_HAS_FILE_SYSTEM - Define it to 1/0 to indicate whether or not a
87+
// file system is/isn't available.
8688
// GTEST_HAS_SEH - Define it to 1/0 to indicate whether the
8789
// compiler supports Microsoft's "Structured
8890
// Exception Handling".
@@ -463,6 +465,11 @@ typedef struct _RTL_CRITICAL_SECTION GTEST_CRITICAL_SECTION;
463465

464466
#endif // GTEST_HAS_STD_WSTRING
465467

468+
#ifndef GTEST_HAS_FILE_SYSTEM
469+
// Most platforms support a file system.
470+
#define GTEST_HAS_FILE_SYSTEM 1
471+
#endif // GTEST_HAS_FILE_SYSTEM
472+
466473
// Determines whether RTTI is available.
467474
#ifndef GTEST_HAS_RTTI
468475
// The user didn't tell us whether RTTI is enabled, so we need to
@@ -580,10 +587,11 @@ typedef struct _RTL_CRITICAL_SECTION GTEST_CRITICAL_SECTION;
580587
// output correctness and to implement death tests.
581588
#ifndef GTEST_HAS_STREAM_REDIRECTION
582589
// By default, we assume that stream redirection is supported on all
583-
// platforms except known mobile / embedded ones.
590+
// platforms except known mobile / embedded ones. Also, if the port doesn't have
591+
// a file system, stream redirection is not supported.
584592
#if GTEST_OS_WINDOWS_MOBILE || GTEST_OS_WINDOWS_PHONE || \
585593
GTEST_OS_WINDOWS_RT || GTEST_OS_ESP8266 || GTEST_OS_XTENSA || \
586-
GTEST_OS_QURT
594+
GTEST_OS_QURT || !GTEST_HAS_FILE_SYSTEM
587595
#define GTEST_HAS_STREAM_REDIRECTION 0
588596
#else
589597
#define GTEST_HAS_STREAM_REDIRECTION 1
@@ -599,7 +607,10 @@ typedef struct _RTL_CRITICAL_SECTION GTEST_CRITICAL_SECTION;
599607
GTEST_OS_FREEBSD || GTEST_OS_NETBSD || GTEST_OS_FUCHSIA || \
600608
GTEST_OS_DRAGONFLY || GTEST_OS_GNU_KFREEBSD || GTEST_OS_HAIKU || \
601609
GTEST_OS_GNU_HURD)
610+
// Death tests require a file system to work properly.
611+
#if GTEST_HAS_FILE_SYSTEM
602612
#define GTEST_HAS_DEATH_TEST 1
613+
#endif // GTEST_HAS_FILE_SYSTEM
603614
#endif
604615

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

19541965
namespace posix {
19551966

1956-
// Functions with a different name on Windows.
1957-
1967+
// File system porting.
1968+
#if GTEST_HAS_FILE_SYSTEM
19581969
#if GTEST_OS_WINDOWS
19591970

19601971
typedef struct _stat StatStruct;
19611972

1962-
#ifdef __BORLANDC__
1963-
inline int DoIsATTY(int fd) { return isatty(fd); }
1964-
inline int StrCaseCmp(const char* s1, const char* s2) {
1965-
return stricmp(s1, s2);
1966-
}
1967-
inline char* StrDup(const char* src) { return strdup(src); }
1968-
#else // !__BORLANDC__
1969-
#if GTEST_OS_WINDOWS_MOBILE || GTEST_OS_ZOS || GTEST_OS_IOS || \
1970-
GTEST_OS_WINDOWS_PHONE || GTEST_OS_WINDOWS_RT || defined(ESP_PLATFORM)
1971-
inline int DoIsATTY(int /* fd */) { return 0; }
1972-
#else
1973-
inline int DoIsATTY(int fd) { return _isatty(fd); }
1974-
#endif // GTEST_OS_WINDOWS_MOBILE
1975-
inline int StrCaseCmp(const char* s1, const char* s2) {
1976-
return _stricmp(s1, s2);
1977-
}
1978-
inline char* StrDup(const char* src) { return _strdup(src); }
1979-
#endif // __BORLANDC__
1980-
19811973
#if GTEST_OS_WINDOWS_MOBILE
19821974
inline int FileNo(FILE* file) { return reinterpret_cast<int>(_fileno(file)); }
19831975
// Stat(), RmDir(), and IsDir() are not needed on Windows CE at this
@@ -1993,15 +1985,10 @@ inline bool IsDir(const StatStruct& st) { return (_S_IFDIR & st.st_mode) != 0; }
19931985
typedef struct stat StatStruct;
19941986

19951987
inline int FileNo(FILE* file) { return fileno(file); }
1996-
inline int DoIsATTY(int fd) { return isatty(fd); }
19971988
inline int Stat(const char* path, StatStruct* buf) {
19981989
// stat function not implemented on ESP8266
19991990
return 0;
20001991
}
2001-
inline int StrCaseCmp(const char* s1, const char* s2) {
2002-
return strcasecmp(s1, s2);
2003-
}
2004-
inline char* StrDup(const char* src) { return strdup(src); }
20051992
inline int RmDir(const char* dir) { return rmdir(dir); }
20061993
inline bool IsDir(const StatStruct& st) { return S_ISDIR(st.st_mode); }
20071994

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

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

2009+
#endif // GTEST_OS_WINDOWS
2010+
#endif // GTEST_HAS_FILE_SYSTEM
2011+
2012+
// Other functions with a different name on Windows.
2013+
2014+
#if GTEST_OS_WINDOWS
2015+
2016+
#ifdef __BORLANDC__
2017+
inline int DoIsATTY(int fd) { return isatty(fd); }
2018+
inline int StrCaseCmp(const char* s1, const char* s2) {
2019+
return stricmp(s1, s2);
2020+
}
2021+
inline char* StrDup(const char* src) { return strdup(src); }
2022+
#else // !__BORLANDC__
2023+
#if GTEST_OS_WINDOWS_MOBILE || GTEST_OS_ZOS || GTEST_OS_IOS || \
2024+
GTEST_OS_WINDOWS_PHONE || GTEST_OS_WINDOWS_RT || defined(ESP_PLATFORM)
2025+
inline int DoIsATTY(int /* fd */) { return 0; }
2026+
#else
2027+
inline int DoIsATTY(int fd) { return _isatty(fd); }
2028+
#endif // GTEST_OS_WINDOWS_MOBILE
2029+
inline int StrCaseCmp(const char* s1, const char* s2) {
2030+
return _stricmp(s1, s2);
2031+
}
2032+
inline char* StrDup(const char* src) { return _strdup(src); }
2033+
#endif // __BORLANDC__
2034+
2035+
#elif GTEST_OS_ESP8266
2036+
2037+
inline int DoIsATTY(int fd) { return isatty(fd); }
2038+
inline int StrCaseCmp(const char* s1, const char* s2) {
2039+
return strcasecmp(s1, s2);
2040+
}
2041+
inline char* StrDup(const char* src) { return strdup(src); }
2042+
2043+
#else
2044+
2045+
inline int DoIsATTY(int fd) { return isatty(fd); }
2046+
inline int StrCaseCmp(const char* s1, const char* s2) {
2047+
return strcasecmp(s1, s2);
2048+
}
2049+
inline char* StrDup(const char* src) { return strdup(src); }
2050+
20272051
#endif // GTEST_OS_WINDOWS
20282052

20292053
inline int IsATTY(int fd) {
@@ -2044,7 +2068,7 @@ GTEST_DISABLE_MSC_DEPRECATED_PUSH_()
20442068
// ChDir(), FReopen(), FDOpen(), Read(), Write(), Close(), and
20452069
// StrError() aren't needed on Windows CE at this time and thus not
20462070
// defined there.
2047-
2071+
#if GTEST_HAS_FILE_SYSTEM
20482072
#if !GTEST_OS_WINDOWS_MOBILE && !GTEST_OS_WINDOWS_PHONE && \
20492073
!GTEST_OS_WINDOWS_RT && !GTEST_OS_ESP8266 && !GTEST_OS_XTENSA && \
20502074
!GTEST_OS_QURT
@@ -2066,7 +2090,7 @@ inline FILE* FReopen(const char* path, const char* mode, FILE* stream) {
20662090
return freopen(path, mode, stream);
20672091
}
20682092
inline FILE* FDOpen(int fd, const char* mode) { return fdopen(fd, mode); }
2069-
#endif
2093+
#endif // !GTEST_OS_WINDOWS_MOBILE && !GTEST_OS_QURT
20702094
inline int FClose(FILE* fp) { return fclose(fp); }
20712095
#if !GTEST_OS_WINDOWS_MOBILE && !GTEST_OS_QURT
20722096
inline int Read(int fd, void* buf, unsigned int count) {
@@ -2076,8 +2100,13 @@ inline int Write(int fd, const void* buf, unsigned int count) {
20762100
return static_cast<int>(write(fd, buf, count));
20772101
}
20782102
inline int Close(int fd) { return close(fd); }
2103+
#endif // !GTEST_OS_WINDOWS_MOBILE && !GTEST_OS_QURT
2104+
#endif // GTEST_HAS_FILE_SYSTEM
2105+
2106+
#if !GTEST_OS_WINDOWS_MOBILE && !GTEST_OS_QURT
20792107
inline const char* StrError(int errnum) { return strerror(errnum); }
2080-
#endif
2108+
#endif // !GTEST_OS_WINDOWS_MOBILE && !GTEST_OS_QURT
2109+
20812110
inline const char* GetEnv(const char* name) {
20822111
#if GTEST_OS_WINDOWS_MOBILE || GTEST_OS_WINDOWS_PHONE || \
20832112
GTEST_OS_WINDOWS_RT || GTEST_OS_ESP8266 || GTEST_OS_XTENSA || \

googletest/src/gtest-filepath.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@
5757
#define GTEST_PATH_MAX_ _POSIX_PATH_MAX
5858
#endif // GTEST_OS_WINDOWS
5959

60+
#if GTEST_HAS_FILE_SYSTEM
61+
6062
namespace testing {
6163
namespace internal {
6264

@@ -404,3 +406,5 @@ void FilePath::Normalize() {
404406

405407
} // namespace internal
406408
} // namespace testing
409+
410+
#endif // GTEST_HAS_FILE_SYSTEM

googletest/src/gtest-internal-inl.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,9 +396,11 @@ class GTEST_API_ UnitTestOptions {
396396
static bool MatchesFilter(const std::string& name, const char* filter);
397397
};
398398

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

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

845+
#if GTEST_HAS_FILE_SYSTEM
843846
// The working directory when the first TEST() or TEST_F() was
844847
// executed.
845848
internal::FilePath original_working_dir_;
849+
#endif // GTEST_HAS_FILE_SYSTEM
846850

847851
// The default test part result reporters.
848852
DefaultGlobalTestPartResultReporter default_global_test_part_result_reporter_;

0 commit comments

Comments
 (0)