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