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