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