1 // Copyright 2008, Google Inc. 2 // All rights reserved. 3 // 4 // Redistribution and use in source and binary forms, with or without 5 // modification, are permitted provided that the following conditions are 6 // met: 7 // 8 // * Redistributions of source code must retain the above copyright 9 // notice, this list of conditions and the following disclaimer. 10 // * Redistributions in binary form must reproduce the above 11 // copyright notice, this list of conditions and the following disclaimer 12 // in the documentation and/or other materials provided with the 13 // distribution. 14 // * Neither the name of Google Inc. nor the names of its 15 // contributors may be used to endorse or promote products derived from 16 // this software without specific prior written permission. 17 // 18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 // 30 // Authors: keith.ray (at) gmail.com (Keith Ray) 31 // 32 // Google Test filepath utilities 33 // 34 // This file tests classes and functions used internally by 35 // Google Test. They are subject to change without notice. 36 // 37 // This file is #included from gtest_unittest.cc, to avoid changing 38 // build or make-files for some existing Google Test clients. Do not 39 // #include this file anywhere else! 40 41 #include <gtest/internal/gtest-filepath.h> 42 #include <gtest/gtest.h> 43 44 // Indicates that this translation unit is part of Google Test's 45 // implementation. It must come before gtest-internal-inl.h is 46 // included, or there will be a compiler error. This trick is to 47 // prevent a user from accidentally including gtest-internal-inl.h in 48 // his code. 49 #define GTEST_IMPLEMENTATION_ 1 50 #include "src/gtest-internal-inl.h" 51 #undef GTEST_IMPLEMENTATION_ 52 53 #ifdef _WIN32_WCE 54 #include <windows.h> // NOLINT 55 #elif GTEST_OS_WINDOWS 56 #include <direct.h> // NOLINT 57 #endif // _WIN32_WCE 58 59 namespace testing { 60 namespace internal { 61 namespace { 62 63 #ifdef _WIN32_WCE 64 // Windows CE doesn't have the remove C function. 65 int remove(const char* path) { 66 LPCWSTR wpath = String::AnsiToUtf16(path); 67 int ret = DeleteFile(wpath) ? 0 : -1; 68 delete [] wpath; 69 return ret; 70 } 71 // Windows CE doesn't have the _rmdir C function. 72 int _rmdir(const char* path) { 73 FilePath filepath(path); 74 LPCWSTR wpath = String::AnsiToUtf16( 75 filepath.RemoveTrailingPathSeparator().c_str()); 76 int ret = RemoveDirectory(wpath) ? 0 : -1; 77 delete [] wpath; 78 return ret; 79 } 80 81 #endif // _WIN32_WCE 82 83 #ifndef _WIN32_WCE 84 85 TEST(GetCurrentDirTest, ReturnsCurrentDir) { 86 const FilePath original_dir = FilePath::GetCurrentDir(); 87 EXPECT_FALSE(original_dir.IsEmpty()); 88 89 #if GTEST_OS_WINDOWS 90 _chdir(GTEST_PATH_SEP_); 91 const FilePath cwd = FilePath::GetCurrentDir(); 92 _chdir(original_dir.c_str()); 93 // Skips the ":". 94 const char* const cwd_without_drive = strchr(cwd.c_str(), ':'); 95 ASSERT_TRUE(cwd_without_drive != NULL); 96 EXPECT_STREQ(GTEST_PATH_SEP_, cwd_without_drive + 1); 97 #else 98 chdir(GTEST_PATH_SEP_); 99 EXPECT_STREQ(GTEST_PATH_SEP_, FilePath::GetCurrentDir().c_str()); 100 chdir(original_dir.c_str()); 101 #endif 102 } 103 104 #endif // _WIN32_WCE 105 106 TEST(IsEmptyTest, ReturnsTrueForEmptyPath) { 107 EXPECT_TRUE(FilePath("").IsEmpty()); 108 EXPECT_TRUE(FilePath(NULL).IsEmpty()); 109 } 110 111 TEST(IsEmptyTest, ReturnsFalseForNonEmptyPath) { 112 EXPECT_FALSE(FilePath("a").IsEmpty()); 113 EXPECT_FALSE(FilePath(".").IsEmpty()); 114 EXPECT_FALSE(FilePath("a/b").IsEmpty()); 115 EXPECT_FALSE(FilePath("a\\b\\").IsEmpty()); 116 } 117 118 // RemoveDirectoryName "" -> "" 119 TEST(RemoveDirectoryNameTest, WhenEmptyName) { 120 EXPECT_STREQ("", FilePath("").RemoveDirectoryName().c_str()); 121 } 122 123 // RemoveDirectoryName "afile" -> "afile" 124 TEST(RemoveDirectoryNameTest, ButNoDirectory) { 125 EXPECT_STREQ("afile", 126 FilePath("afile").RemoveDirectoryName().c_str()); 127 } 128 129 // RemoveDirectoryName "/afile" -> "afile" 130 TEST(RemoveDirectoryNameTest, RootFileShouldGiveFileName) { 131 EXPECT_STREQ("afile", 132 FilePath(GTEST_PATH_SEP_ "afile").RemoveDirectoryName().c_str()); 133 } 134 135 // RemoveDirectoryName "adir/" -> "" 136 TEST(RemoveDirectoryNameTest, WhereThereIsNoFileName) { 137 EXPECT_STREQ("", 138 FilePath("adir" GTEST_PATH_SEP_).RemoveDirectoryName().c_str()); 139 } 140 141 // RemoveDirectoryName "adir/afile" -> "afile" 142 TEST(RemoveDirectoryNameTest, ShouldGiveFileName) { 143 EXPECT_STREQ("afile", 144 FilePath("adir" GTEST_PATH_SEP_ "afile").RemoveDirectoryName().c_str()); 145 } 146 147 // RemoveDirectoryName "adir/subdir/afile" -> "afile" 148 TEST(RemoveDirectoryNameTest, ShouldAlsoGiveFileName) { 149 EXPECT_STREQ("afile", 150 FilePath("adir" GTEST_PATH_SEP_ "subdir" GTEST_PATH_SEP_ "afile") 151 .RemoveDirectoryName().c_str()); 152 } 153 154 155 // RemoveFileName "" -> "./" 156 TEST(RemoveFileNameTest, EmptyName) { 157 #ifdef _WIN32_WCE 158 // On Windows CE, we use the root as the current directory. 159 EXPECT_STREQ(GTEST_PATH_SEP_, 160 FilePath("").RemoveFileName().c_str()); 161 #else 162 EXPECT_STREQ("." GTEST_PATH_SEP_, 163 FilePath("").RemoveFileName().c_str()); 164 #endif 165 } 166 167 // RemoveFileName "adir/" -> "adir/" 168 TEST(RemoveFileNameTest, ButNoFile) { 169 EXPECT_STREQ("adir" GTEST_PATH_SEP_, 170 FilePath("adir" GTEST_PATH_SEP_).RemoveFileName().c_str()); 171 } 172 173 // RemoveFileName "adir/afile" -> "adir/" 174 TEST(RemoveFileNameTest, GivesDirName) { 175 EXPECT_STREQ("adir" GTEST_PATH_SEP_, 176 FilePath("adir" GTEST_PATH_SEP_ "afile") 177 .RemoveFileName().c_str()); 178 } 179 180 // RemoveFileName "adir/subdir/afile" -> "adir/subdir/" 181 TEST(RemoveFileNameTest, GivesDirAndSubDirName) { 182 EXPECT_STREQ("adir" GTEST_PATH_SEP_ "subdir" GTEST_PATH_SEP_, 183 FilePath("adir" GTEST_PATH_SEP_ "subdir" GTEST_PATH_SEP_ "afile") 184 .RemoveFileName().c_str()); 185 } 186 187 // RemoveFileName "/afile" -> "/" 188 TEST(RemoveFileNameTest, GivesRootDir) { 189 EXPECT_STREQ(GTEST_PATH_SEP_, 190 FilePath(GTEST_PATH_SEP_ "afile").RemoveFileName().c_str()); 191 } 192 193 194 TEST(MakeFileNameTest, GenerateWhenNumberIsZero) { 195 FilePath actual = FilePath::MakeFileName(FilePath("foo"), FilePath("bar"), 196 0, "xml"); 197 EXPECT_STREQ("foo" GTEST_PATH_SEP_ "bar.xml", actual.c_str()); 198 } 199 200 TEST(MakeFileNameTest, GenerateFileNameNumberGtZero) { 201 FilePath actual = FilePath::MakeFileName(FilePath("foo"), FilePath("bar"), 202 12, "xml"); 203 EXPECT_STREQ("foo" GTEST_PATH_SEP_ "bar_12.xml", actual.c_str()); 204 } 205 206 TEST(MakeFileNameTest, GenerateFileNameWithSlashNumberIsZero) { 207 FilePath actual = FilePath::MakeFileName(FilePath("foo" GTEST_PATH_SEP_), 208 FilePath("bar"), 0, "xml"); 209 EXPECT_STREQ("foo" GTEST_PATH_SEP_ "bar.xml", actual.c_str()); 210 } 211 212 TEST(MakeFileNameTest, GenerateFileNameWithSlashNumberGtZero) { 213 FilePath actual = FilePath::MakeFileName(FilePath("foo" GTEST_PATH_SEP_), 214 FilePath("bar"), 12, "xml"); 215 EXPECT_STREQ("foo" GTEST_PATH_SEP_ "bar_12.xml", actual.c_str()); 216 } 217 218 TEST(MakeFileNameTest, GenerateWhenNumberIsZeroAndDirIsEmpty) { 219 FilePath actual = FilePath::MakeFileName(FilePath(""), FilePath("bar"), 220 0, "xml"); 221 EXPECT_STREQ("bar.xml", actual.c_str()); 222 } 223 224 TEST(MakeFileNameTest, GenerateWhenNumberIsNotZeroAndDirIsEmpty) { 225 FilePath actual = FilePath::MakeFileName(FilePath(""), FilePath("bar"), 226 14, "xml"); 227 EXPECT_STREQ("bar_14.xml", actual.c_str()); 228 } 229 230 TEST(ConcatPathsTest, WorksWhenDirDoesNotEndWithPathSep) { 231 FilePath actual = FilePath::ConcatPaths(FilePath("foo"), 232 FilePath("bar.xml")); 233 EXPECT_STREQ("foo" GTEST_PATH_SEP_ "bar.xml", actual.c_str()); 234 } 235 236 TEST(ConcatPathsTest, WorksWhenPath1EndsWithPathSep) { 237 FilePath actual = FilePath::ConcatPaths(FilePath("foo" GTEST_PATH_SEP_), 238 FilePath("bar.xml")); 239 EXPECT_STREQ("foo" GTEST_PATH_SEP_ "bar.xml", actual.c_str()); 240 } 241 242 TEST(ConcatPathsTest, Path1BeingEmpty) { 243 FilePath actual = FilePath::ConcatPaths(FilePath(""), 244 FilePath("bar.xml")); 245 EXPECT_STREQ("bar.xml", actual.c_str()); 246 } 247 248 TEST(ConcatPathsTest, Path2BeingEmpty) { 249 FilePath actual = FilePath::ConcatPaths(FilePath("foo"), 250 FilePath("")); 251 EXPECT_STREQ("foo" GTEST_PATH_SEP_, actual.c_str()); 252 } 253 254 TEST(ConcatPathsTest, BothPathBeingEmpty) { 255 FilePath actual = FilePath::ConcatPaths(FilePath(""), 256 FilePath("")); 257 EXPECT_STREQ("", actual.c_str()); 258 } 259 260 TEST(ConcatPathsTest, Path1ContainsPathSep) { 261 FilePath actual = FilePath::ConcatPaths(FilePath("foo" GTEST_PATH_SEP_ "bar"), 262 FilePath("foobar.xml")); 263 EXPECT_STREQ("foo" GTEST_PATH_SEP_ "bar" GTEST_PATH_SEP_ "foobar.xml", 264 actual.c_str()); 265 } 266 267 TEST(ConcatPathsTest, Path2ContainsPathSep) { 268 FilePath actual = FilePath::ConcatPaths( 269 FilePath("foo" GTEST_PATH_SEP_), 270 FilePath("bar" GTEST_PATH_SEP_ "bar.xml")); 271 EXPECT_STREQ("foo" GTEST_PATH_SEP_ "bar" GTEST_PATH_SEP_ "bar.xml", 272 actual.c_str()); 273 } 274 275 TEST(ConcatPathsTest, Path2EndsWithPathSep) { 276 FilePath actual = FilePath::ConcatPaths(FilePath("foo"), 277 FilePath("bar" GTEST_PATH_SEP_)); 278 EXPECT_STREQ("foo" GTEST_PATH_SEP_ "bar" GTEST_PATH_SEP_, actual.c_str()); 279 } 280 281 // RemoveTrailingPathSeparator "" -> "" 282 TEST(RemoveTrailingPathSeparatorTest, EmptyString) { 283 EXPECT_STREQ("", 284 FilePath("").RemoveTrailingPathSeparator().c_str()); 285 } 286 287 // RemoveTrailingPathSeparator "foo" -> "foo" 288 TEST(RemoveTrailingPathSeparatorTest, FileNoSlashString) { 289 EXPECT_STREQ("foo", 290 FilePath("foo").RemoveTrailingPathSeparator().c_str()); 291 } 292 293 // RemoveTrailingPathSeparator "foo/" -> "foo" 294 TEST(RemoveTrailingPathSeparatorTest, ShouldRemoveTrailingSeparator) { 295 EXPECT_STREQ( 296 "foo", 297 FilePath("foo" GTEST_PATH_SEP_).RemoveTrailingPathSeparator().c_str()); 298 } 299 300 // RemoveTrailingPathSeparator "foo/bar/" -> "foo/bar/" 301 TEST(RemoveTrailingPathSeparatorTest, ShouldRemoveLastSeparator) { 302 EXPECT_STREQ("foo" GTEST_PATH_SEP_ "bar", 303 FilePath("foo" GTEST_PATH_SEP_ "bar" GTEST_PATH_SEP_) 304 .RemoveTrailingPathSeparator().c_str()); 305 } 306 307 // RemoveTrailingPathSeparator "foo/bar" -> "foo/bar" 308 TEST(RemoveTrailingPathSeparatorTest, ShouldReturnUnmodified) { 309 EXPECT_STREQ("foo" GTEST_PATH_SEP_ "bar", 310 FilePath("foo" GTEST_PATH_SEP_ "bar") 311 .RemoveTrailingPathSeparator().c_str()); 312 } 313 314 TEST(DirectoryTest, RootDirectoryExists) { 315 #if GTEST_OS_WINDOWS // We are on Windows. 316 char current_drive[_MAX_PATH]; // NOLINT 317 current_drive[0] = static_cast<char>(_getdrive() + 'A' - 1); 318 current_drive[1] = ':'; 319 current_drive[2] = '\\'; 320 current_drive[3] = '\0'; 321 EXPECT_TRUE(FilePath(current_drive).DirectoryExists()); 322 #else 323 EXPECT_TRUE(FilePath("/").DirectoryExists()); 324 #endif // GTEST_OS_WINDOWS 325 } 326 327 #if GTEST_OS_WINDOWS 328 TEST(DirectoryTest, RootOfWrongDriveDoesNotExists) { 329 const int saved_drive_ = _getdrive(); 330 // Find a drive that doesn't exist. Start with 'Z' to avoid common ones. 331 for (char drive = 'Z'; drive >= 'A'; drive--) 332 if (_chdrive(drive - 'A' + 1) == -1) { 333 char non_drive[_MAX_PATH]; // NOLINT 334 non_drive[0] = drive; 335 non_drive[1] = ':'; 336 non_drive[2] = '\\'; 337 non_drive[3] = '\0'; 338 EXPECT_FALSE(FilePath(non_drive).DirectoryExists()); 339 break; 340 } 341 _chdrive(saved_drive_); 342 } 343 #endif // GTEST_OS_WINDOWS 344 345 #ifndef _WIN32_WCE 346 // Windows CE _does_ consider an empty directory to exist. 347 TEST(DirectoryTest, EmptyPathDirectoryDoesNotExist) { 348 EXPECT_FALSE(FilePath("").DirectoryExists()); 349 } 350 #endif // ! _WIN32_WCE 351 352 TEST(DirectoryTest, CurrentDirectoryExists) { 353 #if GTEST_OS_WINDOWS // We are on Windows. 354 #ifndef _WIN32_CE // Windows CE doesn't have a current directory. 355 EXPECT_TRUE(FilePath(".").DirectoryExists()); 356 EXPECT_TRUE(FilePath(".\\").DirectoryExists()); 357 #endif // _WIN32_CE 358 #else 359 EXPECT_TRUE(FilePath(".").DirectoryExists()); 360 EXPECT_TRUE(FilePath("./").DirectoryExists()); 361 #endif // GTEST_OS_WINDOWS 362 } 363 364 TEST(NormalizeTest, NullStringsEqualEmptyDirectory) { 365 EXPECT_STREQ("", FilePath(NULL).c_str()); 366 EXPECT_STREQ("", FilePath(String(NULL)).c_str()); 367 } 368 369 // "foo/bar" == foo//bar" == "foo///bar" 370 TEST(NormalizeTest, MultipleConsecutiveSepaparatorsInMidstring) { 371 EXPECT_STREQ("foo" GTEST_PATH_SEP_ "bar", 372 FilePath("foo" GTEST_PATH_SEP_ "bar").c_str()); 373 EXPECT_STREQ("foo" GTEST_PATH_SEP_ "bar", 374 FilePath("foo" GTEST_PATH_SEP_ GTEST_PATH_SEP_ "bar").c_str()); 375 EXPECT_STREQ("foo" GTEST_PATH_SEP_ "bar", 376 FilePath("foo" GTEST_PATH_SEP_ GTEST_PATH_SEP_ 377 GTEST_PATH_SEP_ "bar").c_str()); 378 } 379 380 // "/bar" == //bar" == "///bar" 381 TEST(NormalizeTest, MultipleConsecutiveSepaparatorsAtStringStart) { 382 EXPECT_STREQ(GTEST_PATH_SEP_ "bar", 383 FilePath(GTEST_PATH_SEP_ "bar").c_str()); 384 EXPECT_STREQ(GTEST_PATH_SEP_ "bar", 385 FilePath(GTEST_PATH_SEP_ GTEST_PATH_SEP_ "bar").c_str()); 386 EXPECT_STREQ(GTEST_PATH_SEP_ "bar", 387 FilePath(GTEST_PATH_SEP_ GTEST_PATH_SEP_ GTEST_PATH_SEP_ "bar").c_str()); 388 } 389 390 // "foo/" == foo//" == "foo///" 391 TEST(NormalizeTest, MultipleConsecutiveSepaparatorsAtStringEnd) { 392 EXPECT_STREQ("foo" GTEST_PATH_SEP_, 393 FilePath("foo" GTEST_PATH_SEP_).c_str()); 394 EXPECT_STREQ("foo" GTEST_PATH_SEP_, 395 FilePath("foo" GTEST_PATH_SEP_ GTEST_PATH_SEP_).c_str()); 396 EXPECT_STREQ("foo" GTEST_PATH_SEP_, 397 FilePath("foo" GTEST_PATH_SEP_ GTEST_PATH_SEP_ GTEST_PATH_SEP_).c_str()); 398 } 399 400 TEST(AssignmentOperatorTest, DefaultAssignedToNonDefault) { 401 FilePath default_path; 402 FilePath non_default_path("path"); 403 non_default_path = default_path; 404 EXPECT_STREQ("", non_default_path.c_str()); 405 EXPECT_STREQ("", default_path.c_str()); // RHS var is unchanged. 406 } 407 408 TEST(AssignmentOperatorTest, NonDefaultAssignedToDefault) { 409 FilePath non_default_path("path"); 410 FilePath default_path; 411 default_path = non_default_path; 412 EXPECT_STREQ("path", default_path.c_str()); 413 EXPECT_STREQ("path", non_default_path.c_str()); // RHS var is unchanged. 414 } 415 416 TEST(AssignmentOperatorTest, ConstAssignedToNonConst) { 417 const FilePath const_default_path("const_path"); 418 FilePath non_default_path("path"); 419 non_default_path = const_default_path; 420 EXPECT_STREQ("const_path", non_default_path.c_str()); 421 } 422 423 class DirectoryCreationTest : public Test { 424 protected: 425 virtual void SetUp() { 426 testdata_path_.Set(FilePath(String::Format("%s%s%s", 427 TempDir().c_str(), GetCurrentExecutableName().c_str(), 428 "_directory_creation" GTEST_PATH_SEP_ "test" GTEST_PATH_SEP_))); 429 testdata_file_.Set(testdata_path_.RemoveTrailingPathSeparator()); 430 431 unique_file0_.Set(FilePath::MakeFileName(testdata_path_, FilePath("unique"), 432 0, "txt")); 433 unique_file1_.Set(FilePath::MakeFileName(testdata_path_, FilePath("unique"), 434 1, "txt")); 435 436 remove(testdata_file_.c_str()); 437 remove(unique_file0_.c_str()); 438 remove(unique_file1_.c_str()); 439 #if GTEST_OS_WINDOWS 440 _rmdir(testdata_path_.c_str()); 441 #else 442 rmdir(testdata_path_.c_str()); 443 #endif // GTEST_OS_WINDOWS 444 } 445 446 virtual void TearDown() { 447 remove(testdata_file_.c_str()); 448 remove(unique_file0_.c_str()); 449 remove(unique_file1_.c_str()); 450 #if GTEST_OS_WINDOWS 451 _rmdir(testdata_path_.c_str()); 452 #else 453 rmdir(testdata_path_.c_str()); 454 #endif // GTEST_OS_WINDOWS 455 } 456 457 String TempDir() const { 458 #ifdef _WIN32_WCE 459 return String("\\temp\\"); 460 461 #elif GTEST_OS_WINDOWS 462 // MSVC 8 deprecates getenv(), so we want to suppress warning 4996 463 // (deprecated function) there. 464 #pragma warning(push) // Saves the current warning state. 465 #pragma warning(disable:4996) // Temporarily disables warning 4996. 466 const char* temp_dir = getenv("TEMP"); 467 #pragma warning(pop) // Restores the warning state. 468 469 if (temp_dir == NULL || temp_dir[0] == '\0') 470 return String("\\temp\\"); 471 else if (String(temp_dir).EndsWith("\\")) 472 return String(temp_dir); 473 else 474 return String::Format("%s\\", temp_dir); 475 #elif GTEST_OS_ANDROID 476 return String("/sdcard/"); 477 #else 478 return String("/tmp/"); 479 #endif 480 } 481 482 void CreateTextFile(const char* filename) { 483 #if GTEST_OS_WINDOWS 484 // MSVC 8 deprecates fopen(), so we want to suppress warning 4996 485 // (deprecated function) there.#pragma warning(push) 486 #pragma warning(push) // Saves the current warning state. 487 #pragma warning(disable:4996) // Temporarily disables warning 4996. 488 FILE* f = fopen(filename, "w"); 489 #pragma warning(pop) // Restores the warning state. 490 #else // We are on Linux or Mac OS. 491 FILE* f = fopen(filename, "w"); 492 #endif // GTEST_OS_WINDOWS 493 fprintf(f, "text\n"); 494 fclose(f); 495 } 496 497 // Strings representing a directory and a file, with identical paths 498 // except for the trailing separator character that distinquishes 499 // a directory named 'test' from a file named 'test'. Example names: 500 FilePath testdata_path_; // "/tmp/directory_creation/test/" 501 FilePath testdata_file_; // "/tmp/directory_creation/test" 502 FilePath unique_file0_; // "/tmp/directory_creation/test/unique.txt" 503 FilePath unique_file1_; // "/tmp/directory_creation/test/unique_1.txt" 504 }; 505 506 TEST_F(DirectoryCreationTest, CreateDirectoriesRecursively) { 507 EXPECT_FALSE(testdata_path_.DirectoryExists()) << testdata_path_.c_str(); 508 EXPECT_TRUE(testdata_path_.CreateDirectoriesRecursively()); 509 EXPECT_TRUE(testdata_path_.DirectoryExists()); 510 } 511 512 TEST_F(DirectoryCreationTest, CreateDirectoriesForAlreadyExistingPath) { 513 EXPECT_FALSE(testdata_path_.DirectoryExists()) << testdata_path_.c_str(); 514 EXPECT_TRUE(testdata_path_.CreateDirectoriesRecursively()); 515 // Call 'create' again... should still succeed. 516 EXPECT_TRUE(testdata_path_.CreateDirectoriesRecursively()); 517 } 518 519 TEST_F(DirectoryCreationTest, CreateDirectoriesAndUniqueFilename) { 520 FilePath file_path(FilePath::GenerateUniqueFileName(testdata_path_, 521 FilePath("unique"), "txt")); 522 EXPECT_STREQ(unique_file0_.c_str(), file_path.c_str()); 523 EXPECT_FALSE(file_path.FileOrDirectoryExists()); // file not there 524 525 testdata_path_.CreateDirectoriesRecursively(); 526 EXPECT_FALSE(file_path.FileOrDirectoryExists()); // file still not there 527 CreateTextFile(file_path.c_str()); 528 EXPECT_TRUE(file_path.FileOrDirectoryExists()); 529 530 FilePath file_path2(FilePath::GenerateUniqueFileName(testdata_path_, 531 FilePath("unique"), "txt")); 532 EXPECT_STREQ(unique_file1_.c_str(), file_path2.c_str()); 533 EXPECT_FALSE(file_path2.FileOrDirectoryExists()); // file not there 534 CreateTextFile(file_path2.c_str()); 535 EXPECT_TRUE(file_path2.FileOrDirectoryExists()); 536 } 537 538 TEST_F(DirectoryCreationTest, CreateDirectoriesFail) { 539 // force a failure by putting a file where we will try to create a directory. 540 CreateTextFile(testdata_file_.c_str()); 541 EXPECT_TRUE(testdata_file_.FileOrDirectoryExists()); 542 EXPECT_FALSE(testdata_file_.DirectoryExists()); 543 EXPECT_FALSE(testdata_file_.CreateDirectoriesRecursively()); 544 } 545 546 TEST(NoDirectoryCreationTest, CreateNoDirectoriesForDefaultXmlFile) { 547 const FilePath test_detail_xml("test_detail.xml"); 548 EXPECT_FALSE(test_detail_xml.CreateDirectoriesRecursively()); 549 } 550 551 TEST(FilePathTest, DefaultConstructor) { 552 FilePath fp; 553 EXPECT_STREQ("", fp.c_str()); 554 } 555 556 TEST(FilePathTest, CharAndCopyConstructors) { 557 const FilePath fp("spicy"); 558 EXPECT_STREQ("spicy", fp.c_str()); 559 560 const FilePath fp_copy(fp); 561 EXPECT_STREQ("spicy", fp_copy.c_str()); 562 } 563 564 TEST(FilePathTest, StringConstructor) { 565 const FilePath fp(String("cider")); 566 EXPECT_STREQ("cider", fp.c_str()); 567 } 568 569 TEST(FilePathTest, Set) { 570 const FilePath apple("apple"); 571 FilePath mac("mac"); 572 mac.Set(apple); // Implement Set() since overloading operator= is forbidden. 573 EXPECT_STREQ("apple", mac.c_str()); 574 EXPECT_STREQ("apple", apple.c_str()); 575 } 576 577 TEST(FilePathTest, ToString) { 578 const FilePath file("drink"); 579 String str(file.ToString()); 580 EXPECT_STREQ("drink", str.c_str()); 581 } 582 583 TEST(FilePathTest, RemoveExtension) { 584 EXPECT_STREQ("app", FilePath("app.exe").RemoveExtension("exe").c_str()); 585 EXPECT_STREQ("APP", FilePath("APP.EXE").RemoveExtension("exe").c_str()); 586 } 587 588 TEST(FilePathTest, RemoveExtensionWhenThereIsNoExtension) { 589 EXPECT_STREQ("app", FilePath("app").RemoveExtension("exe").c_str()); 590 } 591 592 TEST(FilePathTest, IsDirectory) { 593 EXPECT_FALSE(FilePath("cola").IsDirectory()); 594 EXPECT_TRUE(FilePath("koala" GTEST_PATH_SEP_).IsDirectory()); 595 } 596 597 TEST(FilePathTest, IsAbsolutePath) { 598 EXPECT_FALSE(FilePath("is" GTEST_PATH_SEP_ "relative").IsAbsolutePath()); 599 EXPECT_FALSE(FilePath("").IsAbsolutePath()); 600 #if GTEST_OS_WINDOWS 601 EXPECT_TRUE(FilePath("c:\\" GTEST_PATH_SEP_ "is_not" 602 GTEST_PATH_SEP_ "relative").IsAbsolutePath()); 603 EXPECT_FALSE(FilePath("c:foo" GTEST_PATH_SEP_ "bar").IsAbsolutePath()); 604 #else 605 EXPECT_TRUE(FilePath(GTEST_PATH_SEP_ "is_not" GTEST_PATH_SEP_ "relative") 606 .IsAbsolutePath()); 607 #endif // GTEST_OS_WINDOWS 608 } 609 610 } // namespace 611 } // namespace internal 612 } // namespace testing 613 614 #undef GTEST_PATH_SEP_ 615