jochen@chromium.org | a687977 | 2010-02-18 19:02:26 +0900 | [diff] [blame] | 1 | // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
license.bot | f003cfe | 2008-08-24 09:55:55 +0900 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 4 | |
erikkay@google.com | c8ec9e9 | 2008-08-16 02:50:10 +0900 | [diff] [blame] | 5 | #include "build/build_config.h" |
| 6 | |
erikkay@google.com | 014161d | 2008-08-16 02:45:13 +0900 | [diff] [blame] | 7 | #if defined(OS_WIN) |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 8 | #include <windows.h> |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 9 | #include <shellapi.h> |
| 10 | #include <shlobj.h> |
tkent@chromium.org | 8da1416 | 2009-10-09 16:33:39 +0900 | [diff] [blame] | 11 | #include <tchar.h> |
erikkay@google.com | dfb51b2 | 2008-08-16 02:32:10 +0900 | [diff] [blame] | 12 | #endif |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 13 | |
| 14 | #include <fstream> |
| 15 | #include <iostream> |
erikkay@google.com | dfb51b2 | 2008-08-16 02:32:10 +0900 | [diff] [blame] | 16 | #include <set> |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 17 | |
| 18 | #include "base/base_paths.h" |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 19 | #include "base/file_path.h" |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 20 | #include "base/file_util.h" |
| 21 | #include "base/logging.h" |
| 22 | #include "base/path_service.h" |
erikkay@google.com | 8d133f6 | 2009-04-24 00:05:19 +0900 | [diff] [blame] | 23 | #include "base/platform_thread.h" |
erikkay@google.com | 9ac2676 | 2009-04-18 09:42:48 +0900 | [diff] [blame] | 24 | #include "base/time.h" |
brettw@chromium.org | 50c9465 | 2009-10-07 11:10:20 +0900 | [diff] [blame] | 25 | #include "base/utf_string_conversions.h" |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 26 | #include "testing/gtest/include/gtest/gtest.h" |
jeremy@chromium.org | 0d8eba7 | 2008-12-03 04:20:15 +0900 | [diff] [blame] | 27 | #include "testing/platform_test.h" |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 28 | |
phajdan.jr@chromium.org | f9908a7 | 2009-04-04 02:17:58 +0900 | [diff] [blame] | 29 | // This macro helps avoid wrapped lines in the test structs. |
| 30 | #define FPL(x) FILE_PATH_LITERAL(x) |
| 31 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 32 | namespace { |
| 33 | |
thestig@chromium.org | 1dad8c6 | 2010-05-08 03:58:45 +0900 | [diff] [blame] | 34 | const wchar_t bogus_content[] = L"I'm cannon fodder."; |
| 35 | |
yuzo@chromium.org | 2da0f82 | 2009-06-09 14:57:38 +0900 | [diff] [blame] | 36 | const file_util::FileEnumerator::FILE_TYPE FILES_AND_DIRECTORIES = |
| 37 | static_cast<file_util::FileEnumerator::FILE_TYPE>( |
| 38 | file_util::FileEnumerator::FILES | |
| 39 | file_util::FileEnumerator::DIRECTORIES); |
| 40 | |
erikkay@google.com | f240684 | 2008-08-21 00:59:49 +0900 | [diff] [blame] | 41 | // file_util winds up using autoreleased objects on the Mac, so this needs |
| 42 | // to be a PlatformTest |
| 43 | class FileUtilTest : public PlatformTest { |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 44 | protected: |
| 45 | virtual void SetUp() { |
erikkay@google.com | f240684 | 2008-08-21 00:59:49 +0900 | [diff] [blame] | 46 | PlatformTest::SetUp(); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 47 | // Name a subdirectory of the temp directory. |
| 48 | ASSERT_TRUE(PathService::Get(base::DIR_TEMP, &test_dir_)); |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 49 | test_dir_ = test_dir_.Append(FILE_PATH_LITERAL("FileUtilTest")); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 50 | |
| 51 | // Create a fresh, empty copy of this directory. |
| 52 | file_util::Delete(test_dir_, true); |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 53 | file_util::CreateDirectory(test_dir_); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 54 | } |
| 55 | virtual void TearDown() { |
erikkay@google.com | f240684 | 2008-08-21 00:59:49 +0900 | [diff] [blame] | 56 | PlatformTest::TearDown(); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 57 | // Clean up test directory |
erikkay@google.com | dfb51b2 | 2008-08-16 02:32:10 +0900 | [diff] [blame] | 58 | ASSERT_TRUE(file_util::Delete(test_dir_, true)); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 59 | ASSERT_FALSE(file_util::PathExists(test_dir_)); |
| 60 | } |
| 61 | |
| 62 | // the path to temporary directory used to contain the test operations |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 63 | FilePath test_dir_; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 64 | }; |
| 65 | |
| 66 | // Collects all the results from the given file enumerator, and provides an |
| 67 | // interface to query whether a given file is present. |
| 68 | class FindResultCollector { |
| 69 | public: |
evan@chromium.org | 1543ad3 | 2009-08-27 05:00:14 +0900 | [diff] [blame] | 70 | explicit FindResultCollector(file_util::FileEnumerator& enumerator) { |
avi@google.com | 5cb7935 | 2008-12-11 23:55:12 +0900 | [diff] [blame] | 71 | FilePath cur_file; |
| 72 | while (!(cur_file = enumerator.Next()).value().empty()) { |
| 73 | FilePath::StringType path = cur_file.value(); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 74 | // The file should not be returned twice. |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 75 | EXPECT_TRUE(files_.end() == files_.find(path)) |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 76 | << "Same file returned twice"; |
| 77 | |
| 78 | // Save for later. |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 79 | files_.insert(path); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 80 | } |
| 81 | } |
| 82 | |
| 83 | // Returns true if the enumerator found the file. |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 84 | bool HasFile(const FilePath& file) const { |
| 85 | return files_.find(file.value()) != files_.end(); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 86 | } |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 87 | |
erikkay@google.com | dfb51b2 | 2008-08-16 02:32:10 +0900 | [diff] [blame] | 88 | int size() { |
erikkay@google.com | c8ec9e9 | 2008-08-16 02:50:10 +0900 | [diff] [blame] | 89 | return static_cast<int>(files_.size()); |
erikkay@google.com | dfb51b2 | 2008-08-16 02:32:10 +0900 | [diff] [blame] | 90 | } |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 91 | |
| 92 | private: |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 93 | std::set<FilePath::StringType> files_; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 94 | }; |
| 95 | |
| 96 | // Simple function to dump some text into a new file. |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 97 | void CreateTextFile(const FilePath& filename, |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 98 | const std::wstring& contents) { |
| 99 | std::ofstream file; |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 100 | file.open(WideToUTF8(filename.ToWStringHack()).c_str()); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 101 | ASSERT_TRUE(file.is_open()); |
| 102 | file << contents; |
| 103 | file.close(); |
| 104 | } |
| 105 | |
| 106 | // Simple function to take out some text from a file. |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 107 | std::wstring ReadTextFile(const FilePath& filename) { |
erikkay@google.com | dfb51b2 | 2008-08-16 02:32:10 +0900 | [diff] [blame] | 108 | wchar_t contents[64]; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 109 | std::wifstream file; |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 110 | file.open(WideToUTF8(filename.ToWStringHack()).c_str()); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 111 | EXPECT_TRUE(file.is_open()); |
| 112 | file.getline(contents, 64); |
| 113 | file.close(); |
| 114 | return std::wstring(contents); |
| 115 | } |
| 116 | |
erikkay@google.com | 014161d | 2008-08-16 02:45:13 +0900 | [diff] [blame] | 117 | #if defined(OS_WIN) |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 118 | uint64 FileTimeAsUint64(const FILETIME& ft) { |
| 119 | ULARGE_INTEGER u; |
| 120 | u.LowPart = ft.dwLowDateTime; |
| 121 | u.HighPart = ft.dwHighDateTime; |
| 122 | return u.QuadPart; |
| 123 | } |
erikkay@google.com | dfb51b2 | 2008-08-16 02:32:10 +0900 | [diff] [blame] | 124 | #endif |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 125 | |
| 126 | const struct append_case { |
| 127 | const wchar_t* path; |
| 128 | const wchar_t* ending; |
| 129 | const wchar_t* result; |
| 130 | } append_cases[] = { |
erikkay@google.com | 014161d | 2008-08-16 02:45:13 +0900 | [diff] [blame] | 131 | #if defined(OS_WIN) |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 132 | {L"c:\\colon\\backslash", L"path", L"c:\\colon\\backslash\\path"}, |
| 133 | {L"c:\\colon\\backslash\\", L"path", L"c:\\colon\\backslash\\path"}, |
| 134 | {L"c:\\colon\\backslash\\\\", L"path", L"c:\\colon\\backslash\\\\path"}, |
| 135 | {L"c:\\colon\\backslash\\", L"", L"c:\\colon\\backslash\\"}, |
| 136 | {L"c:\\colon\\backslash", L"", L"c:\\colon\\backslash\\"}, |
| 137 | {L"", L"path", L"\\path"}, |
| 138 | {L"", L"", L"\\"}, |
erikkay@google.com | dfb51b2 | 2008-08-16 02:32:10 +0900 | [diff] [blame] | 139 | #elif defined(OS_POSIX) |
| 140 | {L"/foo/bar", L"path", L"/foo/bar/path"}, |
| 141 | {L"/foo/bar/", L"path", L"/foo/bar/path"}, |
| 142 | {L"/foo/bar//", L"path", L"/foo/bar//path"}, |
| 143 | {L"/foo/bar/", L"", L"/foo/bar/"}, |
| 144 | {L"/foo/bar", L"", L"/foo/bar/"}, |
| 145 | {L"", L"path", L"/path"}, |
| 146 | {L"", L"", L"/"}, |
| 147 | #endif |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 148 | }; |
| 149 | |
evan@chromium.org | 1db7f94 | 2010-02-27 00:11:55 +0900 | [diff] [blame] | 150 | #if defined(OS_WIN) |
| 151 | // This function is deprecated, but still used on Windows. |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 152 | TEST_F(FileUtilTest, AppendToPath) { |
erikkay@google.com | dfb51b2 | 2008-08-16 02:32:10 +0900 | [diff] [blame] | 153 | for (unsigned int i = 0; i < arraysize(append_cases); ++i) { |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 154 | const append_case& value = append_cases[i]; |
| 155 | std::wstring result = value.path; |
| 156 | file_util::AppendToPath(&result, value.ending); |
| 157 | EXPECT_EQ(value.result, result); |
| 158 | } |
| 159 | |
| 160 | #ifdef NDEBUG |
| 161 | file_util::AppendToPath(NULL, L"path"); // asserts in debug mode |
| 162 | #endif |
| 163 | } |
evan@chromium.org | 1db7f94 | 2010-02-27 00:11:55 +0900 | [diff] [blame] | 164 | #endif // defined(OS_WIN) |
| 165 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 166 | |
| 167 | static const struct InsertBeforeExtensionCase { |
phajdan.jr@chromium.org | f9908a7 | 2009-04-04 02:17:58 +0900 | [diff] [blame] | 168 | const FilePath::CharType* path; |
| 169 | const FilePath::CharType* suffix; |
| 170 | const FilePath::CharType* result; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 171 | } kInsertBeforeExtension[] = { |
phajdan.jr@chromium.org | f9908a7 | 2009-04-04 02:17:58 +0900 | [diff] [blame] | 172 | {FPL(""), FPL(""), FPL("")}, |
| 173 | {FPL(""), FPL("txt"), FPL("txt")}, |
| 174 | {FPL("."), FPL("txt"), FPL("txt.")}, |
| 175 | {FPL("."), FPL(""), FPL(".")}, |
| 176 | {FPL("foo.dll"), FPL("txt"), FPL("footxt.dll")}, |
| 177 | {FPL("foo.dll"), FPL(".txt"), FPL("foo.txt.dll")}, |
| 178 | {FPL("foo"), FPL("txt"), FPL("footxt")}, |
| 179 | {FPL("foo"), FPL(".txt"), FPL("foo.txt")}, |
| 180 | {FPL("foo.baz.dll"), FPL("txt"), FPL("foo.baztxt.dll")}, |
| 181 | {FPL("foo.baz.dll"), FPL(".txt"), FPL("foo.baz.txt.dll")}, |
| 182 | {FPL("foo.dll"), FPL(""), FPL("foo.dll")}, |
| 183 | {FPL("foo.dll"), FPL("."), FPL("foo..dll")}, |
| 184 | {FPL("foo"), FPL(""), FPL("foo")}, |
| 185 | {FPL("foo"), FPL("."), FPL("foo.")}, |
| 186 | {FPL("foo.baz.dll"), FPL(""), FPL("foo.baz.dll")}, |
| 187 | {FPL("foo.baz.dll"), FPL("."), FPL("foo.baz..dll")}, |
erikkay@google.com | 014161d | 2008-08-16 02:45:13 +0900 | [diff] [blame] | 188 | #if defined(OS_WIN) |
phajdan.jr@chromium.org | f9908a7 | 2009-04-04 02:17:58 +0900 | [diff] [blame] | 189 | {FPL("\\"), FPL(""), FPL("\\")}, |
| 190 | {FPL("\\"), FPL("txt"), FPL("\\txt")}, |
| 191 | {FPL("\\."), FPL("txt"), FPL("\\txt.")}, |
| 192 | {FPL("\\."), FPL(""), FPL("\\.")}, |
| 193 | {FPL("C:\\bar\\foo.dll"), FPL("txt"), FPL("C:\\bar\\footxt.dll")}, |
| 194 | {FPL("C:\\bar.baz\\foodll"), FPL("txt"), FPL("C:\\bar.baz\\foodlltxt")}, |
| 195 | {FPL("C:\\bar.baz\\foo.dll"), FPL("txt"), FPL("C:\\bar.baz\\footxt.dll")}, |
| 196 | {FPL("C:\\bar.baz\\foo.dll.exe"), FPL("txt"), |
| 197 | FPL("C:\\bar.baz\\foo.dlltxt.exe")}, |
| 198 | {FPL("C:\\bar.baz\\foo"), FPL(""), FPL("C:\\bar.baz\\foo")}, |
| 199 | {FPL("C:\\bar.baz\\foo.exe"), FPL(""), FPL("C:\\bar.baz\\foo.exe")}, |
| 200 | {FPL("C:\\bar.baz\\foo.dll.exe"), FPL(""), FPL("C:\\bar.baz\\foo.dll.exe")}, |
| 201 | {FPL("C:\\bar\\baz\\foo.exe"), FPL(" (1)"), FPL("C:\\bar\\baz\\foo (1).exe")}, |
erikkay@google.com | dfb51b2 | 2008-08-16 02:32:10 +0900 | [diff] [blame] | 202 | #elif defined(OS_POSIX) |
phajdan.jr@chromium.org | f9908a7 | 2009-04-04 02:17:58 +0900 | [diff] [blame] | 203 | {FPL("/"), FPL(""), FPL("/")}, |
| 204 | {FPL("/"), FPL("txt"), FPL("/txt")}, |
| 205 | {FPL("/."), FPL("txt"), FPL("/txt.")}, |
| 206 | {FPL("/."), FPL(""), FPL("/.")}, |
| 207 | {FPL("/bar/foo.dll"), FPL("txt"), FPL("/bar/footxt.dll")}, |
| 208 | {FPL("/bar.baz/foodll"), FPL("txt"), FPL("/bar.baz/foodlltxt")}, |
| 209 | {FPL("/bar.baz/foo.dll"), FPL("txt"), FPL("/bar.baz/footxt.dll")}, |
| 210 | {FPL("/bar.baz/foo.dll.exe"), FPL("txt"), FPL("/bar.baz/foo.dlltxt.exe")}, |
| 211 | {FPL("/bar.baz/foo"), FPL(""), FPL("/bar.baz/foo")}, |
| 212 | {FPL("/bar.baz/foo.exe"), FPL(""), FPL("/bar.baz/foo.exe")}, |
| 213 | {FPL("/bar.baz/foo.dll.exe"), FPL(""), FPL("/bar.baz/foo.dll.exe")}, |
| 214 | {FPL("/bar/baz/foo.exe"), FPL(" (1)"), FPL("/bar/baz/foo (1).exe")}, |
erikkay@google.com | dfb51b2 | 2008-08-16 02:32:10 +0900 | [diff] [blame] | 215 | #endif |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 216 | }; |
| 217 | |
| 218 | TEST_F(FileUtilTest, InsertBeforeExtensionTest) { |
erikkay@google.com | dfb51b2 | 2008-08-16 02:32:10 +0900 | [diff] [blame] | 219 | for (unsigned int i = 0; i < arraysize(kInsertBeforeExtension); ++i) { |
phajdan.jr@chromium.org | f9908a7 | 2009-04-04 02:17:58 +0900 | [diff] [blame] | 220 | FilePath path(kInsertBeforeExtension[i].path); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 221 | file_util::InsertBeforeExtension(&path, kInsertBeforeExtension[i].suffix); |
phajdan.jr@chromium.org | f9908a7 | 2009-04-04 02:17:58 +0900 | [diff] [blame] | 222 | EXPECT_EQ(kInsertBeforeExtension[i].result, path.value()); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 223 | } |
| 224 | } |
| 225 | |
| 226 | static const struct filename_case { |
| 227 | const wchar_t* path; |
| 228 | const wchar_t* filename; |
| 229 | } filename_cases[] = { |
erikkay@google.com | 014161d | 2008-08-16 02:45:13 +0900 | [diff] [blame] | 230 | #if defined(OS_WIN) |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 231 | {L"c:\\colon\\backslash", L"backslash"}, |
| 232 | {L"c:\\colon\\backslash\\", L""}, |
| 233 | {L"\\\\filename.exe", L"filename.exe"}, |
| 234 | {L"filename.exe", L"filename.exe"}, |
| 235 | {L"", L""}, |
| 236 | {L"\\\\\\", L""}, |
| 237 | {L"c:/colon/backslash", L"backslash"}, |
| 238 | {L"c:/colon/backslash/", L""}, |
| 239 | {L"//////", L""}, |
| 240 | {L"///filename.exe", L"filename.exe"}, |
erikkay@google.com | dfb51b2 | 2008-08-16 02:32:10 +0900 | [diff] [blame] | 241 | #elif defined(OS_POSIX) |
| 242 | {L"/foo/bar", L"bar"}, |
| 243 | {L"/foo/bar/", L""}, |
| 244 | {L"/filename.exe", L"filename.exe"}, |
| 245 | {L"filename.exe", L"filename.exe"}, |
| 246 | {L"", L""}, |
| 247 | {L"/", L""}, |
| 248 | #endif |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 249 | }; |
| 250 | |
| 251 | TEST_F(FileUtilTest, GetFilenameFromPath) { |
erikkay@google.com | dfb51b2 | 2008-08-16 02:32:10 +0900 | [diff] [blame] | 252 | for (unsigned int i = 0; i < arraysize(filename_cases); ++i) { |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 253 | const filename_case& value = filename_cases[i]; |
| 254 | std::wstring result = file_util::GetFilenameFromPath(value.path); |
| 255 | EXPECT_EQ(value.filename, result); |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | // Test finding the file type from a path name |
| 260 | static const struct extension_case { |
| 261 | const wchar_t* path; |
| 262 | const wchar_t* extension; |
| 263 | } extension_cases[] = { |
erikkay@google.com | 014161d | 2008-08-16 02:45:13 +0900 | [diff] [blame] | 264 | #if defined(OS_WIN) |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 265 | {L"C:\\colon\\backslash\\filename.extension", L"extension"}, |
| 266 | {L"C:\\colon\\backslash\\filename.", L""}, |
| 267 | {L"C:\\colon\\backslash\\filename", L""}, |
| 268 | {L"C:\\colon\\backslash\\", L""}, |
| 269 | {L"C:\\colon\\backslash.\\", L""}, |
| 270 | {L"C:\\colon\\backslash\filename.extension.extension2", L"extension2"}, |
erikkay@google.com | dfb51b2 | 2008-08-16 02:32:10 +0900 | [diff] [blame] | 271 | #elif defined(OS_POSIX) |
| 272 | {L"/foo/bar/filename.extension", L"extension"}, |
| 273 | {L"/foo/bar/filename.", L""}, |
| 274 | {L"/foo/bar/filename", L""}, |
| 275 | {L"/foo/bar/", L""}, |
| 276 | {L"/foo/bar./", L""}, |
| 277 | {L"/foo/bar/filename.extension.extension2", L"extension2"}, |
| 278 | {L".", L""}, |
| 279 | {L"..", L""}, |
| 280 | {L"./foo", L""}, |
| 281 | {L"./foo.extension", L"extension"}, |
| 282 | {L"/foo.extension1/bar.extension2", L"extension2"}, |
| 283 | #endif |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 284 | }; |
| 285 | |
| 286 | TEST_F(FileUtilTest, GetFileExtensionFromPath) { |
erikkay@google.com | dfb51b2 | 2008-08-16 02:32:10 +0900 | [diff] [blame] | 287 | for (unsigned int i = 0; i < arraysize(extension_cases); ++i) { |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 288 | const extension_case& ext = extension_cases[i]; |
| 289 | const std::wstring fext = file_util::GetFileExtensionFromPath(ext.path); |
| 290 | EXPECT_EQ(ext.extension, fext); |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | // Test finding the directory component of a path |
| 295 | static const struct dir_case { |
| 296 | const wchar_t* full_path; |
| 297 | const wchar_t* directory; |
| 298 | } dir_cases[] = { |
erikkay@google.com | 014161d | 2008-08-16 02:45:13 +0900 | [diff] [blame] | 299 | #if defined(OS_WIN) |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 300 | {L"C:\\WINDOWS\\system32\\gdi32.dll", L"C:\\WINDOWS\\system32"}, |
| 301 | {L"C:\\WINDOWS\\system32\\not_exist_thx_1138", L"C:\\WINDOWS\\system32"}, |
| 302 | {L"C:\\WINDOWS\\system32\\", L"C:\\WINDOWS\\system32"}, |
| 303 | {L"C:\\WINDOWS\\system32\\\\", L"C:\\WINDOWS\\system32"}, |
| 304 | {L"C:\\WINDOWS\\system32", L"C:\\WINDOWS"}, |
| 305 | {L"C:\\WINDOWS\\system32.\\", L"C:\\WINDOWS\\system32."}, |
tkent@chromium.org | fce07c7 | 2009-10-15 14:00:25 +0900 | [diff] [blame] | 306 | {L"C:\\", L"C:\\"}, |
erikkay@google.com | dfb51b2 | 2008-08-16 02:32:10 +0900 | [diff] [blame] | 307 | #elif defined(OS_POSIX) |
| 308 | {L"/foo/bar/gdi32.dll", L"/foo/bar"}, |
| 309 | {L"/foo/bar/not_exist_thx_1138", L"/foo/bar"}, |
| 310 | {L"/foo/bar/", L"/foo/bar"}, |
| 311 | {L"/foo/bar//", L"/foo/bar"}, |
| 312 | {L"/foo/bar", L"/foo"}, |
| 313 | {L"/foo/bar./", L"/foo/bar."}, |
| 314 | {L"/", L"/"}, |
| 315 | {L".", L"."}, |
evan@chromium.org | 1543ad3 | 2009-08-27 05:00:14 +0900 | [diff] [blame] | 316 | {L"..", L"."}, // yes, ".." technically lives in "." |
erikkay@google.com | dfb51b2 | 2008-08-16 02:32:10 +0900 | [diff] [blame] | 317 | #endif |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 318 | }; |
| 319 | |
evan@chromium.org | 9c7cbbe | 2010-02-23 21:52:11 +0900 | [diff] [blame] | 320 | #if defined(OS_WIN) |
| 321 | // This function is deprecated, and only exists on Windows anymore. |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 322 | TEST_F(FileUtilTest, GetDirectoryFromPath) { |
erikkay@google.com | dfb51b2 | 2008-08-16 02:32:10 +0900 | [diff] [blame] | 323 | for (unsigned int i = 0; i < arraysize(dir_cases); ++i) { |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 324 | const dir_case& dir = dir_cases[i]; |
| 325 | const std::wstring parent = |
| 326 | file_util::GetDirectoryFromPath(dir.full_path); |
| 327 | EXPECT_EQ(dir.directory, parent); |
| 328 | } |
| 329 | } |
evan@chromium.org | 9c7cbbe | 2010-02-23 21:52:11 +0900 | [diff] [blame] | 330 | #endif |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 331 | |
| 332 | TEST_F(FileUtilTest, CountFilesCreatedAfter) { |
| 333 | // Create old file (that we don't want to count) |
erikkay@google.com | 9ac2676 | 2009-04-18 09:42:48 +0900 | [diff] [blame] | 334 | FilePath old_file_name = test_dir_.Append(FILE_PATH_LITERAL("Old File.txt")); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 335 | CreateTextFile(old_file_name, L"Just call me Mr. Creakybits"); |
| 336 | |
| 337 | // Age to perfection |
evan@chromium.org | 3730132 | 2009-04-21 10:50:39 +0900 | [diff] [blame] | 338 | #if defined(OS_WIN) |
erikkay@google.com | 8d133f6 | 2009-04-24 00:05:19 +0900 | [diff] [blame] | 339 | PlatformThread::Sleep(100); |
evan@chromium.org | 3730132 | 2009-04-21 10:50:39 +0900 | [diff] [blame] | 340 | #elif defined(OS_POSIX) |
| 341 | // We need to wait at least one second here because the precision of |
| 342 | // file creation time is one second. |
erikkay@google.com | 8d133f6 | 2009-04-24 00:05:19 +0900 | [diff] [blame] | 343 | PlatformThread::Sleep(1500); |
evan@chromium.org | 3730132 | 2009-04-21 10:50:39 +0900 | [diff] [blame] | 344 | #endif |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 345 | |
| 346 | // Establish our cutoff time |
erikkay@google.com | 9ac2676 | 2009-04-18 09:42:48 +0900 | [diff] [blame] | 347 | base::Time now(base::Time::NowFromSystemTime()); |
| 348 | EXPECT_EQ(0, file_util::CountFilesCreatedAfter(test_dir_, now)); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 349 | |
| 350 | // Create a new file (that we do want to count) |
erikkay@google.com | 9ac2676 | 2009-04-18 09:42:48 +0900 | [diff] [blame] | 351 | FilePath new_file_name = test_dir_.Append(FILE_PATH_LITERAL("New File.txt")); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 352 | CreateTextFile(new_file_name, L"Waaaaaaaaaaaaaah."); |
| 353 | |
| 354 | // We should see only the new file. |
erikkay@google.com | 9ac2676 | 2009-04-18 09:42:48 +0900 | [diff] [blame] | 355 | EXPECT_EQ(1, file_util::CountFilesCreatedAfter(test_dir_, now)); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 356 | |
| 357 | // Delete new file, we should see no files after cutoff now |
| 358 | EXPECT_TRUE(file_util::Delete(new_file_name, false)); |
erikkay@google.com | 9ac2676 | 2009-04-18 09:42:48 +0900 | [diff] [blame] | 359 | EXPECT_EQ(0, file_util::CountFilesCreatedAfter(test_dir_, now)); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 360 | } |
| 361 | |
cpu@chromium.org | 83f07be | 2010-03-25 06:56:26 +0900 | [diff] [blame] | 362 | TEST_F(FileUtilTest, FileAndDirectorySize) { |
| 363 | // Create three files of 20, 30 and 3 chars (utf8). ComputeDirectorySize |
| 364 | // should return 53 bytes. |
| 365 | FilePath file_01 = test_dir_.Append(FPL("The file 01.txt")); |
| 366 | CreateTextFile(file_01, L"12345678901234567890"); |
| 367 | int64 size_f1 = 0; |
| 368 | ASSERT_TRUE(file_util::GetFileSize(file_01, &size_f1)); |
| 369 | EXPECT_EQ(20ll, size_f1); |
| 370 | |
| 371 | FilePath subdir_path = test_dir_.Append(FPL("Level2")); |
| 372 | file_util::CreateDirectory(subdir_path); |
| 373 | |
| 374 | FilePath file_02 = subdir_path.Append(FPL("The file 02.txt")); |
| 375 | CreateTextFile(file_02, L"123456789012345678901234567890"); |
| 376 | int64 size_f2 = 0; |
| 377 | ASSERT_TRUE(file_util::GetFileSize(file_02, &size_f2)); |
| 378 | EXPECT_EQ(30ll, size_f2); |
| 379 | |
| 380 | FilePath subsubdir_path = subdir_path.Append(FPL("Level3")); |
| 381 | file_util::CreateDirectory(subsubdir_path); |
| 382 | |
| 383 | FilePath file_03 = subsubdir_path.Append(FPL("The file 03.txt")); |
| 384 | CreateTextFile(file_03, L"123"); |
| 385 | |
| 386 | int64 computed_size = file_util::ComputeDirectorySize(test_dir_); |
| 387 | EXPECT_EQ(size_f1 + size_f2 + 3, computed_size); |
| 388 | } |
| 389 | |
skerner@chromium.org | 559baa9 | 2010-05-13 00:13:57 +0900 | [diff] [blame] | 390 | #if defined(OS_POSIX) |
| 391 | TEST_F(FileUtilTest, RealPath) { |
| 392 | // Get the real test directory, in case some future change to the |
| 393 | // test setup makes the path to test_dir_ include a symlink. |
| 394 | FilePath real_test_dir; |
| 395 | ASSERT_TRUE(file_util::RealPath(test_dir_, &real_test_dir)); |
| 396 | |
| 397 | FilePath real_path; |
| 398 | ASSERT_TRUE(file_util::RealPath(real_test_dir, &real_path)); |
| 399 | ASSERT_TRUE(real_test_dir == real_path); |
| 400 | |
| 401 | // Link one file to another. |
| 402 | FilePath link_from = real_test_dir.Append(FPL("from_file")); |
| 403 | FilePath link_to = real_test_dir.Append(FPL("to_file")); |
| 404 | CreateTextFile(link_to, bogus_content); |
| 405 | |
| 406 | ASSERT_EQ(0, symlink(link_to.value().c_str(), link_from.value().c_str())) |
| 407 | << "Failed to create file symlink."; |
| 408 | |
| 409 | // Check that RealPath sees the link. |
| 410 | ASSERT_TRUE(file_util::RealPath(link_from, &real_path)); |
| 411 | ASSERT_TRUE(link_to != link_from); |
| 412 | ASSERT_TRUE(link_to == real_path); |
| 413 | |
| 414 | |
| 415 | // Link to a directory. |
| 416 | link_from = real_test_dir.Append(FPL("from_dir")); |
| 417 | link_to = real_test_dir.Append(FPL("to_dir")); |
| 418 | file_util::CreateDirectory(link_to); |
| 419 | |
| 420 | ASSERT_EQ(0, symlink(link_to.value().c_str(), link_from.value().c_str())) |
| 421 | << "Failed to create directory symlink."; |
| 422 | |
| 423 | ASSERT_TRUE(file_util::RealPath(link_from, &real_path)); |
| 424 | ASSERT_TRUE(link_to != link_from); |
| 425 | ASSERT_TRUE(link_to == real_path); |
| 426 | |
| 427 | |
| 428 | // Test that a loop in the links causes RealPath() to return false. |
| 429 | link_from = real_test_dir.Append(FPL("link_a")); |
| 430 | link_to = real_test_dir.Append(FPL("link_b")); |
| 431 | ASSERT_EQ(0, symlink(link_to.value().c_str(), link_from.value().c_str())) |
| 432 | << "Failed to create loop symlink a."; |
| 433 | ASSERT_EQ(0, symlink(link_from.value().c_str(), link_to.value().c_str())) |
| 434 | << "Failed to create loop symlink b."; |
| 435 | |
| 436 | // Infinite loop! |
| 437 | ASSERT_FALSE(file_util::RealPath(link_from, &real_path)); |
| 438 | } |
| 439 | #endif // defined(OS_POSIX) |
| 440 | |
thestig@chromium.org | 1dad8c6 | 2010-05-08 03:58:45 +0900 | [diff] [blame] | 441 | TEST_F(FileUtilTest, DeleteNonExistent) { |
| 442 | FilePath non_existent = test_dir_.AppendASCII("bogus_file_dne.foobar"); |
| 443 | ASSERT_FALSE(file_util::PathExists(non_existent)); |
zork@chromium.org | 61be4f4 | 2010-05-07 09:05:36 +0900 | [diff] [blame] | 444 | |
thestig@chromium.org | 1dad8c6 | 2010-05-08 03:58:45 +0900 | [diff] [blame] | 445 | EXPECT_TRUE(file_util::Delete(non_existent, false)); |
| 446 | ASSERT_FALSE(file_util::PathExists(non_existent)); |
| 447 | EXPECT_TRUE(file_util::Delete(non_existent, true)); |
| 448 | ASSERT_FALSE(file_util::PathExists(non_existent)); |
| 449 | } |
| 450 | |
| 451 | TEST_F(FileUtilTest, DeleteFile) { |
| 452 | // Create a file |
| 453 | FilePath file_name = test_dir_.Append(FPL("Test DeleteFile 1.txt")); |
| 454 | CreateTextFile(file_name, bogus_content); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 455 | ASSERT_TRUE(file_util::PathExists(file_name)); |
| 456 | |
thestig@chromium.org | 1dad8c6 | 2010-05-08 03:58:45 +0900 | [diff] [blame] | 457 | // Make sure it's deleted |
| 458 | EXPECT_TRUE(file_util::Delete(file_name, false)); |
| 459 | EXPECT_FALSE(file_util::PathExists(file_name)); |
zork@chromium.org | 61be4f4 | 2010-05-07 09:05:36 +0900 | [diff] [blame] | 460 | |
thestig@chromium.org | 1dad8c6 | 2010-05-08 03:58:45 +0900 | [diff] [blame] | 461 | // Test recursive case, create a new file |
| 462 | file_name = test_dir_.Append(FPL("Test DeleteFile 2.txt")); |
| 463 | CreateTextFile(file_name, bogus_content); |
| 464 | ASSERT_TRUE(file_util::PathExists(file_name)); |
| 465 | |
| 466 | // Make sure it's deleted |
| 467 | EXPECT_TRUE(file_util::Delete(file_name, true)); |
| 468 | EXPECT_FALSE(file_util::PathExists(file_name)); |
| 469 | } |
| 470 | |
| 471 | #if defined(OS_WIN) |
| 472 | // Tests that the Delete function works for wild cards, especially |
| 473 | // with the recursion flag. Also coincidentally tests PathExists. |
| 474 | // TODO(erikkay): see if anyone's actually using this feature of the API |
| 475 | TEST_F(FileUtilTest, DeleteWildCard) { |
| 476 | // Create a file and a directory |
| 477 | FilePath file_name = test_dir_.Append(FPL("Test DeleteWildCard.txt")); |
| 478 | CreateTextFile(file_name, bogus_content); |
| 479 | ASSERT_TRUE(file_util::PathExists(file_name)); |
| 480 | |
| 481 | FilePath subdir_path = test_dir_.Append(FPL("DeleteWildCardDir")); |
| 482 | file_util::CreateDirectory(subdir_path); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 483 | ASSERT_TRUE(file_util::PathExists(subdir_path)); |
| 484 | |
thestig@chromium.org | 1dad8c6 | 2010-05-08 03:58:45 +0900 | [diff] [blame] | 485 | // Create the wildcard path |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 486 | FilePath directory_contents = test_dir_; |
thestig@chromium.org | 1dad8c6 | 2010-05-08 03:58:45 +0900 | [diff] [blame] | 487 | directory_contents = directory_contents.Append(FPL("*")); |
| 488 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 489 | // Delete non-recursively and check that only the file is deleted |
thestig@chromium.org | 1dad8c6 | 2010-05-08 03:58:45 +0900 | [diff] [blame] | 490 | EXPECT_TRUE(file_util::Delete(directory_contents, false)); |
erikkay@google.com | dfb51b2 | 2008-08-16 02:32:10 +0900 | [diff] [blame] | 491 | EXPECT_FALSE(file_util::PathExists(file_name)); |
| 492 | EXPECT_TRUE(file_util::PathExists(subdir_path)); |
thestig@chromium.org | afd8dd4 | 2010-05-07 06:56:40 +0900 | [diff] [blame] | 493 | |
zork@chromium.org | 61be4f4 | 2010-05-07 09:05:36 +0900 | [diff] [blame] | 494 | // Delete recursively and make sure all contents are deleted |
thestig@chromium.org | 1dad8c6 | 2010-05-08 03:58:45 +0900 | [diff] [blame] | 495 | EXPECT_TRUE(file_util::Delete(directory_contents, true)); |
thestig@chromium.org | afd8dd4 | 2010-05-07 06:56:40 +0900 | [diff] [blame] | 496 | EXPECT_FALSE(file_util::PathExists(file_name)); |
zork@chromium.org | 61be4f4 | 2010-05-07 09:05:36 +0900 | [diff] [blame] | 497 | EXPECT_FALSE(file_util::PathExists(subdir_path)); |
thestig@chromium.org | afd8dd4 | 2010-05-07 06:56:40 +0900 | [diff] [blame] | 498 | } |
| 499 | |
thestig@chromium.org | 1dad8c6 | 2010-05-08 03:58:45 +0900 | [diff] [blame] | 500 | // TODO(erikkay): see if anyone's actually using this feature of the API |
| 501 | TEST_F(FileUtilTest, DeleteNonExistantWildCard) { |
| 502 | // Create a file and a directory |
| 503 | FilePath subdir_path = test_dir_.Append(FPL("DeleteNonExistantWildCard")); |
| 504 | file_util::CreateDirectory(subdir_path); |
| 505 | ASSERT_TRUE(file_util::PathExists(subdir_path)); |
| 506 | |
| 507 | // Create the wildcard path |
| 508 | FilePath directory_contents = subdir_path; |
| 509 | directory_contents = directory_contents.Append(FPL("*")); |
| 510 | |
| 511 | // Delete non-recursively and check nothing got deleted |
| 512 | EXPECT_TRUE(file_util::Delete(directory_contents, false)); |
| 513 | EXPECT_TRUE(file_util::PathExists(subdir_path)); |
| 514 | |
| 515 | // Delete recursively and check nothing got deleted |
| 516 | EXPECT_TRUE(file_util::Delete(directory_contents, true)); |
| 517 | EXPECT_TRUE(file_util::PathExists(subdir_path)); |
| 518 | } |
| 519 | #endif |
| 520 | |
| 521 | // Tests non-recursive Delete() for a directory. |
| 522 | TEST_F(FileUtilTest, DeleteDirNonRecursive) { |
| 523 | // Create a subdirectory and put a file and two directories inside. |
| 524 | FilePath test_subdir = test_dir_.Append(FPL("DeleteDirNonRecursive")); |
| 525 | file_util::CreateDirectory(test_subdir); |
| 526 | ASSERT_TRUE(file_util::PathExists(test_subdir)); |
| 527 | |
| 528 | FilePath file_name = test_subdir.Append(FPL("Test DeleteDir.txt")); |
| 529 | CreateTextFile(file_name, bogus_content); |
| 530 | ASSERT_TRUE(file_util::PathExists(file_name)); |
| 531 | |
| 532 | FilePath subdir_path1 = test_subdir.Append(FPL("TestSubDir1")); |
| 533 | file_util::CreateDirectory(subdir_path1); |
| 534 | ASSERT_TRUE(file_util::PathExists(subdir_path1)); |
| 535 | |
| 536 | FilePath subdir_path2 = test_subdir.Append(FPL("TestSubDir2")); |
| 537 | file_util::CreateDirectory(subdir_path2); |
| 538 | ASSERT_TRUE(file_util::PathExists(subdir_path2)); |
| 539 | |
| 540 | // Delete non-recursively and check that the empty dir got deleted |
| 541 | EXPECT_TRUE(file_util::Delete(subdir_path2, false)); |
| 542 | EXPECT_FALSE(file_util::PathExists(subdir_path2)); |
| 543 | |
| 544 | // Delete non-recursively and check that nothing got deleted |
| 545 | EXPECT_FALSE(file_util::Delete(test_subdir, false)); |
| 546 | EXPECT_TRUE(file_util::PathExists(test_subdir)); |
| 547 | EXPECT_TRUE(file_util::PathExists(file_name)); |
| 548 | EXPECT_TRUE(file_util::PathExists(subdir_path1)); |
| 549 | } |
| 550 | |
| 551 | // Tests recursive Delete() for a directory. |
| 552 | TEST_F(FileUtilTest, DeleteDirRecursive) { |
| 553 | // Create a subdirectory and put a file and two directories inside. |
| 554 | FilePath test_subdir = test_dir_.Append(FPL("DeleteDirRecursive")); |
| 555 | file_util::CreateDirectory(test_subdir); |
| 556 | ASSERT_TRUE(file_util::PathExists(test_subdir)); |
| 557 | |
| 558 | FilePath file_name = test_subdir.Append(FPL("Test DeleteDirRecursive.txt")); |
| 559 | CreateTextFile(file_name, bogus_content); |
| 560 | ASSERT_TRUE(file_util::PathExists(file_name)); |
| 561 | |
| 562 | FilePath subdir_path1 = test_subdir.Append(FPL("TestSubDir1")); |
| 563 | file_util::CreateDirectory(subdir_path1); |
| 564 | ASSERT_TRUE(file_util::PathExists(subdir_path1)); |
| 565 | |
| 566 | FilePath subdir_path2 = test_subdir.Append(FPL("TestSubDir2")); |
| 567 | file_util::CreateDirectory(subdir_path2); |
| 568 | ASSERT_TRUE(file_util::PathExists(subdir_path2)); |
| 569 | |
| 570 | // Delete recursively and check that the empty dir got deleted |
| 571 | EXPECT_TRUE(file_util::Delete(subdir_path2, true)); |
| 572 | EXPECT_FALSE(file_util::PathExists(subdir_path2)); |
| 573 | |
| 574 | // Delete recursively and check that everything got deleted |
| 575 | EXPECT_TRUE(file_util::Delete(test_subdir, true)); |
| 576 | EXPECT_FALSE(file_util::PathExists(file_name)); |
| 577 | EXPECT_FALSE(file_util::PathExists(subdir_path1)); |
| 578 | EXPECT_FALSE(file_util::PathExists(test_subdir)); |
| 579 | } |
| 580 | |
vandebo@chromium.org | c0cf77e | 2009-10-15 10:11:44 +0900 | [diff] [blame] | 581 | TEST_F(FileUtilTest, MoveFileNew) { |
| 582 | // Create a file |
| 583 | FilePath file_name_from = |
| 584 | test_dir_.Append(FILE_PATH_LITERAL("Move_Test_File.txt")); |
| 585 | CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle"); |
| 586 | ASSERT_TRUE(file_util::PathExists(file_name_from)); |
| 587 | |
| 588 | // The destination |
| 589 | FilePath file_name_to = |
| 590 | test_dir_.Append(FILE_PATH_LITERAL("Move_Test_File_Destination.txt")); |
| 591 | ASSERT_FALSE(file_util::PathExists(file_name_to)); |
| 592 | |
| 593 | EXPECT_TRUE(file_util::Move(file_name_from, file_name_to)); |
| 594 | |
| 595 | // Check everything has been moved. |
| 596 | EXPECT_FALSE(file_util::PathExists(file_name_from)); |
| 597 | EXPECT_TRUE(file_util::PathExists(file_name_to)); |
| 598 | } |
| 599 | |
| 600 | TEST_F(FileUtilTest, MoveFileExists) { |
| 601 | // Create a file |
| 602 | FilePath file_name_from = |
| 603 | test_dir_.Append(FILE_PATH_LITERAL("Move_Test_File.txt")); |
| 604 | CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle"); |
| 605 | ASSERT_TRUE(file_util::PathExists(file_name_from)); |
| 606 | |
| 607 | // The destination name |
| 608 | FilePath file_name_to = |
| 609 | test_dir_.Append(FILE_PATH_LITERAL("Move_Test_File_Destination.txt")); |
| 610 | CreateTextFile(file_name_to, L"Old file content"); |
| 611 | ASSERT_TRUE(file_util::PathExists(file_name_to)); |
| 612 | |
| 613 | EXPECT_TRUE(file_util::Move(file_name_from, file_name_to)); |
| 614 | |
| 615 | // Check everything has been moved. |
| 616 | EXPECT_FALSE(file_util::PathExists(file_name_from)); |
| 617 | EXPECT_TRUE(file_util::PathExists(file_name_to)); |
| 618 | EXPECT_TRUE(L"Gooooooooooooooooooooogle" == ReadTextFile(file_name_to)); |
| 619 | } |
| 620 | |
| 621 | TEST_F(FileUtilTest, MoveFileDirExists) { |
| 622 | // Create a file |
| 623 | FilePath file_name_from = |
| 624 | test_dir_.Append(FILE_PATH_LITERAL("Move_Test_File.txt")); |
| 625 | CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle"); |
| 626 | ASSERT_TRUE(file_util::PathExists(file_name_from)); |
| 627 | |
| 628 | // The destination directory |
| 629 | FilePath dir_name_to = |
| 630 | test_dir_.Append(FILE_PATH_LITERAL("Destination")); |
| 631 | file_util::CreateDirectory(dir_name_to); |
| 632 | ASSERT_TRUE(file_util::PathExists(dir_name_to)); |
| 633 | |
| 634 | EXPECT_FALSE(file_util::Move(file_name_from, dir_name_to)); |
| 635 | } |
| 636 | |
| 637 | |
vandebo@chromium.org | 70cf3f1 | 2009-10-14 02:57:27 +0900 | [diff] [blame] | 638 | TEST_F(FileUtilTest, MoveNew) { |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 639 | // Create a directory |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 640 | FilePath dir_name_from = |
| 641 | test_dir_.Append(FILE_PATH_LITERAL("Move_From_Subdir")); |
| 642 | file_util::CreateDirectory(dir_name_from); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 643 | ASSERT_TRUE(file_util::PathExists(dir_name_from)); |
| 644 | |
| 645 | // Create a file under the directory |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 646 | FilePath file_name_from = |
| 647 | dir_name_from.Append(FILE_PATH_LITERAL("Move_Test_File.txt")); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 648 | CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle"); |
| 649 | ASSERT_TRUE(file_util::PathExists(file_name_from)); |
| 650 | |
| 651 | // Move the directory |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 652 | FilePath dir_name_to = test_dir_.Append(FILE_PATH_LITERAL("Move_To_Subdir")); |
| 653 | FilePath file_name_to = |
| 654 | dir_name_to.Append(FILE_PATH_LITERAL("Move_Test_File.txt")); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 655 | |
| 656 | ASSERT_FALSE(file_util::PathExists(dir_name_to)); |
| 657 | |
| 658 | EXPECT_TRUE(file_util::Move(dir_name_from, dir_name_to)); |
| 659 | |
| 660 | // Check everything has been moved. |
| 661 | EXPECT_FALSE(file_util::PathExists(dir_name_from)); |
| 662 | EXPECT_FALSE(file_util::PathExists(file_name_from)); |
| 663 | EXPECT_TRUE(file_util::PathExists(dir_name_to)); |
| 664 | EXPECT_TRUE(file_util::PathExists(file_name_to)); |
| 665 | } |
| 666 | |
vandebo@chromium.org | 70cf3f1 | 2009-10-14 02:57:27 +0900 | [diff] [blame] | 667 | TEST_F(FileUtilTest, MoveExist) { |
| 668 | // Create a directory |
| 669 | FilePath dir_name_from = |
| 670 | test_dir_.Append(FILE_PATH_LITERAL("Move_From_Subdir")); |
| 671 | file_util::CreateDirectory(dir_name_from); |
| 672 | ASSERT_TRUE(file_util::PathExists(dir_name_from)); |
| 673 | |
| 674 | // Create a file under the directory |
| 675 | FilePath file_name_from = |
| 676 | dir_name_from.Append(FILE_PATH_LITERAL("Move_Test_File.txt")); |
| 677 | CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle"); |
| 678 | ASSERT_TRUE(file_util::PathExists(file_name_from)); |
| 679 | |
| 680 | // Move the directory |
| 681 | FilePath dir_name_exists = |
| 682 | test_dir_.Append(FILE_PATH_LITERAL("Destination")); |
| 683 | |
| 684 | FilePath dir_name_to = |
| 685 | dir_name_exists.Append(FILE_PATH_LITERAL("Move_To_Subdir")); |
| 686 | FilePath file_name_to = |
| 687 | dir_name_to.Append(FILE_PATH_LITERAL("Move_Test_File.txt")); |
| 688 | |
| 689 | // Create the destination directory. |
| 690 | file_util::CreateDirectory(dir_name_exists); |
| 691 | ASSERT_TRUE(file_util::PathExists(dir_name_exists)); |
| 692 | |
| 693 | EXPECT_TRUE(file_util::Move(dir_name_from, dir_name_to)); |
| 694 | |
| 695 | // Check everything has been moved. |
| 696 | EXPECT_FALSE(file_util::PathExists(dir_name_from)); |
| 697 | EXPECT_FALSE(file_util::PathExists(file_name_from)); |
| 698 | EXPECT_TRUE(file_util::PathExists(dir_name_to)); |
| 699 | EXPECT_TRUE(file_util::PathExists(file_name_to)); |
| 700 | } |
| 701 | |
| 702 | TEST_F(FileUtilTest, CopyDirectoryRecursivelyNew) { |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 703 | // Create a directory. |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 704 | FilePath dir_name_from = |
| 705 | test_dir_.Append(FILE_PATH_LITERAL("Copy_From_Subdir")); |
| 706 | file_util::CreateDirectory(dir_name_from); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 707 | ASSERT_TRUE(file_util::PathExists(dir_name_from)); |
| 708 | |
| 709 | // Create a file under the directory. |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 710 | FilePath file_name_from = |
| 711 | dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt")); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 712 | CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle"); |
| 713 | ASSERT_TRUE(file_util::PathExists(file_name_from)); |
| 714 | |
| 715 | // Create a subdirectory. |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 716 | FilePath subdir_name_from = |
| 717 | dir_name_from.Append(FILE_PATH_LITERAL("Subdir")); |
| 718 | file_util::CreateDirectory(subdir_name_from); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 719 | ASSERT_TRUE(file_util::PathExists(subdir_name_from)); |
| 720 | |
| 721 | // Create a file under the subdirectory. |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 722 | FilePath file_name2_from = |
| 723 | subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt")); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 724 | CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle"); |
| 725 | ASSERT_TRUE(file_util::PathExists(file_name2_from)); |
| 726 | |
| 727 | // Copy the directory recursively. |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 728 | FilePath dir_name_to = |
| 729 | test_dir_.Append(FILE_PATH_LITERAL("Copy_To_Subdir")); |
| 730 | FilePath file_name_to = |
| 731 | dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt")); |
| 732 | FilePath subdir_name_to = |
| 733 | dir_name_to.Append(FILE_PATH_LITERAL("Subdir")); |
| 734 | FilePath file_name2_to = |
| 735 | subdir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt")); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 736 | |
| 737 | ASSERT_FALSE(file_util::PathExists(dir_name_to)); |
| 738 | |
| 739 | EXPECT_TRUE(file_util::CopyDirectory(dir_name_from, dir_name_to, true)); |
| 740 | |
| 741 | // Check everything has been copied. |
| 742 | EXPECT_TRUE(file_util::PathExists(dir_name_from)); |
| 743 | EXPECT_TRUE(file_util::PathExists(file_name_from)); |
| 744 | EXPECT_TRUE(file_util::PathExists(subdir_name_from)); |
| 745 | EXPECT_TRUE(file_util::PathExists(file_name2_from)); |
| 746 | EXPECT_TRUE(file_util::PathExists(dir_name_to)); |
| 747 | EXPECT_TRUE(file_util::PathExists(file_name_to)); |
| 748 | EXPECT_TRUE(file_util::PathExists(subdir_name_to)); |
| 749 | EXPECT_TRUE(file_util::PathExists(file_name2_to)); |
| 750 | } |
| 751 | |
vandebo@chromium.org | 70cf3f1 | 2009-10-14 02:57:27 +0900 | [diff] [blame] | 752 | TEST_F(FileUtilTest, CopyDirectoryRecursivelyExists) { |
| 753 | // Create a directory. |
| 754 | FilePath dir_name_from = |
| 755 | test_dir_.Append(FILE_PATH_LITERAL("Copy_From_Subdir")); |
| 756 | file_util::CreateDirectory(dir_name_from); |
| 757 | ASSERT_TRUE(file_util::PathExists(dir_name_from)); |
| 758 | |
| 759 | // Create a file under the directory. |
| 760 | FilePath file_name_from = |
| 761 | dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt")); |
| 762 | CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle"); |
| 763 | ASSERT_TRUE(file_util::PathExists(file_name_from)); |
| 764 | |
| 765 | // Create a subdirectory. |
| 766 | FilePath subdir_name_from = |
| 767 | dir_name_from.Append(FILE_PATH_LITERAL("Subdir")); |
| 768 | file_util::CreateDirectory(subdir_name_from); |
| 769 | ASSERT_TRUE(file_util::PathExists(subdir_name_from)); |
| 770 | |
| 771 | // Create a file under the subdirectory. |
| 772 | FilePath file_name2_from = |
| 773 | subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt")); |
| 774 | CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle"); |
| 775 | ASSERT_TRUE(file_util::PathExists(file_name2_from)); |
| 776 | |
| 777 | // Copy the directory recursively. |
| 778 | FilePath dir_name_exists = |
| 779 | test_dir_.Append(FILE_PATH_LITERAL("Destination")); |
| 780 | |
| 781 | FilePath dir_name_to = |
| 782 | dir_name_exists.Append(FILE_PATH_LITERAL("Copy_From_Subdir")); |
| 783 | FilePath file_name_to = |
| 784 | dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt")); |
| 785 | FilePath subdir_name_to = |
| 786 | dir_name_to.Append(FILE_PATH_LITERAL("Subdir")); |
| 787 | FilePath file_name2_to = |
| 788 | subdir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt")); |
| 789 | |
| 790 | // Create the destination directory. |
| 791 | file_util::CreateDirectory(dir_name_exists); |
| 792 | ASSERT_TRUE(file_util::PathExists(dir_name_exists)); |
| 793 | |
| 794 | EXPECT_TRUE(file_util::CopyDirectory(dir_name_from, dir_name_exists, true)); |
| 795 | |
| 796 | // Check everything has been copied. |
| 797 | EXPECT_TRUE(file_util::PathExists(dir_name_from)); |
| 798 | EXPECT_TRUE(file_util::PathExists(file_name_from)); |
| 799 | EXPECT_TRUE(file_util::PathExists(subdir_name_from)); |
| 800 | EXPECT_TRUE(file_util::PathExists(file_name2_from)); |
| 801 | EXPECT_TRUE(file_util::PathExists(dir_name_to)); |
| 802 | EXPECT_TRUE(file_util::PathExists(file_name_to)); |
| 803 | EXPECT_TRUE(file_util::PathExists(subdir_name_to)); |
| 804 | EXPECT_TRUE(file_util::PathExists(file_name2_to)); |
| 805 | } |
| 806 | |
| 807 | TEST_F(FileUtilTest, CopyDirectoryNew) { |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 808 | // Create a directory. |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 809 | FilePath dir_name_from = |
| 810 | test_dir_.Append(FILE_PATH_LITERAL("Copy_From_Subdir")); |
| 811 | file_util::CreateDirectory(dir_name_from); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 812 | ASSERT_TRUE(file_util::PathExists(dir_name_from)); |
| 813 | |
| 814 | // Create a file under the directory. |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 815 | FilePath file_name_from = |
| 816 | dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt")); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 817 | CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle"); |
| 818 | ASSERT_TRUE(file_util::PathExists(file_name_from)); |
| 819 | |
| 820 | // Create a subdirectory. |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 821 | FilePath subdir_name_from = |
| 822 | dir_name_from.Append(FILE_PATH_LITERAL("Subdir")); |
| 823 | file_util::CreateDirectory(subdir_name_from); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 824 | ASSERT_TRUE(file_util::PathExists(subdir_name_from)); |
| 825 | |
| 826 | // Create a file under the subdirectory. |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 827 | FilePath file_name2_from = |
| 828 | subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt")); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 829 | CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle"); |
| 830 | ASSERT_TRUE(file_util::PathExists(file_name2_from)); |
| 831 | |
| 832 | // Copy the directory not recursively. |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 833 | FilePath dir_name_to = |
| 834 | test_dir_.Append(FILE_PATH_LITERAL("Copy_To_Subdir")); |
| 835 | FilePath file_name_to = |
| 836 | dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt")); |
| 837 | FilePath subdir_name_to = |
| 838 | dir_name_to.Append(FILE_PATH_LITERAL("Subdir")); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 839 | |
| 840 | ASSERT_FALSE(file_util::PathExists(dir_name_to)); |
| 841 | |
| 842 | EXPECT_TRUE(file_util::CopyDirectory(dir_name_from, dir_name_to, false)); |
| 843 | |
| 844 | // Check everything has been copied. |
| 845 | EXPECT_TRUE(file_util::PathExists(dir_name_from)); |
| 846 | EXPECT_TRUE(file_util::PathExists(file_name_from)); |
| 847 | EXPECT_TRUE(file_util::PathExists(subdir_name_from)); |
| 848 | EXPECT_TRUE(file_util::PathExists(file_name2_from)); |
| 849 | EXPECT_TRUE(file_util::PathExists(dir_name_to)); |
| 850 | EXPECT_TRUE(file_util::PathExists(file_name_to)); |
| 851 | EXPECT_FALSE(file_util::PathExists(subdir_name_to)); |
| 852 | } |
| 853 | |
vandebo@chromium.org | 70cf3f1 | 2009-10-14 02:57:27 +0900 | [diff] [blame] | 854 | TEST_F(FileUtilTest, CopyDirectoryExists) { |
| 855 | // Create a directory. |
| 856 | FilePath dir_name_from = |
| 857 | test_dir_.Append(FILE_PATH_LITERAL("Copy_From_Subdir")); |
| 858 | file_util::CreateDirectory(dir_name_from); |
| 859 | ASSERT_TRUE(file_util::PathExists(dir_name_from)); |
| 860 | |
| 861 | // Create a file under the directory. |
| 862 | FilePath file_name_from = |
| 863 | dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt")); |
| 864 | CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle"); |
| 865 | ASSERT_TRUE(file_util::PathExists(file_name_from)); |
| 866 | |
| 867 | // Create a subdirectory. |
| 868 | FilePath subdir_name_from = |
| 869 | dir_name_from.Append(FILE_PATH_LITERAL("Subdir")); |
| 870 | file_util::CreateDirectory(subdir_name_from); |
| 871 | ASSERT_TRUE(file_util::PathExists(subdir_name_from)); |
| 872 | |
| 873 | // Create a file under the subdirectory. |
| 874 | FilePath file_name2_from = |
| 875 | subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt")); |
| 876 | CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle"); |
| 877 | ASSERT_TRUE(file_util::PathExists(file_name2_from)); |
| 878 | |
| 879 | // Copy the directory not recursively. |
| 880 | FilePath dir_name_to = |
| 881 | test_dir_.Append(FILE_PATH_LITERAL("Copy_To_Subdir")); |
| 882 | FilePath file_name_to = |
| 883 | dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt")); |
| 884 | FilePath subdir_name_to = |
| 885 | dir_name_to.Append(FILE_PATH_LITERAL("Subdir")); |
| 886 | |
| 887 | // Create the destination directory. |
| 888 | file_util::CreateDirectory(dir_name_to); |
| 889 | ASSERT_TRUE(file_util::PathExists(dir_name_to)); |
| 890 | |
| 891 | EXPECT_TRUE(file_util::CopyDirectory(dir_name_from, dir_name_to, false)); |
| 892 | |
| 893 | // Check everything has been copied. |
| 894 | EXPECT_TRUE(file_util::PathExists(dir_name_from)); |
| 895 | EXPECT_TRUE(file_util::PathExists(file_name_from)); |
| 896 | EXPECT_TRUE(file_util::PathExists(subdir_name_from)); |
| 897 | EXPECT_TRUE(file_util::PathExists(file_name2_from)); |
| 898 | EXPECT_TRUE(file_util::PathExists(dir_name_to)); |
| 899 | EXPECT_TRUE(file_util::PathExists(file_name_to)); |
| 900 | EXPECT_FALSE(file_util::PathExists(subdir_name_to)); |
| 901 | } |
| 902 | |
vandebo@chromium.org | c0cf77e | 2009-10-15 10:11:44 +0900 | [diff] [blame] | 903 | TEST_F(FileUtilTest, CopyFileWithCopyDirectoryRecursiveToNew) { |
| 904 | // Create a file |
| 905 | FilePath file_name_from = |
| 906 | test_dir_.Append(FILE_PATH_LITERAL("Copy_Test_File.txt")); |
| 907 | CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle"); |
| 908 | ASSERT_TRUE(file_util::PathExists(file_name_from)); |
| 909 | |
| 910 | // The destination name |
| 911 | FilePath file_name_to = |
| 912 | test_dir_.Append(FILE_PATH_LITERAL("Copy_Test_File_Destination.txt")); |
| 913 | ASSERT_FALSE(file_util::PathExists(file_name_to)); |
| 914 | |
| 915 | EXPECT_TRUE(file_util::CopyDirectory(file_name_from, file_name_to, true)); |
| 916 | |
| 917 | // Check the has been copied |
| 918 | EXPECT_TRUE(file_util::PathExists(file_name_to)); |
| 919 | } |
| 920 | |
| 921 | TEST_F(FileUtilTest, CopyFileWithCopyDirectoryRecursiveToExisting) { |
| 922 | // Create a file |
| 923 | FilePath file_name_from = |
| 924 | test_dir_.Append(FILE_PATH_LITERAL("Copy_Test_File.txt")); |
| 925 | CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle"); |
| 926 | ASSERT_TRUE(file_util::PathExists(file_name_from)); |
| 927 | |
| 928 | // The destination name |
| 929 | FilePath file_name_to = |
| 930 | test_dir_.Append(FILE_PATH_LITERAL("Copy_Test_File_Destination.txt")); |
| 931 | CreateTextFile(file_name_to, L"Old file content"); |
| 932 | ASSERT_TRUE(file_util::PathExists(file_name_to)); |
| 933 | |
| 934 | EXPECT_TRUE(file_util::CopyDirectory(file_name_from, file_name_to, true)); |
| 935 | |
| 936 | // Check the has been copied |
| 937 | EXPECT_TRUE(file_util::PathExists(file_name_to)); |
| 938 | EXPECT_TRUE(L"Gooooooooooooooooooooogle" == ReadTextFile(file_name_to)); |
| 939 | } |
| 940 | |
| 941 | TEST_F(FileUtilTest, CopyFileWithCopyDirectoryRecursiveToExistingDirectory) { |
| 942 | // Create a file |
| 943 | FilePath file_name_from = |
| 944 | test_dir_.Append(FILE_PATH_LITERAL("Copy_Test_File.txt")); |
| 945 | CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle"); |
| 946 | ASSERT_TRUE(file_util::PathExists(file_name_from)); |
| 947 | |
| 948 | // The destination |
| 949 | FilePath dir_name_to = |
| 950 | test_dir_.Append(FILE_PATH_LITERAL("Destination")); |
| 951 | file_util::CreateDirectory(dir_name_to); |
| 952 | ASSERT_TRUE(file_util::PathExists(dir_name_to)); |
| 953 | FilePath file_name_to = |
| 954 | dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt")); |
| 955 | |
| 956 | EXPECT_TRUE(file_util::CopyDirectory(file_name_from, dir_name_to, true)); |
| 957 | |
| 958 | // Check the has been copied |
| 959 | EXPECT_TRUE(file_util::PathExists(file_name_to)); |
| 960 | } |
| 961 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 962 | TEST_F(FileUtilTest, CopyFile) { |
| 963 | // Create a directory |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 964 | FilePath dir_name_from = |
| 965 | test_dir_.Append(FILE_PATH_LITERAL("Copy_From_Subdir")); |
| 966 | file_util::CreateDirectory(dir_name_from); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 967 | ASSERT_TRUE(file_util::PathExists(dir_name_from)); |
| 968 | |
| 969 | // Create a file under the directory |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 970 | FilePath file_name_from = |
| 971 | dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt")); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 972 | const std::wstring file_contents(L"Gooooooooooooooooooooogle"); |
| 973 | CreateTextFile(file_name_from, file_contents); |
| 974 | ASSERT_TRUE(file_util::PathExists(file_name_from)); |
| 975 | |
| 976 | // Copy the file. |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 977 | FilePath dest_file = dir_name_from.Append(FILE_PATH_LITERAL("DestFile.txt")); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 978 | ASSERT_TRUE(file_util::CopyFile(file_name_from, dest_file)); |
mmoss@google.com | 733df6b | 2008-09-12 01:09:11 +0900 | [diff] [blame] | 979 | |
erikkay@google.com | dfb51b2 | 2008-08-16 02:32:10 +0900 | [diff] [blame] | 980 | // Copy the file to another location using '..' in the path. |
evan@chromium.org | 1543ad3 | 2009-08-27 05:00:14 +0900 | [diff] [blame] | 981 | FilePath dest_file2(dir_name_from); |
| 982 | dest_file2 = dest_file2.AppendASCII(".."); |
| 983 | dest_file2 = dest_file2.AppendASCII("DestFile.txt"); |
| 984 | ASSERT_TRUE(file_util::CopyFile(file_name_from, dest_file2)); |
| 985 | |
| 986 | FilePath dest_file2_test(dir_name_from); |
| 987 | dest_file2_test = dest_file2_test.DirName(); |
| 988 | dest_file2_test = dest_file2_test.AppendASCII("DestFile.txt"); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 989 | |
| 990 | // Check everything has been copied. |
| 991 | EXPECT_TRUE(file_util::PathExists(file_name_from)); |
| 992 | EXPECT_TRUE(file_util::PathExists(dest_file)); |
| 993 | const std::wstring read_contents = ReadTextFile(dest_file); |
| 994 | EXPECT_EQ(file_contents, read_contents); |
evan@chromium.org | 1543ad3 | 2009-08-27 05:00:14 +0900 | [diff] [blame] | 995 | EXPECT_TRUE(file_util::PathExists(dest_file2_test)); |
| 996 | EXPECT_TRUE(file_util::PathExists(dest_file2)); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 997 | } |
| 998 | |
erikkay@google.com | dfb51b2 | 2008-08-16 02:32:10 +0900 | [diff] [blame] | 999 | // TODO(erikkay): implement |
erikkay@google.com | 014161d | 2008-08-16 02:45:13 +0900 | [diff] [blame] | 1000 | #if defined(OS_WIN) |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 1001 | TEST_F(FileUtilTest, GetFileCreationLocalTime) { |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 1002 | FilePath file_name = test_dir_.Append(L"Test File.txt"); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 1003 | |
| 1004 | SYSTEMTIME start_time; |
| 1005 | GetLocalTime(&start_time); |
| 1006 | Sleep(100); |
| 1007 | CreateTextFile(file_name, L"New file!"); |
| 1008 | Sleep(100); |
| 1009 | SYSTEMTIME end_time; |
| 1010 | GetLocalTime(&end_time); |
| 1011 | |
| 1012 | SYSTEMTIME file_creation_time; |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 1013 | file_util::GetFileCreationLocalTime(file_name.value(), &file_creation_time); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 1014 | |
| 1015 | FILETIME start_filetime; |
| 1016 | SystemTimeToFileTime(&start_time, &start_filetime); |
| 1017 | FILETIME end_filetime; |
| 1018 | SystemTimeToFileTime(&end_time, &end_filetime); |
| 1019 | FILETIME file_creation_filetime; |
| 1020 | SystemTimeToFileTime(&file_creation_time, &file_creation_filetime); |
| 1021 | |
| 1022 | EXPECT_EQ(-1, CompareFileTime(&start_filetime, &file_creation_filetime)) << |
| 1023 | "start time: " << FileTimeAsUint64(start_filetime) << ", " << |
| 1024 | "creation time: " << FileTimeAsUint64(file_creation_filetime); |
| 1025 | |
| 1026 | EXPECT_EQ(-1, CompareFileTime(&file_creation_filetime, &end_filetime)) << |
| 1027 | "creation time: " << FileTimeAsUint64(file_creation_filetime) << ", " << |
| 1028 | "end time: " << FileTimeAsUint64(end_filetime); |
| 1029 | |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 1030 | ASSERT_TRUE(DeleteFile(file_name.value().c_str())); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 1031 | } |
erikkay@google.com | dfb51b2 | 2008-08-16 02:32:10 +0900 | [diff] [blame] | 1032 | #endif |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 1033 | |
erikkay@google.com | f240684 | 2008-08-21 00:59:49 +0900 | [diff] [blame] | 1034 | // file_util winds up using autoreleased objects on the Mac, so this needs |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 1035 | // to be a PlatformTest. |
erikkay@google.com | f240684 | 2008-08-21 00:59:49 +0900 | [diff] [blame] | 1036 | typedef PlatformTest ReadOnlyFileUtilTest; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 1037 | |
erikkay@google.com | f240684 | 2008-08-21 00:59:49 +0900 | [diff] [blame] | 1038 | TEST_F(ReadOnlyFileUtilTest, ContentsEqual) { |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 1039 | FilePath data_dir; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 1040 | ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &data_dir)); |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 1041 | data_dir = data_dir.Append(FILE_PATH_LITERAL("base")) |
| 1042 | .Append(FILE_PATH_LITERAL("data")) |
| 1043 | .Append(FILE_PATH_LITERAL("file_util_unittest")); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 1044 | ASSERT_TRUE(file_util::PathExists(data_dir)); |
| 1045 | |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 1046 | FilePath original_file = |
| 1047 | data_dir.Append(FILE_PATH_LITERAL("original.txt")); |
| 1048 | FilePath same_file = |
| 1049 | data_dir.Append(FILE_PATH_LITERAL("same.txt")); |
| 1050 | FilePath same_length_file = |
| 1051 | data_dir.Append(FILE_PATH_LITERAL("same_length.txt")); |
| 1052 | FilePath different_file = |
| 1053 | data_dir.Append(FILE_PATH_LITERAL("different.txt")); |
| 1054 | FilePath different_first_file = |
| 1055 | data_dir.Append(FILE_PATH_LITERAL("different_first.txt")); |
| 1056 | FilePath different_last_file = |
| 1057 | data_dir.Append(FILE_PATH_LITERAL("different_last.txt")); |
| 1058 | FilePath empty1_file = |
| 1059 | data_dir.Append(FILE_PATH_LITERAL("empty1.txt")); |
| 1060 | FilePath empty2_file = |
| 1061 | data_dir.Append(FILE_PATH_LITERAL("empty2.txt")); |
| 1062 | FilePath shortened_file = |
| 1063 | data_dir.Append(FILE_PATH_LITERAL("shortened.txt")); |
| 1064 | FilePath binary_file = |
| 1065 | data_dir.Append(FILE_PATH_LITERAL("binary_file.bin")); |
| 1066 | FilePath binary_file_same = |
| 1067 | data_dir.Append(FILE_PATH_LITERAL("binary_file_same.bin")); |
| 1068 | FilePath binary_file_diff = |
| 1069 | data_dir.Append(FILE_PATH_LITERAL("binary_file_diff.bin")); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 1070 | |
| 1071 | EXPECT_TRUE(file_util::ContentsEqual(original_file, original_file)); |
| 1072 | EXPECT_TRUE(file_util::ContentsEqual(original_file, same_file)); |
| 1073 | EXPECT_FALSE(file_util::ContentsEqual(original_file, same_length_file)); |
| 1074 | EXPECT_FALSE(file_util::ContentsEqual(original_file, different_file)); |
thakis@chromium.org | 506f091 | 2009-12-02 07:14:22 +0900 | [diff] [blame] | 1075 | EXPECT_FALSE(file_util::ContentsEqual( |
| 1076 | FilePath(FILE_PATH_LITERAL("bogusname")), |
| 1077 | FilePath(FILE_PATH_LITERAL("bogusname")))); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 1078 | EXPECT_FALSE(file_util::ContentsEqual(original_file, different_first_file)); |
| 1079 | EXPECT_FALSE(file_util::ContentsEqual(original_file, different_last_file)); |
| 1080 | EXPECT_TRUE(file_util::ContentsEqual(empty1_file, empty2_file)); |
| 1081 | EXPECT_FALSE(file_util::ContentsEqual(original_file, shortened_file)); |
| 1082 | EXPECT_FALSE(file_util::ContentsEqual(shortened_file, original_file)); |
| 1083 | EXPECT_TRUE(file_util::ContentsEqual(binary_file, binary_file_same)); |
| 1084 | EXPECT_FALSE(file_util::ContentsEqual(binary_file, binary_file_diff)); |
| 1085 | } |
| 1086 | |
mark@chromium.org | 95c9ec9 | 2009-06-27 06:17:24 +0900 | [diff] [blame] | 1087 | TEST_F(ReadOnlyFileUtilTest, TextContentsEqual) { |
| 1088 | FilePath data_dir; |
| 1089 | ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &data_dir)); |
| 1090 | data_dir = data_dir.Append(FILE_PATH_LITERAL("base")) |
| 1091 | .Append(FILE_PATH_LITERAL("data")) |
| 1092 | .Append(FILE_PATH_LITERAL("file_util_unittest")); |
| 1093 | ASSERT_TRUE(file_util::PathExists(data_dir)); |
| 1094 | |
| 1095 | FilePath original_file = |
| 1096 | data_dir.Append(FILE_PATH_LITERAL("original.txt")); |
| 1097 | FilePath same_file = |
| 1098 | data_dir.Append(FILE_PATH_LITERAL("same.txt")); |
| 1099 | FilePath crlf_file = |
| 1100 | data_dir.Append(FILE_PATH_LITERAL("crlf.txt")); |
| 1101 | FilePath shortened_file = |
| 1102 | data_dir.Append(FILE_PATH_LITERAL("shortened.txt")); |
| 1103 | FilePath different_file = |
| 1104 | data_dir.Append(FILE_PATH_LITERAL("different.txt")); |
| 1105 | FilePath different_first_file = |
| 1106 | data_dir.Append(FILE_PATH_LITERAL("different_first.txt")); |
| 1107 | FilePath different_last_file = |
| 1108 | data_dir.Append(FILE_PATH_LITERAL("different_last.txt")); |
| 1109 | FilePath first1_file = |
| 1110 | data_dir.Append(FILE_PATH_LITERAL("first1.txt")); |
| 1111 | FilePath first2_file = |
| 1112 | data_dir.Append(FILE_PATH_LITERAL("first2.txt")); |
| 1113 | FilePath empty1_file = |
| 1114 | data_dir.Append(FILE_PATH_LITERAL("empty1.txt")); |
| 1115 | FilePath empty2_file = |
| 1116 | data_dir.Append(FILE_PATH_LITERAL("empty2.txt")); |
| 1117 | FilePath blank_line_file = |
| 1118 | data_dir.Append(FILE_PATH_LITERAL("blank_line.txt")); |
| 1119 | FilePath blank_line_crlf_file = |
| 1120 | data_dir.Append(FILE_PATH_LITERAL("blank_line_crlf.txt")); |
| 1121 | |
| 1122 | EXPECT_TRUE(file_util::TextContentsEqual(original_file, same_file)); |
| 1123 | EXPECT_TRUE(file_util::TextContentsEqual(original_file, crlf_file)); |
| 1124 | EXPECT_FALSE(file_util::TextContentsEqual(original_file, shortened_file)); |
| 1125 | EXPECT_FALSE(file_util::TextContentsEqual(original_file, different_file)); |
| 1126 | EXPECT_FALSE(file_util::TextContentsEqual(original_file, |
| 1127 | different_first_file)); |
| 1128 | EXPECT_FALSE(file_util::TextContentsEqual(original_file, |
| 1129 | different_last_file)); |
| 1130 | EXPECT_FALSE(file_util::TextContentsEqual(first1_file, first2_file)); |
| 1131 | EXPECT_TRUE(file_util::TextContentsEqual(empty1_file, empty2_file)); |
| 1132 | EXPECT_FALSE(file_util::TextContentsEqual(original_file, empty1_file)); |
| 1133 | EXPECT_TRUE(file_util::TextContentsEqual(blank_line_file, |
| 1134 | blank_line_crlf_file)); |
| 1135 | } |
| 1136 | |
erikkay@google.com | dfb51b2 | 2008-08-16 02:32:10 +0900 | [diff] [blame] | 1137 | // We don't need equivalent functionality outside of Windows. |
erikkay@google.com | 014161d | 2008-08-16 02:45:13 +0900 | [diff] [blame] | 1138 | #if defined(OS_WIN) |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 1139 | TEST_F(FileUtilTest, ResolveShortcutTest) { |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 1140 | FilePath target_file = test_dir_.Append(L"Target.txt"); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 1141 | CreateTextFile(target_file, L"This is the target."); |
| 1142 | |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 1143 | FilePath link_file = test_dir_.Append(L"Link.lnk"); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 1144 | |
| 1145 | HRESULT result; |
| 1146 | IShellLink *shell = NULL; |
| 1147 | IPersistFile *persist = NULL; |
| 1148 | |
| 1149 | CoInitialize(NULL); |
| 1150 | // Temporarily create a shortcut for test |
| 1151 | result = CoCreateInstance(CLSID_ShellLink, NULL, |
| 1152 | CLSCTX_INPROC_SERVER, IID_IShellLink, |
| 1153 | reinterpret_cast<LPVOID*>(&shell)); |
| 1154 | EXPECT_TRUE(SUCCEEDED(result)); |
| 1155 | result = shell->QueryInterface(IID_IPersistFile, |
| 1156 | reinterpret_cast<LPVOID*>(&persist)); |
| 1157 | EXPECT_TRUE(SUCCEEDED(result)); |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 1158 | result = shell->SetPath(target_file.value().c_str()); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 1159 | EXPECT_TRUE(SUCCEEDED(result)); |
| 1160 | result = shell->SetDescription(L"ResolveShortcutTest"); |
| 1161 | EXPECT_TRUE(SUCCEEDED(result)); |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 1162 | result = persist->Save(link_file.value().c_str(), TRUE); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 1163 | EXPECT_TRUE(SUCCEEDED(result)); |
| 1164 | if (persist) |
| 1165 | persist->Release(); |
| 1166 | if (shell) |
| 1167 | shell->Release(); |
| 1168 | |
| 1169 | bool is_solved; |
evan@chromium.org | a489904 | 2009-08-25 10:51:44 +0900 | [diff] [blame] | 1170 | is_solved = file_util::ResolveShortcut(&link_file); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 1171 | EXPECT_TRUE(is_solved); |
| 1172 | std::wstring contents; |
evan@chromium.org | a489904 | 2009-08-25 10:51:44 +0900 | [diff] [blame] | 1173 | contents = ReadTextFile(link_file); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 1174 | EXPECT_EQ(L"This is the target.", contents); |
| 1175 | |
ericroman@google.com | dbff4f5 | 2008-08-19 01:00:38 +0900 | [diff] [blame] | 1176 | // Cleaning |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 1177 | DeleteFile(target_file.value().c_str()); |
evan@chromium.org | a489904 | 2009-08-25 10:51:44 +0900 | [diff] [blame] | 1178 | DeleteFile(link_file.value().c_str()); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 1179 | CoUninitialize(); |
| 1180 | } |
| 1181 | |
| 1182 | TEST_F(FileUtilTest, CreateShortcutTest) { |
| 1183 | const wchar_t file_contents[] = L"This is another target."; |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 1184 | FilePath target_file = test_dir_.Append(L"Target1.txt"); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 1185 | CreateTextFile(target_file, file_contents); |
| 1186 | |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 1187 | FilePath link_file = test_dir_.Append(L"Link1.lnk"); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 1188 | |
| 1189 | CoInitialize(NULL); |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 1190 | EXPECT_TRUE(file_util::CreateShortcutLink(target_file.value().c_str(), |
| 1191 | link_file.value().c_str(), |
xiyuan@chromium.org | d9e9bb4 | 2009-11-19 18:18:50 +0900 | [diff] [blame] | 1192 | NULL, NULL, NULL, NULL, 0, NULL)); |
evan@chromium.org | a489904 | 2009-08-25 10:51:44 +0900 | [diff] [blame] | 1193 | FilePath resolved_name = link_file; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 1194 | EXPECT_TRUE(file_util::ResolveShortcut(&resolved_name)); |
evan@chromium.org | a489904 | 2009-08-25 10:51:44 +0900 | [diff] [blame] | 1195 | std::wstring read_contents = ReadTextFile(resolved_name); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 1196 | EXPECT_EQ(file_contents, read_contents); |
| 1197 | |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 1198 | DeleteFile(target_file.value().c_str()); |
| 1199 | DeleteFile(link_file.value().c_str()); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 1200 | CoUninitialize(); |
| 1201 | } |
huanr@chromium.org | 7f2c6af | 2009-03-12 03:37:48 +0900 | [diff] [blame] | 1202 | |
| 1203 | TEST_F(FileUtilTest, CopyAndDeleteDirectoryTest) { |
| 1204 | // Create a directory |
| 1205 | FilePath dir_name_from = |
| 1206 | test_dir_.Append(FILE_PATH_LITERAL("CopyAndDelete_From_Subdir")); |
| 1207 | file_util::CreateDirectory(dir_name_from); |
| 1208 | ASSERT_TRUE(file_util::PathExists(dir_name_from)); |
| 1209 | |
| 1210 | // Create a file under the directory |
| 1211 | FilePath file_name_from = |
| 1212 | dir_name_from.Append(FILE_PATH_LITERAL("CopyAndDelete_Test_File.txt")); |
| 1213 | CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle"); |
| 1214 | ASSERT_TRUE(file_util::PathExists(file_name_from)); |
| 1215 | |
| 1216 | // Move the directory by using CopyAndDeleteDirectory |
| 1217 | FilePath dir_name_to = test_dir_.Append( |
| 1218 | FILE_PATH_LITERAL("CopyAndDelete_To_Subdir")); |
| 1219 | FilePath file_name_to = |
| 1220 | dir_name_to.Append(FILE_PATH_LITERAL("CopyAndDelete_Test_File.txt")); |
| 1221 | |
| 1222 | ASSERT_FALSE(file_util::PathExists(dir_name_to)); |
| 1223 | |
| 1224 | EXPECT_TRUE(file_util::CopyAndDeleteDirectory(dir_name_from, dir_name_to)); |
| 1225 | |
| 1226 | // Check everything has been moved. |
| 1227 | EXPECT_FALSE(file_util::PathExists(dir_name_from)); |
| 1228 | EXPECT_FALSE(file_util::PathExists(file_name_from)); |
| 1229 | EXPECT_TRUE(file_util::PathExists(dir_name_to)); |
| 1230 | EXPECT_TRUE(file_util::PathExists(file_name_to)); |
| 1231 | } |
tkent@chromium.org | 8da1416 | 2009-10-09 16:33:39 +0900 | [diff] [blame] | 1232 | |
| 1233 | TEST_F(FileUtilTest, GetTempDirTest) { |
| 1234 | static const TCHAR* kTmpKey = _T("TMP"); |
| 1235 | static const TCHAR* kTmpValues[] = { |
| 1236 | _T(""), _T("C:"), _T("C:\\"), _T("C:\\tmp"), _T("C:\\tmp\\") |
| 1237 | }; |
| 1238 | // Save the original $TMP. |
| 1239 | size_t original_tmp_size; |
| 1240 | TCHAR* original_tmp; |
| 1241 | ASSERT_EQ(0, ::_tdupenv_s(&original_tmp, &original_tmp_size, kTmpKey)); |
| 1242 | // original_tmp may be NULL. |
| 1243 | |
| 1244 | for (unsigned int i = 0; i < arraysize(kTmpValues); ++i) { |
| 1245 | FilePath path; |
| 1246 | ::_tputenv_s(kTmpKey, kTmpValues[i]); |
| 1247 | file_util::GetTempDir(&path); |
| 1248 | EXPECT_TRUE(path.IsAbsolute()) << "$TMP=" << kTmpValues[i] << |
| 1249 | " result=" << path.value(); |
| 1250 | } |
| 1251 | |
| 1252 | // Restore the original $TMP. |
| 1253 | if (original_tmp) { |
| 1254 | ::_tputenv_s(kTmpKey, original_tmp); |
| 1255 | free(original_tmp); |
| 1256 | } else { |
| 1257 | ::_tputenv_s(kTmpKey, _T("")); |
| 1258 | } |
| 1259 | } |
| 1260 | #endif // OS_WIN |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 1261 | |
erikkay@chromium.org | 18f0dde | 2009-08-19 01:07:55 +0900 | [diff] [blame] | 1262 | TEST_F(FileUtilTest, CreateTemporaryFileTest) { |
| 1263 | FilePath temp_files[3]; |
jrg@chromium.org | d505c3a | 2009-02-04 09:58:39 +0900 | [diff] [blame] | 1264 | for (int i = 0; i < 3; i++) { |
erikkay@chromium.org | 18f0dde | 2009-08-19 01:07:55 +0900 | [diff] [blame] | 1265 | ASSERT_TRUE(file_util::CreateTemporaryFile(&(temp_files[i]))); |
jrg@chromium.org | d505c3a | 2009-02-04 09:58:39 +0900 | [diff] [blame] | 1266 | EXPECT_TRUE(file_util::PathExists(temp_files[i])); |
| 1267 | EXPECT_FALSE(file_util::DirectoryExists(temp_files[i])); |
| 1268 | } |
| 1269 | for (int i = 0; i < 3; i++) |
| 1270 | EXPECT_FALSE(temp_files[i] == temp_files[(i+1)%3]); |
| 1271 | for (int i = 0; i < 3; i++) |
| 1272 | EXPECT_TRUE(file_util::Delete(temp_files[i], false)); |
| 1273 | } |
| 1274 | |
erikkay@chromium.org | 18f0dde | 2009-08-19 01:07:55 +0900 | [diff] [blame] | 1275 | TEST_F(FileUtilTest, CreateAndOpenTemporaryFileTest) { |
jrg@chromium.org | d505c3a | 2009-02-04 09:58:39 +0900 | [diff] [blame] | 1276 | FilePath names[3]; |
| 1277 | FILE *fps[3]; |
| 1278 | int i; |
| 1279 | |
| 1280 | // Create; make sure they are open and exist. |
| 1281 | for (i = 0; i < 3; ++i) { |
| 1282 | fps[i] = file_util::CreateAndOpenTemporaryFile(&(names[i])); |
| 1283 | ASSERT_TRUE(fps[i]); |
| 1284 | EXPECT_TRUE(file_util::PathExists(names[i])); |
| 1285 | } |
| 1286 | |
| 1287 | // Make sure all names are unique. |
| 1288 | for (i = 0; i < 3; ++i) { |
| 1289 | EXPECT_FALSE(names[i] == names[(i+1)%3]); |
| 1290 | } |
| 1291 | |
| 1292 | // Close and delete. |
| 1293 | for (i = 0; i < 3; ++i) { |
| 1294 | EXPECT_TRUE(file_util::CloseFile(fps[i])); |
| 1295 | EXPECT_TRUE(file_util::Delete(names[i], false)); |
| 1296 | } |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 1297 | } |
| 1298 | |
| 1299 | TEST_F(FileUtilTest, CreateNewTempDirectoryTest) { |
evan@chromium.org | 1543ad3 | 2009-08-27 05:00:14 +0900 | [diff] [blame] | 1300 | FilePath temp_dir; |
| 1301 | ASSERT_TRUE(file_util::CreateNewTempDirectory(FilePath::StringType(), |
| 1302 | &temp_dir)); |
mmoss@google.com | 733df6b | 2008-09-12 01:09:11 +0900 | [diff] [blame] | 1303 | EXPECT_TRUE(file_util::PathExists(temp_dir)); |
| 1304 | EXPECT_TRUE(file_util::Delete(temp_dir, false)); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 1305 | } |
| 1306 | |
skerner@chromium.org | e443239 | 2010-05-01 02:00:09 +0900 | [diff] [blame] | 1307 | TEST_F(FileUtilTest, CreateNewTemporaryDirInDirTest) { |
| 1308 | FilePath new_dir; |
| 1309 | ASSERT_TRUE(file_util::CreateTemporaryDirInDir( |
| 1310 | test_dir_, |
| 1311 | FILE_PATH_LITERAL("CreateNewTemporaryDirInDirTest"), |
| 1312 | &new_dir)); |
| 1313 | EXPECT_TRUE(file_util::PathExists(new_dir)); |
| 1314 | EXPECT_TRUE(test_dir_.IsParent(new_dir)); |
| 1315 | EXPECT_TRUE(file_util::Delete(new_dir, false)); |
| 1316 | } |
| 1317 | |
jrg@chromium.org | d505c3a | 2009-02-04 09:58:39 +0900 | [diff] [blame] | 1318 | TEST_F(FileUtilTest, GetShmemTempDirTest) { |
| 1319 | FilePath dir; |
| 1320 | EXPECT_TRUE(file_util::GetShmemTempDir(&dir)); |
| 1321 | EXPECT_TRUE(file_util::DirectoryExists(dir)); |
| 1322 | } |
| 1323 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 1324 | TEST_F(FileUtilTest, CreateDirectoryTest) { |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 1325 | FilePath test_root = |
| 1326 | test_dir_.Append(FILE_PATH_LITERAL("create_directory_test")); |
erikkay@google.com | 014161d | 2008-08-16 02:45:13 +0900 | [diff] [blame] | 1327 | #if defined(OS_WIN) |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 1328 | FilePath test_path = |
| 1329 | test_root.Append(FILE_PATH_LITERAL("dir\\tree\\likely\\doesnt\\exist\\")); |
erikkay@google.com | dfb51b2 | 2008-08-16 02:32:10 +0900 | [diff] [blame] | 1330 | #elif defined(OS_POSIX) |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 1331 | FilePath test_path = |
| 1332 | test_root.Append(FILE_PATH_LITERAL("dir/tree/likely/doesnt/exist/")); |
erikkay@google.com | dfb51b2 | 2008-08-16 02:32:10 +0900 | [diff] [blame] | 1333 | #endif |
mmoss@google.com | 733df6b | 2008-09-12 01:09:11 +0900 | [diff] [blame] | 1334 | |
| 1335 | EXPECT_FALSE(file_util::PathExists(test_path)); |
| 1336 | EXPECT_TRUE(file_util::CreateDirectory(test_path)); |
| 1337 | EXPECT_TRUE(file_util::PathExists(test_path)); |
| 1338 | // CreateDirectory returns true if the DirectoryExists returns true. |
| 1339 | EXPECT_TRUE(file_util::CreateDirectory(test_path)); |
| 1340 | |
| 1341 | // Doesn't work to create it on top of a non-dir |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 1342 | test_path = test_path.Append(FILE_PATH_LITERAL("foobar.txt")); |
mmoss@google.com | 733df6b | 2008-09-12 01:09:11 +0900 | [diff] [blame] | 1343 | EXPECT_FALSE(file_util::PathExists(test_path)); |
| 1344 | CreateTextFile(test_path, L"test file"); |
| 1345 | EXPECT_TRUE(file_util::PathExists(test_path)); |
| 1346 | EXPECT_FALSE(file_util::CreateDirectory(test_path)); |
| 1347 | |
| 1348 | EXPECT_TRUE(file_util::Delete(test_root, true)); |
| 1349 | EXPECT_FALSE(file_util::PathExists(test_root)); |
| 1350 | EXPECT_FALSE(file_util::PathExists(test_path)); |
joi@chromium.org | 9cd6dd2 | 2009-11-27 23:54:41 +0900 | [diff] [blame] | 1351 | |
| 1352 | // Verify assumptions made by the Windows implementation: |
| 1353 | // 1. The current directory always exists. |
| 1354 | // 2. The root directory always exists. |
| 1355 | ASSERT_TRUE(file_util::DirectoryExists( |
| 1356 | FilePath(FilePath::kCurrentDirectory))); |
| 1357 | FilePath top_level = test_root; |
| 1358 | while (top_level != top_level.DirName()) { |
| 1359 | top_level = top_level.DirName(); |
| 1360 | } |
| 1361 | ASSERT_TRUE(file_util::DirectoryExists(top_level)); |
| 1362 | |
| 1363 | // Given these assumptions hold, it should be safe to |
| 1364 | // test that "creating" these directories succeeds. |
| 1365 | EXPECT_TRUE(file_util::CreateDirectory( |
| 1366 | FilePath(FilePath::kCurrentDirectory))); |
| 1367 | EXPECT_TRUE(file_util::CreateDirectory(top_level)); |
huanr@chromium.org | 57c9dc3 | 2009-12-18 05:42:40 +0900 | [diff] [blame] | 1368 | |
| 1369 | #if defined(OS_WIN) |
| 1370 | FilePath invalid_drive(FILE_PATH_LITERAL("o:\\")); |
| 1371 | FilePath invalid_path = |
| 1372 | invalid_drive.Append(FILE_PATH_LITERAL("some\\inaccessible\\dir")); |
| 1373 | if (!file_util::PathExists(invalid_drive)) { |
| 1374 | EXPECT_FALSE(file_util::CreateDirectory(invalid_path)); |
| 1375 | } |
| 1376 | #endif |
mmoss@google.com | 733df6b | 2008-09-12 01:09:11 +0900 | [diff] [blame] | 1377 | } |
| 1378 | |
| 1379 | TEST_F(FileUtilTest, DetectDirectoryTest) { |
| 1380 | // Check a directory |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 1381 | FilePath test_root = |
| 1382 | test_dir_.Append(FILE_PATH_LITERAL("detect_directory_test")); |
mmoss@google.com | 733df6b | 2008-09-12 01:09:11 +0900 | [diff] [blame] | 1383 | EXPECT_FALSE(file_util::PathExists(test_root)); |
| 1384 | EXPECT_TRUE(file_util::CreateDirectory(test_root)); |
| 1385 | EXPECT_TRUE(file_util::PathExists(test_root)); |
| 1386 | EXPECT_TRUE(file_util::DirectoryExists(test_root)); |
| 1387 | |
| 1388 | // Check a file |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 1389 | FilePath test_path = |
| 1390 | test_root.Append(FILE_PATH_LITERAL("foobar.txt")); |
mmoss@google.com | 733df6b | 2008-09-12 01:09:11 +0900 | [diff] [blame] | 1391 | EXPECT_FALSE(file_util::PathExists(test_path)); |
| 1392 | CreateTextFile(test_path, L"test file"); |
| 1393 | EXPECT_TRUE(file_util::PathExists(test_path)); |
| 1394 | EXPECT_FALSE(file_util::DirectoryExists(test_path)); |
| 1395 | EXPECT_TRUE(file_util::Delete(test_path, false)); |
| 1396 | |
| 1397 | EXPECT_TRUE(file_util::Delete(test_root, true)); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 1398 | } |
| 1399 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 1400 | static const struct ReplaceExtensionCase { |
| 1401 | std::wstring file_name; |
estade@chromium.org | 6334320 | 2008-12-05 05:46:06 +0900 | [diff] [blame] | 1402 | FilePath::StringType extension; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 1403 | std::wstring result; |
| 1404 | } kReplaceExtension[] = { |
estade@chromium.org | 6334320 | 2008-12-05 05:46:06 +0900 | [diff] [blame] | 1405 | {L"", FILE_PATH_LITERAL(""), L""}, |
| 1406 | {L"", FILE_PATH_LITERAL("txt"), L".txt"}, |
| 1407 | {L".", FILE_PATH_LITERAL("txt"), L".txt"}, |
| 1408 | {L".", FILE_PATH_LITERAL(""), L""}, |
| 1409 | {L"foo.dll", FILE_PATH_LITERAL("txt"), L"foo.txt"}, |
| 1410 | {L"foo.dll", FILE_PATH_LITERAL(".txt"), L"foo.txt"}, |
| 1411 | {L"foo", FILE_PATH_LITERAL("txt"), L"foo.txt"}, |
| 1412 | {L"foo", FILE_PATH_LITERAL(".txt"), L"foo.txt"}, |
| 1413 | {L"foo.baz.dll", FILE_PATH_LITERAL("txt"), L"foo.baz.txt"}, |
| 1414 | {L"foo.baz.dll", FILE_PATH_LITERAL(".txt"), L"foo.baz.txt"}, |
| 1415 | {L"foo.dll", FILE_PATH_LITERAL(""), L"foo"}, |
| 1416 | {L"foo.dll", FILE_PATH_LITERAL("."), L"foo"}, |
| 1417 | {L"foo", FILE_PATH_LITERAL(""), L"foo"}, |
| 1418 | {L"foo", FILE_PATH_LITERAL("."), L"foo"}, |
| 1419 | {L"foo.baz.dll", FILE_PATH_LITERAL(""), L"foo.baz"}, |
| 1420 | {L"foo.baz.dll", FILE_PATH_LITERAL("."), L"foo.baz"}, |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 1421 | }; |
| 1422 | |
| 1423 | TEST_F(FileUtilTest, ReplaceExtensionTest) { |
erikkay@google.com | dfb51b2 | 2008-08-16 02:32:10 +0900 | [diff] [blame] | 1424 | for (unsigned int i = 0; i < arraysize(kReplaceExtension); ++i) { |
estade@chromium.org | 6334320 | 2008-12-05 05:46:06 +0900 | [diff] [blame] | 1425 | FilePath path = FilePath::FromWStringHack(kReplaceExtension[i].file_name); |
| 1426 | file_util::ReplaceExtension(&path, kReplaceExtension[i].extension); |
| 1427 | EXPECT_EQ(kReplaceExtension[i].result, path.ToWStringHack()); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 1428 | } |
| 1429 | } |
| 1430 | |
sky@google.com | 71e7c6f | 2008-09-20 02:32:18 +0900 | [diff] [blame] | 1431 | // Make sure ReplaceExtension doesn't replace an extension that occurs as one of |
| 1432 | // the directory names of the path. |
| 1433 | TEST_F(FileUtilTest, ReplaceExtensionTestWithPathSeparators) { |
estade@chromium.org | 6334320 | 2008-12-05 05:46:06 +0900 | [diff] [blame] | 1434 | FilePath path; |
| 1435 | path = path.Append(FILE_PATH_LITERAL("foo.bar")); |
| 1436 | path = path.Append(FILE_PATH_LITERAL("foo")); |
sky@google.com | 71e7c6f | 2008-09-20 02:32:18 +0900 | [diff] [blame] | 1437 | // '/foo.bar/foo' with extension '.baz' |
estade@chromium.org | 6334320 | 2008-12-05 05:46:06 +0900 | [diff] [blame] | 1438 | FilePath result_path = path; |
| 1439 | file_util::ReplaceExtension(&result_path, FILE_PATH_LITERAL(".baz")); |
phajdan.jr@chromium.org | f9908a7 | 2009-04-04 02:17:58 +0900 | [diff] [blame] | 1440 | EXPECT_EQ(path.value() + FILE_PATH_LITERAL(".baz"), |
| 1441 | result_path.value()); |
sky@google.com | 71e7c6f | 2008-09-20 02:32:18 +0900 | [diff] [blame] | 1442 | } |
| 1443 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 1444 | TEST_F(FileUtilTest, FileEnumeratorTest) { |
| 1445 | // Test an empty directory. |
yuzo@chromium.org | 2da0f82 | 2009-06-09 14:57:38 +0900 | [diff] [blame] | 1446 | file_util::FileEnumerator f0(test_dir_, true, FILES_AND_DIRECTORIES); |
avi@google.com | 5cb7935 | 2008-12-11 23:55:12 +0900 | [diff] [blame] | 1447 | EXPECT_EQ(f0.Next().value(), FILE_PATH_LITERAL("")); |
| 1448 | EXPECT_EQ(f0.Next().value(), FILE_PATH_LITERAL("")); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 1449 | |
yuzo@chromium.org | 2da0f82 | 2009-06-09 14:57:38 +0900 | [diff] [blame] | 1450 | // Test an empty directory, non-recursively, including "..". |
| 1451 | file_util::FileEnumerator f0_dotdot(test_dir_, false, |
| 1452 | static_cast<file_util::FileEnumerator::FILE_TYPE>( |
| 1453 | FILES_AND_DIRECTORIES | file_util::FileEnumerator::INCLUDE_DOT_DOT)); |
| 1454 | EXPECT_EQ(test_dir_.Append(FILE_PATH_LITERAL("..")).value(), |
| 1455 | f0_dotdot.Next().value()); |
| 1456 | EXPECT_EQ(FILE_PATH_LITERAL(""), |
| 1457 | f0_dotdot.Next().value()); |
| 1458 | |
erikkay@google.com | dfb51b2 | 2008-08-16 02:32:10 +0900 | [diff] [blame] | 1459 | // create the directories |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 1460 | FilePath dir1 = test_dir_.Append(FILE_PATH_LITERAL("dir1")); |
erikkay@google.com | dfb51b2 | 2008-08-16 02:32:10 +0900 | [diff] [blame] | 1461 | EXPECT_TRUE(file_util::CreateDirectory(dir1)); |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 1462 | FilePath dir2 = test_dir_.Append(FILE_PATH_LITERAL("dir2")); |
erikkay@google.com | dfb51b2 | 2008-08-16 02:32:10 +0900 | [diff] [blame] | 1463 | EXPECT_TRUE(file_util::CreateDirectory(dir2)); |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 1464 | FilePath dir2inner = dir2.Append(FILE_PATH_LITERAL("inner")); |
erikkay@google.com | dfb51b2 | 2008-08-16 02:32:10 +0900 | [diff] [blame] | 1465 | EXPECT_TRUE(file_util::CreateDirectory(dir2inner)); |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 1466 | |
erikkay@google.com | dfb51b2 | 2008-08-16 02:32:10 +0900 | [diff] [blame] | 1467 | // create the files |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 1468 | FilePath dir2file = dir2.Append(FILE_PATH_LITERAL("dir2file.txt")); |
erikkay@google.com | dfb51b2 | 2008-08-16 02:32:10 +0900 | [diff] [blame] | 1469 | CreateTextFile(dir2file, L""); |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 1470 | FilePath dir2innerfile = dir2inner.Append(FILE_PATH_LITERAL("innerfile.txt")); |
erikkay@google.com | dfb51b2 | 2008-08-16 02:32:10 +0900 | [diff] [blame] | 1471 | CreateTextFile(dir2innerfile, L""); |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 1472 | FilePath file1 = test_dir_.Append(FILE_PATH_LITERAL("file1.txt")); |
erikkay@google.com | dfb51b2 | 2008-08-16 02:32:10 +0900 | [diff] [blame] | 1473 | CreateTextFile(file1, L""); |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 1474 | FilePath file2_rel = |
| 1475 | dir2.Append(FilePath::kParentDirectory) |
| 1476 | .Append(FILE_PATH_LITERAL("file2.txt")); |
erikkay@google.com | dfb51b2 | 2008-08-16 02:32:10 +0900 | [diff] [blame] | 1477 | CreateTextFile(file2_rel, L""); |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 1478 | FilePath file2_abs = test_dir_.Append(FILE_PATH_LITERAL("file2.txt")); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 1479 | |
| 1480 | // Only enumerate files. |
avi@google.com | 5cb7935 | 2008-12-11 23:55:12 +0900 | [diff] [blame] | 1481 | file_util::FileEnumerator f1(test_dir_, true, |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 1482 | file_util::FileEnumerator::FILES); |
| 1483 | FindResultCollector c1(f1); |
erikkay@google.com | dfb51b2 | 2008-08-16 02:32:10 +0900 | [diff] [blame] | 1484 | EXPECT_TRUE(c1.HasFile(file1)); |
| 1485 | EXPECT_TRUE(c1.HasFile(file2_abs)); |
| 1486 | EXPECT_TRUE(c1.HasFile(dir2file)); |
| 1487 | EXPECT_TRUE(c1.HasFile(dir2innerfile)); |
| 1488 | EXPECT_EQ(c1.size(), 4); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 1489 | |
| 1490 | // Only enumerate directories. |
avi@google.com | 5cb7935 | 2008-12-11 23:55:12 +0900 | [diff] [blame] | 1491 | file_util::FileEnumerator f2(test_dir_, true, |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 1492 | file_util::FileEnumerator::DIRECTORIES); |
| 1493 | FindResultCollector c2(f2); |
erikkay@google.com | dfb51b2 | 2008-08-16 02:32:10 +0900 | [diff] [blame] | 1494 | EXPECT_TRUE(c2.HasFile(dir1)); |
| 1495 | EXPECT_TRUE(c2.HasFile(dir2)); |
| 1496 | EXPECT_TRUE(c2.HasFile(dir2inner)); |
| 1497 | EXPECT_EQ(c2.size(), 3); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 1498 | |
tim@chromium.org | 989d097 | 2008-10-16 11:42:45 +0900 | [diff] [blame] | 1499 | // Only enumerate directories non-recursively. |
| 1500 | file_util::FileEnumerator f2_non_recursive( |
avi@google.com | 5cb7935 | 2008-12-11 23:55:12 +0900 | [diff] [blame] | 1501 | test_dir_, false, file_util::FileEnumerator::DIRECTORIES); |
tim@chromium.org | 989d097 | 2008-10-16 11:42:45 +0900 | [diff] [blame] | 1502 | FindResultCollector c2_non_recursive(f2_non_recursive); |
| 1503 | EXPECT_TRUE(c2_non_recursive.HasFile(dir1)); |
| 1504 | EXPECT_TRUE(c2_non_recursive.HasFile(dir2)); |
| 1505 | EXPECT_EQ(c2_non_recursive.size(), 2); |
| 1506 | |
yuzo@chromium.org | 2da0f82 | 2009-06-09 14:57:38 +0900 | [diff] [blame] | 1507 | // Only enumerate directories, non-recursively, including "..". |
| 1508 | file_util::FileEnumerator f2_dotdot( |
| 1509 | test_dir_, false, |
| 1510 | static_cast<file_util::FileEnumerator::FILE_TYPE>( |
| 1511 | file_util::FileEnumerator::DIRECTORIES | |
| 1512 | file_util::FileEnumerator::INCLUDE_DOT_DOT)); |
| 1513 | FindResultCollector c2_dotdot(f2_dotdot); |
| 1514 | EXPECT_TRUE(c2_dotdot.HasFile(dir1)); |
| 1515 | EXPECT_TRUE(c2_dotdot.HasFile(dir2)); |
| 1516 | EXPECT_TRUE(c2_dotdot.HasFile(test_dir_.Append(FILE_PATH_LITERAL("..")))); |
| 1517 | EXPECT_EQ(c2_dotdot.size(), 3); |
| 1518 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 1519 | // Enumerate files and directories. |
yuzo@chromium.org | 2da0f82 | 2009-06-09 14:57:38 +0900 | [diff] [blame] | 1520 | file_util::FileEnumerator f3(test_dir_, true, FILES_AND_DIRECTORIES); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 1521 | FindResultCollector c3(f3); |
erikkay@google.com | dfb51b2 | 2008-08-16 02:32:10 +0900 | [diff] [blame] | 1522 | EXPECT_TRUE(c3.HasFile(dir1)); |
| 1523 | EXPECT_TRUE(c3.HasFile(dir2)); |
| 1524 | EXPECT_TRUE(c3.HasFile(file1)); |
| 1525 | EXPECT_TRUE(c3.HasFile(file2_abs)); |
| 1526 | EXPECT_TRUE(c3.HasFile(dir2file)); |
| 1527 | EXPECT_TRUE(c3.HasFile(dir2inner)); |
| 1528 | EXPECT_TRUE(c3.HasFile(dir2innerfile)); |
| 1529 | EXPECT_EQ(c3.size(), 7); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 1530 | |
| 1531 | // Non-recursive operation. |
yuzo@chromium.org | 2da0f82 | 2009-06-09 14:57:38 +0900 | [diff] [blame] | 1532 | file_util::FileEnumerator f4(test_dir_, false, FILES_AND_DIRECTORIES); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 1533 | FindResultCollector c4(f4); |
erikkay@google.com | dfb51b2 | 2008-08-16 02:32:10 +0900 | [diff] [blame] | 1534 | EXPECT_TRUE(c4.HasFile(dir2)); |
| 1535 | EXPECT_TRUE(c4.HasFile(dir2)); |
| 1536 | EXPECT_TRUE(c4.HasFile(file1)); |
| 1537 | EXPECT_TRUE(c4.HasFile(file2_abs)); |
| 1538 | EXPECT_EQ(c4.size(), 4); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 1539 | |
| 1540 | // Enumerate with a pattern. |
yuzo@chromium.org | 2da0f82 | 2009-06-09 14:57:38 +0900 | [diff] [blame] | 1541 | file_util::FileEnumerator f5(test_dir_, true, FILES_AND_DIRECTORIES, |
avi@google.com | 5cb7935 | 2008-12-11 23:55:12 +0900 | [diff] [blame] | 1542 | FILE_PATH_LITERAL("dir*")); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 1543 | FindResultCollector c5(f5); |
erikkay@google.com | dfb51b2 | 2008-08-16 02:32:10 +0900 | [diff] [blame] | 1544 | EXPECT_TRUE(c5.HasFile(dir1)); |
| 1545 | EXPECT_TRUE(c5.HasFile(dir2)); |
| 1546 | EXPECT_TRUE(c5.HasFile(dir2file)); |
| 1547 | EXPECT_TRUE(c5.HasFile(dir2inner)); |
| 1548 | EXPECT_TRUE(c5.HasFile(dir2innerfile)); |
| 1549 | EXPECT_EQ(c5.size(), 5); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 1550 | |
| 1551 | // Make sure the destructor closes the find handle while in the middle of a |
| 1552 | // query to allow TearDown to delete the directory. |
yuzo@chromium.org | 2da0f82 | 2009-06-09 14:57:38 +0900 | [diff] [blame] | 1553 | file_util::FileEnumerator f6(test_dir_, true, FILES_AND_DIRECTORIES); |
avi@google.com | 5cb7935 | 2008-12-11 23:55:12 +0900 | [diff] [blame] | 1554 | EXPECT_FALSE(f6.Next().value().empty()); // Should have found something |
| 1555 | // (we don't care what). |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 1556 | } |
license.bot | f003cfe | 2008-08-24 09:55:55 +0900 | [diff] [blame] | 1557 | |
aa@chromium.org | a4dbdf2 | 2009-01-10 07:14:27 +0900 | [diff] [blame] | 1558 | TEST_F(FileUtilTest, Contains) { |
thestig@chromium.org | 4cfbf7a | 2009-03-11 03:20:44 +0900 | [diff] [blame] | 1559 | FilePath data_dir = test_dir_.Append(FILE_PATH_LITERAL("FilePathTest")); |
aa@chromium.org | a4dbdf2 | 2009-01-10 07:14:27 +0900 | [diff] [blame] | 1560 | |
| 1561 | // Create a fresh, empty copy of this directory. |
rvargas@google.com | 5a0ae3b | 2009-01-31 10:19:57 +0900 | [diff] [blame] | 1562 | if (file_util::PathExists(data_dir)) { |
| 1563 | ASSERT_TRUE(file_util::Delete(data_dir, true)); |
| 1564 | } |
aa@chromium.org | a4dbdf2 | 2009-01-10 07:14:27 +0900 | [diff] [blame] | 1565 | ASSERT_TRUE(file_util::CreateDirectory(data_dir)); |
| 1566 | |
| 1567 | FilePath foo(data_dir.Append(FILE_PATH_LITERAL("foo"))); |
| 1568 | FilePath bar(foo.Append(FILE_PATH_LITERAL("bar.txt"))); |
| 1569 | FilePath baz(data_dir.Append(FILE_PATH_LITERAL("baz.txt"))); |
| 1570 | FilePath foobar(data_dir.Append(FILE_PATH_LITERAL("foobar.txt"))); |
| 1571 | |
| 1572 | // Annoyingly, the directories must actually exist in order for realpath(), |
| 1573 | // which Contains() relies on in posix, to work. |
| 1574 | ASSERT_TRUE(file_util::CreateDirectory(foo)); |
| 1575 | std::string data("hello"); |
phajdan.jr@chromium.org | f9908a7 | 2009-04-04 02:17:58 +0900 | [diff] [blame] | 1576 | ASSERT_TRUE(file_util::WriteFile(bar, data.c_str(), data.length())); |
| 1577 | ASSERT_TRUE(file_util::WriteFile(baz, data.c_str(), data.length())); |
| 1578 | ASSERT_TRUE(file_util::WriteFile(foobar, data.c_str(), data.length())); |
aa@chromium.org | a4dbdf2 | 2009-01-10 07:14:27 +0900 | [diff] [blame] | 1579 | |
| 1580 | EXPECT_TRUE(file_util::ContainsPath(foo, bar)); |
| 1581 | EXPECT_FALSE(file_util::ContainsPath(foo, baz)); |
| 1582 | EXPECT_FALSE(file_util::ContainsPath(foo, foobar)); |
| 1583 | EXPECT_FALSE(file_util::ContainsPath(foo, foo)); |
| 1584 | |
evan@chromium.org | 875bb6e | 2009-12-29 09:32:52 +0900 | [diff] [blame] | 1585 | // Platform-specific concerns. |
aa@chromium.org | a4dbdf2 | 2009-01-10 07:14:27 +0900 | [diff] [blame] | 1586 | FilePath foo_caps(data_dir.Append(FILE_PATH_LITERAL("FOO"))); |
| 1587 | #if defined(OS_WIN) |
| 1588 | EXPECT_TRUE(file_util::ContainsPath(foo, |
| 1589 | foo_caps.Append(FILE_PATH_LITERAL("bar.txt")))); |
jrg@chromium.org | d505c3a | 2009-02-04 09:58:39 +0900 | [diff] [blame] | 1590 | EXPECT_TRUE(file_util::ContainsPath(foo, |
aa@chromium.org | a4dbdf2 | 2009-01-10 07:14:27 +0900 | [diff] [blame] | 1591 | FilePath(foo.value() + FILE_PATH_LITERAL("/bar.txt")))); |
evan@chromium.org | 875bb6e | 2009-12-29 09:32:52 +0900 | [diff] [blame] | 1592 | #elif defined(OS_MACOSX) |
| 1593 | // We can't really do this test on OS X since the case-sensitivity of the |
| 1594 | // filesystem is configurable. |
| 1595 | #elif defined(OS_POSIX) |
aa@chromium.org | a4dbdf2 | 2009-01-10 07:14:27 +0900 | [diff] [blame] | 1596 | EXPECT_FALSE(file_util::ContainsPath(foo, |
| 1597 | foo_caps.Append(FILE_PATH_LITERAL("bar.txt")))); |
aa@chromium.org | a4dbdf2 | 2009-01-10 07:14:27 +0900 | [diff] [blame] | 1598 | #endif |
| 1599 | } |
| 1600 | |
jochen@chromium.org | a687977 | 2010-02-18 19:02:26 +0900 | [diff] [blame] | 1601 | TEST_F(FileUtilTest, LastModified) { |
| 1602 | FilePath data_dir = test_dir_.Append(FILE_PATH_LITERAL("FilePathTest")); |
| 1603 | |
| 1604 | // Create a fresh, empty copy of this directory. |
| 1605 | if (file_util::PathExists(data_dir)) { |
| 1606 | ASSERT_TRUE(file_util::Delete(data_dir, true)); |
| 1607 | } |
| 1608 | ASSERT_TRUE(file_util::CreateDirectory(data_dir)); |
| 1609 | |
| 1610 | FilePath foobar(data_dir.Append(FILE_PATH_LITERAL("foobar.txt"))); |
| 1611 | std::string data("hello"); |
| 1612 | ASSERT_TRUE(file_util::WriteFile(foobar, data.c_str(), data.length())); |
| 1613 | |
| 1614 | base::Time modification_time; |
| 1615 | // Note that this timestamp is divisible by two (seconds) - FAT stores |
| 1616 | // modification times with 2s resolution. |
| 1617 | ASSERT_TRUE(base::Time::FromString(L"Tue, 15 Nov 1994, 12:45:26 GMT", |
| 1618 | &modification_time)); |
| 1619 | ASSERT_TRUE(file_util::SetLastModifiedTime(foobar, modification_time)); |
| 1620 | file_util::FileInfo file_info; |
| 1621 | ASSERT_TRUE(file_util::GetFileInfo(foobar, &file_info)); |
| 1622 | ASSERT_TRUE(file_info.last_modified == modification_time); |
| 1623 | } |
| 1624 | |
mark@chromium.org | 1768480 | 2008-09-10 09:16:28 +0900 | [diff] [blame] | 1625 | } // namespace |