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