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