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