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
59namespace testing {
60namespace internal {
61namespace {
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.
68int 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.
75int _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
86TEST(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  // Skips the ":".
96  const char* const cwd_without_drive = strchr(cwd.c_str(), ':');
97  ASSERT_TRUE(cwd_without_drive != NULL);
98  EXPECT_STREQ(GTEST_PATH_SEP_, cwd_without_drive + 1);
99#else
100  EXPECT_STREQ(GTEST_PATH_SEP_, cwd.c_str());
101#endif
102}
103
104#endif  // GTEST_OS_WINDOWS_MOBILE
105
106TEST(IsEmptyTest, ReturnsTrueForEmptyPath) {
107  EXPECT_TRUE(FilePath("").IsEmpty());
108  EXPECT_TRUE(FilePath(NULL).IsEmpty());
109}
110
111TEST(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 "" -> ""
119TEST(RemoveDirectoryNameTest, WhenEmptyName) {
120  EXPECT_STREQ("", FilePath("").RemoveDirectoryName().c_str());
121}
122
123// RemoveDirectoryName "afile" -> "afile"
124TEST(RemoveDirectoryNameTest, ButNoDirectory) {
125  EXPECT_STREQ("afile",
126      FilePath("afile").RemoveDirectoryName().c_str());
127}
128
129// RemoveDirectoryName "/afile" -> "afile"
130TEST(RemoveDirectoryNameTest, RootFileShouldGiveFileName) {
131  EXPECT_STREQ("afile",
132      FilePath(GTEST_PATH_SEP_ "afile").RemoveDirectoryName().c_str());
133}
134
135// RemoveDirectoryName "adir/" -> ""
136TEST(RemoveDirectoryNameTest, WhereThereIsNoFileName) {
137  EXPECT_STREQ("",
138      FilePath("adir" GTEST_PATH_SEP_).RemoveDirectoryName().c_str());
139}
140
141// RemoveDirectoryName "adir/afile" -> "afile"
142TEST(RemoveDirectoryNameTest, ShouldGiveFileName) {
143  EXPECT_STREQ("afile",
144      FilePath("adir" GTEST_PATH_SEP_ "afile").RemoveDirectoryName().c_str());
145}
146
147// RemoveDirectoryName "adir/subdir/afile" -> "afile"
148TEST(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 "" -> "./"
156TEST(RemoveFileNameTest, EmptyName) {
157#if GTEST_OS_WINDOWS_MOBILE
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/"
168TEST(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/"
174TEST(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/"
181TEST(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" -> "/"
188TEST(RemoveFileNameTest, GivesRootDir) {
189  EXPECT_STREQ(GTEST_PATH_SEP_,
190      FilePath(GTEST_PATH_SEP_ "afile").RemoveFileName().c_str());
191}
192
193
194TEST(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
200TEST(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
206TEST(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
212TEST(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
218TEST(MakeFileNameTest, GenerateWhenNumberIsZeroAndDirIsEmpty) {
219  FilePath actual = FilePath::MakeFileName(FilePath(""), FilePath("bar"),
220      0, "xml");
221  EXPECT_STREQ("bar.xml", actual.c_str());
222}
223
224TEST(MakeFileNameTest, GenerateWhenNumberIsNotZeroAndDirIsEmpty) {
225  FilePath actual = FilePath::MakeFileName(FilePath(""), FilePath("bar"),
226      14, "xml");
227  EXPECT_STREQ("bar_14.xml", actual.c_str());
228}
229
230TEST(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
236TEST(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
242TEST(ConcatPathsTest, Path1BeingEmpty) {
243  FilePath actual = FilePath::ConcatPaths(FilePath(""),
244                                          FilePath("bar.xml"));
245  EXPECT_STREQ("bar.xml", actual.c_str());
246}
247
248TEST(ConcatPathsTest, Path2BeingEmpty) {
249  FilePath actual = FilePath::ConcatPaths(FilePath("foo"),
250                                          FilePath(""));
251  EXPECT_STREQ("foo" GTEST_PATH_SEP_, actual.c_str());
252}
253
254TEST(ConcatPathsTest, BothPathBeingEmpty) {
255  FilePath actual = FilePath::ConcatPaths(FilePath(""),
256                                          FilePath(""));
257  EXPECT_STREQ("", actual.c_str());
258}
259
260TEST(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
267TEST(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
275TEST(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 "" -> ""
282TEST(RemoveTrailingPathSeparatorTest, EmptyString) {
283  EXPECT_STREQ("",
284      FilePath("").RemoveTrailingPathSeparator().c_str());
285}
286
287// RemoveTrailingPathSeparator "foo" -> "foo"
288TEST(RemoveTrailingPathSeparatorTest, FileNoSlashString) {
289  EXPECT_STREQ("foo",
290      FilePath("foo").RemoveTrailingPathSeparator().c_str());
291}
292
293// RemoveTrailingPathSeparator "foo/" -> "foo"
294TEST(RemoveTrailingPathSeparatorTest, ShouldRemoveTrailingSeparator) {
295  EXPECT_STREQ(
296      "foo",
297      FilePath("foo" GTEST_PATH_SEP_).RemoveTrailingPathSeparator().c_str());
298}
299
300// RemoveTrailingPathSeparator "foo/bar/" -> "foo/bar/"
301TEST(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"
308TEST(RemoveTrailingPathSeparatorTest, ShouldReturnUnmodified) {
309  EXPECT_STREQ("foo" GTEST_PATH_SEP_ "bar",
310               FilePath("foo" GTEST_PATH_SEP_ "bar")
311               .RemoveTrailingPathSeparator().c_str());
312}
313
314TEST(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
328TEST(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#if !GTEST_OS_WINDOWS_MOBILE
346// Windows CE _does_ consider an empty directory to exist.
347TEST(DirectoryTest, EmptyPathDirectoryDoesNotExist) {
348  EXPECT_FALSE(FilePath("").DirectoryExists());
349}
350#endif  // !GTEST_OS_WINDOWS_MOBILE
351
352TEST(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
364TEST(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"
370TEST(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"
381TEST(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///"
391TEST(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
400TEST(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
408TEST(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
416TEST(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
423class DirectoryCreationTest : public Test {
424 protected:
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    posix::RmDir(testdata_path_.c_str());
440  }
441
442  virtual void TearDown() {
443    remove(testdata_file_.c_str());
444    remove(unique_file0_.c_str());
445    remove(unique_file1_.c_str());
446    posix::RmDir(testdata_path_.c_str());
447  }
448
449  String TempDir() const {
450#if GTEST_OS_WINDOWS_MOBILE
451    return String("\\temp\\");
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  // GTEST_OS_WINDOWS_MOBILE
463  }
464
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
480TEST_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
486TEST_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
493TEST_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
512TEST_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
520TEST(NoDirectoryCreationTest, CreateNoDirectoriesForDefaultXmlFile) {
521  const FilePath test_detail_xml("test_detail.xml");
522  EXPECT_FALSE(test_detail_xml.CreateDirectoriesRecursively());
523}
524
525TEST(FilePathTest, DefaultConstructor) {
526  FilePath fp;
527  EXPECT_STREQ("", fp.c_str());
528}
529
530TEST(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
538TEST(FilePathTest, StringConstructor) {
539  const FilePath fp(String("cider"));
540  EXPECT_STREQ("cider", fp.c_str());
541}
542
543TEST(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
551TEST(FilePathTest, ToString) {
552  const FilePath file("drink");
553  String str(file.ToString());
554  EXPECT_STREQ("drink", str.c_str());
555}
556
557TEST(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
562TEST(FilePathTest, RemoveExtensionWhenThereIsNoExtension) {
563  EXPECT_STREQ("app", FilePath("app").RemoveExtension("exe").c_str());
564}
565
566TEST(FilePathTest, IsDirectory) {
567  EXPECT_FALSE(FilePath("cola").IsDirectory());
568  EXPECT_TRUE(FilePath("koala" GTEST_PATH_SEP_).IsDirectory());
569}
570
571TEST(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