1 // Copyright 2008, Google Inc.
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
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
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.
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.
30 // Authors: keith.ray@gmail.com (Keith Ray)
32 // Google Test filepath utilities
34 // This file tests classes and functions used internally by
35 // Google Test. They are subject to change without notice.
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!
41 #include <gtest/internal/gtest-filepath.h>
42 #include <gtest/gtest.h>
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
49 #define GTEST_IMPLEMENTATION_ 1
50 #include "src/gtest-internal-inl.h"
51 #undef GTEST_IMPLEMENTATION_
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
63 #if GTEST_OS_WINDOWS_MOBILE
64 // TODO(wan@google.com): Move these to the POSIX adapter section in
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;
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;
86 TEST(GetCurrentDirTest, ReturnsCurrentDir) {
87 const FilePath original_dir = FilePath::GetCurrentDir();
88 EXPECT_FALSE(original_dir.IsEmpty());
90 posix::ChDir(GTEST_PATH_SEP_);
91 const FilePath cwd = FilePath::GetCurrentDir();
92 posix::ChDir(original_dir.c_str());
96 const char* const cwd_without_drive = strchr(cwd.c_str(), ':');
97 ASSERT_TRUE(cwd_without_drive != NULL);
98 EXPECT_STREQ(GTEST_PATH_SEP_, cwd_without_drive + 1);
100 EXPECT_STREQ(GTEST_PATH_SEP_, cwd.c_str());
104 #endif // GTEST_OS_WINDOWS_MOBILE
106 TEST(IsEmptyTest, ReturnsTrueForEmptyPath) {
107 EXPECT_TRUE(FilePath("").IsEmpty());
108 EXPECT_TRUE(FilePath(NULL).IsEmpty());
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());
118 // RemoveDirectoryName "" -> ""
119 TEST(RemoveDirectoryNameTest, WhenEmptyName) {
120 EXPECT_STREQ("", FilePath("").RemoveDirectoryName().c_str());
123 // RemoveDirectoryName "afile" -> "afile"
124 TEST(RemoveDirectoryNameTest, ButNoDirectory) {
125 EXPECT_STREQ("afile",
126 FilePath("afile").RemoveDirectoryName().c_str());
129 // RemoveDirectoryName "/afile" -> "afile"
130 TEST(RemoveDirectoryNameTest, RootFileShouldGiveFileName) {
131 EXPECT_STREQ("afile",
132 FilePath(GTEST_PATH_SEP_ "afile").RemoveDirectoryName().c_str());
135 // RemoveDirectoryName "adir/" -> ""
136 TEST(RemoveDirectoryNameTest, WhereThereIsNoFileName) {
138 FilePath("adir" GTEST_PATH_SEP_).RemoveDirectoryName().c_str());
141 // RemoveDirectoryName "adir/afile" -> "afile"
142 TEST(RemoveDirectoryNameTest, ShouldGiveFileName) {
143 EXPECT_STREQ("afile",
144 FilePath("adir" GTEST_PATH_SEP_ "afile").RemoveDirectoryName().c_str());
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());
154 #if GTEST_HAS_ALT_PATH_SEP_
156 // Tests that RemoveDirectoryName() works with the alternate separator
159 // RemoveDirectoryName("/afile") -> "afile"
160 TEST(RemoveDirectoryNameTest, RootFileShouldGiveFileNameForAlternateSeparator) {
161 EXPECT_STREQ("afile",
162 FilePath("/afile").RemoveDirectoryName().c_str());
165 // RemoveDirectoryName("adir/") -> ""
166 TEST(RemoveDirectoryNameTest, WhereThereIsNoFileNameForAlternateSeparator) {
168 FilePath("adir/").RemoveDirectoryName().c_str());
171 // RemoveDirectoryName("adir/afile") -> "afile"
172 TEST(RemoveDirectoryNameTest, ShouldGiveFileNameForAlternateSeparator) {
173 EXPECT_STREQ("afile",
174 FilePath("adir/afile").RemoveDirectoryName().c_str());
177 // RemoveDirectoryName("adir/subdir/afile") -> "afile"
178 TEST(RemoveDirectoryNameTest, ShouldAlsoGiveFileNameForAlternateSeparator) {
179 EXPECT_STREQ("afile",
180 FilePath("adir/subdir/afile").RemoveDirectoryName().c_str());
185 // RemoveFileName "" -> "./"
186 TEST(RemoveFileNameTest, EmptyName) {
187 #if GTEST_OS_WINDOWS_MOBILE
188 // On Windows CE, we use the root as the current directory.
189 EXPECT_STREQ(GTEST_PATH_SEP_,
190 FilePath("").RemoveFileName().c_str());
192 EXPECT_STREQ("." GTEST_PATH_SEP_,
193 FilePath("").RemoveFileName().c_str());
197 // RemoveFileName "adir/" -> "adir/"
198 TEST(RemoveFileNameTest, ButNoFile) {
199 EXPECT_STREQ("adir" GTEST_PATH_SEP_,
200 FilePath("adir" GTEST_PATH_SEP_).RemoveFileName().c_str());
203 // RemoveFileName "adir/afile" -> "adir/"
204 TEST(RemoveFileNameTest, GivesDirName) {
205 EXPECT_STREQ("adir" GTEST_PATH_SEP_,
206 FilePath("adir" GTEST_PATH_SEP_ "afile")
207 .RemoveFileName().c_str());
210 // RemoveFileName "adir/subdir/afile" -> "adir/subdir/"
211 TEST(RemoveFileNameTest, GivesDirAndSubDirName) {
212 EXPECT_STREQ("adir" GTEST_PATH_SEP_ "subdir" GTEST_PATH_SEP_,
213 FilePath("adir" GTEST_PATH_SEP_ "subdir" GTEST_PATH_SEP_ "afile")
214 .RemoveFileName().c_str());
217 // RemoveFileName "/afile" -> "/"
218 TEST(RemoveFileNameTest, GivesRootDir) {
219 EXPECT_STREQ(GTEST_PATH_SEP_,
220 FilePath(GTEST_PATH_SEP_ "afile").RemoveFileName().c_str());
223 #if GTEST_HAS_ALT_PATH_SEP_
225 // Tests that RemoveFileName() works with the alternate separator on
228 // RemoveFileName("adir/") -> "adir/"
229 TEST(RemoveFileNameTest, ButNoFileForAlternateSeparator) {
230 EXPECT_STREQ("adir" GTEST_PATH_SEP_,
231 FilePath("adir/").RemoveFileName().c_str());
234 // RemoveFileName("adir/afile") -> "adir/"
235 TEST(RemoveFileNameTest, GivesDirNameForAlternateSeparator) {
236 EXPECT_STREQ("adir" GTEST_PATH_SEP_,
237 FilePath("adir/afile").RemoveFileName().c_str());
240 // RemoveFileName("adir/subdir/afile") -> "adir/subdir/"
241 TEST(RemoveFileNameTest, GivesDirAndSubDirNameForAlternateSeparator) {
242 EXPECT_STREQ("adir" GTEST_PATH_SEP_ "subdir" GTEST_PATH_SEP_,
243 FilePath("adir/subdir/afile").RemoveFileName().c_str());
246 // RemoveFileName("/afile") -> "\"
247 TEST(RemoveFileNameTest, GivesRootDirForAlternateSeparator) {
248 EXPECT_STREQ(GTEST_PATH_SEP_,
249 FilePath("/afile").RemoveFileName().c_str());
254 TEST(MakeFileNameTest, GenerateWhenNumberIsZero) {
255 FilePath actual = FilePath::MakeFileName(FilePath("foo"), FilePath("bar"),
257 EXPECT_STREQ("foo" GTEST_PATH_SEP_ "bar.xml", actual.c_str());
260 TEST(MakeFileNameTest, GenerateFileNameNumberGtZero) {
261 FilePath actual = FilePath::MakeFileName(FilePath("foo"), FilePath("bar"),
263 EXPECT_STREQ("foo" GTEST_PATH_SEP_ "bar_12.xml", actual.c_str());
266 TEST(MakeFileNameTest, GenerateFileNameWithSlashNumberIsZero) {
267 FilePath actual = FilePath::MakeFileName(FilePath("foo" GTEST_PATH_SEP_),
268 FilePath("bar"), 0, "xml");
269 EXPECT_STREQ("foo" GTEST_PATH_SEP_ "bar.xml", actual.c_str());
272 TEST(MakeFileNameTest, GenerateFileNameWithSlashNumberGtZero) {
273 FilePath actual = FilePath::MakeFileName(FilePath("foo" GTEST_PATH_SEP_),
274 FilePath("bar"), 12, "xml");
275 EXPECT_STREQ("foo" GTEST_PATH_SEP_ "bar_12.xml", actual.c_str());
278 TEST(MakeFileNameTest, GenerateWhenNumberIsZeroAndDirIsEmpty) {
279 FilePath actual = FilePath::MakeFileName(FilePath(""), FilePath("bar"),
281 EXPECT_STREQ("bar.xml", actual.c_str());
284 TEST(MakeFileNameTest, GenerateWhenNumberIsNotZeroAndDirIsEmpty) {
285 FilePath actual = FilePath::MakeFileName(FilePath(""), FilePath("bar"),
287 EXPECT_STREQ("bar_14.xml", actual.c_str());
290 TEST(ConcatPathsTest, WorksWhenDirDoesNotEndWithPathSep) {
291 FilePath actual = FilePath::ConcatPaths(FilePath("foo"),
292 FilePath("bar.xml"));
293 EXPECT_STREQ("foo" GTEST_PATH_SEP_ "bar.xml", actual.c_str());
296 TEST(ConcatPathsTest, WorksWhenPath1EndsWithPathSep) {
297 FilePath actual = FilePath::ConcatPaths(FilePath("foo" GTEST_PATH_SEP_),
298 FilePath("bar.xml"));
299 EXPECT_STREQ("foo" GTEST_PATH_SEP_ "bar.xml", actual.c_str());
302 TEST(ConcatPathsTest, Path1BeingEmpty) {
303 FilePath actual = FilePath::ConcatPaths(FilePath(""),
304 FilePath("bar.xml"));
305 EXPECT_STREQ("bar.xml", actual.c_str());
308 TEST(ConcatPathsTest, Path2BeingEmpty) {
309 FilePath actual = FilePath::ConcatPaths(FilePath("foo"),
311 EXPECT_STREQ("foo" GTEST_PATH_SEP_, actual.c_str());
314 TEST(ConcatPathsTest, BothPathBeingEmpty) {
315 FilePath actual = FilePath::ConcatPaths(FilePath(""),
317 EXPECT_STREQ("", actual.c_str());
320 TEST(ConcatPathsTest, Path1ContainsPathSep) {
321 FilePath actual = FilePath::ConcatPaths(FilePath("foo" GTEST_PATH_SEP_ "bar"),
322 FilePath("foobar.xml"));
323 EXPECT_STREQ("foo" GTEST_PATH_SEP_ "bar" GTEST_PATH_SEP_ "foobar.xml",
327 TEST(ConcatPathsTest, Path2ContainsPathSep) {
328 FilePath actual = FilePath::ConcatPaths(
329 FilePath("foo" GTEST_PATH_SEP_),
330 FilePath("bar" GTEST_PATH_SEP_ "bar.xml"));
331 EXPECT_STREQ("foo" GTEST_PATH_SEP_ "bar" GTEST_PATH_SEP_ "bar.xml",
335 TEST(ConcatPathsTest, Path2EndsWithPathSep) {
336 FilePath actual = FilePath::ConcatPaths(FilePath("foo"),
337 FilePath("bar" GTEST_PATH_SEP_));
338 EXPECT_STREQ("foo" GTEST_PATH_SEP_ "bar" GTEST_PATH_SEP_, actual.c_str());
341 // RemoveTrailingPathSeparator "" -> ""
342 TEST(RemoveTrailingPathSeparatorTest, EmptyString) {
344 FilePath("").RemoveTrailingPathSeparator().c_str());
347 // RemoveTrailingPathSeparator "foo" -> "foo"
348 TEST(RemoveTrailingPathSeparatorTest, FileNoSlashString) {
350 FilePath("foo").RemoveTrailingPathSeparator().c_str());
353 // RemoveTrailingPathSeparator "foo/" -> "foo"
354 TEST(RemoveTrailingPathSeparatorTest, ShouldRemoveTrailingSeparator) {
357 FilePath("foo" GTEST_PATH_SEP_).RemoveTrailingPathSeparator().c_str());
358 #if GTEST_HAS_ALT_PATH_SEP_
360 FilePath("foo/").RemoveTrailingPathSeparator().c_str());
364 // RemoveTrailingPathSeparator "foo/bar/" -> "foo/bar/"
365 TEST(RemoveTrailingPathSeparatorTest, ShouldRemoveLastSeparator) {
366 EXPECT_STREQ("foo" GTEST_PATH_SEP_ "bar",
367 FilePath("foo" GTEST_PATH_SEP_ "bar" GTEST_PATH_SEP_)
368 .RemoveTrailingPathSeparator().c_str());
371 // RemoveTrailingPathSeparator "foo/bar" -> "foo/bar"
372 TEST(RemoveTrailingPathSeparatorTest, ShouldReturnUnmodified) {
373 EXPECT_STREQ("foo" GTEST_PATH_SEP_ "bar",
374 FilePath("foo" GTEST_PATH_SEP_ "bar")
375 .RemoveTrailingPathSeparator().c_str());
378 TEST(DirectoryTest, RootDirectoryExists) {
379 #if GTEST_OS_WINDOWS // We are on Windows.
380 char current_drive[_MAX_PATH]; // NOLINT
381 current_drive[0] = static_cast<char>(_getdrive() + 'A' - 1);
382 current_drive[1] = ':';
383 current_drive[2] = '\\';
384 current_drive[3] = '\0';
385 EXPECT_TRUE(FilePath(current_drive).DirectoryExists());
387 EXPECT_TRUE(FilePath("/").DirectoryExists());
388 #endif // GTEST_OS_WINDOWS
392 TEST(DirectoryTest, RootOfWrongDriveDoesNotExists) {
393 const int saved_drive_ = _getdrive();
394 // Find a drive that doesn't exist. Start with 'Z' to avoid common ones.
395 for (char drive = 'Z'; drive >= 'A'; drive--)
396 if (_chdrive(drive - 'A' + 1) == -1) {
397 char non_drive[_MAX_PATH]; // NOLINT
398 non_drive[0] = drive;
402 EXPECT_FALSE(FilePath(non_drive).DirectoryExists());
405 _chdrive(saved_drive_);
407 #endif // GTEST_OS_WINDOWS
409 #if !GTEST_OS_WINDOWS_MOBILE
410 // Windows CE _does_ consider an empty directory to exist.
411 TEST(DirectoryTest, EmptyPathDirectoryDoesNotExist) {
412 EXPECT_FALSE(FilePath("").DirectoryExists());
414 #endif // !GTEST_OS_WINDOWS_MOBILE
416 TEST(DirectoryTest, CurrentDirectoryExists) {
417 #if GTEST_OS_WINDOWS // We are on Windows.
418 #ifndef _WIN32_CE // Windows CE doesn't have a current directory.
419 EXPECT_TRUE(FilePath(".").DirectoryExists());
420 EXPECT_TRUE(FilePath(".\\").DirectoryExists());
423 EXPECT_TRUE(FilePath(".").DirectoryExists());
424 EXPECT_TRUE(FilePath("./").DirectoryExists());
425 #endif // GTEST_OS_WINDOWS
428 TEST(NormalizeTest, NullStringsEqualEmptyDirectory) {
429 EXPECT_STREQ("", FilePath(NULL).c_str());
430 EXPECT_STREQ("", FilePath(String(NULL)).c_str());
433 // "foo/bar" == foo//bar" == "foo///bar"
434 TEST(NormalizeTest, MultipleConsecutiveSepaparatorsInMidstring) {
435 EXPECT_STREQ("foo" GTEST_PATH_SEP_ "bar",
436 FilePath("foo" GTEST_PATH_SEP_ "bar").c_str());
437 EXPECT_STREQ("foo" GTEST_PATH_SEP_ "bar",
438 FilePath("foo" GTEST_PATH_SEP_ GTEST_PATH_SEP_ "bar").c_str());
439 EXPECT_STREQ("foo" GTEST_PATH_SEP_ "bar",
440 FilePath("foo" GTEST_PATH_SEP_ GTEST_PATH_SEP_
441 GTEST_PATH_SEP_ "bar").c_str());
444 // "/bar" == //bar" == "///bar"
445 TEST(NormalizeTest, MultipleConsecutiveSepaparatorsAtStringStart) {
446 EXPECT_STREQ(GTEST_PATH_SEP_ "bar",
447 FilePath(GTEST_PATH_SEP_ "bar").c_str());
448 EXPECT_STREQ(GTEST_PATH_SEP_ "bar",
449 FilePath(GTEST_PATH_SEP_ GTEST_PATH_SEP_ "bar").c_str());
450 EXPECT_STREQ(GTEST_PATH_SEP_ "bar",
451 FilePath(GTEST_PATH_SEP_ GTEST_PATH_SEP_ GTEST_PATH_SEP_ "bar").c_str());
454 // "foo/" == foo//" == "foo///"
455 TEST(NormalizeTest, MultipleConsecutiveSepaparatorsAtStringEnd) {
456 EXPECT_STREQ("foo" GTEST_PATH_SEP_,
457 FilePath("foo" GTEST_PATH_SEP_).c_str());
458 EXPECT_STREQ("foo" GTEST_PATH_SEP_,
459 FilePath("foo" GTEST_PATH_SEP_ GTEST_PATH_SEP_).c_str());
460 EXPECT_STREQ("foo" GTEST_PATH_SEP_,
461 FilePath("foo" GTEST_PATH_SEP_ GTEST_PATH_SEP_ GTEST_PATH_SEP_).c_str());
464 #if GTEST_HAS_ALT_PATH_SEP_
466 // Tests that separators at the end of the string are normalized
467 // regardless of their combination (e.g. "foo\" =="foo/\" ==
469 TEST(NormalizeTest, MixAlternateSeparatorAtStringEnd) {
470 EXPECT_STREQ("foo" GTEST_PATH_SEP_,
471 FilePath("foo/").c_str());
472 EXPECT_STREQ("foo" GTEST_PATH_SEP_,
473 FilePath("foo" GTEST_PATH_SEP_ "/").c_str());
474 EXPECT_STREQ("foo" GTEST_PATH_SEP_,
475 FilePath("foo//" GTEST_PATH_SEP_).c_str());
480 TEST(AssignmentOperatorTest, DefaultAssignedToNonDefault) {
481 FilePath default_path;
482 FilePath non_default_path("path");
483 non_default_path = default_path;
484 EXPECT_STREQ("", non_default_path.c_str());
485 EXPECT_STREQ("", default_path.c_str()); // RHS var is unchanged.
488 TEST(AssignmentOperatorTest, NonDefaultAssignedToDefault) {
489 FilePath non_default_path("path");
490 FilePath default_path;
491 default_path = non_default_path;
492 EXPECT_STREQ("path", default_path.c_str());
493 EXPECT_STREQ("path", non_default_path.c_str()); // RHS var is unchanged.
496 TEST(AssignmentOperatorTest, ConstAssignedToNonConst) {
497 const FilePath const_default_path("const_path");
498 FilePath non_default_path("path");
499 non_default_path = const_default_path;
500 EXPECT_STREQ("const_path", non_default_path.c_str());
503 class DirectoryCreationTest : public Test {
505 virtual void SetUp() {
506 testdata_path_.Set(FilePath(String::Format("%s%s%s",
507 TempDir().c_str(), GetCurrentExecutableName().c_str(),
508 "_directory_creation" GTEST_PATH_SEP_ "test" GTEST_PATH_SEP_)));
509 testdata_file_.Set(testdata_path_.RemoveTrailingPathSeparator());
511 unique_file0_.Set(FilePath::MakeFileName(testdata_path_, FilePath("unique"),
513 unique_file1_.Set(FilePath::MakeFileName(testdata_path_, FilePath("unique"),
516 remove(testdata_file_.c_str());
517 remove(unique_file0_.c_str());
518 remove(unique_file1_.c_str());
519 posix::RmDir(testdata_path_.c_str());
522 virtual void TearDown() {
523 remove(testdata_file_.c_str());
524 remove(unique_file0_.c_str());
525 remove(unique_file1_.c_str());
526 posix::RmDir(testdata_path_.c_str());
529 String TempDir() const {
530 #if GTEST_OS_WINDOWS_MOBILE
531 return String("\\temp\\");
532 #elif GTEST_OS_WINDOWS
533 const char* temp_dir = posix::GetEnv("TEMP");
534 if (temp_dir == NULL || temp_dir[0] == '\0')
535 return String("\\temp\\");
536 else if (String(temp_dir).EndsWith("\\"))
537 return String(temp_dir);
539 return String::Format("%s\\", temp_dir);
541 return String("/tmp/");
542 #endif // GTEST_OS_WINDOWS_MOBILE
545 void CreateTextFile(const char* filename) {
546 FILE* f = posix::FOpen(filename, "w");
547 fprintf(f, "text\n");
551 // Strings representing a directory and a file, with identical paths
552 // except for the trailing separator character that distinquishes
553 // a directory named 'test' from a file named 'test'. Example names:
554 FilePath testdata_path_; // "/tmp/directory_creation/test/"
555 FilePath testdata_file_; // "/tmp/directory_creation/test"
556 FilePath unique_file0_; // "/tmp/directory_creation/test/unique.txt"
557 FilePath unique_file1_; // "/tmp/directory_creation/test/unique_1.txt"
560 TEST_F(DirectoryCreationTest, CreateDirectoriesRecursively) {
561 EXPECT_FALSE(testdata_path_.DirectoryExists()) << testdata_path_.c_str();
562 EXPECT_TRUE(testdata_path_.CreateDirectoriesRecursively());
563 EXPECT_TRUE(testdata_path_.DirectoryExists());
566 TEST_F(DirectoryCreationTest, CreateDirectoriesForAlreadyExistingPath) {
567 EXPECT_FALSE(testdata_path_.DirectoryExists()) << testdata_path_.c_str();
568 EXPECT_TRUE(testdata_path_.CreateDirectoriesRecursively());
569 // Call 'create' again... should still succeed.
570 EXPECT_TRUE(testdata_path_.CreateDirectoriesRecursively());
573 TEST_F(DirectoryCreationTest, CreateDirectoriesAndUniqueFilename) {
574 FilePath file_path(FilePath::GenerateUniqueFileName(testdata_path_,
575 FilePath("unique"), "txt"));
576 EXPECT_STREQ(unique_file0_.c_str(), file_path.c_str());
577 EXPECT_FALSE(file_path.FileOrDirectoryExists()); // file not there
579 testdata_path_.CreateDirectoriesRecursively();
580 EXPECT_FALSE(file_path.FileOrDirectoryExists()); // file still not there
581 CreateTextFile(file_path.c_str());
582 EXPECT_TRUE(file_path.FileOrDirectoryExists());
584 FilePath file_path2(FilePath::GenerateUniqueFileName(testdata_path_,
585 FilePath("unique"), "txt"));
586 EXPECT_STREQ(unique_file1_.c_str(), file_path2.c_str());
587 EXPECT_FALSE(file_path2.FileOrDirectoryExists()); // file not there
588 CreateTextFile(file_path2.c_str());
589 EXPECT_TRUE(file_path2.FileOrDirectoryExists());
592 TEST_F(DirectoryCreationTest, CreateDirectoriesFail) {
593 // force a failure by putting a file where we will try to create a directory.
594 CreateTextFile(testdata_file_.c_str());
595 EXPECT_TRUE(testdata_file_.FileOrDirectoryExists());
596 EXPECT_FALSE(testdata_file_.DirectoryExists());
597 EXPECT_FALSE(testdata_file_.CreateDirectoriesRecursively());
600 TEST(NoDirectoryCreationTest, CreateNoDirectoriesForDefaultXmlFile) {
601 const FilePath test_detail_xml("test_detail.xml");
602 EXPECT_FALSE(test_detail_xml.CreateDirectoriesRecursively());
605 TEST(FilePathTest, DefaultConstructor) {
607 EXPECT_STREQ("", fp.c_str());
610 TEST(FilePathTest, CharAndCopyConstructors) {
611 const FilePath fp("spicy");
612 EXPECT_STREQ("spicy", fp.c_str());
614 const FilePath fp_copy(fp);
615 EXPECT_STREQ("spicy", fp_copy.c_str());
618 TEST(FilePathTest, StringConstructor) {
619 const FilePath fp(String("cider"));
620 EXPECT_STREQ("cider", fp.c_str());
623 TEST(FilePathTest, Set) {
624 const FilePath apple("apple");
626 mac.Set(apple); // Implement Set() since overloading operator= is forbidden.
627 EXPECT_STREQ("apple", mac.c_str());
628 EXPECT_STREQ("apple", apple.c_str());
631 TEST(FilePathTest, ToString) {
632 const FilePath file("drink");
633 String str(file.ToString());
634 EXPECT_STREQ("drink", str.c_str());
637 TEST(FilePathTest, RemoveExtension) {
638 EXPECT_STREQ("app", FilePath("app.exe").RemoveExtension("exe").c_str());
639 EXPECT_STREQ("APP", FilePath("APP.EXE").RemoveExtension("exe").c_str());
642 TEST(FilePathTest, RemoveExtensionWhenThereIsNoExtension) {
643 EXPECT_STREQ("app", FilePath("app").RemoveExtension("exe").c_str());
646 TEST(FilePathTest, IsDirectory) {
647 EXPECT_FALSE(FilePath("cola").IsDirectory());
648 EXPECT_TRUE(FilePath("koala" GTEST_PATH_SEP_).IsDirectory());
649 #if GTEST_HAS_ALT_PATH_SEP_
650 EXPECT_TRUE(FilePath("koala/").IsDirectory());
654 TEST(FilePathTest, IsAbsolutePath) {
655 EXPECT_FALSE(FilePath("is" GTEST_PATH_SEP_ "relative").IsAbsolutePath());
656 EXPECT_FALSE(FilePath("").IsAbsolutePath());
658 EXPECT_TRUE(FilePath("c:\\" GTEST_PATH_SEP_ "is_not"
659 GTEST_PATH_SEP_ "relative").IsAbsolutePath());
660 EXPECT_FALSE(FilePath("c:foo" GTEST_PATH_SEP_ "bar").IsAbsolutePath());
661 EXPECT_TRUE(FilePath("c:/" GTEST_PATH_SEP_ "is_not"
662 GTEST_PATH_SEP_ "relative").IsAbsolutePath());
664 EXPECT_TRUE(FilePath(GTEST_PATH_SEP_ "is_not" GTEST_PATH_SEP_ "relative")
666 #endif // GTEST_OS_WINDOWS
669 TEST(FilePathTest, IsRootDirectory) {
671 EXPECT_TRUE(FilePath("a:\\").IsRootDirectory());
672 EXPECT_TRUE(FilePath("Z:/").IsRootDirectory());
673 EXPECT_TRUE(FilePath("e://").IsRootDirectory());
674 EXPECT_FALSE(FilePath("").IsRootDirectory());
675 EXPECT_FALSE(FilePath("b:").IsRootDirectory());
676 EXPECT_FALSE(FilePath("b:a").IsRootDirectory());
677 EXPECT_FALSE(FilePath("8:/").IsRootDirectory());
678 EXPECT_FALSE(FilePath("c|/").IsRootDirectory());
680 EXPECT_TRUE(FilePath("/").IsRootDirectory());
681 EXPECT_TRUE(FilePath("//").IsRootDirectory());
682 EXPECT_FALSE(FilePath("").IsRootDirectory());
683 EXPECT_FALSE(FilePath("\\").IsRootDirectory());
684 EXPECT_FALSE(FilePath("/x").IsRootDirectory());
689 } // namespace internal
690 } // namespace testing