michael@0: // Copyright 2008, Google Inc. michael@0: // All rights reserved. michael@0: // michael@0: // Redistribution and use in source and binary forms, with or without michael@0: // modification, are permitted provided that the following conditions are michael@0: // met: michael@0: // michael@0: // * Redistributions of source code must retain the above copyright michael@0: // notice, this list of conditions and the following disclaimer. michael@0: // * Redistributions in binary form must reproduce the above michael@0: // copyright notice, this list of conditions and the following disclaimer michael@0: // in the documentation and/or other materials provided with the michael@0: // distribution. michael@0: // * Neither the name of Google Inc. nor the names of its michael@0: // contributors may be used to endorse or promote products derived from michael@0: // this software without specific prior written permission. michael@0: // michael@0: // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS michael@0: // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT michael@0: // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR michael@0: // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT michael@0: // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, michael@0: // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT michael@0: // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, michael@0: // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY michael@0: // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT michael@0: // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE michael@0: // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. michael@0: // michael@0: // Authors: keith.ray@gmail.com (Keith Ray) michael@0: // michael@0: // Google Test filepath utilities michael@0: // michael@0: // This file tests classes and functions used internally by michael@0: // Google Test. They are subject to change without notice. michael@0: // michael@0: // This file is #included from gtest_unittest.cc, to avoid changing michael@0: // build or make-files for some existing Google Test clients. Do not michael@0: // #include this file anywhere else! michael@0: michael@0: #include "gtest/internal/gtest-filepath.h" michael@0: #include "gtest/gtest.h" michael@0: michael@0: // Indicates that this translation unit is part of Google Test's michael@0: // implementation. It must come before gtest-internal-inl.h is michael@0: // included, or there will be a compiler error. This trick is to michael@0: // prevent a user from accidentally including gtest-internal-inl.h in michael@0: // his code. michael@0: #define GTEST_IMPLEMENTATION_ 1 michael@0: #include "src/gtest-internal-inl.h" michael@0: #undef GTEST_IMPLEMENTATION_ michael@0: michael@0: #if GTEST_OS_WINDOWS_MOBILE michael@0: # include // NOLINT michael@0: #elif GTEST_OS_WINDOWS michael@0: # include // NOLINT michael@0: #endif // GTEST_OS_WINDOWS_MOBILE michael@0: michael@0: namespace testing { michael@0: namespace internal { michael@0: namespace { michael@0: michael@0: #if GTEST_OS_WINDOWS_MOBILE michael@0: // TODO(wan@google.com): Move these to the POSIX adapter section in michael@0: // gtest-port.h. michael@0: michael@0: // Windows CE doesn't have the remove C function. michael@0: int remove(const char* path) { michael@0: LPCWSTR wpath = String::AnsiToUtf16(path); michael@0: int ret = DeleteFile(wpath) ? 0 : -1; michael@0: delete [] wpath; michael@0: return ret; michael@0: } michael@0: // Windows CE doesn't have the _rmdir C function. michael@0: int _rmdir(const char* path) { michael@0: FilePath filepath(path); michael@0: LPCWSTR wpath = String::AnsiToUtf16( michael@0: filepath.RemoveTrailingPathSeparator().c_str()); michael@0: int ret = RemoveDirectory(wpath) ? 0 : -1; michael@0: delete [] wpath; michael@0: return ret; michael@0: } michael@0: michael@0: #else michael@0: michael@0: TEST(GetCurrentDirTest, ReturnsCurrentDir) { michael@0: const FilePath original_dir = FilePath::GetCurrentDir(); michael@0: EXPECT_FALSE(original_dir.IsEmpty()); michael@0: michael@0: posix::ChDir(GTEST_PATH_SEP_); michael@0: const FilePath cwd = FilePath::GetCurrentDir(); michael@0: posix::ChDir(original_dir.c_str()); michael@0: michael@0: # if GTEST_OS_WINDOWS michael@0: michael@0: // Skips the ":". michael@0: const char* const cwd_without_drive = strchr(cwd.c_str(), ':'); michael@0: ASSERT_TRUE(cwd_without_drive != NULL); michael@0: EXPECT_STREQ(GTEST_PATH_SEP_, cwd_without_drive + 1); michael@0: michael@0: # else michael@0: michael@0: EXPECT_STREQ(GTEST_PATH_SEP_, cwd.c_str()); michael@0: michael@0: # endif michael@0: } michael@0: michael@0: #endif // GTEST_OS_WINDOWS_MOBILE michael@0: michael@0: TEST(IsEmptyTest, ReturnsTrueForEmptyPath) { michael@0: EXPECT_TRUE(FilePath("").IsEmpty()); michael@0: EXPECT_TRUE(FilePath(NULL).IsEmpty()); michael@0: } michael@0: michael@0: TEST(IsEmptyTest, ReturnsFalseForNonEmptyPath) { michael@0: EXPECT_FALSE(FilePath("a").IsEmpty()); michael@0: EXPECT_FALSE(FilePath(".").IsEmpty()); michael@0: EXPECT_FALSE(FilePath("a/b").IsEmpty()); michael@0: EXPECT_FALSE(FilePath("a\\b\\").IsEmpty()); michael@0: } michael@0: michael@0: // RemoveDirectoryName "" -> "" michael@0: TEST(RemoveDirectoryNameTest, WhenEmptyName) { michael@0: EXPECT_STREQ("", FilePath("").RemoveDirectoryName().c_str()); michael@0: } michael@0: michael@0: // RemoveDirectoryName "afile" -> "afile" michael@0: TEST(RemoveDirectoryNameTest, ButNoDirectory) { michael@0: EXPECT_STREQ("afile", michael@0: FilePath("afile").RemoveDirectoryName().c_str()); michael@0: } michael@0: michael@0: // RemoveDirectoryName "/afile" -> "afile" michael@0: TEST(RemoveDirectoryNameTest, RootFileShouldGiveFileName) { michael@0: EXPECT_STREQ("afile", michael@0: FilePath(GTEST_PATH_SEP_ "afile").RemoveDirectoryName().c_str()); michael@0: } michael@0: michael@0: // RemoveDirectoryName "adir/" -> "" michael@0: TEST(RemoveDirectoryNameTest, WhereThereIsNoFileName) { michael@0: EXPECT_STREQ("", michael@0: FilePath("adir" GTEST_PATH_SEP_).RemoveDirectoryName().c_str()); michael@0: } michael@0: michael@0: // RemoveDirectoryName "adir/afile" -> "afile" michael@0: TEST(RemoveDirectoryNameTest, ShouldGiveFileName) { michael@0: EXPECT_STREQ("afile", michael@0: FilePath("adir" GTEST_PATH_SEP_ "afile").RemoveDirectoryName().c_str()); michael@0: } michael@0: michael@0: // RemoveDirectoryName "adir/subdir/afile" -> "afile" michael@0: TEST(RemoveDirectoryNameTest, ShouldAlsoGiveFileName) { michael@0: EXPECT_STREQ("afile", michael@0: FilePath("adir" GTEST_PATH_SEP_ "subdir" GTEST_PATH_SEP_ "afile") michael@0: .RemoveDirectoryName().c_str()); michael@0: } michael@0: michael@0: #if GTEST_HAS_ALT_PATH_SEP_ michael@0: michael@0: // Tests that RemoveDirectoryName() works with the alternate separator michael@0: // on Windows. michael@0: michael@0: // RemoveDirectoryName("/afile") -> "afile" michael@0: TEST(RemoveDirectoryNameTest, RootFileShouldGiveFileNameForAlternateSeparator) { michael@0: EXPECT_STREQ("afile", michael@0: FilePath("/afile").RemoveDirectoryName().c_str()); michael@0: } michael@0: michael@0: // RemoveDirectoryName("adir/") -> "" michael@0: TEST(RemoveDirectoryNameTest, WhereThereIsNoFileNameForAlternateSeparator) { michael@0: EXPECT_STREQ("", michael@0: FilePath("adir/").RemoveDirectoryName().c_str()); michael@0: } michael@0: michael@0: // RemoveDirectoryName("adir/afile") -> "afile" michael@0: TEST(RemoveDirectoryNameTest, ShouldGiveFileNameForAlternateSeparator) { michael@0: EXPECT_STREQ("afile", michael@0: FilePath("adir/afile").RemoveDirectoryName().c_str()); michael@0: } michael@0: michael@0: // RemoveDirectoryName("adir/subdir/afile") -> "afile" michael@0: TEST(RemoveDirectoryNameTest, ShouldAlsoGiveFileNameForAlternateSeparator) { michael@0: EXPECT_STREQ("afile", michael@0: FilePath("adir/subdir/afile").RemoveDirectoryName().c_str()); michael@0: } michael@0: michael@0: #endif michael@0: michael@0: // RemoveFileName "" -> "./" michael@0: TEST(RemoveFileNameTest, EmptyName) { michael@0: #if GTEST_OS_WINDOWS_MOBILE michael@0: // On Windows CE, we use the root as the current directory. michael@0: EXPECT_STREQ(GTEST_PATH_SEP_, michael@0: FilePath("").RemoveFileName().c_str()); michael@0: #else michael@0: EXPECT_STREQ("." GTEST_PATH_SEP_, michael@0: FilePath("").RemoveFileName().c_str()); michael@0: #endif michael@0: } michael@0: michael@0: // RemoveFileName "adir/" -> "adir/" michael@0: TEST(RemoveFileNameTest, ButNoFile) { michael@0: EXPECT_STREQ("adir" GTEST_PATH_SEP_, michael@0: FilePath("adir" GTEST_PATH_SEP_).RemoveFileName().c_str()); michael@0: } michael@0: michael@0: // RemoveFileName "adir/afile" -> "adir/" michael@0: TEST(RemoveFileNameTest, GivesDirName) { michael@0: EXPECT_STREQ("adir" GTEST_PATH_SEP_, michael@0: FilePath("adir" GTEST_PATH_SEP_ "afile") michael@0: .RemoveFileName().c_str()); michael@0: } michael@0: michael@0: // RemoveFileName "adir/subdir/afile" -> "adir/subdir/" michael@0: TEST(RemoveFileNameTest, GivesDirAndSubDirName) { michael@0: EXPECT_STREQ("adir" GTEST_PATH_SEP_ "subdir" GTEST_PATH_SEP_, michael@0: FilePath("adir" GTEST_PATH_SEP_ "subdir" GTEST_PATH_SEP_ "afile") michael@0: .RemoveFileName().c_str()); michael@0: } michael@0: michael@0: // RemoveFileName "/afile" -> "/" michael@0: TEST(RemoveFileNameTest, GivesRootDir) { michael@0: EXPECT_STREQ(GTEST_PATH_SEP_, michael@0: FilePath(GTEST_PATH_SEP_ "afile").RemoveFileName().c_str()); michael@0: } michael@0: michael@0: #if GTEST_HAS_ALT_PATH_SEP_ michael@0: michael@0: // Tests that RemoveFileName() works with the alternate separator on michael@0: // Windows. michael@0: michael@0: // RemoveFileName("adir/") -> "adir/" michael@0: TEST(RemoveFileNameTest, ButNoFileForAlternateSeparator) { michael@0: EXPECT_STREQ("adir" GTEST_PATH_SEP_, michael@0: FilePath("adir/").RemoveFileName().c_str()); michael@0: } michael@0: michael@0: // RemoveFileName("adir/afile") -> "adir/" michael@0: TEST(RemoveFileNameTest, GivesDirNameForAlternateSeparator) { michael@0: EXPECT_STREQ("adir" GTEST_PATH_SEP_, michael@0: FilePath("adir/afile").RemoveFileName().c_str()); michael@0: } michael@0: michael@0: // RemoveFileName("adir/subdir/afile") -> "adir/subdir/" michael@0: TEST(RemoveFileNameTest, GivesDirAndSubDirNameForAlternateSeparator) { michael@0: EXPECT_STREQ("adir" GTEST_PATH_SEP_ "subdir" GTEST_PATH_SEP_, michael@0: FilePath("adir/subdir/afile").RemoveFileName().c_str()); michael@0: } michael@0: michael@0: // RemoveFileName("/afile") -> "\" michael@0: TEST(RemoveFileNameTest, GivesRootDirForAlternateSeparator) { michael@0: EXPECT_STREQ(GTEST_PATH_SEP_, michael@0: FilePath("/afile").RemoveFileName().c_str()); michael@0: } michael@0: michael@0: #endif michael@0: michael@0: TEST(MakeFileNameTest, GenerateWhenNumberIsZero) { michael@0: FilePath actual = FilePath::MakeFileName(FilePath("foo"), FilePath("bar"), michael@0: 0, "xml"); michael@0: EXPECT_STREQ("foo" GTEST_PATH_SEP_ "bar.xml", actual.c_str()); michael@0: } michael@0: michael@0: TEST(MakeFileNameTest, GenerateFileNameNumberGtZero) { michael@0: FilePath actual = FilePath::MakeFileName(FilePath("foo"), FilePath("bar"), michael@0: 12, "xml"); michael@0: EXPECT_STREQ("foo" GTEST_PATH_SEP_ "bar_12.xml", actual.c_str()); michael@0: } michael@0: michael@0: TEST(MakeFileNameTest, GenerateFileNameWithSlashNumberIsZero) { michael@0: FilePath actual = FilePath::MakeFileName(FilePath("foo" GTEST_PATH_SEP_), michael@0: FilePath("bar"), 0, "xml"); michael@0: EXPECT_STREQ("foo" GTEST_PATH_SEP_ "bar.xml", actual.c_str()); michael@0: } michael@0: michael@0: TEST(MakeFileNameTest, GenerateFileNameWithSlashNumberGtZero) { michael@0: FilePath actual = FilePath::MakeFileName(FilePath("foo" GTEST_PATH_SEP_), michael@0: FilePath("bar"), 12, "xml"); michael@0: EXPECT_STREQ("foo" GTEST_PATH_SEP_ "bar_12.xml", actual.c_str()); michael@0: } michael@0: michael@0: TEST(MakeFileNameTest, GenerateWhenNumberIsZeroAndDirIsEmpty) { michael@0: FilePath actual = FilePath::MakeFileName(FilePath(""), FilePath("bar"), michael@0: 0, "xml"); michael@0: EXPECT_STREQ("bar.xml", actual.c_str()); michael@0: } michael@0: michael@0: TEST(MakeFileNameTest, GenerateWhenNumberIsNotZeroAndDirIsEmpty) { michael@0: FilePath actual = FilePath::MakeFileName(FilePath(""), FilePath("bar"), michael@0: 14, "xml"); michael@0: EXPECT_STREQ("bar_14.xml", actual.c_str()); michael@0: } michael@0: michael@0: TEST(ConcatPathsTest, WorksWhenDirDoesNotEndWithPathSep) { michael@0: FilePath actual = FilePath::ConcatPaths(FilePath("foo"), michael@0: FilePath("bar.xml")); michael@0: EXPECT_STREQ("foo" GTEST_PATH_SEP_ "bar.xml", actual.c_str()); michael@0: } michael@0: michael@0: TEST(ConcatPathsTest, WorksWhenPath1EndsWithPathSep) { michael@0: FilePath actual = FilePath::ConcatPaths(FilePath("foo" GTEST_PATH_SEP_), michael@0: FilePath("bar.xml")); michael@0: EXPECT_STREQ("foo" GTEST_PATH_SEP_ "bar.xml", actual.c_str()); michael@0: } michael@0: michael@0: TEST(ConcatPathsTest, Path1BeingEmpty) { michael@0: FilePath actual = FilePath::ConcatPaths(FilePath(""), michael@0: FilePath("bar.xml")); michael@0: EXPECT_STREQ("bar.xml", actual.c_str()); michael@0: } michael@0: michael@0: TEST(ConcatPathsTest, Path2BeingEmpty) { michael@0: FilePath actual = FilePath::ConcatPaths(FilePath("foo"), michael@0: FilePath("")); michael@0: EXPECT_STREQ("foo" GTEST_PATH_SEP_, actual.c_str()); michael@0: } michael@0: michael@0: TEST(ConcatPathsTest, BothPathBeingEmpty) { michael@0: FilePath actual = FilePath::ConcatPaths(FilePath(""), michael@0: FilePath("")); michael@0: EXPECT_STREQ("", actual.c_str()); michael@0: } michael@0: michael@0: TEST(ConcatPathsTest, Path1ContainsPathSep) { michael@0: FilePath actual = FilePath::ConcatPaths(FilePath("foo" GTEST_PATH_SEP_ "bar"), michael@0: FilePath("foobar.xml")); michael@0: EXPECT_STREQ("foo" GTEST_PATH_SEP_ "bar" GTEST_PATH_SEP_ "foobar.xml", michael@0: actual.c_str()); michael@0: } michael@0: michael@0: TEST(ConcatPathsTest, Path2ContainsPathSep) { michael@0: FilePath actual = FilePath::ConcatPaths( michael@0: FilePath("foo" GTEST_PATH_SEP_), michael@0: FilePath("bar" GTEST_PATH_SEP_ "bar.xml")); michael@0: EXPECT_STREQ("foo" GTEST_PATH_SEP_ "bar" GTEST_PATH_SEP_ "bar.xml", michael@0: actual.c_str()); michael@0: } michael@0: michael@0: TEST(ConcatPathsTest, Path2EndsWithPathSep) { michael@0: FilePath actual = FilePath::ConcatPaths(FilePath("foo"), michael@0: FilePath("bar" GTEST_PATH_SEP_)); michael@0: EXPECT_STREQ("foo" GTEST_PATH_SEP_ "bar" GTEST_PATH_SEP_, actual.c_str()); michael@0: } michael@0: michael@0: // RemoveTrailingPathSeparator "" -> "" michael@0: TEST(RemoveTrailingPathSeparatorTest, EmptyString) { michael@0: EXPECT_STREQ("", michael@0: FilePath("").RemoveTrailingPathSeparator().c_str()); michael@0: } michael@0: michael@0: // RemoveTrailingPathSeparator "foo" -> "foo" michael@0: TEST(RemoveTrailingPathSeparatorTest, FileNoSlashString) { michael@0: EXPECT_STREQ("foo", michael@0: FilePath("foo").RemoveTrailingPathSeparator().c_str()); michael@0: } michael@0: michael@0: // RemoveTrailingPathSeparator "foo/" -> "foo" michael@0: TEST(RemoveTrailingPathSeparatorTest, ShouldRemoveTrailingSeparator) { michael@0: EXPECT_STREQ( michael@0: "foo", michael@0: FilePath("foo" GTEST_PATH_SEP_).RemoveTrailingPathSeparator().c_str()); michael@0: #if GTEST_HAS_ALT_PATH_SEP_ michael@0: EXPECT_STREQ("foo", michael@0: FilePath("foo/").RemoveTrailingPathSeparator().c_str()); michael@0: #endif michael@0: } michael@0: michael@0: // RemoveTrailingPathSeparator "foo/bar/" -> "foo/bar/" michael@0: TEST(RemoveTrailingPathSeparatorTest, ShouldRemoveLastSeparator) { michael@0: EXPECT_STREQ("foo" GTEST_PATH_SEP_ "bar", michael@0: FilePath("foo" GTEST_PATH_SEP_ "bar" GTEST_PATH_SEP_) michael@0: .RemoveTrailingPathSeparator().c_str()); michael@0: } michael@0: michael@0: // RemoveTrailingPathSeparator "foo/bar" -> "foo/bar" michael@0: TEST(RemoveTrailingPathSeparatorTest, ShouldReturnUnmodified) { michael@0: EXPECT_STREQ("foo" GTEST_PATH_SEP_ "bar", michael@0: FilePath("foo" GTEST_PATH_SEP_ "bar") michael@0: .RemoveTrailingPathSeparator().c_str()); michael@0: } michael@0: michael@0: TEST(DirectoryTest, RootDirectoryExists) { michael@0: #if GTEST_OS_WINDOWS // We are on Windows. michael@0: char current_drive[_MAX_PATH]; // NOLINT michael@0: current_drive[0] = static_cast(_getdrive() + 'A' - 1); michael@0: current_drive[1] = ':'; michael@0: current_drive[2] = '\\'; michael@0: current_drive[3] = '\0'; michael@0: EXPECT_TRUE(FilePath(current_drive).DirectoryExists()); michael@0: #else michael@0: EXPECT_TRUE(FilePath("/").DirectoryExists()); michael@0: #endif // GTEST_OS_WINDOWS michael@0: } michael@0: michael@0: #if GTEST_OS_WINDOWS michael@0: TEST(DirectoryTest, RootOfWrongDriveDoesNotExists) { michael@0: const int saved_drive_ = _getdrive(); michael@0: // Find a drive that doesn't exist. Start with 'Z' to avoid common ones. michael@0: for (char drive = 'Z'; drive >= 'A'; drive--) michael@0: if (_chdrive(drive - 'A' + 1) == -1) { michael@0: char non_drive[_MAX_PATH]; // NOLINT michael@0: non_drive[0] = drive; michael@0: non_drive[1] = ':'; michael@0: non_drive[2] = '\\'; michael@0: non_drive[3] = '\0'; michael@0: EXPECT_FALSE(FilePath(non_drive).DirectoryExists()); michael@0: break; michael@0: } michael@0: _chdrive(saved_drive_); michael@0: } michael@0: #endif // GTEST_OS_WINDOWS michael@0: michael@0: #if !GTEST_OS_WINDOWS_MOBILE michael@0: // Windows CE _does_ consider an empty directory to exist. michael@0: TEST(DirectoryTest, EmptyPathDirectoryDoesNotExist) { michael@0: EXPECT_FALSE(FilePath("").DirectoryExists()); michael@0: } michael@0: #endif // !GTEST_OS_WINDOWS_MOBILE michael@0: michael@0: TEST(DirectoryTest, CurrentDirectoryExists) { michael@0: #if GTEST_OS_WINDOWS // We are on Windows. michael@0: # ifndef _WIN32_CE // Windows CE doesn't have a current directory. michael@0: michael@0: EXPECT_TRUE(FilePath(".").DirectoryExists()); michael@0: EXPECT_TRUE(FilePath(".\\").DirectoryExists()); michael@0: michael@0: # endif // _WIN32_CE michael@0: #else michael@0: EXPECT_TRUE(FilePath(".").DirectoryExists()); michael@0: EXPECT_TRUE(FilePath("./").DirectoryExists()); michael@0: #endif // GTEST_OS_WINDOWS michael@0: } michael@0: michael@0: TEST(NormalizeTest, NullStringsEqualEmptyDirectory) { michael@0: EXPECT_STREQ("", FilePath(NULL).c_str()); michael@0: EXPECT_STREQ("", FilePath(String(NULL)).c_str()); michael@0: } michael@0: michael@0: // "foo/bar" == foo//bar" == "foo///bar" michael@0: TEST(NormalizeTest, MultipleConsecutiveSepaparatorsInMidstring) { michael@0: EXPECT_STREQ("foo" GTEST_PATH_SEP_ "bar", michael@0: FilePath("foo" GTEST_PATH_SEP_ "bar").c_str()); michael@0: EXPECT_STREQ("foo" GTEST_PATH_SEP_ "bar", michael@0: FilePath("foo" GTEST_PATH_SEP_ GTEST_PATH_SEP_ "bar").c_str()); michael@0: EXPECT_STREQ("foo" GTEST_PATH_SEP_ "bar", michael@0: FilePath("foo" GTEST_PATH_SEP_ GTEST_PATH_SEP_ michael@0: GTEST_PATH_SEP_ "bar").c_str()); michael@0: } michael@0: michael@0: // "/bar" == //bar" == "///bar" michael@0: TEST(NormalizeTest, MultipleConsecutiveSepaparatorsAtStringStart) { michael@0: EXPECT_STREQ(GTEST_PATH_SEP_ "bar", michael@0: FilePath(GTEST_PATH_SEP_ "bar").c_str()); michael@0: EXPECT_STREQ(GTEST_PATH_SEP_ "bar", michael@0: FilePath(GTEST_PATH_SEP_ GTEST_PATH_SEP_ "bar").c_str()); michael@0: EXPECT_STREQ(GTEST_PATH_SEP_ "bar", michael@0: FilePath(GTEST_PATH_SEP_ GTEST_PATH_SEP_ GTEST_PATH_SEP_ "bar").c_str()); michael@0: } michael@0: michael@0: // "foo/" == foo//" == "foo///" michael@0: TEST(NormalizeTest, MultipleConsecutiveSepaparatorsAtStringEnd) { michael@0: EXPECT_STREQ("foo" GTEST_PATH_SEP_, michael@0: FilePath("foo" GTEST_PATH_SEP_).c_str()); michael@0: EXPECT_STREQ("foo" GTEST_PATH_SEP_, michael@0: FilePath("foo" GTEST_PATH_SEP_ GTEST_PATH_SEP_).c_str()); michael@0: EXPECT_STREQ("foo" GTEST_PATH_SEP_, michael@0: FilePath("foo" GTEST_PATH_SEP_ GTEST_PATH_SEP_ GTEST_PATH_SEP_).c_str()); michael@0: } michael@0: michael@0: #if GTEST_HAS_ALT_PATH_SEP_ michael@0: michael@0: // Tests that separators at the end of the string are normalized michael@0: // regardless of their combination (e.g. "foo\" =="foo/\" == michael@0: // "foo\\/"). michael@0: TEST(NormalizeTest, MixAlternateSeparatorAtStringEnd) { michael@0: EXPECT_STREQ("foo" GTEST_PATH_SEP_, michael@0: FilePath("foo/").c_str()); michael@0: EXPECT_STREQ("foo" GTEST_PATH_SEP_, michael@0: FilePath("foo" GTEST_PATH_SEP_ "/").c_str()); michael@0: EXPECT_STREQ("foo" GTEST_PATH_SEP_, michael@0: FilePath("foo//" GTEST_PATH_SEP_).c_str()); michael@0: } michael@0: michael@0: #endif michael@0: michael@0: TEST(AssignmentOperatorTest, DefaultAssignedToNonDefault) { michael@0: FilePath default_path; michael@0: FilePath non_default_path("path"); michael@0: non_default_path = default_path; michael@0: EXPECT_STREQ("", non_default_path.c_str()); michael@0: EXPECT_STREQ("", default_path.c_str()); // RHS var is unchanged. michael@0: } michael@0: michael@0: TEST(AssignmentOperatorTest, NonDefaultAssignedToDefault) { michael@0: FilePath non_default_path("path"); michael@0: FilePath default_path; michael@0: default_path = non_default_path; michael@0: EXPECT_STREQ("path", default_path.c_str()); michael@0: EXPECT_STREQ("path", non_default_path.c_str()); // RHS var is unchanged. michael@0: } michael@0: michael@0: TEST(AssignmentOperatorTest, ConstAssignedToNonConst) { michael@0: const FilePath const_default_path("const_path"); michael@0: FilePath non_default_path("path"); michael@0: non_default_path = const_default_path; michael@0: EXPECT_STREQ("const_path", non_default_path.c_str()); michael@0: } michael@0: michael@0: class DirectoryCreationTest : public Test { michael@0: protected: michael@0: virtual void SetUp() { michael@0: testdata_path_.Set(FilePath(String::Format("%s%s%s", michael@0: TempDir().c_str(), GetCurrentExecutableName().c_str(), michael@0: "_directory_creation" GTEST_PATH_SEP_ "test" GTEST_PATH_SEP_))); michael@0: testdata_file_.Set(testdata_path_.RemoveTrailingPathSeparator()); michael@0: michael@0: unique_file0_.Set(FilePath::MakeFileName(testdata_path_, FilePath("unique"), michael@0: 0, "txt")); michael@0: unique_file1_.Set(FilePath::MakeFileName(testdata_path_, FilePath("unique"), michael@0: 1, "txt")); michael@0: michael@0: remove(testdata_file_.c_str()); michael@0: remove(unique_file0_.c_str()); michael@0: remove(unique_file1_.c_str()); michael@0: posix::RmDir(testdata_path_.c_str()); michael@0: } michael@0: michael@0: virtual void TearDown() { michael@0: remove(testdata_file_.c_str()); michael@0: remove(unique_file0_.c_str()); michael@0: remove(unique_file1_.c_str()); michael@0: posix::RmDir(testdata_path_.c_str()); michael@0: } michael@0: michael@0: String TempDir() const { michael@0: #if GTEST_OS_WINDOWS_MOBILE michael@0: return String("\\temp\\"); michael@0: #elif GTEST_OS_WINDOWS michael@0: const char* temp_dir = posix::GetEnv("TEMP"); michael@0: if (temp_dir == NULL || temp_dir[0] == '\0') michael@0: return String("\\temp\\"); michael@0: else if (String(temp_dir).EndsWith("\\")) michael@0: return String(temp_dir); michael@0: else michael@0: return String::Format("%s\\", temp_dir); michael@0: #else michael@0: return String("/tmp/"); michael@0: #endif // GTEST_OS_WINDOWS_MOBILE michael@0: } michael@0: michael@0: void CreateTextFile(const char* filename) { michael@0: FILE* f = posix::FOpen(filename, "w"); michael@0: fprintf(f, "text\n"); michael@0: fclose(f); michael@0: } michael@0: michael@0: // Strings representing a directory and a file, with identical paths michael@0: // except for the trailing separator character that distinquishes michael@0: // a directory named 'test' from a file named 'test'. Example names: michael@0: FilePath testdata_path_; // "/tmp/directory_creation/test/" michael@0: FilePath testdata_file_; // "/tmp/directory_creation/test" michael@0: FilePath unique_file0_; // "/tmp/directory_creation/test/unique.txt" michael@0: FilePath unique_file1_; // "/tmp/directory_creation/test/unique_1.txt" michael@0: }; michael@0: michael@0: TEST_F(DirectoryCreationTest, CreateDirectoriesRecursively) { michael@0: EXPECT_FALSE(testdata_path_.DirectoryExists()) << testdata_path_.c_str(); michael@0: EXPECT_TRUE(testdata_path_.CreateDirectoriesRecursively()); michael@0: EXPECT_TRUE(testdata_path_.DirectoryExists()); michael@0: } michael@0: michael@0: TEST_F(DirectoryCreationTest, CreateDirectoriesForAlreadyExistingPath) { michael@0: EXPECT_FALSE(testdata_path_.DirectoryExists()) << testdata_path_.c_str(); michael@0: EXPECT_TRUE(testdata_path_.CreateDirectoriesRecursively()); michael@0: // Call 'create' again... should still succeed. michael@0: EXPECT_TRUE(testdata_path_.CreateDirectoriesRecursively()); michael@0: } michael@0: michael@0: TEST_F(DirectoryCreationTest, CreateDirectoriesAndUniqueFilename) { michael@0: FilePath file_path(FilePath::GenerateUniqueFileName(testdata_path_, michael@0: FilePath("unique"), "txt")); michael@0: EXPECT_STREQ(unique_file0_.c_str(), file_path.c_str()); michael@0: EXPECT_FALSE(file_path.FileOrDirectoryExists()); // file not there michael@0: michael@0: testdata_path_.CreateDirectoriesRecursively(); michael@0: EXPECT_FALSE(file_path.FileOrDirectoryExists()); // file still not there michael@0: CreateTextFile(file_path.c_str()); michael@0: EXPECT_TRUE(file_path.FileOrDirectoryExists()); michael@0: michael@0: FilePath file_path2(FilePath::GenerateUniqueFileName(testdata_path_, michael@0: FilePath("unique"), "txt")); michael@0: EXPECT_STREQ(unique_file1_.c_str(), file_path2.c_str()); michael@0: EXPECT_FALSE(file_path2.FileOrDirectoryExists()); // file not there michael@0: CreateTextFile(file_path2.c_str()); michael@0: EXPECT_TRUE(file_path2.FileOrDirectoryExists()); michael@0: } michael@0: michael@0: TEST_F(DirectoryCreationTest, CreateDirectoriesFail) { michael@0: // force a failure by putting a file where we will try to create a directory. michael@0: CreateTextFile(testdata_file_.c_str()); michael@0: EXPECT_TRUE(testdata_file_.FileOrDirectoryExists()); michael@0: EXPECT_FALSE(testdata_file_.DirectoryExists()); michael@0: EXPECT_FALSE(testdata_file_.CreateDirectoriesRecursively()); michael@0: } michael@0: michael@0: TEST(NoDirectoryCreationTest, CreateNoDirectoriesForDefaultXmlFile) { michael@0: const FilePath test_detail_xml("test_detail.xml"); michael@0: EXPECT_FALSE(test_detail_xml.CreateDirectoriesRecursively()); michael@0: } michael@0: michael@0: TEST(FilePathTest, DefaultConstructor) { michael@0: FilePath fp; michael@0: EXPECT_STREQ("", fp.c_str()); michael@0: } michael@0: michael@0: TEST(FilePathTest, CharAndCopyConstructors) { michael@0: const FilePath fp("spicy"); michael@0: EXPECT_STREQ("spicy", fp.c_str()); michael@0: michael@0: const FilePath fp_copy(fp); michael@0: EXPECT_STREQ("spicy", fp_copy.c_str()); michael@0: } michael@0: michael@0: TEST(FilePathTest, StringConstructor) { michael@0: const FilePath fp(String("cider")); michael@0: EXPECT_STREQ("cider", fp.c_str()); michael@0: } michael@0: michael@0: TEST(FilePathTest, Set) { michael@0: const FilePath apple("apple"); michael@0: FilePath mac("mac"); michael@0: mac.Set(apple); // Implement Set() since overloading operator= is forbidden. michael@0: EXPECT_STREQ("apple", mac.c_str()); michael@0: EXPECT_STREQ("apple", apple.c_str()); michael@0: } michael@0: michael@0: TEST(FilePathTest, ToString) { michael@0: const FilePath file("drink"); michael@0: String str(file.ToString()); michael@0: EXPECT_STREQ("drink", str.c_str()); michael@0: } michael@0: michael@0: TEST(FilePathTest, RemoveExtension) { michael@0: EXPECT_STREQ("app", FilePath("app.exe").RemoveExtension("exe").c_str()); michael@0: EXPECT_STREQ("APP", FilePath("APP.EXE").RemoveExtension("exe").c_str()); michael@0: } michael@0: michael@0: TEST(FilePathTest, RemoveExtensionWhenThereIsNoExtension) { michael@0: EXPECT_STREQ("app", FilePath("app").RemoveExtension("exe").c_str()); michael@0: } michael@0: michael@0: TEST(FilePathTest, IsDirectory) { michael@0: EXPECT_FALSE(FilePath("cola").IsDirectory()); michael@0: EXPECT_TRUE(FilePath("koala" GTEST_PATH_SEP_).IsDirectory()); michael@0: #if GTEST_HAS_ALT_PATH_SEP_ michael@0: EXPECT_TRUE(FilePath("koala/").IsDirectory()); michael@0: #endif michael@0: } michael@0: michael@0: TEST(FilePathTest, IsAbsolutePath) { michael@0: EXPECT_FALSE(FilePath("is" GTEST_PATH_SEP_ "relative").IsAbsolutePath()); michael@0: EXPECT_FALSE(FilePath("").IsAbsolutePath()); michael@0: #if GTEST_OS_WINDOWS michael@0: EXPECT_TRUE(FilePath("c:\\" GTEST_PATH_SEP_ "is_not" michael@0: GTEST_PATH_SEP_ "relative").IsAbsolutePath()); michael@0: EXPECT_FALSE(FilePath("c:foo" GTEST_PATH_SEP_ "bar").IsAbsolutePath()); michael@0: EXPECT_TRUE(FilePath("c:/" GTEST_PATH_SEP_ "is_not" michael@0: GTEST_PATH_SEP_ "relative").IsAbsolutePath()); michael@0: #else michael@0: EXPECT_TRUE(FilePath(GTEST_PATH_SEP_ "is_not" GTEST_PATH_SEP_ "relative") michael@0: .IsAbsolutePath()); michael@0: #endif // GTEST_OS_WINDOWS michael@0: } michael@0: michael@0: TEST(FilePathTest, IsRootDirectory) { michael@0: #if GTEST_OS_WINDOWS michael@0: EXPECT_TRUE(FilePath("a:\\").IsRootDirectory()); michael@0: EXPECT_TRUE(FilePath("Z:/").IsRootDirectory()); michael@0: EXPECT_TRUE(FilePath("e://").IsRootDirectory()); michael@0: EXPECT_FALSE(FilePath("").IsRootDirectory()); michael@0: EXPECT_FALSE(FilePath("b:").IsRootDirectory()); michael@0: EXPECT_FALSE(FilePath("b:a").IsRootDirectory()); michael@0: EXPECT_FALSE(FilePath("8:/").IsRootDirectory()); michael@0: EXPECT_FALSE(FilePath("c|/").IsRootDirectory()); michael@0: #else michael@0: EXPECT_TRUE(FilePath("/").IsRootDirectory()); michael@0: EXPECT_TRUE(FilePath("//").IsRootDirectory()); michael@0: EXPECT_FALSE(FilePath("").IsRootDirectory()); michael@0: EXPECT_FALSE(FilePath("\\").IsRootDirectory()); michael@0: EXPECT_FALSE(FilePath("/x").IsRootDirectory()); michael@0: #endif michael@0: } michael@0: michael@0: } // namespace michael@0: } // namespace internal michael@0: } // namespace testing