41 #include "gtest/internal/gtest-filepath.h" 42 #include "gtest/gtest.h" 49 #define GTEST_IMPLEMENTATION_ 1 50 #include "src/gtest-internal-inl.h" 51 #undef GTEST_IMPLEMENTATION_ 53 #if GTEST_OS_WINDOWS_MOBILE 55 #elif GTEST_OS_WINDOWS 57 #endif // GTEST_OS_WINDOWS_MOBILE 63 #if GTEST_OS_WINDOWS_MOBILE 68 int remove(
const char* path) {
69 LPCWSTR wpath = String::AnsiToUtf16(path);
70 int ret = DeleteFile(wpath) ? 0 : -1;
75 int _rmdir(
const char* path) {
76 FilePath filepath(path);
77 LPCWSTR wpath = String::AnsiToUtf16(
78 filepath.RemoveTrailingPathSeparator().c_str());
79 int ret = RemoveDirectory(wpath) ? 0 : -1;
86 TEST(GetCurrentDirTest, ReturnsCurrentDir) {
97 const char*
const cwd_without_drive = strchr(cwd.c_str(),
':');
108 #endif // GTEST_OS_WINDOWS_MOBILE 110 TEST(IsEmptyTest, ReturnsTrueForEmptyPath) {
114 TEST(IsEmptyTest, ReturnsFalseForNonEmptyPath) {
122 TEST(RemoveDirectoryNameTest, WhenEmptyName) {
123 EXPECT_EQ(
"", FilePath(
"").RemoveDirectoryName().
string());
127 TEST(RemoveDirectoryNameTest, ButNoDirectory) {
129 FilePath(
"afile").RemoveDirectoryName().
string());
133 TEST(RemoveDirectoryNameTest, RootFileShouldGiveFileName) {
139 TEST(RemoveDirectoryNameTest, WhereThereIsNoFileName) {
145 TEST(RemoveDirectoryNameTest, ShouldGiveFileName) {
147 FilePath(
"adir" GTEST_PATH_SEP_ "afile").RemoveDirectoryName().
string());
151 TEST(RemoveDirectoryNameTest, ShouldAlsoGiveFileName) {
154 .RemoveDirectoryName().
string());
157 #if GTEST_HAS_ALT_PATH_SEP_ 163 TEST(RemoveDirectoryNameTest, RootFileShouldGiveFileNameForAlternateSeparator) {
164 EXPECT_EQ(
"afile", FilePath(
"/afile").RemoveDirectoryName().
string());
168 TEST(RemoveDirectoryNameTest, WhereThereIsNoFileNameForAlternateSeparator) {
169 EXPECT_EQ(
"", FilePath(
"adir/").RemoveDirectoryName().
string());
173 TEST(RemoveDirectoryNameTest, ShouldGiveFileNameForAlternateSeparator) {
174 EXPECT_EQ(
"afile", FilePath(
"adir/afile").RemoveDirectoryName().
string());
178 TEST(RemoveDirectoryNameTest, ShouldAlsoGiveFileNameForAlternateSeparator) {
180 FilePath(
"adir/subdir/afile").RemoveDirectoryName().
string());
186 TEST(RemoveFileNameTest, EmptyName) {
187 #if GTEST_OS_WINDOWS_MOBILE 196 TEST(RemoveFileNameTest, ButNoFile) {
202 TEST(RemoveFileNameTest, GivesDirName) {
208 TEST(RemoveFileNameTest, GivesDirAndSubDirName) {
210 FilePath(
"adir" GTEST_PATH_SEP_
"subdir" GTEST_PATH_SEP_
"afile")
211 .RemoveFileName().
string());
215 TEST(RemoveFileNameTest, GivesRootDir) {
220 #if GTEST_HAS_ALT_PATH_SEP_ 226 TEST(RemoveFileNameTest, ButNoFileForAlternateSeparator) {
228 FilePath(
"adir/").RemoveFileName().
string());
232 TEST(RemoveFileNameTest, GivesDirNameForAlternateSeparator) {
234 FilePath(
"adir/afile").RemoveFileName().
string());
238 TEST(RemoveFileNameTest, GivesDirAndSubDirNameForAlternateSeparator) {
240 FilePath(
"adir/subdir/afile").RemoveFileName().
string());
244 TEST(RemoveFileNameTest, GivesRootDirForAlternateSeparator) {
250 TEST(MakeFileNameTest, GenerateWhenNumberIsZero) {
256 TEST(MakeFileNameTest, GenerateFileNameNumberGtZero) {
262 TEST(MakeFileNameTest, GenerateFileNameWithSlashNumberIsZero) {
264 FilePath(
"bar"), 0,
"xml");
268 TEST(MakeFileNameTest, GenerateFileNameWithSlashNumberGtZero) {
270 FilePath(
"bar"), 12,
"xml");
274 TEST(MakeFileNameTest, GenerateWhenNumberIsZeroAndDirIsEmpty) {
280 TEST(MakeFileNameTest, GenerateWhenNumberIsNotZeroAndDirIsEmpty) {
283 EXPECT_EQ(
"bar_14.xml", actual.string());
286 TEST(ConcatPathsTest, WorksWhenDirDoesNotEndWithPathSep) {
288 FilePath(
"bar.xml"));
292 TEST(ConcatPathsTest, WorksWhenPath1EndsWithPathSep) {
294 FilePath(
"bar.xml"));
298 TEST(ConcatPathsTest, Path1BeingEmpty) {
300 FilePath(
"bar.xml"));
304 TEST(ConcatPathsTest, Path2BeingEmpty) {
309 TEST(ConcatPathsTest, BothPathBeingEmpty) {
315 TEST(ConcatPathsTest, Path1ContainsPathSep) {
317 FilePath(
"foobar.xml"));
322 TEST(ConcatPathsTest, Path2ContainsPathSep) {
330 TEST(ConcatPathsTest, Path2EndsWithPathSep) {
337 TEST(RemoveTrailingPathSeparatorTest, EmptyString) {
338 EXPECT_EQ(
"", FilePath(
"").RemoveTrailingPathSeparator().
string());
342 TEST(RemoveTrailingPathSeparatorTest, FileNoSlashString) {
343 EXPECT_EQ(
"foo", FilePath(
"foo").RemoveTrailingPathSeparator().
string());
347 TEST(RemoveTrailingPathSeparatorTest, ShouldRemoveTrailingSeparator) {
349 FilePath(
"foo" GTEST_PATH_SEP_).RemoveTrailingPathSeparator().
string());
350 #if GTEST_HAS_ALT_PATH_SEP_ 351 EXPECT_EQ(
"foo", FilePath(
"foo/").RemoveTrailingPathSeparator().
string());
356 TEST(RemoveTrailingPathSeparatorTest, ShouldRemoveLastSeparator) {
359 .RemoveTrailingPathSeparator().
string());
363 TEST(RemoveTrailingPathSeparatorTest, ShouldReturnUnmodified) {
366 .RemoveTrailingPathSeparator().
string());
369 TEST(DirectoryTest, RootDirectoryExists) {
370 #if GTEST_OS_WINDOWS // We are on Windows. 371 char current_drive[_MAX_PATH];
372 current_drive[0] =
static_cast<char>(_getdrive() +
'A' - 1);
373 current_drive[1] =
':';
374 current_drive[2] =
'\\';
375 current_drive[3] =
'\0';
376 EXPECT_TRUE(FilePath(current_drive).DirectoryExists());
379 #endif // GTEST_OS_WINDOWS 383 TEST(DirectoryTest, RootOfWrongDriveDoesNotExists) {
384 const int saved_drive_ = _getdrive();
386 for (
char drive =
'Z'; drive >=
'A'; drive--)
387 if (_chdrive(drive -
'A' + 1) == -1) {
388 char non_drive[_MAX_PATH];
389 non_drive[0] = drive;
396 _chdrive(saved_drive_);
398 #endif // GTEST_OS_WINDOWS 400 #if !GTEST_OS_WINDOWS_MOBILE 402 TEST(DirectoryTest, EmptyPathDirectoryDoesNotExist) {
405 #endif // !GTEST_OS_WINDOWS_MOBILE 407 TEST(DirectoryTest, CurrentDirectoryExists) {
408 #if GTEST_OS_WINDOWS // We are on Windows. 409 # ifndef _WIN32_CE // Windows CE doesn't have a current directory. 418 #endif // GTEST_OS_WINDOWS 422 TEST(NormalizeTest, MultipleConsecutiveSepaparatorsInMidstring) {
428 FilePath(
"foo" GTEST_PATH_SEP_ GTEST_PATH_SEP_
429 GTEST_PATH_SEP_
"bar").
string());
433 TEST(NormalizeTest, MultipleConsecutiveSepaparatorsAtStringStart) {
439 FilePath(GTEST_PATH_SEP_ GTEST_PATH_SEP_ GTEST_PATH_SEP_
"bar").
string());
443 TEST(NormalizeTest, MultipleConsecutiveSepaparatorsAtStringEnd) {
449 FilePath(
"foo" GTEST_PATH_SEP_ GTEST_PATH_SEP_ GTEST_PATH_SEP_).
string());
452 #if GTEST_HAS_ALT_PATH_SEP_ 457 TEST(NormalizeTest, MixAlternateSeparatorAtStringEnd) {
459 FilePath(
"foo/").
string());
468 TEST(AssignmentOperatorTest, DefaultAssignedToNonDefault) {
469 FilePath default_path;
470 FilePath non_default_path(
"path");
471 non_default_path = default_path;
472 EXPECT_EQ(
"", non_default_path.string());
476 TEST(AssignmentOperatorTest, NonDefaultAssignedToDefault) {
477 FilePath non_default_path(
"path");
478 FilePath default_path;
479 default_path = non_default_path;
480 EXPECT_EQ(
"path", default_path.string());
481 EXPECT_EQ(
"path", non_default_path.string());
484 TEST(AssignmentOperatorTest, ConstAssignedToNonConst) {
485 const FilePath const_default_path(
"const_path");
486 FilePath non_default_path(
"path");
487 non_default_path = const_default_path;
488 EXPECT_EQ(
"const_path", non_default_path.string());
491 class DirectoryCreationTest :
public Test {
493 virtual void SetUp() {
510 virtual void TearDown() {
518 #if GTEST_OS_WINDOWS_MOBILE 520 #elif GTEST_OS_WINDOWS 522 if (temp_dir == NULL || temp_dir[0] ==
'\0')
524 else if (temp_dir[strlen(temp_dir) - 1] ==
'\\')
528 #elif GTEST_OS_LINUX_ANDROID 532 #endif // GTEST_OS_WINDOWS_MOBILE 535 void CreateTextFile(
const char* filename) {
550 TEST_F(DirectoryCreationTest, CreateDirectoriesRecursively) {
556 TEST_F(DirectoryCreationTest, CreateDirectoriesForAlreadyExistingPath) {
563 TEST_F(DirectoryCreationTest, CreateDirectoriesAndUniqueFilename) {
565 FilePath(
"unique"),
"txt"));
571 CreateTextFile(file_path.c_str());
575 FilePath(
"unique"),
"txt"));
578 CreateTextFile(file_path2.c_str());
582 TEST_F(DirectoryCreationTest, CreateDirectoriesFail) {
590 TEST(NoDirectoryCreationTest, CreateNoDirectoriesForDefaultXmlFile) {
591 const FilePath test_detail_xml(
"test_detail.xml");
592 EXPECT_FALSE(test_detail_xml.CreateDirectoriesRecursively());
595 TEST(FilePathTest, DefaultConstructor) {
600 TEST(FilePathTest, CharAndCopyConstructors) {
601 const FilePath fp(
"spicy");
604 const FilePath fp_copy(fp);
608 TEST(FilePathTest, StringConstructor) {
613 TEST(FilePathTest, Set) {
614 const FilePath apple(
"apple");
622 const FilePath file(
"drink");
626 TEST(FilePathTest, RemoveExtension) {
627 EXPECT_EQ(
"app", FilePath(
"app.cc").RemoveExtension(
"cc").
string());
628 EXPECT_EQ(
"app", FilePath(
"app.exe").RemoveExtension(
"exe").
string());
629 EXPECT_EQ(
"APP", FilePath(
"APP.EXE").RemoveExtension(
"exe").
string());
632 TEST(FilePathTest, RemoveExtensionWhenThereIsNoExtension) {
633 EXPECT_EQ(
"app", FilePath(
"app").RemoveExtension(
"exe").
string());
636 TEST(FilePathTest, IsDirectory) {
639 #if GTEST_HAS_ALT_PATH_SEP_ 644 TEST(FilePathTest, IsAbsolutePath) {
650 EXPECT_FALSE(FilePath(
"c:foo" GTEST_PATH_SEP_
"bar").IsAbsolutePath());
651 EXPECT_TRUE(FilePath(
"c:/" GTEST_PATH_SEP_
"is_not" 652 GTEST_PATH_SEP_
"relative").IsAbsolutePath());
654 EXPECT_TRUE(FilePath(GTEST_PATH_SEP_
"is_not" GTEST_PATH_SEP_
"relative")
656 #endif // GTEST_OS_WINDOWS 659 TEST(FilePathTest, IsRootDirectory) {
static FilePath ConcatPaths(const FilePath &directory, const FilePath &relative_path)
FILE * FOpen(const char *path, const char *mode)
int ChDir(const char *dir)
#define EXPECT_TRUE(condition)
#define EXPECT_STREQ(expected, actual)
TEST_F(ListenerTest, DoesFoo)
int RmDir(const char *dir)
#define EXPECT_FALSE(condition)
GTEST_API_ FilePath GetCurrentExecutableName()
#define ASSERT_TRUE(condition)
static FilePath GenerateUniqueFileName(const FilePath &directory, const FilePath &base_name, const char *extension)
static FilePath GetCurrentDir()
static FilePath MakeFileName(const FilePath &directory, const FilePath &base_name, int number, const char *extension)
std::string ToString(const AttributeId &value)
FMT_API int fprintf(std::FILE *f, CStringRef format, ArgList args)
const char * GetEnv(const char *name)
#define EXPECT_EQ(expected, actual)
TEST(IsXDigitTest, WorksForNarrowAscii)