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 | afd8dd4 | 2010-05-07 06:56:40 +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 | |
thestig@chromium.org | afd8dd4 | 2010-05-07 06:56:40 +0900 | [diff] [blame^] | 390 | TEST_F(FileUtilTest, DeleteNonExistent) { |
| 391 | FilePath non_existent = test_dir_.AppendASCII("bogus_file_dne.foobar"); |
| 392 | ASSERT_FALSE(file_util::PathExists(non_existent)); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 393 | |
thestig@chromium.org | afd8dd4 | 2010-05-07 06:56:40 +0900 | [diff] [blame^] | 394 | EXPECT_TRUE(file_util::Delete(non_existent, false)); |
| 395 | ASSERT_FALSE(file_util::PathExists(non_existent)); |
| 396 | EXPECT_TRUE(file_util::Delete(non_existent, true)); |
| 397 | ASSERT_FALSE(file_util::PathExists(non_existent)); |
| 398 | } |
| 399 | |
| 400 | TEST_F(FileUtilTest, DeleteFile) { |
| 401 | // Create a file |
| 402 | FilePath file_name = test_dir_.Append(FPL("Test DeleteFile 1.txt")); |
| 403 | CreateTextFile(file_name, bogus_content); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 404 | ASSERT_TRUE(file_util::PathExists(file_name)); |
| 405 | |
thestig@chromium.org | afd8dd4 | 2010-05-07 06:56:40 +0900 | [diff] [blame^] | 406 | // Make sure it's deleted |
| 407 | EXPECT_TRUE(file_util::Delete(file_name, false)); |
| 408 | EXPECT_FALSE(file_util::PathExists(file_name)); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 409 | |
thestig@chromium.org | afd8dd4 | 2010-05-07 06:56:40 +0900 | [diff] [blame^] | 410 | // Test recursive case, create a new file |
| 411 | file_name = test_dir_.Append(FPL("Test DeleteFile 2.txt")); |
| 412 | CreateTextFile(file_name, bogus_content); |
| 413 | ASSERT_TRUE(file_util::PathExists(file_name)); |
| 414 | |
| 415 | // Make sure it's deleted |
| 416 | EXPECT_TRUE(file_util::Delete(file_name, true)); |
| 417 | EXPECT_FALSE(file_util::PathExists(file_name)); |
| 418 | } |
| 419 | |
| 420 | #if defined(OS_WIN) |
| 421 | // Tests that the Delete function works for wild cards, especially |
| 422 | // with the recursion flag. Also coincidentally tests PathExists. |
| 423 | // TODO(erikkay): see if anyone's actually using this feature of the API |
| 424 | TEST_F(FileUtilTest, DeleteWildCard) { |
| 425 | // Create a file and a directory |
| 426 | FilePath file_name = test_dir_.Append(FPL("Test DeleteWildCard.txt")); |
| 427 | CreateTextFile(file_name, bogus_content); |
| 428 | ASSERT_TRUE(file_util::PathExists(file_name)); |
| 429 | |
| 430 | FilePath subdir_path = test_dir_.Append(FPL("DeleteWildCardDir")); |
| 431 | file_util::CreateDirectory(subdir_path); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 432 | ASSERT_TRUE(file_util::PathExists(subdir_path)); |
| 433 | |
thestig@chromium.org | afd8dd4 | 2010-05-07 06:56:40 +0900 | [diff] [blame^] | 434 | // Create the wildcard path |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 435 | FilePath directory_contents = test_dir_; |
thestig@chromium.org | afd8dd4 | 2010-05-07 06:56:40 +0900 | [diff] [blame^] | 436 | directory_contents = directory_contents.Append(FPL("*")); |
| 437 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 438 | // Delete non-recursively and check that only the file is deleted |
thestig@chromium.org | afd8dd4 | 2010-05-07 06:56:40 +0900 | [diff] [blame^] | 439 | EXPECT_TRUE(file_util::Delete(directory_contents, false)); |
erikkay@google.com | dfb51b2 | 2008-08-16 02:32:10 +0900 | [diff] [blame] | 440 | EXPECT_FALSE(file_util::PathExists(file_name)); |
| 441 | EXPECT_TRUE(file_util::PathExists(subdir_path)); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 442 | |
| 443 | // Delete recursively and make sure all contents are deleted |
thestig@chromium.org | afd8dd4 | 2010-05-07 06:56:40 +0900 | [diff] [blame^] | 444 | EXPECT_TRUE(file_util::Delete(directory_contents, true)); |
erikkay@google.com | dfb51b2 | 2008-08-16 02:32:10 +0900 | [diff] [blame] | 445 | EXPECT_FALSE(file_util::PathExists(file_name)); |
| 446 | EXPECT_FALSE(file_util::PathExists(subdir_path)); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 447 | } |
| 448 | |
thestig@chromium.org | afd8dd4 | 2010-05-07 06:56:40 +0900 | [diff] [blame^] | 449 | // TODO(erikkay): see if anyone's actually using this feature of the API |
| 450 | TEST_F(FileUtilTest, DeleteNonExistantWildCard) { |
| 451 | // Create a file and a directory |
| 452 | FilePath subdir_path = test_dir_.Append(FPL("DeleteNonExistantWildCard")); |
| 453 | file_util::CreateDirectory(subdir_path); |
| 454 | ASSERT_TRUE(file_util::PathExists(subdir_path)); |
| 455 | |
| 456 | // Create the wildcard path |
| 457 | FilePath directory_contents = subdir_path; |
| 458 | directory_contents = directory_contents.Append(FPL("*")); |
| 459 | |
| 460 | // Delete non-recursively and check nothing got deleted |
| 461 | EXPECT_TRUE(file_util::Delete(directory_contents, false)); |
| 462 | EXPECT_TRUE(file_util::PathExists(subdir_path)); |
| 463 | |
| 464 | // Delete recursively and check nothing got deleted |
| 465 | EXPECT_TRUE(file_util::Delete(directory_contents, true)); |
| 466 | EXPECT_TRUE(file_util::PathExists(subdir_path)); |
| 467 | } |
| 468 | #endif |
| 469 | |
| 470 | // Tests non-recursive Delete() for a directory. |
| 471 | TEST_F(FileUtilTest, DeleteDirNonRecursive) { |
| 472 | // Create a subdirectory and put a file and two directories inside. |
| 473 | FilePath test_subdir = test_dir_.Append(FPL("DeleteDirNonRecursive")); |
| 474 | file_util::CreateDirectory(test_subdir); |
| 475 | ASSERT_TRUE(file_util::PathExists(test_subdir)); |
| 476 | |
| 477 | FilePath file_name = test_subdir.Append(FPL("Test DeleteDir.txt")); |
| 478 | CreateTextFile(file_name, bogus_content); |
| 479 | ASSERT_TRUE(file_util::PathExists(file_name)); |
| 480 | |
| 481 | FilePath subdir_path1 = test_subdir.Append(FPL("TestSubDir1")); |
| 482 | file_util::CreateDirectory(subdir_path1); |
| 483 | ASSERT_TRUE(file_util::PathExists(subdir_path1)); |
| 484 | |
| 485 | FilePath subdir_path2 = test_subdir.Append(FPL("TestSubDir2")); |
| 486 | file_util::CreateDirectory(subdir_path2); |
| 487 | ASSERT_TRUE(file_util::PathExists(subdir_path2)); |
| 488 | |
| 489 | // Delete non-recursively and check that the empty dir got deleted |
| 490 | EXPECT_TRUE(file_util::Delete(subdir_path2, false)); |
| 491 | EXPECT_FALSE(file_util::PathExists(subdir_path2)); |
| 492 | |
| 493 | // Delete non-recursively and check that nothing got deleted |
| 494 | EXPECT_FALSE(file_util::Delete(test_subdir, false)); |
| 495 | EXPECT_TRUE(file_util::PathExists(test_subdir)); |
| 496 | EXPECT_TRUE(file_util::PathExists(file_name)); |
| 497 | EXPECT_TRUE(file_util::PathExists(subdir_path1)); |
| 498 | } |
| 499 | |
| 500 | // Tests recursive Delete() for a directory. |
| 501 | TEST_F(FileUtilTest, DeleteDirRecursive) { |
| 502 | // Create a subdirectory and put a file and two directories inside. |
| 503 | FilePath test_subdir = test_dir_.Append(FPL("DeleteDirRecursive")); |
| 504 | file_util::CreateDirectory(test_subdir); |
| 505 | ASSERT_TRUE(file_util::PathExists(test_subdir)); |
| 506 | |
| 507 | FilePath file_name = test_subdir.Append(FPL("Test DeleteDirRecursive.txt")); |
| 508 | CreateTextFile(file_name, bogus_content); |
| 509 | ASSERT_TRUE(file_util::PathExists(file_name)); |
| 510 | |
| 511 | FilePath subdir_path1 = test_subdir.Append(FPL("TestSubDir1")); |
| 512 | file_util::CreateDirectory(subdir_path1); |
| 513 | ASSERT_TRUE(file_util::PathExists(subdir_path1)); |
| 514 | |
| 515 | FilePath subdir_path2 = test_subdir.Append(FPL("TestSubDir2")); |
| 516 | file_util::CreateDirectory(subdir_path2); |
| 517 | ASSERT_TRUE(file_util::PathExists(subdir_path2)); |
| 518 | |
| 519 | // Delete recursively and check that the empty dir got deleted |
| 520 | EXPECT_TRUE(file_util::Delete(subdir_path2, true)); |
| 521 | EXPECT_FALSE(file_util::PathExists(subdir_path2)); |
| 522 | |
| 523 | // Delete recursively and check that everything got deleted |
| 524 | EXPECT_TRUE(file_util::Delete(test_subdir, true)); |
| 525 | EXPECT_FALSE(file_util::PathExists(file_name)); |
| 526 | EXPECT_FALSE(file_util::PathExists(subdir_path1)); |
| 527 | EXPECT_FALSE(file_util::PathExists(test_subdir)); |
| 528 | } |
| 529 | |
vandebo@chromium.org | c0cf77e | 2009-10-15 10:11:44 +0900 | [diff] [blame] | 530 | TEST_F(FileUtilTest, MoveFileNew) { |
| 531 | // Create a file |
| 532 | FilePath file_name_from = |
| 533 | test_dir_.Append(FILE_PATH_LITERAL("Move_Test_File.txt")); |
| 534 | CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle"); |
| 535 | ASSERT_TRUE(file_util::PathExists(file_name_from)); |
| 536 | |
| 537 | // The destination |
| 538 | FilePath file_name_to = |
| 539 | test_dir_.Append(FILE_PATH_LITERAL("Move_Test_File_Destination.txt")); |
| 540 | ASSERT_FALSE(file_util::PathExists(file_name_to)); |
| 541 | |
| 542 | EXPECT_TRUE(file_util::Move(file_name_from, file_name_to)); |
| 543 | |
| 544 | // Check everything has been moved. |
| 545 | EXPECT_FALSE(file_util::PathExists(file_name_from)); |
| 546 | EXPECT_TRUE(file_util::PathExists(file_name_to)); |
| 547 | } |
| 548 | |
| 549 | TEST_F(FileUtilTest, MoveFileExists) { |
| 550 | // Create a file |
| 551 | FilePath file_name_from = |
| 552 | test_dir_.Append(FILE_PATH_LITERAL("Move_Test_File.txt")); |
| 553 | CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle"); |
| 554 | ASSERT_TRUE(file_util::PathExists(file_name_from)); |
| 555 | |
| 556 | // The destination name |
| 557 | FilePath file_name_to = |
| 558 | test_dir_.Append(FILE_PATH_LITERAL("Move_Test_File_Destination.txt")); |
| 559 | CreateTextFile(file_name_to, L"Old file content"); |
| 560 | ASSERT_TRUE(file_util::PathExists(file_name_to)); |
| 561 | |
| 562 | EXPECT_TRUE(file_util::Move(file_name_from, file_name_to)); |
| 563 | |
| 564 | // Check everything has been moved. |
| 565 | EXPECT_FALSE(file_util::PathExists(file_name_from)); |
| 566 | EXPECT_TRUE(file_util::PathExists(file_name_to)); |
| 567 | EXPECT_TRUE(L"Gooooooooooooooooooooogle" == ReadTextFile(file_name_to)); |
| 568 | } |
| 569 | |
| 570 | TEST_F(FileUtilTest, MoveFileDirExists) { |
| 571 | // Create a file |
| 572 | FilePath file_name_from = |
| 573 | test_dir_.Append(FILE_PATH_LITERAL("Move_Test_File.txt")); |
| 574 | CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle"); |
| 575 | ASSERT_TRUE(file_util::PathExists(file_name_from)); |
| 576 | |
| 577 | // The destination directory |
| 578 | FilePath dir_name_to = |
| 579 | test_dir_.Append(FILE_PATH_LITERAL("Destination")); |
| 580 | file_util::CreateDirectory(dir_name_to); |
| 581 | ASSERT_TRUE(file_util::PathExists(dir_name_to)); |
| 582 | |
| 583 | EXPECT_FALSE(file_util::Move(file_name_from, dir_name_to)); |
| 584 | } |
| 585 | |
| 586 | |
vandebo@chromium.org | 70cf3f1 | 2009-10-14 02:57:27 +0900 | [diff] [blame] | 587 | TEST_F(FileUtilTest, MoveNew) { |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 588 | // Create a directory |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 589 | FilePath dir_name_from = |
| 590 | test_dir_.Append(FILE_PATH_LITERAL("Move_From_Subdir")); |
| 591 | file_util::CreateDirectory(dir_name_from); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 592 | ASSERT_TRUE(file_util::PathExists(dir_name_from)); |
| 593 | |
| 594 | // Create a file under the directory |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 595 | FilePath file_name_from = |
| 596 | dir_name_from.Append(FILE_PATH_LITERAL("Move_Test_File.txt")); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 597 | CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle"); |
| 598 | ASSERT_TRUE(file_util::PathExists(file_name_from)); |
| 599 | |
| 600 | // Move the directory |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 601 | FilePath dir_name_to = test_dir_.Append(FILE_PATH_LITERAL("Move_To_Subdir")); |
| 602 | FilePath file_name_to = |
| 603 | dir_name_to.Append(FILE_PATH_LITERAL("Move_Test_File.txt")); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 604 | |
| 605 | ASSERT_FALSE(file_util::PathExists(dir_name_to)); |
| 606 | |
| 607 | EXPECT_TRUE(file_util::Move(dir_name_from, dir_name_to)); |
| 608 | |
| 609 | // Check everything has been moved. |
| 610 | EXPECT_FALSE(file_util::PathExists(dir_name_from)); |
| 611 | EXPECT_FALSE(file_util::PathExists(file_name_from)); |
| 612 | EXPECT_TRUE(file_util::PathExists(dir_name_to)); |
| 613 | EXPECT_TRUE(file_util::PathExists(file_name_to)); |
| 614 | } |
| 615 | |
vandebo@chromium.org | 70cf3f1 | 2009-10-14 02:57:27 +0900 | [diff] [blame] | 616 | TEST_F(FileUtilTest, MoveExist) { |
| 617 | // Create a directory |
| 618 | FilePath dir_name_from = |
| 619 | test_dir_.Append(FILE_PATH_LITERAL("Move_From_Subdir")); |
| 620 | file_util::CreateDirectory(dir_name_from); |
| 621 | ASSERT_TRUE(file_util::PathExists(dir_name_from)); |
| 622 | |
| 623 | // Create a file under the directory |
| 624 | FilePath file_name_from = |
| 625 | dir_name_from.Append(FILE_PATH_LITERAL("Move_Test_File.txt")); |
| 626 | CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle"); |
| 627 | ASSERT_TRUE(file_util::PathExists(file_name_from)); |
| 628 | |
| 629 | // Move the directory |
| 630 | FilePath dir_name_exists = |
| 631 | test_dir_.Append(FILE_PATH_LITERAL("Destination")); |
| 632 | |
| 633 | FilePath dir_name_to = |
| 634 | dir_name_exists.Append(FILE_PATH_LITERAL("Move_To_Subdir")); |
| 635 | FilePath file_name_to = |
| 636 | dir_name_to.Append(FILE_PATH_LITERAL("Move_Test_File.txt")); |
| 637 | |
| 638 | // Create the destination directory. |
| 639 | file_util::CreateDirectory(dir_name_exists); |
| 640 | ASSERT_TRUE(file_util::PathExists(dir_name_exists)); |
| 641 | |
| 642 | EXPECT_TRUE(file_util::Move(dir_name_from, dir_name_to)); |
| 643 | |
| 644 | // Check everything has been moved. |
| 645 | EXPECT_FALSE(file_util::PathExists(dir_name_from)); |
| 646 | EXPECT_FALSE(file_util::PathExists(file_name_from)); |
| 647 | EXPECT_TRUE(file_util::PathExists(dir_name_to)); |
| 648 | EXPECT_TRUE(file_util::PathExists(file_name_to)); |
| 649 | } |
| 650 | |
| 651 | TEST_F(FileUtilTest, CopyDirectoryRecursivelyNew) { |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 652 | // Create a directory. |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 653 | FilePath dir_name_from = |
| 654 | test_dir_.Append(FILE_PATH_LITERAL("Copy_From_Subdir")); |
| 655 | file_util::CreateDirectory(dir_name_from); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 656 | ASSERT_TRUE(file_util::PathExists(dir_name_from)); |
| 657 | |
| 658 | // Create a file under the directory. |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 659 | FilePath file_name_from = |
| 660 | dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt")); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 661 | CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle"); |
| 662 | ASSERT_TRUE(file_util::PathExists(file_name_from)); |
| 663 | |
| 664 | // Create a subdirectory. |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 665 | FilePath subdir_name_from = |
| 666 | dir_name_from.Append(FILE_PATH_LITERAL("Subdir")); |
| 667 | file_util::CreateDirectory(subdir_name_from); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 668 | ASSERT_TRUE(file_util::PathExists(subdir_name_from)); |
| 669 | |
| 670 | // Create a file under the subdirectory. |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 671 | FilePath file_name2_from = |
| 672 | subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt")); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 673 | CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle"); |
| 674 | ASSERT_TRUE(file_util::PathExists(file_name2_from)); |
| 675 | |
| 676 | // Copy the directory recursively. |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 677 | FilePath dir_name_to = |
| 678 | test_dir_.Append(FILE_PATH_LITERAL("Copy_To_Subdir")); |
| 679 | FilePath file_name_to = |
| 680 | dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt")); |
| 681 | FilePath subdir_name_to = |
| 682 | dir_name_to.Append(FILE_PATH_LITERAL("Subdir")); |
| 683 | FilePath file_name2_to = |
| 684 | subdir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt")); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 685 | |
| 686 | ASSERT_FALSE(file_util::PathExists(dir_name_to)); |
| 687 | |
| 688 | EXPECT_TRUE(file_util::CopyDirectory(dir_name_from, dir_name_to, true)); |
| 689 | |
| 690 | // Check everything has been copied. |
| 691 | EXPECT_TRUE(file_util::PathExists(dir_name_from)); |
| 692 | EXPECT_TRUE(file_util::PathExists(file_name_from)); |
| 693 | EXPECT_TRUE(file_util::PathExists(subdir_name_from)); |
| 694 | EXPECT_TRUE(file_util::PathExists(file_name2_from)); |
| 695 | EXPECT_TRUE(file_util::PathExists(dir_name_to)); |
| 696 | EXPECT_TRUE(file_util::PathExists(file_name_to)); |
| 697 | EXPECT_TRUE(file_util::PathExists(subdir_name_to)); |
| 698 | EXPECT_TRUE(file_util::PathExists(file_name2_to)); |
| 699 | } |
| 700 | |
vandebo@chromium.org | 70cf3f1 | 2009-10-14 02:57:27 +0900 | [diff] [blame] | 701 | TEST_F(FileUtilTest, CopyDirectoryRecursivelyExists) { |
| 702 | // Create a directory. |
| 703 | FilePath dir_name_from = |
| 704 | test_dir_.Append(FILE_PATH_LITERAL("Copy_From_Subdir")); |
| 705 | file_util::CreateDirectory(dir_name_from); |
| 706 | ASSERT_TRUE(file_util::PathExists(dir_name_from)); |
| 707 | |
| 708 | // Create a file under the directory. |
| 709 | FilePath file_name_from = |
| 710 | dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt")); |
| 711 | CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle"); |
| 712 | ASSERT_TRUE(file_util::PathExists(file_name_from)); |
| 713 | |
| 714 | // Create a subdirectory. |
| 715 | FilePath subdir_name_from = |
| 716 | dir_name_from.Append(FILE_PATH_LITERAL("Subdir")); |
| 717 | file_util::CreateDirectory(subdir_name_from); |
| 718 | ASSERT_TRUE(file_util::PathExists(subdir_name_from)); |
| 719 | |
| 720 | // Create a file under the subdirectory. |
| 721 | FilePath file_name2_from = |
| 722 | subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt")); |
| 723 | CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle"); |
| 724 | ASSERT_TRUE(file_util::PathExists(file_name2_from)); |
| 725 | |
| 726 | // Copy the directory recursively. |
| 727 | FilePath dir_name_exists = |
| 728 | test_dir_.Append(FILE_PATH_LITERAL("Destination")); |
| 729 | |
| 730 | FilePath dir_name_to = |
| 731 | dir_name_exists.Append(FILE_PATH_LITERAL("Copy_From_Subdir")); |
| 732 | FilePath file_name_to = |
| 733 | dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt")); |
| 734 | FilePath subdir_name_to = |
| 735 | dir_name_to.Append(FILE_PATH_LITERAL("Subdir")); |
| 736 | FilePath file_name2_to = |
| 737 | subdir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt")); |
| 738 | |
| 739 | // Create the destination directory. |
| 740 | file_util::CreateDirectory(dir_name_exists); |
| 741 | ASSERT_TRUE(file_util::PathExists(dir_name_exists)); |
| 742 | |
| 743 | EXPECT_TRUE(file_util::CopyDirectory(dir_name_from, dir_name_exists, true)); |
| 744 | |
| 745 | // Check everything has been copied. |
| 746 | EXPECT_TRUE(file_util::PathExists(dir_name_from)); |
| 747 | EXPECT_TRUE(file_util::PathExists(file_name_from)); |
| 748 | EXPECT_TRUE(file_util::PathExists(subdir_name_from)); |
| 749 | EXPECT_TRUE(file_util::PathExists(file_name2_from)); |
| 750 | EXPECT_TRUE(file_util::PathExists(dir_name_to)); |
| 751 | EXPECT_TRUE(file_util::PathExists(file_name_to)); |
| 752 | EXPECT_TRUE(file_util::PathExists(subdir_name_to)); |
| 753 | EXPECT_TRUE(file_util::PathExists(file_name2_to)); |
| 754 | } |
| 755 | |
| 756 | TEST_F(FileUtilTest, CopyDirectoryNew) { |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 757 | // Create a directory. |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 758 | FilePath dir_name_from = |
| 759 | test_dir_.Append(FILE_PATH_LITERAL("Copy_From_Subdir")); |
| 760 | file_util::CreateDirectory(dir_name_from); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 761 | ASSERT_TRUE(file_util::PathExists(dir_name_from)); |
| 762 | |
| 763 | // Create a file under the directory. |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 764 | FilePath file_name_from = |
| 765 | dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt")); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 766 | CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle"); |
| 767 | ASSERT_TRUE(file_util::PathExists(file_name_from)); |
| 768 | |
| 769 | // Create a subdirectory. |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 770 | FilePath subdir_name_from = |
| 771 | dir_name_from.Append(FILE_PATH_LITERAL("Subdir")); |
| 772 | file_util::CreateDirectory(subdir_name_from); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 773 | ASSERT_TRUE(file_util::PathExists(subdir_name_from)); |
| 774 | |
| 775 | // Create a file under the subdirectory. |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 776 | FilePath file_name2_from = |
| 777 | subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt")); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 778 | CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle"); |
| 779 | ASSERT_TRUE(file_util::PathExists(file_name2_from)); |
| 780 | |
| 781 | // Copy the directory not recursively. |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 782 | FilePath dir_name_to = |
| 783 | test_dir_.Append(FILE_PATH_LITERAL("Copy_To_Subdir")); |
| 784 | FilePath file_name_to = |
| 785 | dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt")); |
| 786 | FilePath subdir_name_to = |
| 787 | dir_name_to.Append(FILE_PATH_LITERAL("Subdir")); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 788 | |
| 789 | ASSERT_FALSE(file_util::PathExists(dir_name_to)); |
| 790 | |
| 791 | EXPECT_TRUE(file_util::CopyDirectory(dir_name_from, dir_name_to, false)); |
| 792 | |
| 793 | // Check everything has been copied. |
| 794 | EXPECT_TRUE(file_util::PathExists(dir_name_from)); |
| 795 | EXPECT_TRUE(file_util::PathExists(file_name_from)); |
| 796 | EXPECT_TRUE(file_util::PathExists(subdir_name_from)); |
| 797 | EXPECT_TRUE(file_util::PathExists(file_name2_from)); |
| 798 | EXPECT_TRUE(file_util::PathExists(dir_name_to)); |
| 799 | EXPECT_TRUE(file_util::PathExists(file_name_to)); |
| 800 | EXPECT_FALSE(file_util::PathExists(subdir_name_to)); |
| 801 | } |
| 802 | |
vandebo@chromium.org | 70cf3f1 | 2009-10-14 02:57:27 +0900 | [diff] [blame] | 803 | TEST_F(FileUtilTest, CopyDirectoryExists) { |
| 804 | // Create a directory. |
| 805 | FilePath dir_name_from = |
| 806 | test_dir_.Append(FILE_PATH_LITERAL("Copy_From_Subdir")); |
| 807 | file_util::CreateDirectory(dir_name_from); |
| 808 | ASSERT_TRUE(file_util::PathExists(dir_name_from)); |
| 809 | |
| 810 | // Create a file under the directory. |
| 811 | FilePath file_name_from = |
| 812 | dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt")); |
| 813 | CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle"); |
| 814 | ASSERT_TRUE(file_util::PathExists(file_name_from)); |
| 815 | |
| 816 | // Create a subdirectory. |
| 817 | FilePath subdir_name_from = |
| 818 | dir_name_from.Append(FILE_PATH_LITERAL("Subdir")); |
| 819 | file_util::CreateDirectory(subdir_name_from); |
| 820 | ASSERT_TRUE(file_util::PathExists(subdir_name_from)); |
| 821 | |
| 822 | // Create a file under the subdirectory. |
| 823 | FilePath file_name2_from = |
| 824 | subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt")); |
| 825 | CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle"); |
| 826 | ASSERT_TRUE(file_util::PathExists(file_name2_from)); |
| 827 | |
| 828 | // Copy the directory not recursively. |
| 829 | FilePath dir_name_to = |
| 830 | test_dir_.Append(FILE_PATH_LITERAL("Copy_To_Subdir")); |
| 831 | FilePath file_name_to = |
| 832 | dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt")); |
| 833 | FilePath subdir_name_to = |
| 834 | dir_name_to.Append(FILE_PATH_LITERAL("Subdir")); |
| 835 | |
| 836 | // Create the destination directory. |
| 837 | file_util::CreateDirectory(dir_name_to); |
| 838 | ASSERT_TRUE(file_util::PathExists(dir_name_to)); |
| 839 | |
| 840 | EXPECT_TRUE(file_util::CopyDirectory(dir_name_from, dir_name_to, false)); |
| 841 | |
| 842 | // Check everything has been copied. |
| 843 | EXPECT_TRUE(file_util::PathExists(dir_name_from)); |
| 844 | EXPECT_TRUE(file_util::PathExists(file_name_from)); |
| 845 | EXPECT_TRUE(file_util::PathExists(subdir_name_from)); |
| 846 | EXPECT_TRUE(file_util::PathExists(file_name2_from)); |
| 847 | EXPECT_TRUE(file_util::PathExists(dir_name_to)); |
| 848 | EXPECT_TRUE(file_util::PathExists(file_name_to)); |
| 849 | EXPECT_FALSE(file_util::PathExists(subdir_name_to)); |
| 850 | } |
| 851 | |
vandebo@chromium.org | c0cf77e | 2009-10-15 10:11:44 +0900 | [diff] [blame] | 852 | TEST_F(FileUtilTest, CopyFileWithCopyDirectoryRecursiveToNew) { |
| 853 | // Create a file |
| 854 | FilePath file_name_from = |
| 855 | test_dir_.Append(FILE_PATH_LITERAL("Copy_Test_File.txt")); |
| 856 | CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle"); |
| 857 | ASSERT_TRUE(file_util::PathExists(file_name_from)); |
| 858 | |
| 859 | // The destination name |
| 860 | FilePath file_name_to = |
| 861 | test_dir_.Append(FILE_PATH_LITERAL("Copy_Test_File_Destination.txt")); |
| 862 | ASSERT_FALSE(file_util::PathExists(file_name_to)); |
| 863 | |
| 864 | EXPECT_TRUE(file_util::CopyDirectory(file_name_from, file_name_to, true)); |
| 865 | |
| 866 | // Check the has been copied |
| 867 | EXPECT_TRUE(file_util::PathExists(file_name_to)); |
| 868 | } |
| 869 | |
| 870 | TEST_F(FileUtilTest, CopyFileWithCopyDirectoryRecursiveToExisting) { |
| 871 | // Create a file |
| 872 | FilePath file_name_from = |
| 873 | test_dir_.Append(FILE_PATH_LITERAL("Copy_Test_File.txt")); |
| 874 | CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle"); |
| 875 | ASSERT_TRUE(file_util::PathExists(file_name_from)); |
| 876 | |
| 877 | // The destination name |
| 878 | FilePath file_name_to = |
| 879 | test_dir_.Append(FILE_PATH_LITERAL("Copy_Test_File_Destination.txt")); |
| 880 | CreateTextFile(file_name_to, L"Old file content"); |
| 881 | ASSERT_TRUE(file_util::PathExists(file_name_to)); |
| 882 | |
| 883 | EXPECT_TRUE(file_util::CopyDirectory(file_name_from, file_name_to, true)); |
| 884 | |
| 885 | // Check the has been copied |
| 886 | EXPECT_TRUE(file_util::PathExists(file_name_to)); |
| 887 | EXPECT_TRUE(L"Gooooooooooooooooooooogle" == ReadTextFile(file_name_to)); |
| 888 | } |
| 889 | |
| 890 | TEST_F(FileUtilTest, CopyFileWithCopyDirectoryRecursiveToExistingDirectory) { |
| 891 | // Create a file |
| 892 | FilePath file_name_from = |
| 893 | test_dir_.Append(FILE_PATH_LITERAL("Copy_Test_File.txt")); |
| 894 | CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle"); |
| 895 | ASSERT_TRUE(file_util::PathExists(file_name_from)); |
| 896 | |
| 897 | // The destination |
| 898 | FilePath dir_name_to = |
| 899 | test_dir_.Append(FILE_PATH_LITERAL("Destination")); |
| 900 | file_util::CreateDirectory(dir_name_to); |
| 901 | ASSERT_TRUE(file_util::PathExists(dir_name_to)); |
| 902 | FilePath file_name_to = |
| 903 | dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt")); |
| 904 | |
| 905 | EXPECT_TRUE(file_util::CopyDirectory(file_name_from, dir_name_to, true)); |
| 906 | |
| 907 | // Check the has been copied |
| 908 | EXPECT_TRUE(file_util::PathExists(file_name_to)); |
| 909 | } |
| 910 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 911 | TEST_F(FileUtilTest, CopyFile) { |
| 912 | // Create a directory |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 913 | FilePath dir_name_from = |
| 914 | test_dir_.Append(FILE_PATH_LITERAL("Copy_From_Subdir")); |
| 915 | file_util::CreateDirectory(dir_name_from); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 916 | ASSERT_TRUE(file_util::PathExists(dir_name_from)); |
| 917 | |
| 918 | // Create a file under the directory |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 919 | FilePath file_name_from = |
| 920 | dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt")); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 921 | const std::wstring file_contents(L"Gooooooooooooooooooooogle"); |
| 922 | CreateTextFile(file_name_from, file_contents); |
| 923 | ASSERT_TRUE(file_util::PathExists(file_name_from)); |
| 924 | |
| 925 | // Copy the file. |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 926 | FilePath dest_file = dir_name_from.Append(FILE_PATH_LITERAL("DestFile.txt")); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 927 | ASSERT_TRUE(file_util::CopyFile(file_name_from, dest_file)); |
mmoss@google.com | 733df6b | 2008-09-12 01:09:11 +0900 | [diff] [blame] | 928 | |
erikkay@google.com | dfb51b2 | 2008-08-16 02:32:10 +0900 | [diff] [blame] | 929 | // Copy the file to another location using '..' in the path. |
evan@chromium.org | 1543ad3 | 2009-08-27 05:00:14 +0900 | [diff] [blame] | 930 | FilePath dest_file2(dir_name_from); |
| 931 | dest_file2 = dest_file2.AppendASCII(".."); |
| 932 | dest_file2 = dest_file2.AppendASCII("DestFile.txt"); |
| 933 | ASSERT_TRUE(file_util::CopyFile(file_name_from, dest_file2)); |
| 934 | |
| 935 | FilePath dest_file2_test(dir_name_from); |
| 936 | dest_file2_test = dest_file2_test.DirName(); |
| 937 | dest_file2_test = dest_file2_test.AppendASCII("DestFile.txt"); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 938 | |
| 939 | // Check everything has been copied. |
| 940 | EXPECT_TRUE(file_util::PathExists(file_name_from)); |
| 941 | EXPECT_TRUE(file_util::PathExists(dest_file)); |
| 942 | const std::wstring read_contents = ReadTextFile(dest_file); |
| 943 | EXPECT_EQ(file_contents, read_contents); |
evan@chromium.org | 1543ad3 | 2009-08-27 05:00:14 +0900 | [diff] [blame] | 944 | EXPECT_TRUE(file_util::PathExists(dest_file2_test)); |
| 945 | EXPECT_TRUE(file_util::PathExists(dest_file2)); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 946 | } |
| 947 | |
erikkay@google.com | dfb51b2 | 2008-08-16 02:32:10 +0900 | [diff] [blame] | 948 | // TODO(erikkay): implement |
erikkay@google.com | 014161d | 2008-08-16 02:45:13 +0900 | [diff] [blame] | 949 | #if defined(OS_WIN) |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 950 | TEST_F(FileUtilTest, GetFileCreationLocalTime) { |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 951 | FilePath file_name = test_dir_.Append(L"Test File.txt"); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 952 | |
| 953 | SYSTEMTIME start_time; |
| 954 | GetLocalTime(&start_time); |
| 955 | Sleep(100); |
| 956 | CreateTextFile(file_name, L"New file!"); |
| 957 | Sleep(100); |
| 958 | SYSTEMTIME end_time; |
| 959 | GetLocalTime(&end_time); |
| 960 | |
| 961 | SYSTEMTIME file_creation_time; |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 962 | file_util::GetFileCreationLocalTime(file_name.value(), &file_creation_time); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 963 | |
| 964 | FILETIME start_filetime; |
| 965 | SystemTimeToFileTime(&start_time, &start_filetime); |
| 966 | FILETIME end_filetime; |
| 967 | SystemTimeToFileTime(&end_time, &end_filetime); |
| 968 | FILETIME file_creation_filetime; |
| 969 | SystemTimeToFileTime(&file_creation_time, &file_creation_filetime); |
| 970 | |
| 971 | EXPECT_EQ(-1, CompareFileTime(&start_filetime, &file_creation_filetime)) << |
| 972 | "start time: " << FileTimeAsUint64(start_filetime) << ", " << |
| 973 | "creation time: " << FileTimeAsUint64(file_creation_filetime); |
| 974 | |
| 975 | EXPECT_EQ(-1, CompareFileTime(&file_creation_filetime, &end_filetime)) << |
| 976 | "creation time: " << FileTimeAsUint64(file_creation_filetime) << ", " << |
| 977 | "end time: " << FileTimeAsUint64(end_filetime); |
| 978 | |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 979 | ASSERT_TRUE(DeleteFile(file_name.value().c_str())); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 980 | } |
erikkay@google.com | dfb51b2 | 2008-08-16 02:32:10 +0900 | [diff] [blame] | 981 | #endif |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 982 | |
erikkay@google.com | f240684 | 2008-08-21 00:59:49 +0900 | [diff] [blame] | 983 | // 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] | 984 | // to be a PlatformTest. |
erikkay@google.com | f240684 | 2008-08-21 00:59:49 +0900 | [diff] [blame] | 985 | typedef PlatformTest ReadOnlyFileUtilTest; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 986 | |
erikkay@google.com | f240684 | 2008-08-21 00:59:49 +0900 | [diff] [blame] | 987 | TEST_F(ReadOnlyFileUtilTest, ContentsEqual) { |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 988 | FilePath data_dir; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 989 | ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &data_dir)); |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 990 | data_dir = data_dir.Append(FILE_PATH_LITERAL("base")) |
| 991 | .Append(FILE_PATH_LITERAL("data")) |
| 992 | .Append(FILE_PATH_LITERAL("file_util_unittest")); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 993 | ASSERT_TRUE(file_util::PathExists(data_dir)); |
| 994 | |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 995 | FilePath original_file = |
| 996 | data_dir.Append(FILE_PATH_LITERAL("original.txt")); |
| 997 | FilePath same_file = |
| 998 | data_dir.Append(FILE_PATH_LITERAL("same.txt")); |
| 999 | FilePath same_length_file = |
| 1000 | data_dir.Append(FILE_PATH_LITERAL("same_length.txt")); |
| 1001 | FilePath different_file = |
| 1002 | data_dir.Append(FILE_PATH_LITERAL("different.txt")); |
| 1003 | FilePath different_first_file = |
| 1004 | data_dir.Append(FILE_PATH_LITERAL("different_first.txt")); |
| 1005 | FilePath different_last_file = |
| 1006 | data_dir.Append(FILE_PATH_LITERAL("different_last.txt")); |
| 1007 | FilePath empty1_file = |
| 1008 | data_dir.Append(FILE_PATH_LITERAL("empty1.txt")); |
| 1009 | FilePath empty2_file = |
| 1010 | data_dir.Append(FILE_PATH_LITERAL("empty2.txt")); |
| 1011 | FilePath shortened_file = |
| 1012 | data_dir.Append(FILE_PATH_LITERAL("shortened.txt")); |
| 1013 | FilePath binary_file = |
| 1014 | data_dir.Append(FILE_PATH_LITERAL("binary_file.bin")); |
| 1015 | FilePath binary_file_same = |
| 1016 | data_dir.Append(FILE_PATH_LITERAL("binary_file_same.bin")); |
| 1017 | FilePath binary_file_diff = |
| 1018 | data_dir.Append(FILE_PATH_LITERAL("binary_file_diff.bin")); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 1019 | |
| 1020 | EXPECT_TRUE(file_util::ContentsEqual(original_file, original_file)); |
| 1021 | EXPECT_TRUE(file_util::ContentsEqual(original_file, same_file)); |
| 1022 | EXPECT_FALSE(file_util::ContentsEqual(original_file, same_length_file)); |
| 1023 | EXPECT_FALSE(file_util::ContentsEqual(original_file, different_file)); |
thakis@chromium.org | 506f091 | 2009-12-02 07:14:22 +0900 | [diff] [blame] | 1024 | EXPECT_FALSE(file_util::ContentsEqual( |
| 1025 | FilePath(FILE_PATH_LITERAL("bogusname")), |
| 1026 | FilePath(FILE_PATH_LITERAL("bogusname")))); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 1027 | EXPECT_FALSE(file_util::ContentsEqual(original_file, different_first_file)); |
| 1028 | EXPECT_FALSE(file_util::ContentsEqual(original_file, different_last_file)); |
| 1029 | EXPECT_TRUE(file_util::ContentsEqual(empty1_file, empty2_file)); |
| 1030 | EXPECT_FALSE(file_util::ContentsEqual(original_file, shortened_file)); |
| 1031 | EXPECT_FALSE(file_util::ContentsEqual(shortened_file, original_file)); |
| 1032 | EXPECT_TRUE(file_util::ContentsEqual(binary_file, binary_file_same)); |
| 1033 | EXPECT_FALSE(file_util::ContentsEqual(binary_file, binary_file_diff)); |
| 1034 | } |
| 1035 | |
mark@chromium.org | 95c9ec9 | 2009-06-27 06:17:24 +0900 | [diff] [blame] | 1036 | TEST_F(ReadOnlyFileUtilTest, TextContentsEqual) { |
| 1037 | FilePath data_dir; |
| 1038 | ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &data_dir)); |
| 1039 | data_dir = data_dir.Append(FILE_PATH_LITERAL("base")) |
| 1040 | .Append(FILE_PATH_LITERAL("data")) |
| 1041 | .Append(FILE_PATH_LITERAL("file_util_unittest")); |
| 1042 | ASSERT_TRUE(file_util::PathExists(data_dir)); |
| 1043 | |
| 1044 | FilePath original_file = |
| 1045 | data_dir.Append(FILE_PATH_LITERAL("original.txt")); |
| 1046 | FilePath same_file = |
| 1047 | data_dir.Append(FILE_PATH_LITERAL("same.txt")); |
| 1048 | FilePath crlf_file = |
| 1049 | data_dir.Append(FILE_PATH_LITERAL("crlf.txt")); |
| 1050 | FilePath shortened_file = |
| 1051 | data_dir.Append(FILE_PATH_LITERAL("shortened.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 first1_file = |
| 1059 | data_dir.Append(FILE_PATH_LITERAL("first1.txt")); |
| 1060 | FilePath first2_file = |
| 1061 | data_dir.Append(FILE_PATH_LITERAL("first2.txt")); |
| 1062 | FilePath empty1_file = |
| 1063 | data_dir.Append(FILE_PATH_LITERAL("empty1.txt")); |
| 1064 | FilePath empty2_file = |
| 1065 | data_dir.Append(FILE_PATH_LITERAL("empty2.txt")); |
| 1066 | FilePath blank_line_file = |
| 1067 | data_dir.Append(FILE_PATH_LITERAL("blank_line.txt")); |
| 1068 | FilePath blank_line_crlf_file = |
| 1069 | data_dir.Append(FILE_PATH_LITERAL("blank_line_crlf.txt")); |
| 1070 | |
| 1071 | EXPECT_TRUE(file_util::TextContentsEqual(original_file, same_file)); |
| 1072 | EXPECT_TRUE(file_util::TextContentsEqual(original_file, crlf_file)); |
| 1073 | EXPECT_FALSE(file_util::TextContentsEqual(original_file, shortened_file)); |
| 1074 | EXPECT_FALSE(file_util::TextContentsEqual(original_file, different_file)); |
| 1075 | EXPECT_FALSE(file_util::TextContentsEqual(original_file, |
| 1076 | different_first_file)); |
| 1077 | EXPECT_FALSE(file_util::TextContentsEqual(original_file, |
| 1078 | different_last_file)); |
| 1079 | EXPECT_FALSE(file_util::TextContentsEqual(first1_file, first2_file)); |
| 1080 | EXPECT_TRUE(file_util::TextContentsEqual(empty1_file, empty2_file)); |
| 1081 | EXPECT_FALSE(file_util::TextContentsEqual(original_file, empty1_file)); |
| 1082 | EXPECT_TRUE(file_util::TextContentsEqual(blank_line_file, |
| 1083 | blank_line_crlf_file)); |
| 1084 | } |
| 1085 | |
erikkay@google.com | dfb51b2 | 2008-08-16 02:32:10 +0900 | [diff] [blame] | 1086 | // We don't need equivalent functionality outside of Windows. |
erikkay@google.com | 014161d | 2008-08-16 02:45:13 +0900 | [diff] [blame] | 1087 | #if defined(OS_WIN) |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 1088 | TEST_F(FileUtilTest, ResolveShortcutTest) { |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 1089 | FilePath target_file = test_dir_.Append(L"Target.txt"); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 1090 | CreateTextFile(target_file, L"This is the target."); |
| 1091 | |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 1092 | FilePath link_file = test_dir_.Append(L"Link.lnk"); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 1093 | |
| 1094 | HRESULT result; |
| 1095 | IShellLink *shell = NULL; |
| 1096 | IPersistFile *persist = NULL; |
| 1097 | |
| 1098 | CoInitialize(NULL); |
| 1099 | // Temporarily create a shortcut for test |
| 1100 | result = CoCreateInstance(CLSID_ShellLink, NULL, |
| 1101 | CLSCTX_INPROC_SERVER, IID_IShellLink, |
| 1102 | reinterpret_cast<LPVOID*>(&shell)); |
| 1103 | EXPECT_TRUE(SUCCEEDED(result)); |
| 1104 | result = shell->QueryInterface(IID_IPersistFile, |
| 1105 | reinterpret_cast<LPVOID*>(&persist)); |
| 1106 | EXPECT_TRUE(SUCCEEDED(result)); |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 1107 | result = shell->SetPath(target_file.value().c_str()); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 1108 | EXPECT_TRUE(SUCCEEDED(result)); |
| 1109 | result = shell->SetDescription(L"ResolveShortcutTest"); |
| 1110 | EXPECT_TRUE(SUCCEEDED(result)); |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 1111 | result = persist->Save(link_file.value().c_str(), TRUE); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 1112 | EXPECT_TRUE(SUCCEEDED(result)); |
| 1113 | if (persist) |
| 1114 | persist->Release(); |
| 1115 | if (shell) |
| 1116 | shell->Release(); |
| 1117 | |
| 1118 | bool is_solved; |
evan@chromium.org | a489904 | 2009-08-25 10:51:44 +0900 | [diff] [blame] | 1119 | is_solved = file_util::ResolveShortcut(&link_file); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 1120 | EXPECT_TRUE(is_solved); |
| 1121 | std::wstring contents; |
evan@chromium.org | a489904 | 2009-08-25 10:51:44 +0900 | [diff] [blame] | 1122 | contents = ReadTextFile(link_file); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 1123 | EXPECT_EQ(L"This is the target.", contents); |
| 1124 | |
ericroman@google.com | dbff4f5 | 2008-08-19 01:00:38 +0900 | [diff] [blame] | 1125 | // Cleaning |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 1126 | DeleteFile(target_file.value().c_str()); |
evan@chromium.org | a489904 | 2009-08-25 10:51:44 +0900 | [diff] [blame] | 1127 | DeleteFile(link_file.value().c_str()); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 1128 | CoUninitialize(); |
| 1129 | } |
| 1130 | |
| 1131 | TEST_F(FileUtilTest, CreateShortcutTest) { |
| 1132 | const wchar_t file_contents[] = L"This is another target."; |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 1133 | FilePath target_file = test_dir_.Append(L"Target1.txt"); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 1134 | CreateTextFile(target_file, file_contents); |
| 1135 | |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 1136 | FilePath link_file = test_dir_.Append(L"Link1.lnk"); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 1137 | |
| 1138 | CoInitialize(NULL); |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 1139 | EXPECT_TRUE(file_util::CreateShortcutLink(target_file.value().c_str(), |
| 1140 | link_file.value().c_str(), |
xiyuan@chromium.org | d9e9bb4 | 2009-11-19 18:18:50 +0900 | [diff] [blame] | 1141 | NULL, NULL, NULL, NULL, 0, NULL)); |
evan@chromium.org | a489904 | 2009-08-25 10:51:44 +0900 | [diff] [blame] | 1142 | FilePath resolved_name = link_file; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 1143 | EXPECT_TRUE(file_util::ResolveShortcut(&resolved_name)); |
evan@chromium.org | a489904 | 2009-08-25 10:51:44 +0900 | [diff] [blame] | 1144 | std::wstring read_contents = ReadTextFile(resolved_name); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 1145 | EXPECT_EQ(file_contents, read_contents); |
| 1146 | |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 1147 | DeleteFile(target_file.value().c_str()); |
| 1148 | DeleteFile(link_file.value().c_str()); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 1149 | CoUninitialize(); |
| 1150 | } |
huanr@chromium.org | 7f2c6af | 2009-03-12 03:37:48 +0900 | [diff] [blame] | 1151 | |
| 1152 | TEST_F(FileUtilTest, CopyAndDeleteDirectoryTest) { |
| 1153 | // Create a directory |
| 1154 | FilePath dir_name_from = |
| 1155 | test_dir_.Append(FILE_PATH_LITERAL("CopyAndDelete_From_Subdir")); |
| 1156 | file_util::CreateDirectory(dir_name_from); |
| 1157 | ASSERT_TRUE(file_util::PathExists(dir_name_from)); |
| 1158 | |
| 1159 | // Create a file under the directory |
| 1160 | FilePath file_name_from = |
| 1161 | dir_name_from.Append(FILE_PATH_LITERAL("CopyAndDelete_Test_File.txt")); |
| 1162 | CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle"); |
| 1163 | ASSERT_TRUE(file_util::PathExists(file_name_from)); |
| 1164 | |
| 1165 | // Move the directory by using CopyAndDeleteDirectory |
| 1166 | FilePath dir_name_to = test_dir_.Append( |
| 1167 | FILE_PATH_LITERAL("CopyAndDelete_To_Subdir")); |
| 1168 | FilePath file_name_to = |
| 1169 | dir_name_to.Append(FILE_PATH_LITERAL("CopyAndDelete_Test_File.txt")); |
| 1170 | |
| 1171 | ASSERT_FALSE(file_util::PathExists(dir_name_to)); |
| 1172 | |
| 1173 | EXPECT_TRUE(file_util::CopyAndDeleteDirectory(dir_name_from, dir_name_to)); |
| 1174 | |
| 1175 | // Check everything has been moved. |
| 1176 | EXPECT_FALSE(file_util::PathExists(dir_name_from)); |
| 1177 | EXPECT_FALSE(file_util::PathExists(file_name_from)); |
| 1178 | EXPECT_TRUE(file_util::PathExists(dir_name_to)); |
| 1179 | EXPECT_TRUE(file_util::PathExists(file_name_to)); |
| 1180 | } |
tkent@chromium.org | 8da1416 | 2009-10-09 16:33:39 +0900 | [diff] [blame] | 1181 | |
| 1182 | TEST_F(FileUtilTest, GetTempDirTest) { |
| 1183 | static const TCHAR* kTmpKey = _T("TMP"); |
| 1184 | static const TCHAR* kTmpValues[] = { |
| 1185 | _T(""), _T("C:"), _T("C:\\"), _T("C:\\tmp"), _T("C:\\tmp\\") |
| 1186 | }; |
| 1187 | // Save the original $TMP. |
| 1188 | size_t original_tmp_size; |
| 1189 | TCHAR* original_tmp; |
| 1190 | ASSERT_EQ(0, ::_tdupenv_s(&original_tmp, &original_tmp_size, kTmpKey)); |
| 1191 | // original_tmp may be NULL. |
| 1192 | |
| 1193 | for (unsigned int i = 0; i < arraysize(kTmpValues); ++i) { |
| 1194 | FilePath path; |
| 1195 | ::_tputenv_s(kTmpKey, kTmpValues[i]); |
| 1196 | file_util::GetTempDir(&path); |
| 1197 | EXPECT_TRUE(path.IsAbsolute()) << "$TMP=" << kTmpValues[i] << |
| 1198 | " result=" << path.value(); |
| 1199 | } |
| 1200 | |
| 1201 | // Restore the original $TMP. |
| 1202 | if (original_tmp) { |
| 1203 | ::_tputenv_s(kTmpKey, original_tmp); |
| 1204 | free(original_tmp); |
| 1205 | } else { |
| 1206 | ::_tputenv_s(kTmpKey, _T("")); |
| 1207 | } |
| 1208 | } |
| 1209 | #endif // OS_WIN |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 1210 | |
erikkay@chromium.org | 18f0dde | 2009-08-19 01:07:55 +0900 | [diff] [blame] | 1211 | TEST_F(FileUtilTest, CreateTemporaryFileTest) { |
| 1212 | FilePath temp_files[3]; |
jrg@chromium.org | d505c3a | 2009-02-04 09:58:39 +0900 | [diff] [blame] | 1213 | for (int i = 0; i < 3; i++) { |
erikkay@chromium.org | 18f0dde | 2009-08-19 01:07:55 +0900 | [diff] [blame] | 1214 | ASSERT_TRUE(file_util::CreateTemporaryFile(&(temp_files[i]))); |
jrg@chromium.org | d505c3a | 2009-02-04 09:58:39 +0900 | [diff] [blame] | 1215 | EXPECT_TRUE(file_util::PathExists(temp_files[i])); |
| 1216 | EXPECT_FALSE(file_util::DirectoryExists(temp_files[i])); |
| 1217 | } |
| 1218 | for (int i = 0; i < 3; i++) |
| 1219 | EXPECT_FALSE(temp_files[i] == temp_files[(i+1)%3]); |
| 1220 | for (int i = 0; i < 3; i++) |
| 1221 | EXPECT_TRUE(file_util::Delete(temp_files[i], false)); |
| 1222 | } |
| 1223 | |
erikkay@chromium.org | 18f0dde | 2009-08-19 01:07:55 +0900 | [diff] [blame] | 1224 | TEST_F(FileUtilTest, CreateAndOpenTemporaryFileTest) { |
jrg@chromium.org | d505c3a | 2009-02-04 09:58:39 +0900 | [diff] [blame] | 1225 | FilePath names[3]; |
| 1226 | FILE *fps[3]; |
| 1227 | int i; |
| 1228 | |
| 1229 | // Create; make sure they are open and exist. |
| 1230 | for (i = 0; i < 3; ++i) { |
| 1231 | fps[i] = file_util::CreateAndOpenTemporaryFile(&(names[i])); |
| 1232 | ASSERT_TRUE(fps[i]); |
| 1233 | EXPECT_TRUE(file_util::PathExists(names[i])); |
| 1234 | } |
| 1235 | |
| 1236 | // Make sure all names are unique. |
| 1237 | for (i = 0; i < 3; ++i) { |
| 1238 | EXPECT_FALSE(names[i] == names[(i+1)%3]); |
| 1239 | } |
| 1240 | |
| 1241 | // Close and delete. |
| 1242 | for (i = 0; i < 3; ++i) { |
| 1243 | EXPECT_TRUE(file_util::CloseFile(fps[i])); |
| 1244 | EXPECT_TRUE(file_util::Delete(names[i], false)); |
| 1245 | } |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 1246 | } |
| 1247 | |
| 1248 | TEST_F(FileUtilTest, CreateNewTempDirectoryTest) { |
evan@chromium.org | 1543ad3 | 2009-08-27 05:00:14 +0900 | [diff] [blame] | 1249 | FilePath temp_dir; |
| 1250 | ASSERT_TRUE(file_util::CreateNewTempDirectory(FilePath::StringType(), |
| 1251 | &temp_dir)); |
mmoss@google.com | 733df6b | 2008-09-12 01:09:11 +0900 | [diff] [blame] | 1252 | EXPECT_TRUE(file_util::PathExists(temp_dir)); |
| 1253 | EXPECT_TRUE(file_util::Delete(temp_dir, false)); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 1254 | } |
| 1255 | |
skerner@chromium.org | e443239 | 2010-05-01 02:00:09 +0900 | [diff] [blame] | 1256 | TEST_F(FileUtilTest, CreateNewTemporaryDirInDirTest) { |
| 1257 | FilePath new_dir; |
| 1258 | ASSERT_TRUE(file_util::CreateTemporaryDirInDir( |
| 1259 | test_dir_, |
| 1260 | FILE_PATH_LITERAL("CreateNewTemporaryDirInDirTest"), |
| 1261 | &new_dir)); |
| 1262 | EXPECT_TRUE(file_util::PathExists(new_dir)); |
| 1263 | EXPECT_TRUE(test_dir_.IsParent(new_dir)); |
| 1264 | EXPECT_TRUE(file_util::Delete(new_dir, false)); |
| 1265 | } |
| 1266 | |
jrg@chromium.org | d505c3a | 2009-02-04 09:58:39 +0900 | [diff] [blame] | 1267 | TEST_F(FileUtilTest, GetShmemTempDirTest) { |
| 1268 | FilePath dir; |
| 1269 | EXPECT_TRUE(file_util::GetShmemTempDir(&dir)); |
| 1270 | EXPECT_TRUE(file_util::DirectoryExists(dir)); |
| 1271 | } |
| 1272 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 1273 | TEST_F(FileUtilTest, CreateDirectoryTest) { |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 1274 | FilePath test_root = |
| 1275 | test_dir_.Append(FILE_PATH_LITERAL("create_directory_test")); |
erikkay@google.com | 014161d | 2008-08-16 02:45:13 +0900 | [diff] [blame] | 1276 | #if defined(OS_WIN) |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 1277 | FilePath test_path = |
| 1278 | test_root.Append(FILE_PATH_LITERAL("dir\\tree\\likely\\doesnt\\exist\\")); |
erikkay@google.com | dfb51b2 | 2008-08-16 02:32:10 +0900 | [diff] [blame] | 1279 | #elif defined(OS_POSIX) |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 1280 | FilePath test_path = |
| 1281 | test_root.Append(FILE_PATH_LITERAL("dir/tree/likely/doesnt/exist/")); |
erikkay@google.com | dfb51b2 | 2008-08-16 02:32:10 +0900 | [diff] [blame] | 1282 | #endif |
mmoss@google.com | 733df6b | 2008-09-12 01:09:11 +0900 | [diff] [blame] | 1283 | |
| 1284 | EXPECT_FALSE(file_util::PathExists(test_path)); |
| 1285 | EXPECT_TRUE(file_util::CreateDirectory(test_path)); |
| 1286 | EXPECT_TRUE(file_util::PathExists(test_path)); |
| 1287 | // CreateDirectory returns true if the DirectoryExists returns true. |
| 1288 | EXPECT_TRUE(file_util::CreateDirectory(test_path)); |
| 1289 | |
| 1290 | // 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] | 1291 | test_path = test_path.Append(FILE_PATH_LITERAL("foobar.txt")); |
mmoss@google.com | 733df6b | 2008-09-12 01:09:11 +0900 | [diff] [blame] | 1292 | EXPECT_FALSE(file_util::PathExists(test_path)); |
| 1293 | CreateTextFile(test_path, L"test file"); |
| 1294 | EXPECT_TRUE(file_util::PathExists(test_path)); |
| 1295 | EXPECT_FALSE(file_util::CreateDirectory(test_path)); |
| 1296 | |
| 1297 | EXPECT_TRUE(file_util::Delete(test_root, true)); |
| 1298 | EXPECT_FALSE(file_util::PathExists(test_root)); |
| 1299 | EXPECT_FALSE(file_util::PathExists(test_path)); |
joi@chromium.org | 9cd6dd2 | 2009-11-27 23:54:41 +0900 | [diff] [blame] | 1300 | |
| 1301 | // Verify assumptions made by the Windows implementation: |
| 1302 | // 1. The current directory always exists. |
| 1303 | // 2. The root directory always exists. |
| 1304 | ASSERT_TRUE(file_util::DirectoryExists( |
| 1305 | FilePath(FilePath::kCurrentDirectory))); |
| 1306 | FilePath top_level = test_root; |
| 1307 | while (top_level != top_level.DirName()) { |
| 1308 | top_level = top_level.DirName(); |
| 1309 | } |
| 1310 | ASSERT_TRUE(file_util::DirectoryExists(top_level)); |
| 1311 | |
| 1312 | // Given these assumptions hold, it should be safe to |
| 1313 | // test that "creating" these directories succeeds. |
| 1314 | EXPECT_TRUE(file_util::CreateDirectory( |
| 1315 | FilePath(FilePath::kCurrentDirectory))); |
| 1316 | EXPECT_TRUE(file_util::CreateDirectory(top_level)); |
huanr@chromium.org | 57c9dc3 | 2009-12-18 05:42:40 +0900 | [diff] [blame] | 1317 | |
| 1318 | #if defined(OS_WIN) |
| 1319 | FilePath invalid_drive(FILE_PATH_LITERAL("o:\\")); |
| 1320 | FilePath invalid_path = |
| 1321 | invalid_drive.Append(FILE_PATH_LITERAL("some\\inaccessible\\dir")); |
| 1322 | if (!file_util::PathExists(invalid_drive)) { |
| 1323 | EXPECT_FALSE(file_util::CreateDirectory(invalid_path)); |
| 1324 | } |
| 1325 | #endif |
mmoss@google.com | 733df6b | 2008-09-12 01:09:11 +0900 | [diff] [blame] | 1326 | } |
| 1327 | |
| 1328 | TEST_F(FileUtilTest, DetectDirectoryTest) { |
| 1329 | // Check a directory |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 1330 | FilePath test_root = |
| 1331 | test_dir_.Append(FILE_PATH_LITERAL("detect_directory_test")); |
mmoss@google.com | 733df6b | 2008-09-12 01:09:11 +0900 | [diff] [blame] | 1332 | EXPECT_FALSE(file_util::PathExists(test_root)); |
| 1333 | EXPECT_TRUE(file_util::CreateDirectory(test_root)); |
| 1334 | EXPECT_TRUE(file_util::PathExists(test_root)); |
| 1335 | EXPECT_TRUE(file_util::DirectoryExists(test_root)); |
| 1336 | |
| 1337 | // Check a file |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 1338 | FilePath test_path = |
| 1339 | test_root.Append(FILE_PATH_LITERAL("foobar.txt")); |
mmoss@google.com | 733df6b | 2008-09-12 01:09:11 +0900 | [diff] [blame] | 1340 | EXPECT_FALSE(file_util::PathExists(test_path)); |
| 1341 | CreateTextFile(test_path, L"test file"); |
| 1342 | EXPECT_TRUE(file_util::PathExists(test_path)); |
| 1343 | EXPECT_FALSE(file_util::DirectoryExists(test_path)); |
| 1344 | EXPECT_TRUE(file_util::Delete(test_path, false)); |
| 1345 | |
| 1346 | EXPECT_TRUE(file_util::Delete(test_root, true)); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 1347 | } |
| 1348 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 1349 | static const struct ReplaceExtensionCase { |
| 1350 | std::wstring file_name; |
estade@chromium.org | 6334320 | 2008-12-05 05:46:06 +0900 | [diff] [blame] | 1351 | FilePath::StringType extension; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 1352 | std::wstring result; |
| 1353 | } kReplaceExtension[] = { |
estade@chromium.org | 6334320 | 2008-12-05 05:46:06 +0900 | [diff] [blame] | 1354 | {L"", FILE_PATH_LITERAL(""), L""}, |
| 1355 | {L"", FILE_PATH_LITERAL("txt"), L".txt"}, |
| 1356 | {L".", FILE_PATH_LITERAL("txt"), L".txt"}, |
| 1357 | {L".", FILE_PATH_LITERAL(""), L""}, |
| 1358 | {L"foo.dll", FILE_PATH_LITERAL("txt"), L"foo.txt"}, |
| 1359 | {L"foo.dll", FILE_PATH_LITERAL(".txt"), L"foo.txt"}, |
| 1360 | {L"foo", FILE_PATH_LITERAL("txt"), L"foo.txt"}, |
| 1361 | {L"foo", FILE_PATH_LITERAL(".txt"), L"foo.txt"}, |
| 1362 | {L"foo.baz.dll", FILE_PATH_LITERAL("txt"), L"foo.baz.txt"}, |
| 1363 | {L"foo.baz.dll", FILE_PATH_LITERAL(".txt"), L"foo.baz.txt"}, |
| 1364 | {L"foo.dll", FILE_PATH_LITERAL(""), L"foo"}, |
| 1365 | {L"foo.dll", FILE_PATH_LITERAL("."), L"foo"}, |
| 1366 | {L"foo", FILE_PATH_LITERAL(""), L"foo"}, |
| 1367 | {L"foo", FILE_PATH_LITERAL("."), L"foo"}, |
| 1368 | {L"foo.baz.dll", FILE_PATH_LITERAL(""), L"foo.baz"}, |
| 1369 | {L"foo.baz.dll", FILE_PATH_LITERAL("."), L"foo.baz"}, |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 1370 | }; |
| 1371 | |
| 1372 | TEST_F(FileUtilTest, ReplaceExtensionTest) { |
erikkay@google.com | dfb51b2 | 2008-08-16 02:32:10 +0900 | [diff] [blame] | 1373 | for (unsigned int i = 0; i < arraysize(kReplaceExtension); ++i) { |
estade@chromium.org | 6334320 | 2008-12-05 05:46:06 +0900 | [diff] [blame] | 1374 | FilePath path = FilePath::FromWStringHack(kReplaceExtension[i].file_name); |
| 1375 | file_util::ReplaceExtension(&path, kReplaceExtension[i].extension); |
| 1376 | EXPECT_EQ(kReplaceExtension[i].result, path.ToWStringHack()); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 1377 | } |
| 1378 | } |
| 1379 | |
sky@google.com | 71e7c6f | 2008-09-20 02:32:18 +0900 | [diff] [blame] | 1380 | // Make sure ReplaceExtension doesn't replace an extension that occurs as one of |
| 1381 | // the directory names of the path. |
| 1382 | TEST_F(FileUtilTest, ReplaceExtensionTestWithPathSeparators) { |
estade@chromium.org | 6334320 | 2008-12-05 05:46:06 +0900 | [diff] [blame] | 1383 | FilePath path; |
| 1384 | path = path.Append(FILE_PATH_LITERAL("foo.bar")); |
| 1385 | path = path.Append(FILE_PATH_LITERAL("foo")); |
sky@google.com | 71e7c6f | 2008-09-20 02:32:18 +0900 | [diff] [blame] | 1386 | // '/foo.bar/foo' with extension '.baz' |
estade@chromium.org | 6334320 | 2008-12-05 05:46:06 +0900 | [diff] [blame] | 1387 | FilePath result_path = path; |
| 1388 | file_util::ReplaceExtension(&result_path, FILE_PATH_LITERAL(".baz")); |
phajdan.jr@chromium.org | f9908a7 | 2009-04-04 02:17:58 +0900 | [diff] [blame] | 1389 | EXPECT_EQ(path.value() + FILE_PATH_LITERAL(".baz"), |
| 1390 | result_path.value()); |
sky@google.com | 71e7c6f | 2008-09-20 02:32:18 +0900 | [diff] [blame] | 1391 | } |
| 1392 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 1393 | TEST_F(FileUtilTest, FileEnumeratorTest) { |
| 1394 | // Test an empty directory. |
yuzo@chromium.org | 2da0f82 | 2009-06-09 14:57:38 +0900 | [diff] [blame] | 1395 | file_util::FileEnumerator f0(test_dir_, true, FILES_AND_DIRECTORIES); |
avi@google.com | 5cb7935 | 2008-12-11 23:55:12 +0900 | [diff] [blame] | 1396 | EXPECT_EQ(f0.Next().value(), FILE_PATH_LITERAL("")); |
| 1397 | EXPECT_EQ(f0.Next().value(), FILE_PATH_LITERAL("")); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 1398 | |
yuzo@chromium.org | 2da0f82 | 2009-06-09 14:57:38 +0900 | [diff] [blame] | 1399 | // Test an empty directory, non-recursively, including "..". |
| 1400 | file_util::FileEnumerator f0_dotdot(test_dir_, false, |
| 1401 | static_cast<file_util::FileEnumerator::FILE_TYPE>( |
| 1402 | FILES_AND_DIRECTORIES | file_util::FileEnumerator::INCLUDE_DOT_DOT)); |
| 1403 | EXPECT_EQ(test_dir_.Append(FILE_PATH_LITERAL("..")).value(), |
| 1404 | f0_dotdot.Next().value()); |
| 1405 | EXPECT_EQ(FILE_PATH_LITERAL(""), |
| 1406 | f0_dotdot.Next().value()); |
| 1407 | |
erikkay@google.com | dfb51b2 | 2008-08-16 02:32:10 +0900 | [diff] [blame] | 1408 | // create the directories |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 1409 | FilePath dir1 = test_dir_.Append(FILE_PATH_LITERAL("dir1")); |
erikkay@google.com | dfb51b2 | 2008-08-16 02:32:10 +0900 | [diff] [blame] | 1410 | EXPECT_TRUE(file_util::CreateDirectory(dir1)); |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 1411 | FilePath dir2 = test_dir_.Append(FILE_PATH_LITERAL("dir2")); |
erikkay@google.com | dfb51b2 | 2008-08-16 02:32:10 +0900 | [diff] [blame] | 1412 | EXPECT_TRUE(file_util::CreateDirectory(dir2)); |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 1413 | FilePath dir2inner = dir2.Append(FILE_PATH_LITERAL("inner")); |
erikkay@google.com | dfb51b2 | 2008-08-16 02:32:10 +0900 | [diff] [blame] | 1414 | EXPECT_TRUE(file_util::CreateDirectory(dir2inner)); |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 1415 | |
erikkay@google.com | dfb51b2 | 2008-08-16 02:32:10 +0900 | [diff] [blame] | 1416 | // create the files |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 1417 | FilePath dir2file = dir2.Append(FILE_PATH_LITERAL("dir2file.txt")); |
erikkay@google.com | dfb51b2 | 2008-08-16 02:32:10 +0900 | [diff] [blame] | 1418 | CreateTextFile(dir2file, L""); |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 1419 | FilePath dir2innerfile = dir2inner.Append(FILE_PATH_LITERAL("innerfile.txt")); |
erikkay@google.com | dfb51b2 | 2008-08-16 02:32:10 +0900 | [diff] [blame] | 1420 | CreateTextFile(dir2innerfile, L""); |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 1421 | FilePath file1 = test_dir_.Append(FILE_PATH_LITERAL("file1.txt")); |
erikkay@google.com | dfb51b2 | 2008-08-16 02:32:10 +0900 | [diff] [blame] | 1422 | CreateTextFile(file1, L""); |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 1423 | FilePath file2_rel = |
| 1424 | dir2.Append(FilePath::kParentDirectory) |
| 1425 | .Append(FILE_PATH_LITERAL("file2.txt")); |
erikkay@google.com | dfb51b2 | 2008-08-16 02:32:10 +0900 | [diff] [blame] | 1426 | CreateTextFile(file2_rel, L""); |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 1427 | FilePath file2_abs = test_dir_.Append(FILE_PATH_LITERAL("file2.txt")); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 1428 | |
| 1429 | // Only enumerate files. |
avi@google.com | 5cb7935 | 2008-12-11 23:55:12 +0900 | [diff] [blame] | 1430 | file_util::FileEnumerator f1(test_dir_, true, |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 1431 | file_util::FileEnumerator::FILES); |
| 1432 | FindResultCollector c1(f1); |
erikkay@google.com | dfb51b2 | 2008-08-16 02:32:10 +0900 | [diff] [blame] | 1433 | EXPECT_TRUE(c1.HasFile(file1)); |
| 1434 | EXPECT_TRUE(c1.HasFile(file2_abs)); |
| 1435 | EXPECT_TRUE(c1.HasFile(dir2file)); |
| 1436 | EXPECT_TRUE(c1.HasFile(dir2innerfile)); |
| 1437 | EXPECT_EQ(c1.size(), 4); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 1438 | |
| 1439 | // Only enumerate directories. |
avi@google.com | 5cb7935 | 2008-12-11 23:55:12 +0900 | [diff] [blame] | 1440 | file_util::FileEnumerator f2(test_dir_, true, |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 1441 | file_util::FileEnumerator::DIRECTORIES); |
| 1442 | FindResultCollector c2(f2); |
erikkay@google.com | dfb51b2 | 2008-08-16 02:32:10 +0900 | [diff] [blame] | 1443 | EXPECT_TRUE(c2.HasFile(dir1)); |
| 1444 | EXPECT_TRUE(c2.HasFile(dir2)); |
| 1445 | EXPECT_TRUE(c2.HasFile(dir2inner)); |
| 1446 | EXPECT_EQ(c2.size(), 3); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 1447 | |
tim@chromium.org | 989d097 | 2008-10-16 11:42:45 +0900 | [diff] [blame] | 1448 | // Only enumerate directories non-recursively. |
| 1449 | file_util::FileEnumerator f2_non_recursive( |
avi@google.com | 5cb7935 | 2008-12-11 23:55:12 +0900 | [diff] [blame] | 1450 | test_dir_, false, file_util::FileEnumerator::DIRECTORIES); |
tim@chromium.org | 989d097 | 2008-10-16 11:42:45 +0900 | [diff] [blame] | 1451 | FindResultCollector c2_non_recursive(f2_non_recursive); |
| 1452 | EXPECT_TRUE(c2_non_recursive.HasFile(dir1)); |
| 1453 | EXPECT_TRUE(c2_non_recursive.HasFile(dir2)); |
| 1454 | EXPECT_EQ(c2_non_recursive.size(), 2); |
| 1455 | |
yuzo@chromium.org | 2da0f82 | 2009-06-09 14:57:38 +0900 | [diff] [blame] | 1456 | // Only enumerate directories, non-recursively, including "..". |
| 1457 | file_util::FileEnumerator f2_dotdot( |
| 1458 | test_dir_, false, |
| 1459 | static_cast<file_util::FileEnumerator::FILE_TYPE>( |
| 1460 | file_util::FileEnumerator::DIRECTORIES | |
| 1461 | file_util::FileEnumerator::INCLUDE_DOT_DOT)); |
| 1462 | FindResultCollector c2_dotdot(f2_dotdot); |
| 1463 | EXPECT_TRUE(c2_dotdot.HasFile(dir1)); |
| 1464 | EXPECT_TRUE(c2_dotdot.HasFile(dir2)); |
| 1465 | EXPECT_TRUE(c2_dotdot.HasFile(test_dir_.Append(FILE_PATH_LITERAL("..")))); |
| 1466 | EXPECT_EQ(c2_dotdot.size(), 3); |
| 1467 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 1468 | // Enumerate files and directories. |
yuzo@chromium.org | 2da0f82 | 2009-06-09 14:57:38 +0900 | [diff] [blame] | 1469 | file_util::FileEnumerator f3(test_dir_, true, FILES_AND_DIRECTORIES); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 1470 | FindResultCollector c3(f3); |
erikkay@google.com | dfb51b2 | 2008-08-16 02:32:10 +0900 | [diff] [blame] | 1471 | EXPECT_TRUE(c3.HasFile(dir1)); |
| 1472 | EXPECT_TRUE(c3.HasFile(dir2)); |
| 1473 | EXPECT_TRUE(c3.HasFile(file1)); |
| 1474 | EXPECT_TRUE(c3.HasFile(file2_abs)); |
| 1475 | EXPECT_TRUE(c3.HasFile(dir2file)); |
| 1476 | EXPECT_TRUE(c3.HasFile(dir2inner)); |
| 1477 | EXPECT_TRUE(c3.HasFile(dir2innerfile)); |
| 1478 | EXPECT_EQ(c3.size(), 7); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 1479 | |
| 1480 | // Non-recursive operation. |
yuzo@chromium.org | 2da0f82 | 2009-06-09 14:57:38 +0900 | [diff] [blame] | 1481 | file_util::FileEnumerator f4(test_dir_, false, FILES_AND_DIRECTORIES); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 1482 | FindResultCollector c4(f4); |
erikkay@google.com | dfb51b2 | 2008-08-16 02:32:10 +0900 | [diff] [blame] | 1483 | EXPECT_TRUE(c4.HasFile(dir2)); |
| 1484 | EXPECT_TRUE(c4.HasFile(dir2)); |
| 1485 | EXPECT_TRUE(c4.HasFile(file1)); |
| 1486 | EXPECT_TRUE(c4.HasFile(file2_abs)); |
| 1487 | EXPECT_EQ(c4.size(), 4); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 1488 | |
| 1489 | // Enumerate with a pattern. |
yuzo@chromium.org | 2da0f82 | 2009-06-09 14:57:38 +0900 | [diff] [blame] | 1490 | file_util::FileEnumerator f5(test_dir_, true, FILES_AND_DIRECTORIES, |
avi@google.com | 5cb7935 | 2008-12-11 23:55:12 +0900 | [diff] [blame] | 1491 | FILE_PATH_LITERAL("dir*")); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 1492 | FindResultCollector c5(f5); |
erikkay@google.com | dfb51b2 | 2008-08-16 02:32:10 +0900 | [diff] [blame] | 1493 | EXPECT_TRUE(c5.HasFile(dir1)); |
| 1494 | EXPECT_TRUE(c5.HasFile(dir2)); |
| 1495 | EXPECT_TRUE(c5.HasFile(dir2file)); |
| 1496 | EXPECT_TRUE(c5.HasFile(dir2inner)); |
| 1497 | EXPECT_TRUE(c5.HasFile(dir2innerfile)); |
| 1498 | EXPECT_EQ(c5.size(), 5); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 1499 | |
| 1500 | // Make sure the destructor closes the find handle while in the middle of a |
| 1501 | // query to allow TearDown to delete the directory. |
yuzo@chromium.org | 2da0f82 | 2009-06-09 14:57:38 +0900 | [diff] [blame] | 1502 | file_util::FileEnumerator f6(test_dir_, true, FILES_AND_DIRECTORIES); |
avi@google.com | 5cb7935 | 2008-12-11 23:55:12 +0900 | [diff] [blame] | 1503 | EXPECT_FALSE(f6.Next().value().empty()); // Should have found something |
| 1504 | // (we don't care what). |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 1505 | } |
license.bot | f003cfe | 2008-08-24 09:55:55 +0900 | [diff] [blame] | 1506 | |
aa@chromium.org | a4dbdf2 | 2009-01-10 07:14:27 +0900 | [diff] [blame] | 1507 | TEST_F(FileUtilTest, Contains) { |
thestig@chromium.org | 4cfbf7a | 2009-03-11 03:20:44 +0900 | [diff] [blame] | 1508 | FilePath data_dir = test_dir_.Append(FILE_PATH_LITERAL("FilePathTest")); |
aa@chromium.org | a4dbdf2 | 2009-01-10 07:14:27 +0900 | [diff] [blame] | 1509 | |
| 1510 | // Create a fresh, empty copy of this directory. |
rvargas@google.com | 5a0ae3b | 2009-01-31 10:19:57 +0900 | [diff] [blame] | 1511 | if (file_util::PathExists(data_dir)) { |
| 1512 | ASSERT_TRUE(file_util::Delete(data_dir, true)); |
| 1513 | } |
aa@chromium.org | a4dbdf2 | 2009-01-10 07:14:27 +0900 | [diff] [blame] | 1514 | ASSERT_TRUE(file_util::CreateDirectory(data_dir)); |
| 1515 | |
| 1516 | FilePath foo(data_dir.Append(FILE_PATH_LITERAL("foo"))); |
| 1517 | FilePath bar(foo.Append(FILE_PATH_LITERAL("bar.txt"))); |
| 1518 | FilePath baz(data_dir.Append(FILE_PATH_LITERAL("baz.txt"))); |
| 1519 | FilePath foobar(data_dir.Append(FILE_PATH_LITERAL("foobar.txt"))); |
| 1520 | |
| 1521 | // Annoyingly, the directories must actually exist in order for realpath(), |
| 1522 | // which Contains() relies on in posix, to work. |
| 1523 | ASSERT_TRUE(file_util::CreateDirectory(foo)); |
| 1524 | std::string data("hello"); |
phajdan.jr@chromium.org | f9908a7 | 2009-04-04 02:17:58 +0900 | [diff] [blame] | 1525 | ASSERT_TRUE(file_util::WriteFile(bar, data.c_str(), data.length())); |
| 1526 | ASSERT_TRUE(file_util::WriteFile(baz, data.c_str(), data.length())); |
| 1527 | ASSERT_TRUE(file_util::WriteFile(foobar, data.c_str(), data.length())); |
aa@chromium.org | a4dbdf2 | 2009-01-10 07:14:27 +0900 | [diff] [blame] | 1528 | |
| 1529 | EXPECT_TRUE(file_util::ContainsPath(foo, bar)); |
| 1530 | EXPECT_FALSE(file_util::ContainsPath(foo, baz)); |
| 1531 | EXPECT_FALSE(file_util::ContainsPath(foo, foobar)); |
| 1532 | EXPECT_FALSE(file_util::ContainsPath(foo, foo)); |
| 1533 | |
evan@chromium.org | 875bb6e | 2009-12-29 09:32:52 +0900 | [diff] [blame] | 1534 | // Platform-specific concerns. |
aa@chromium.org | a4dbdf2 | 2009-01-10 07:14:27 +0900 | [diff] [blame] | 1535 | FilePath foo_caps(data_dir.Append(FILE_PATH_LITERAL("FOO"))); |
| 1536 | #if defined(OS_WIN) |
| 1537 | EXPECT_TRUE(file_util::ContainsPath(foo, |
| 1538 | foo_caps.Append(FILE_PATH_LITERAL("bar.txt")))); |
jrg@chromium.org | d505c3a | 2009-02-04 09:58:39 +0900 | [diff] [blame] | 1539 | EXPECT_TRUE(file_util::ContainsPath(foo, |
aa@chromium.org | a4dbdf2 | 2009-01-10 07:14:27 +0900 | [diff] [blame] | 1540 | FilePath(foo.value() + FILE_PATH_LITERAL("/bar.txt")))); |
evan@chromium.org | 875bb6e | 2009-12-29 09:32:52 +0900 | [diff] [blame] | 1541 | #elif defined(OS_MACOSX) |
| 1542 | // We can't really do this test on OS X since the case-sensitivity of the |
| 1543 | // filesystem is configurable. |
| 1544 | #elif defined(OS_POSIX) |
aa@chromium.org | a4dbdf2 | 2009-01-10 07:14:27 +0900 | [diff] [blame] | 1545 | EXPECT_FALSE(file_util::ContainsPath(foo, |
| 1546 | foo_caps.Append(FILE_PATH_LITERAL("bar.txt")))); |
aa@chromium.org | a4dbdf2 | 2009-01-10 07:14:27 +0900 | [diff] [blame] | 1547 | #endif |
| 1548 | } |
| 1549 | |
jochen@chromium.org | a687977 | 2010-02-18 19:02:26 +0900 | [diff] [blame] | 1550 | TEST_F(FileUtilTest, LastModified) { |
| 1551 | FilePath data_dir = test_dir_.Append(FILE_PATH_LITERAL("FilePathTest")); |
| 1552 | |
| 1553 | // Create a fresh, empty copy of this directory. |
| 1554 | if (file_util::PathExists(data_dir)) { |
| 1555 | ASSERT_TRUE(file_util::Delete(data_dir, true)); |
| 1556 | } |
| 1557 | ASSERT_TRUE(file_util::CreateDirectory(data_dir)); |
| 1558 | |
| 1559 | FilePath foobar(data_dir.Append(FILE_PATH_LITERAL("foobar.txt"))); |
| 1560 | std::string data("hello"); |
| 1561 | ASSERT_TRUE(file_util::WriteFile(foobar, data.c_str(), data.length())); |
| 1562 | |
| 1563 | base::Time modification_time; |
| 1564 | // Note that this timestamp is divisible by two (seconds) - FAT stores |
| 1565 | // modification times with 2s resolution. |
| 1566 | ASSERT_TRUE(base::Time::FromString(L"Tue, 15 Nov 1994, 12:45:26 GMT", |
| 1567 | &modification_time)); |
| 1568 | ASSERT_TRUE(file_util::SetLastModifiedTime(foobar, modification_time)); |
| 1569 | file_util::FileInfo file_info; |
| 1570 | ASSERT_TRUE(file_util::GetFileInfo(foobar, &file_info)); |
| 1571 | ASSERT_TRUE(file_info.last_modified == modification_time); |
| 1572 | } |
| 1573 | |
mark@chromium.org | 1768480 | 2008-09-10 09:16:28 +0900 | [diff] [blame] | 1574 | } // namespace |