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