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 #if GTEST_OS_WINDOWS_MOBILE 54 # include <windows.h> // NOLINT 55 #elif GTEST_OS_WINDOWS 56 # include <direct.h> // NOLINT 57 #endif // GTEST_OS_WINDOWS_MOBILE 58 59 namespace testing { 60 namespace internal { 61 namespace { 62 63 #if GTEST_OS_WINDOWS_MOBILE 64 // TODO(wan (at) google.com): Move these to the POSIX adapter section in 65 // gtest-port.h. 66 67 // Windows CE doesn't have the remove C function. 68 int remove(const char* path) { 69 LPCWSTR wpath = String::AnsiToUtf16(path); 70 int ret = DeleteFile(wpath) ? 0 : -1; 71 delete [] wpath; 72 return ret; 73 } 74 // Windows CE doesn't have the _rmdir C function. 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; 80 delete [] wpath; 81 return ret; 82 } 83 84 #else 85 86 TEST(GetCurrentDirTest, ReturnsCurrentDir) { 87 const FilePath original_dir = FilePath::GetCurrentDir(); 88 EXPECT_FALSE(original_dir.IsEmpty()); 89 90 posix::ChDir(GTEST_PATH_SEP_); 91 const FilePath cwd = FilePath::GetCurrentDir(); 92 posix::ChDir(original_dir.c_str()); 93 94 # if GTEST_OS_WINDOWS 95 96 // Skips the ":". 97 const char* const cwd_without_drive = strchr(cwd.c_str(), ':'); 98 ASSERT_TRUE(cwd_without_drive != NULL); 99 EXPECT_STREQ(GTEST_PATH_SEP_, cwd_without_drive + 1); 100 101 # else 102 103 EXPECT_STREQ(GTEST_PATH_SEP_, cwd.c_str()); 104 105 # endif 106 } 107 108 #endif // GTEST_OS_WINDOWS_MOBILE 109 110 TEST(IsEmptyTest, ReturnsTrueForEmptyPath) { 111 EXPECT_TRUE(FilePath("").IsEmpty()); 112 EXPECT_TRUE(FilePath(NULL).IsEmpty()); 113 } 114 115 TEST(IsEmptyTest, ReturnsFalseForNonEmptyPath) { 116 EXPECT_FALSE(FilePath("a").IsEmpty()); 117 EXPECT_FALSE(FilePath(".").IsEmpty()); 118 EXPECT_FALSE(FilePath("a/b").IsEmpty()); 119 EXPECT_FALSE(FilePath("a\\b\\").IsEmpty()); 120 } 121 122 // RemoveDirectoryName "" -> "" 123 TEST(RemoveDirectoryNameTest, WhenEmptyName) { 124 EXPECT_STREQ("", FilePath("").RemoveDirectoryName().c_str()); 125 } 126 127 // RemoveDirectoryName "afile" -> "afile" 128 TEST(RemoveDirectoryNameTest, ButNoDirectory) { 129 EXPECT_STREQ("afile", 130 FilePath("afile").RemoveDirectoryName().c_str()); 131 } 132 133 // RemoveDirectoryName "/afile" -> "afile" 134 TEST(RemoveDirectoryNameTest, RootFileShouldGiveFileName) { 135 EXPECT_STREQ("afile", 136 FilePath(GTEST_PATH_SEP_ "afile").RemoveDirectoryName().c_str()); 137 } 138 139 // RemoveDirectoryName "adir/" -> "" 140 TEST(RemoveDirectoryNameTest, WhereThereIsNoFileName) { 141 EXPECT_STREQ("", 142 FilePath("adir" GTEST_PATH_SEP_).RemoveDirectoryName().c_str()); 143 } 144 145 // RemoveDirectoryName "adir/afile" -> "afile" 146 TEST(RemoveDirectoryNameTest, ShouldGiveFileName) { 147 EXPECT_STREQ("afile", 148 FilePath("adir" GTEST_PATH_SEP_ "afile").RemoveDirectoryName().c_str()); 149 } 150 151 // RemoveDirectoryName "adir/subdir/afile" -> "afile" 152 TEST(RemoveDirectoryNameTest, ShouldAlsoGiveFileName) { 153 EXPECT_STREQ("afile", 154 FilePath("adir" GTEST_PATH_SEP_ "subdir" GTEST_PATH_SEP_ "afile") 155 .RemoveDirectoryName().c_str()); 156 } 157 158 #if GTEST_HAS_ALT_PATH_SEP_ 159 160 // Tests that RemoveDirectoryName() works with the alternate separator 161 // on Windows. 162 163 // RemoveDirectoryName("/afile") -> "afile" 164 TEST(RemoveDirectoryNameTest, RootFileShouldGiveFileNameForAlternateSeparator) { 165 EXPECT_STREQ("afile", 166 FilePath("/afile").RemoveDirectoryName().c_str()); 167 } 168 169 // RemoveDirectoryName("adir/") -> "" 170 TEST(RemoveDirectoryNameTest, WhereThereIsNoFileNameForAlternateSeparator) { 171 EXPECT_STREQ("", 172 FilePath("adir/").RemoveDirectoryName().c_str()); 173 } 174 175 // RemoveDirectoryName("adir/afile") -> "afile" 176 TEST(RemoveDirectoryNameTest, ShouldGiveFileNameForAlternateSeparator) { 177 EXPECT_STREQ("afile", 178 FilePath("adir/afile").RemoveDirectoryName().c_str()); 179 } 180 181 // RemoveDirectoryName("adir/subdir/afile") -> "afile" 182 TEST(RemoveDirectoryNameTest, ShouldAlsoGiveFileNameForAlternateSeparator) { 183 EXPECT_STREQ("afile", 184 FilePath("adir/subdir/afile").RemoveDirectoryName().c_str()); 185 } 186 187 #endif 188 189 // RemoveFileName "" -> "./" 190 TEST(RemoveFileNameTest, EmptyName) { 191 #if GTEST_OS_WINDOWS_MOBILE 192 // On Windows CE, we use the root as the current directory. 193 EXPECT_STREQ(GTEST_PATH_SEP_, 194 FilePath("").RemoveFileName().c_str()); 195 #else 196 EXPECT_STREQ("." GTEST_PATH_SEP_, 197 FilePath("").RemoveFileName().c_str()); 198 #endif 199 } 200 201 // RemoveFileName "adir/" -> "adir/" 202 TEST(RemoveFileNameTest, ButNoFile) { 203 EXPECT_STREQ("adir" GTEST_PATH_SEP_, 204 FilePath("adir" GTEST_PATH_SEP_).RemoveFileName().c_str()); 205 } 206 207 // RemoveFileName "adir/afile" -> "adir/" 208 TEST(RemoveFileNameTest, GivesDirName) { 209 EXPECT_STREQ("adir" GTEST_PATH_SEP_, 210 FilePath("adir" GTEST_PATH_SEP_ "afile") 211 .RemoveFileName().c_str()); 212 } 213 214 // RemoveFileName "adir/subdir/afile" -> "adir/subdir/" 215 TEST(RemoveFileNameTest, GivesDirAndSubDirName) { 216 EXPECT_STREQ("adir" GTEST_PATH_SEP_ "subdir" GTEST_PATH_SEP_, 217 FilePath("adir" GTEST_PATH_SEP_ "subdir" GTEST_PATH_SEP_ "afile") 218 .RemoveFileName().c_str()); 219 } 220 221 // RemoveFileName "/afile" -> "/" 222 TEST(RemoveFileNameTest, GivesRootDir) { 223 EXPECT_STREQ(GTEST_PATH_SEP_, 224 FilePath(GTEST_PATH_SEP_ "afile").RemoveFileName().c_str()); 225 } 226 227 #if GTEST_HAS_ALT_PATH_SEP_ 228 229 // Tests that RemoveFileName() works with the alternate separator on 230 // Windows. 231 232 // RemoveFileName("adir/") -> "adir/" 233 TEST(RemoveFileNameTest, ButNoFileForAlternateSeparator) { 234 EXPECT_STREQ("adir" GTEST_PATH_SEP_, 235 FilePath("adir/").RemoveFileName().c_str()); 236 } 237 238 // RemoveFileName("adir/afile") -> "adir/" 239 TEST(RemoveFileNameTest, GivesDirNameForAlternateSeparator) { 240 EXPECT_STREQ("adir" GTEST_PATH_SEP_, 241 FilePath("adir/afile").RemoveFileName().c_str()); 242 } 243 244 // RemoveFileName("adir/subdir/afile") -> "adir/subdir/" 245 TEST(RemoveFileNameTest, GivesDirAndSubDirNameForAlternateSeparator) { 246 EXPECT_STREQ("adir" GTEST_PATH_SEP_ "subdir" GTEST_PATH_SEP_, 247 FilePath("adir/subdir/afile").RemoveFileName().c_str()); 248 } 249 250 // RemoveFileName("/afile") -> "\" 251 TEST(RemoveFileNameTest, GivesRootDirForAlternateSeparator) { 252 EXPECT_STREQ(GTEST_PATH_SEP_, 253 FilePath("/afile").RemoveFileName().c_str()); 254 } 255 256 #endif 257 258 TEST(MakeFileNameTest, GenerateWhenNumberIsZero) { 259 FilePath actual = FilePath::MakeFileName(FilePath("foo"), FilePath("bar"), 260 0, "xml"); 261 EXPECT_STREQ("foo" GTEST_PATH_SEP_ "bar.xml", actual.c_str()); 262 } 263 264 TEST(MakeFileNameTest, GenerateFileNameNumberGtZero) { 265 FilePath actual = FilePath::MakeFileName(FilePath("foo"), FilePath("bar"), 266 12, "xml"); 267 EXPECT_STREQ("foo" GTEST_PATH_SEP_ "bar_12.xml", actual.c_str()); 268 } 269 270 TEST(MakeFileNameTest, GenerateFileNameWithSlashNumberIsZero) { 271 FilePath actual = FilePath::MakeFileName(FilePath("foo" GTEST_PATH_SEP_), 272 FilePath("bar"), 0, "xml"); 273 EXPECT_STREQ("foo" GTEST_PATH_SEP_ "bar.xml", actual.c_str()); 274 } 275 276 TEST(MakeFileNameTest, GenerateFileNameWithSlashNumberGtZero) { 277 FilePath actual = FilePath::MakeFileName(FilePath("foo" GTEST_PATH_SEP_), 278 FilePath("bar"), 12, "xml"); 279 EXPECT_STREQ("foo" GTEST_PATH_SEP_ "bar_12.xml", actual.c_str()); 280 } 281 282 TEST(MakeFileNameTest, GenerateWhenNumberIsZeroAndDirIsEmpty) { 283 FilePath actual = FilePath::MakeFileName(FilePath(""), FilePath("bar"), 284 0, "xml"); 285 EXPECT_STREQ("bar.xml", actual.c_str()); 286 } 287 288 TEST(MakeFileNameTest, GenerateWhenNumberIsNotZeroAndDirIsEmpty) { 289 FilePath actual = FilePath::MakeFileName(FilePath(""), FilePath("bar"), 290 14, "xml"); 291 EXPECT_STREQ("bar_14.xml", actual.c_str()); 292 } 293 294 TEST(ConcatPathsTest, WorksWhenDirDoesNotEndWithPathSep) { 295 FilePath actual = FilePath::ConcatPaths(FilePath("foo"), 296 FilePath("bar.xml")); 297 EXPECT_STREQ("foo" GTEST_PATH_SEP_ "bar.xml", actual.c_str()); 298 } 299 300 TEST(ConcatPathsTest, WorksWhenPath1EndsWithPathSep) { 301 FilePath actual = FilePath::ConcatPaths(FilePath("foo" GTEST_PATH_SEP_), 302 FilePath("bar.xml")); 303 EXPECT_STREQ("foo" GTEST_PATH_SEP_ "bar.xml", actual.c_str()); 304 } 305 306 TEST(ConcatPathsTest, Path1BeingEmpty) { 307 FilePath actual = FilePath::ConcatPaths(FilePath(""), 308 FilePath("bar.xml")); 309 EXPECT_STREQ("bar.xml", actual.c_str()); 310 } 311 312 TEST(ConcatPathsTest, Path2BeingEmpty) { 313 FilePath actual = FilePath::ConcatPaths(FilePath("foo"), 314 FilePath("")); 315 EXPECT_STREQ("foo" GTEST_PATH_SEP_, actual.c_str()); 316 } 317 318 TEST(ConcatPathsTest, BothPathBeingEmpty) { 319 FilePath actual = FilePath::ConcatPaths(FilePath(""), 320 FilePath("")); 321 EXPECT_STREQ("", actual.c_str()); 322 } 323 324 TEST(ConcatPathsTest, Path1ContainsPathSep) { 325 FilePath actual = FilePath::ConcatPaths(FilePath("foo" GTEST_PATH_SEP_ "bar"), 326 FilePath("foobar.xml")); 327 EXPECT_STREQ("foo" GTEST_PATH_SEP_ "bar" GTEST_PATH_SEP_ "foobar.xml", 328 actual.c_str()); 329 } 330 331 TEST(ConcatPathsTest, Path2ContainsPathSep) { 332 FilePath actual = FilePath::ConcatPaths( 333 FilePath("foo" GTEST_PATH_SEP_), 334 FilePath("bar" GTEST_PATH_SEP_ "bar.xml")); 335 EXPECT_STREQ("foo" GTEST_PATH_SEP_ "bar" GTEST_PATH_SEP_ "bar.xml", 336 actual.c_str()); 337 } 338 339 TEST(ConcatPathsTest, Path2EndsWithPathSep) { 340 FilePath actual = FilePath::ConcatPaths(FilePath("foo"), 341 FilePath("bar" GTEST_PATH_SEP_)); 342 EXPECT_STREQ("foo" GTEST_PATH_SEP_ "bar" GTEST_PATH_SEP_, actual.c_str()); 343 } 344 345 // RemoveTrailingPathSeparator "" -> "" 346 TEST(RemoveTrailingPathSeparatorTest, EmptyString) { 347 EXPECT_STREQ("", 348 FilePath("").RemoveTrailingPathSeparator().c_str()); 349 } 350 351 // RemoveTrailingPathSeparator "foo" -> "foo" 352 TEST(RemoveTrailingPathSeparatorTest, FileNoSlashString) { 353 EXPECT_STREQ("foo", 354 FilePath("foo").RemoveTrailingPathSeparator().c_str()); 355 } 356 357 // RemoveTrailingPathSeparator "foo/" -> "foo" 358 TEST(RemoveTrailingPathSeparatorTest, ShouldRemoveTrailingSeparator) { 359 EXPECT_STREQ( 360 "foo", 361 FilePath("foo" GTEST_PATH_SEP_).RemoveTrailingPathSeparator().c_str()); 362 #if GTEST_HAS_ALT_PATH_SEP_ 363 EXPECT_STREQ("foo", 364 FilePath("foo/").RemoveTrailingPathSeparator().c_str()); 365 #endif 366 } 367 368 // RemoveTrailingPathSeparator "foo/bar/" -> "foo/bar/" 369 TEST(RemoveTrailingPathSeparatorTest, ShouldRemoveLastSeparator) { 370 EXPECT_STREQ("foo" GTEST_PATH_SEP_ "bar", 371 FilePath("foo" GTEST_PATH_SEP_ "bar" GTEST_PATH_SEP_) 372 .RemoveTrailingPathSeparator().c_str()); 373 } 374 375 // RemoveTrailingPathSeparator "foo/bar" -> "foo/bar" 376 TEST(RemoveTrailingPathSeparatorTest, ShouldReturnUnmodified) { 377 EXPECT_STREQ("foo" GTEST_PATH_SEP_ "bar", 378 FilePath("foo" GTEST_PATH_SEP_ "bar") 379 .RemoveTrailingPathSeparator().c_str()); 380 } 381 382 TEST(DirectoryTest, RootDirectoryExists) { 383 #if GTEST_OS_WINDOWS // We are on Windows. 384 char current_drive[_MAX_PATH]; // NOLINT 385 current_drive[0] = static_cast<char>(_getdrive() + 'A' - 1); 386 current_drive[1] = ':'; 387 current_drive[2] = '\\'; 388 current_drive[3] = '\0'; 389 EXPECT_TRUE(FilePath(current_drive).DirectoryExists()); 390 #else 391 EXPECT_TRUE(FilePath("/").DirectoryExists()); 392 #endif // GTEST_OS_WINDOWS 393 } 394 395 #if GTEST_OS_WINDOWS 396 TEST(DirectoryTest, RootOfWrongDriveDoesNotExists) { 397 const int saved_drive_ = _getdrive(); 398 // Find a drive that doesn't exist. Start with 'Z' to avoid common ones. 399 for (char drive = 'Z'; drive >= 'A'; drive--) 400 if (_chdrive(drive - 'A' + 1) == -1) { 401 char non_drive[_MAX_PATH]; // NOLINT 402 non_drive[0] = drive; 403 non_drive[1] = ':'; 404 non_drive[2] = '\\'; 405 non_drive[3] = '\0'; 406 EXPECT_FALSE(FilePath(non_drive).DirectoryExists()); 407 break; 408 } 409 _chdrive(saved_drive_); 410 } 411 #endif // GTEST_OS_WINDOWS 412 413 #if !GTEST_OS_WINDOWS_MOBILE 414 // Windows CE _does_ consider an empty directory to exist. 415 TEST(DirectoryTest, EmptyPathDirectoryDoesNotExist) { 416 EXPECT_FALSE(FilePath("").DirectoryExists()); 417 } 418 #endif // !GTEST_OS_WINDOWS_MOBILE 419 420 TEST(DirectoryTest, CurrentDirectoryExists) { 421 #if GTEST_OS_WINDOWS // We are on Windows. 422 # ifndef _WIN32_CE // Windows CE doesn't have a current directory. 423 424 EXPECT_TRUE(FilePath(".").DirectoryExists()); 425 EXPECT_TRUE(FilePath(".\\").DirectoryExists()); 426 427 # endif // _WIN32_CE 428 #else 429 EXPECT_TRUE(FilePath(".").DirectoryExists()); 430 EXPECT_TRUE(FilePath("./").DirectoryExists()); 431 #endif // GTEST_OS_WINDOWS 432 } 433 434 TEST(NormalizeTest, NullStringsEqualEmptyDirectory) { 435 EXPECT_STREQ("", FilePath(NULL).c_str()); 436 EXPECT_STREQ("", FilePath(String(NULL)).c_str()); 437 } 438 439 // "foo/bar" == foo//bar" == "foo///bar" 440 TEST(NormalizeTest, MultipleConsecutiveSepaparatorsInMidstring) { 441 EXPECT_STREQ("foo" GTEST_PATH_SEP_ "bar", 442 FilePath("foo" GTEST_PATH_SEP_ "bar").c_str()); 443 EXPECT_STREQ("foo" GTEST_PATH_SEP_ "bar", 444 FilePath("foo" GTEST_PATH_SEP_ GTEST_PATH_SEP_ "bar").c_str()); 445 EXPECT_STREQ("foo" GTEST_PATH_SEP_ "bar", 446 FilePath("foo" GTEST_PATH_SEP_ GTEST_PATH_SEP_ 447 GTEST_PATH_SEP_ "bar").c_str()); 448 } 449 450 // "/bar" == //bar" == "///bar" 451 TEST(NormalizeTest, MultipleConsecutiveSepaparatorsAtStringStart) { 452 EXPECT_STREQ(GTEST_PATH_SEP_ "bar", 453 FilePath(GTEST_PATH_SEP_ "bar").c_str()); 454 EXPECT_STREQ(GTEST_PATH_SEP_ "bar", 455 FilePath(GTEST_PATH_SEP_ GTEST_PATH_SEP_ "bar").c_str()); 456 EXPECT_STREQ(GTEST_PATH_SEP_ "bar", 457 FilePath(GTEST_PATH_SEP_ GTEST_PATH_SEP_ GTEST_PATH_SEP_ "bar").c_str()); 458 } 459 460 // "foo/" == foo//" == "foo///" 461 TEST(NormalizeTest, MultipleConsecutiveSepaparatorsAtStringEnd) { 462 EXPECT_STREQ("foo" GTEST_PATH_SEP_, 463 FilePath("foo" GTEST_PATH_SEP_).c_str()); 464 EXPECT_STREQ("foo" GTEST_PATH_SEP_, 465 FilePath("foo" GTEST_PATH_SEP_ GTEST_PATH_SEP_).c_str()); 466 EXPECT_STREQ("foo" GTEST_PATH_SEP_, 467 FilePath("foo" GTEST_PATH_SEP_ GTEST_PATH_SEP_ GTEST_PATH_SEP_).c_str()); 468 } 469 470 #if GTEST_HAS_ALT_PATH_SEP_ 471 472 // Tests that separators at the end of the string are normalized 473 // regardless of their combination (e.g. "foo\" =="foo/\" == 474 // "foo\\/"). 475 TEST(NormalizeTest, MixAlternateSeparatorAtStringEnd) { 476 EXPECT_STREQ("foo" GTEST_PATH_SEP_, 477 FilePath("foo/").c_str()); 478 EXPECT_STREQ("foo" GTEST_PATH_SEP_, 479 FilePath("foo" GTEST_PATH_SEP_ "/").c_str()); 480 EXPECT_STREQ("foo" GTEST_PATH_SEP_, 481 FilePath("foo//" GTEST_PATH_SEP_).c_str()); 482 } 483 484 #endif 485 486 TEST(AssignmentOperatorTest, DefaultAssignedToNonDefault) { 487 FilePath default_path; 488 FilePath non_default_path("path"); 489 non_default_path = default_path; 490 EXPECT_STREQ("", non_default_path.c_str()); 491 EXPECT_STREQ("", default_path.c_str()); // RHS var is unchanged. 492 } 493 494 TEST(AssignmentOperatorTest, NonDefaultAssignedToDefault) { 495 FilePath non_default_path("path"); 496 FilePath default_path; 497 default_path = non_default_path; 498 EXPECT_STREQ("path", default_path.c_str()); 499 EXPECT_STREQ("path", non_default_path.c_str()); // RHS var is unchanged. 500 } 501 502 TEST(AssignmentOperatorTest, ConstAssignedToNonConst) { 503 const FilePath const_default_path("const_path"); 504 FilePath non_default_path("path"); 505 non_default_path = const_default_path; 506 EXPECT_STREQ("const_path", non_default_path.c_str()); 507 } 508 509 class DirectoryCreationTest : public Test { 510 protected: 511 virtual void SetUp() { 512 testdata_path_.Set(FilePath(String::Format("%s%s%s", 513 TempDir().c_str(), GetCurrentExecutableName().c_str(), 514 "_directory_creation" GTEST_PATH_SEP_ "test" GTEST_PATH_SEP_))); 515 testdata_file_.Set(testdata_path_.RemoveTrailingPathSeparator()); 516 517 unique_file0_.Set(FilePath::MakeFileName(testdata_path_, FilePath("unique"), 518 0, "txt")); 519 unique_file1_.Set(FilePath::MakeFileName(testdata_path_, FilePath("unique"), 520 1, "txt")); 521 522 remove(testdata_file_.c_str()); 523 remove(unique_file0_.c_str()); 524 remove(unique_file1_.c_str()); 525 posix::RmDir(testdata_path_.c_str()); 526 } 527 528 virtual void TearDown() { 529 remove(testdata_file_.c_str()); 530 remove(unique_file0_.c_str()); 531 remove(unique_file1_.c_str()); 532 posix::RmDir(testdata_path_.c_str()); 533 } 534 535 String TempDir() const { 536 #if GTEST_OS_WINDOWS_MOBILE 537 return String("\\temp\\"); 538 #elif GTEST_OS_WINDOWS 539 const char* temp_dir = posix::GetEnv("TEMP"); 540 if (temp_dir == NULL || temp_dir[0] == '\0') 541 return String("\\temp\\"); 542 else if (String(temp_dir).EndsWith("\\")) 543 return String(temp_dir); 544 else 545 return String::Format("%s\\", temp_dir); 546 #else 547 return String("/tmp/"); 548 #endif // GTEST_OS_WINDOWS_MOBILE 549 } 550 551 void CreateTextFile(const char* filename) { 552 FILE* f = posix::FOpen(filename, "w"); 553 fprintf(f, "text\n"); 554 fclose(f); 555 } 556 557 // Strings representing a directory and a file, with identical paths 558 // except for the trailing separator character that distinquishes 559 // a directory named 'test' from a file named 'test'. Example names: 560 FilePath testdata_path_; // "/tmp/directory_creation/test/" 561 FilePath testdata_file_; // "/tmp/directory_creation/test" 562 FilePath unique_file0_; // "/tmp/directory_creation/test/unique.txt" 563 FilePath unique_file1_; // "/tmp/directory_creation/test/unique_1.txt" 564 }; 565 566 TEST_F(DirectoryCreationTest, CreateDirectoriesRecursively) { 567 EXPECT_FALSE(testdata_path_.DirectoryExists()) << testdata_path_.c_str(); 568 EXPECT_TRUE(testdata_path_.CreateDirectoriesRecursively()); 569 EXPECT_TRUE(testdata_path_.DirectoryExists()); 570 } 571 572 TEST_F(DirectoryCreationTest, CreateDirectoriesForAlreadyExistingPath) { 573 EXPECT_FALSE(testdata_path_.DirectoryExists()) << testdata_path_.c_str(); 574 EXPECT_TRUE(testdata_path_.CreateDirectoriesRecursively()); 575 // Call 'create' again... should still succeed. 576 EXPECT_TRUE(testdata_path_.CreateDirectoriesRecursively()); 577 } 578 579 TEST_F(DirectoryCreationTest, CreateDirectoriesAndUniqueFilename) { 580 FilePath file_path(FilePath::GenerateUniqueFileName(testdata_path_, 581 FilePath("unique"), "txt")); 582 EXPECT_STREQ(unique_file0_.c_str(), file_path.c_str()); 583 EXPECT_FALSE(file_path.FileOrDirectoryExists()); // file not there 584 585 testdata_path_.CreateDirectoriesRecursively(); 586 EXPECT_FALSE(file_path.FileOrDirectoryExists()); // file still not there 587 CreateTextFile(file_path.c_str()); 588 EXPECT_TRUE(file_path.FileOrDirectoryExists()); 589 590 FilePath file_path2(FilePath::GenerateUniqueFileName(testdata_path_, 591 FilePath("unique"), "txt")); 592 EXPECT_STREQ(unique_file1_.c_str(), file_path2.c_str()); 593 EXPECT_FALSE(file_path2.FileOrDirectoryExists()); // file not there 594 CreateTextFile(file_path2.c_str()); 595 EXPECT_TRUE(file_path2.FileOrDirectoryExists()); 596 } 597 598 TEST_F(DirectoryCreationTest, CreateDirectoriesFail) { 599 // force a failure by putting a file where we will try to create a directory. 600 CreateTextFile(testdata_file_.c_str()); 601 EXPECT_TRUE(testdata_file_.FileOrDirectoryExists()); 602 EXPECT_FALSE(testdata_file_.DirectoryExists()); 603 EXPECT_FALSE(testdata_file_.CreateDirectoriesRecursively()); 604 } 605 606 TEST(NoDirectoryCreationTest, CreateNoDirectoriesForDefaultXmlFile) { 607 const FilePath test_detail_xml("test_detail.xml"); 608 EXPECT_FALSE(test_detail_xml.CreateDirectoriesRecursively()); 609 } 610 611 TEST(FilePathTest, DefaultConstructor) { 612 FilePath fp; 613 EXPECT_STREQ("", fp.c_str()); 614 } 615 616 TEST(FilePathTest, CharAndCopyConstructors) { 617 const FilePath fp("spicy"); 618 EXPECT_STREQ("spicy", fp.c_str()); 619 620 const FilePath fp_copy(fp); 621 EXPECT_STREQ("spicy", fp_copy.c_str()); 622 } 623 624 TEST(FilePathTest, StringConstructor) { 625 const FilePath fp(String("cider")); 626 EXPECT_STREQ("cider", fp.c_str()); 627 } 628 629 TEST(FilePathTest, Set) { 630 const FilePath apple("apple"); 631 FilePath mac("mac"); 632 mac.Set(apple); // Implement Set() since overloading operator= is forbidden. 633 EXPECT_STREQ("apple", mac.c_str()); 634 EXPECT_STREQ("apple", apple.c_str()); 635 } 636 637 TEST(FilePathTest, ToString) { 638 const FilePath file("drink"); 639 String str(file.ToString()); 640 EXPECT_STREQ("drink", str.c_str()); 641 } 642 643 TEST(FilePathTest, RemoveExtension) { 644 EXPECT_STREQ("app", FilePath("app.exe").RemoveExtension("exe").c_str()); 645 EXPECT_STREQ("APP", FilePath("APP.EXE").RemoveExtension("exe").c_str()); 646 } 647 648 TEST(FilePathTest, RemoveExtensionWhenThereIsNoExtension) { 649 EXPECT_STREQ("app", FilePath("app").RemoveExtension("exe").c_str()); 650 } 651 652 TEST(FilePathTest, IsDirectory) { 653 EXPECT_FALSE(FilePath("cola").IsDirectory()); 654 EXPECT_TRUE(FilePath("koala" GTEST_PATH_SEP_).IsDirectory()); 655 #if GTEST_HAS_ALT_PATH_SEP_ 656 EXPECT_TRUE(FilePath("koala/").IsDirectory()); 657 #endif 658 } 659 660 TEST(FilePathTest, IsAbsolutePath) { 661 EXPECT_FALSE(FilePath("is" GTEST_PATH_SEP_ "relative").IsAbsolutePath()); 662 EXPECT_FALSE(FilePath("").IsAbsolutePath()); 663 #if GTEST_OS_WINDOWS 664 EXPECT_TRUE(FilePath("c:\\" GTEST_PATH_SEP_ "is_not" 665 GTEST_PATH_SEP_ "relative").IsAbsolutePath()); 666 EXPECT_FALSE(FilePath("c:foo" GTEST_PATH_SEP_ "bar").IsAbsolutePath()); 667 EXPECT_TRUE(FilePath("c:/" GTEST_PATH_SEP_ "is_not" 668 GTEST_PATH_SEP_ "relative").IsAbsolutePath()); 669 #else 670 EXPECT_TRUE(FilePath(GTEST_PATH_SEP_ "is_not" GTEST_PATH_SEP_ "relative") 671 .IsAbsolutePath()); 672 #endif // GTEST_OS_WINDOWS 673 } 674 675 TEST(FilePathTest, IsRootDirectory) { 676 #if GTEST_OS_WINDOWS 677 EXPECT_TRUE(FilePath("a:\\").IsRootDirectory()); 678 EXPECT_TRUE(FilePath("Z:/").IsRootDirectory()); 679 EXPECT_TRUE(FilePath("e://").IsRootDirectory()); 680 EXPECT_FALSE(FilePath("").IsRootDirectory()); 681 EXPECT_FALSE(FilePath("b:").IsRootDirectory()); 682 EXPECT_FALSE(FilePath("b:a").IsRootDirectory()); 683 EXPECT_FALSE(FilePath("8:/").IsRootDirectory()); 684 EXPECT_FALSE(FilePath("c|/").IsRootDirectory()); 685 #else 686 EXPECT_TRUE(FilePath("/").IsRootDirectory()); 687 EXPECT_TRUE(FilePath("//").IsRootDirectory()); 688 EXPECT_FALSE(FilePath("").IsRootDirectory()); 689 EXPECT_FALSE(FilePath("\\").IsRootDirectory()); 690 EXPECT_FALSE(FilePath("/x").IsRootDirectory()); 691 #endif 692 } 693 694 } // namespace 695 } // namespace internal 696 } // namespace testing 697