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