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