blob: 6ac55f63f1c947416be075611406ac45be07152b [file] [log] [blame]
brettw@chromium.org86aca862009-10-09 02:38:30 +09001// Copyright (c) 2009 The Chromium Authors. All rights reserved.
license.botf003cfe2008-08-24 09:55:55 +09002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit3f4a7322008-07-27 06:49:38 +09004
erikkay@google.comc8ec9e92008-08-16 02:50:10 +09005#include "build/build_config.h"
6
erikkay@google.com014161d2008-08-16 02:45:13 +09007#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +09008#include <windows.h>
initial.commit3f4a7322008-07-27 06:49:38 +09009#include <shellapi.h>
10#include <shlobj.h>
tkent@chromium.org8da14162009-10-09 16:33:39 +090011#include <tchar.h>
erikkay@google.comdfb51b22008-08-16 02:32:10 +090012#endif
initial.commit3f4a7322008-07-27 06:49:38 +090013
14#include <fstream>
15#include <iostream>
erikkay@google.comdfb51b22008-08-16 02:32:10 +090016#include <set>
initial.commit3f4a7322008-07-27 06:49:38 +090017
18#include "base/base_paths.h"
evanm@google.com874d1672008-10-31 08:54:04 +090019#include "base/file_path.h"
initial.commit3f4a7322008-07-27 06:49:38 +090020#include "base/file_util.h"
21#include "base/logging.h"
22#include "base/path_service.h"
erikkay@google.com8d133f62009-04-24 00:05:19 +090023#include "base/platform_thread.h"
erikkay@google.com9ac26762009-04-18 09:42:48 +090024#include "base/time.h"
brettw@chromium.org50c94652009-10-07 11:10:20 +090025#include "base/utf_string_conversions.h"
initial.commit3f4a7322008-07-27 06:49:38 +090026#include "testing/gtest/include/gtest/gtest.h"
jeremy@chromium.org0d8eba72008-12-03 04:20:15 +090027#include "testing/platform_test.h"
initial.commit3f4a7322008-07-27 06:49:38 +090028
phajdan.jr@chromium.orgf9908a72009-04-04 02:17:58 +090029// This macro helps avoid wrapped lines in the test structs.
30#define FPL(x) FILE_PATH_LITERAL(x)
31
initial.commit3f4a7322008-07-27 06:49:38 +090032namespace {
33
yuzo@chromium.org2da0f822009-06-09 14:57:38 +090034const file_util::FileEnumerator::FILE_TYPE FILES_AND_DIRECTORIES =
35 static_cast<file_util::FileEnumerator::FILE_TYPE>(
36 file_util::FileEnumerator::FILES |
37 file_util::FileEnumerator::DIRECTORIES);
38
erikkay@google.comf2406842008-08-21 00:59:49 +090039// file_util winds up using autoreleased objects on the Mac, so this needs
40// to be a PlatformTest
41class FileUtilTest : public PlatformTest {
initial.commit3f4a7322008-07-27 06:49:38 +090042 protected:
43 virtual void SetUp() {
erikkay@google.comf2406842008-08-21 00:59:49 +090044 PlatformTest::SetUp();
initial.commit3f4a7322008-07-27 06:49:38 +090045 // Name a subdirectory of the temp directory.
46 ASSERT_TRUE(PathService::Get(base::DIR_TEMP, &test_dir_));
evanm@google.com874d1672008-10-31 08:54:04 +090047 test_dir_ = test_dir_.Append(FILE_PATH_LITERAL("FileUtilTest"));
initial.commit3f4a7322008-07-27 06:49:38 +090048
49 // Create a fresh, empty copy of this directory.
50 file_util::Delete(test_dir_, true);
evanm@google.com874d1672008-10-31 08:54:04 +090051 file_util::CreateDirectory(test_dir_);
initial.commit3f4a7322008-07-27 06:49:38 +090052 }
53 virtual void TearDown() {
erikkay@google.comf2406842008-08-21 00:59:49 +090054 PlatformTest::TearDown();
initial.commit3f4a7322008-07-27 06:49:38 +090055 // Clean up test directory
erikkay@google.comdfb51b22008-08-16 02:32:10 +090056 ASSERT_TRUE(file_util::Delete(test_dir_, true));
initial.commit3f4a7322008-07-27 06:49:38 +090057 ASSERT_FALSE(file_util::PathExists(test_dir_));
58 }
59
60 // the path to temporary directory used to contain the test operations
evanm@google.com874d1672008-10-31 08:54:04 +090061 FilePath test_dir_;
initial.commit3f4a7322008-07-27 06:49:38 +090062};
63
64// Collects all the results from the given file enumerator, and provides an
65// interface to query whether a given file is present.
66class FindResultCollector {
67 public:
evan@chromium.org1543ad32009-08-27 05:00:14 +090068 explicit FindResultCollector(file_util::FileEnumerator& enumerator) {
avi@google.com5cb79352008-12-11 23:55:12 +090069 FilePath cur_file;
70 while (!(cur_file = enumerator.Next()).value().empty()) {
71 FilePath::StringType path = cur_file.value();
initial.commit3f4a7322008-07-27 06:49:38 +090072 // The file should not be returned twice.
evanm@google.com874d1672008-10-31 08:54:04 +090073 EXPECT_TRUE(files_.end() == files_.find(path))
initial.commit3f4a7322008-07-27 06:49:38 +090074 << "Same file returned twice";
75
76 // Save for later.
evanm@google.com874d1672008-10-31 08:54:04 +090077 files_.insert(path);
initial.commit3f4a7322008-07-27 06:49:38 +090078 }
79 }
80
81 // Returns true if the enumerator found the file.
evanm@google.com874d1672008-10-31 08:54:04 +090082 bool HasFile(const FilePath& file) const {
83 return files_.find(file.value()) != files_.end();
initial.commit3f4a7322008-07-27 06:49:38 +090084 }
evanm@google.com874d1672008-10-31 08:54:04 +090085
erikkay@google.comdfb51b22008-08-16 02:32:10 +090086 int size() {
erikkay@google.comc8ec9e92008-08-16 02:50:10 +090087 return static_cast<int>(files_.size());
erikkay@google.comdfb51b22008-08-16 02:32:10 +090088 }
initial.commit3f4a7322008-07-27 06:49:38 +090089
90 private:
evanm@google.com874d1672008-10-31 08:54:04 +090091 std::set<FilePath::StringType> files_;
initial.commit3f4a7322008-07-27 06:49:38 +090092};
93
94// Simple function to dump some text into a new file.
evanm@google.com874d1672008-10-31 08:54:04 +090095void CreateTextFile(const FilePath& filename,
initial.commit3f4a7322008-07-27 06:49:38 +090096 const std::wstring& contents) {
97 std::ofstream file;
evanm@google.com874d1672008-10-31 08:54:04 +090098 file.open(WideToUTF8(filename.ToWStringHack()).c_str());
initial.commit3f4a7322008-07-27 06:49:38 +090099 ASSERT_TRUE(file.is_open());
100 file << contents;
101 file.close();
102}
103
104// Simple function to take out some text from a file.
evanm@google.com874d1672008-10-31 08:54:04 +0900105std::wstring ReadTextFile(const FilePath& filename) {
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900106 wchar_t contents[64];
initial.commit3f4a7322008-07-27 06:49:38 +0900107 std::wifstream file;
evanm@google.com874d1672008-10-31 08:54:04 +0900108 file.open(WideToUTF8(filename.ToWStringHack()).c_str());
initial.commit3f4a7322008-07-27 06:49:38 +0900109 EXPECT_TRUE(file.is_open());
110 file.getline(contents, 64);
111 file.close();
112 return std::wstring(contents);
113}
114
erikkay@google.com014161d2008-08-16 02:45:13 +0900115#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +0900116uint64 FileTimeAsUint64(const FILETIME& ft) {
117 ULARGE_INTEGER u;
118 u.LowPart = ft.dwLowDateTime;
119 u.HighPart = ft.dwHighDateTime;
120 return u.QuadPart;
121}
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900122#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900123
124const struct append_case {
125 const wchar_t* path;
126 const wchar_t* ending;
127 const wchar_t* result;
128} append_cases[] = {
erikkay@google.com014161d2008-08-16 02:45:13 +0900129#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +0900130 {L"c:\\colon\\backslash", L"path", L"c:\\colon\\backslash\\path"},
131 {L"c:\\colon\\backslash\\", L"path", L"c:\\colon\\backslash\\path"},
132 {L"c:\\colon\\backslash\\\\", L"path", L"c:\\colon\\backslash\\\\path"},
133 {L"c:\\colon\\backslash\\", L"", L"c:\\colon\\backslash\\"},
134 {L"c:\\colon\\backslash", L"", L"c:\\colon\\backslash\\"},
135 {L"", L"path", L"\\path"},
136 {L"", L"", L"\\"},
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900137#elif defined(OS_POSIX)
138 {L"/foo/bar", L"path", L"/foo/bar/path"},
139 {L"/foo/bar/", L"path", L"/foo/bar/path"},
140 {L"/foo/bar//", L"path", L"/foo/bar//path"},
141 {L"/foo/bar/", L"", L"/foo/bar/"},
142 {L"/foo/bar", L"", L"/foo/bar/"},
143 {L"", L"path", L"/path"},
144 {L"", L"", L"/"},
145#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900146};
147
initial.commit3f4a7322008-07-27 06:49:38 +0900148TEST_F(FileUtilTest, AppendToPath) {
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900149 for (unsigned int i = 0; i < arraysize(append_cases); ++i) {
initial.commit3f4a7322008-07-27 06:49:38 +0900150 const append_case& value = append_cases[i];
151 std::wstring result = value.path;
152 file_util::AppendToPath(&result, value.ending);
153 EXPECT_EQ(value.result, result);
154 }
155
156#ifdef NDEBUG
157 file_util::AppendToPath(NULL, L"path"); // asserts in debug mode
158#endif
159}
160
161static const struct InsertBeforeExtensionCase {
phajdan.jr@chromium.orgf9908a72009-04-04 02:17:58 +0900162 const FilePath::CharType* path;
163 const FilePath::CharType* suffix;
164 const FilePath::CharType* result;
initial.commit3f4a7322008-07-27 06:49:38 +0900165} kInsertBeforeExtension[] = {
phajdan.jr@chromium.orgf9908a72009-04-04 02:17:58 +0900166 {FPL(""), FPL(""), FPL("")},
167 {FPL(""), FPL("txt"), FPL("txt")},
168 {FPL("."), FPL("txt"), FPL("txt.")},
169 {FPL("."), FPL(""), FPL(".")},
170 {FPL("foo.dll"), FPL("txt"), FPL("footxt.dll")},
171 {FPL("foo.dll"), FPL(".txt"), FPL("foo.txt.dll")},
172 {FPL("foo"), FPL("txt"), FPL("footxt")},
173 {FPL("foo"), FPL(".txt"), FPL("foo.txt")},
174 {FPL("foo.baz.dll"), FPL("txt"), FPL("foo.baztxt.dll")},
175 {FPL("foo.baz.dll"), FPL(".txt"), FPL("foo.baz.txt.dll")},
176 {FPL("foo.dll"), FPL(""), FPL("foo.dll")},
177 {FPL("foo.dll"), FPL("."), FPL("foo..dll")},
178 {FPL("foo"), FPL(""), FPL("foo")},
179 {FPL("foo"), FPL("."), FPL("foo.")},
180 {FPL("foo.baz.dll"), FPL(""), FPL("foo.baz.dll")},
181 {FPL("foo.baz.dll"), FPL("."), FPL("foo.baz..dll")},
erikkay@google.com014161d2008-08-16 02:45:13 +0900182#if defined(OS_WIN)
phajdan.jr@chromium.orgf9908a72009-04-04 02:17:58 +0900183 {FPL("\\"), FPL(""), FPL("\\")},
184 {FPL("\\"), FPL("txt"), FPL("\\txt")},
185 {FPL("\\."), FPL("txt"), FPL("\\txt.")},
186 {FPL("\\."), FPL(""), FPL("\\.")},
187 {FPL("C:\\bar\\foo.dll"), FPL("txt"), FPL("C:\\bar\\footxt.dll")},
188 {FPL("C:\\bar.baz\\foodll"), FPL("txt"), FPL("C:\\bar.baz\\foodlltxt")},
189 {FPL("C:\\bar.baz\\foo.dll"), FPL("txt"), FPL("C:\\bar.baz\\footxt.dll")},
190 {FPL("C:\\bar.baz\\foo.dll.exe"), FPL("txt"),
191 FPL("C:\\bar.baz\\foo.dlltxt.exe")},
192 {FPL("C:\\bar.baz\\foo"), FPL(""), FPL("C:\\bar.baz\\foo")},
193 {FPL("C:\\bar.baz\\foo.exe"), FPL(""), FPL("C:\\bar.baz\\foo.exe")},
194 {FPL("C:\\bar.baz\\foo.dll.exe"), FPL(""), FPL("C:\\bar.baz\\foo.dll.exe")},
195 {FPL("C:\\bar\\baz\\foo.exe"), FPL(" (1)"), FPL("C:\\bar\\baz\\foo (1).exe")},
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900196#elif defined(OS_POSIX)
phajdan.jr@chromium.orgf9908a72009-04-04 02:17:58 +0900197 {FPL("/"), FPL(""), FPL("/")},
198 {FPL("/"), FPL("txt"), FPL("/txt")},
199 {FPL("/."), FPL("txt"), FPL("/txt.")},
200 {FPL("/."), FPL(""), FPL("/.")},
201 {FPL("/bar/foo.dll"), FPL("txt"), FPL("/bar/footxt.dll")},
202 {FPL("/bar.baz/foodll"), FPL("txt"), FPL("/bar.baz/foodlltxt")},
203 {FPL("/bar.baz/foo.dll"), FPL("txt"), FPL("/bar.baz/footxt.dll")},
204 {FPL("/bar.baz/foo.dll.exe"), FPL("txt"), FPL("/bar.baz/foo.dlltxt.exe")},
205 {FPL("/bar.baz/foo"), FPL(""), FPL("/bar.baz/foo")},
206 {FPL("/bar.baz/foo.exe"), FPL(""), FPL("/bar.baz/foo.exe")},
207 {FPL("/bar.baz/foo.dll.exe"), FPL(""), FPL("/bar.baz/foo.dll.exe")},
208 {FPL("/bar/baz/foo.exe"), FPL(" (1)"), FPL("/bar/baz/foo (1).exe")},
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900209#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900210};
211
212TEST_F(FileUtilTest, InsertBeforeExtensionTest) {
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900213 for (unsigned int i = 0; i < arraysize(kInsertBeforeExtension); ++i) {
phajdan.jr@chromium.orgf9908a72009-04-04 02:17:58 +0900214 FilePath path(kInsertBeforeExtension[i].path);
initial.commit3f4a7322008-07-27 06:49:38 +0900215 file_util::InsertBeforeExtension(&path, kInsertBeforeExtension[i].suffix);
phajdan.jr@chromium.orgf9908a72009-04-04 02:17:58 +0900216 EXPECT_EQ(kInsertBeforeExtension[i].result, path.value());
initial.commit3f4a7322008-07-27 06:49:38 +0900217 }
218}
219
220static const struct filename_case {
221 const wchar_t* path;
222 const wchar_t* filename;
223} filename_cases[] = {
erikkay@google.com014161d2008-08-16 02:45:13 +0900224#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +0900225 {L"c:\\colon\\backslash", L"backslash"},
226 {L"c:\\colon\\backslash\\", L""},
227 {L"\\\\filename.exe", L"filename.exe"},
228 {L"filename.exe", L"filename.exe"},
229 {L"", L""},
230 {L"\\\\\\", L""},
231 {L"c:/colon/backslash", L"backslash"},
232 {L"c:/colon/backslash/", L""},
233 {L"//////", L""},
234 {L"///filename.exe", L"filename.exe"},
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900235#elif defined(OS_POSIX)
236 {L"/foo/bar", L"bar"},
237 {L"/foo/bar/", L""},
238 {L"/filename.exe", L"filename.exe"},
239 {L"filename.exe", L"filename.exe"},
240 {L"", L""},
241 {L"/", L""},
242#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900243};
244
245TEST_F(FileUtilTest, GetFilenameFromPath) {
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900246 for (unsigned int i = 0; i < arraysize(filename_cases); ++i) {
initial.commit3f4a7322008-07-27 06:49:38 +0900247 const filename_case& value = filename_cases[i];
248 std::wstring result = file_util::GetFilenameFromPath(value.path);
249 EXPECT_EQ(value.filename, result);
250 }
251}
252
253// Test finding the file type from a path name
254static const struct extension_case {
255 const wchar_t* path;
256 const wchar_t* extension;
257} extension_cases[] = {
erikkay@google.com014161d2008-08-16 02:45:13 +0900258#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +0900259 {L"C:\\colon\\backslash\\filename.extension", L"extension"},
260 {L"C:\\colon\\backslash\\filename.", L""},
261 {L"C:\\colon\\backslash\\filename", L""},
262 {L"C:\\colon\\backslash\\", L""},
263 {L"C:\\colon\\backslash.\\", L""},
264 {L"C:\\colon\\backslash\filename.extension.extension2", L"extension2"},
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900265#elif defined(OS_POSIX)
266 {L"/foo/bar/filename.extension", L"extension"},
267 {L"/foo/bar/filename.", L""},
268 {L"/foo/bar/filename", L""},
269 {L"/foo/bar/", L""},
270 {L"/foo/bar./", L""},
271 {L"/foo/bar/filename.extension.extension2", L"extension2"},
272 {L".", L""},
273 {L"..", L""},
274 {L"./foo", L""},
275 {L"./foo.extension", L"extension"},
276 {L"/foo.extension1/bar.extension2", L"extension2"},
277#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900278};
279
280TEST_F(FileUtilTest, GetFileExtensionFromPath) {
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900281 for (unsigned int i = 0; i < arraysize(extension_cases); ++i) {
initial.commit3f4a7322008-07-27 06:49:38 +0900282 const extension_case& ext = extension_cases[i];
283 const std::wstring fext = file_util::GetFileExtensionFromPath(ext.path);
284 EXPECT_EQ(ext.extension, fext);
285 }
286}
287
288// Test finding the directory component of a path
289static const struct dir_case {
290 const wchar_t* full_path;
291 const wchar_t* directory;
292} dir_cases[] = {
erikkay@google.com014161d2008-08-16 02:45:13 +0900293#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +0900294 {L"C:\\WINDOWS\\system32\\gdi32.dll", L"C:\\WINDOWS\\system32"},
295 {L"C:\\WINDOWS\\system32\\not_exist_thx_1138", L"C:\\WINDOWS\\system32"},
296 {L"C:\\WINDOWS\\system32\\", L"C:\\WINDOWS\\system32"},
297 {L"C:\\WINDOWS\\system32\\\\", L"C:\\WINDOWS\\system32"},
298 {L"C:\\WINDOWS\\system32", L"C:\\WINDOWS"},
299 {L"C:\\WINDOWS\\system32.\\", L"C:\\WINDOWS\\system32."},
300 {L"C:\\", L"C:"},
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900301#elif defined(OS_POSIX)
302 {L"/foo/bar/gdi32.dll", L"/foo/bar"},
303 {L"/foo/bar/not_exist_thx_1138", L"/foo/bar"},
304 {L"/foo/bar/", L"/foo/bar"},
305 {L"/foo/bar//", L"/foo/bar"},
306 {L"/foo/bar", L"/foo"},
307 {L"/foo/bar./", L"/foo/bar."},
308 {L"/", L"/"},
309 {L".", L"."},
evan@chromium.org1543ad32009-08-27 05:00:14 +0900310 {L"..", L"."}, // yes, ".." technically lives in "."
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900311#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900312};
313
314TEST_F(FileUtilTest, GetDirectoryFromPath) {
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900315 for (unsigned int i = 0; i < arraysize(dir_cases); ++i) {
initial.commit3f4a7322008-07-27 06:49:38 +0900316 const dir_case& dir = dir_cases[i];
317 const std::wstring parent =
318 file_util::GetDirectoryFromPath(dir.full_path);
319 EXPECT_EQ(dir.directory, parent);
320 }
321}
322
323TEST_F(FileUtilTest, CountFilesCreatedAfter) {
324 // Create old file (that we don't want to count)
erikkay@google.com9ac26762009-04-18 09:42:48 +0900325 FilePath old_file_name = test_dir_.Append(FILE_PATH_LITERAL("Old File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900326 CreateTextFile(old_file_name, L"Just call me Mr. Creakybits");
327
328 // Age to perfection
evan@chromium.org37301322009-04-21 10:50:39 +0900329#if defined(OS_WIN)
erikkay@google.com8d133f62009-04-24 00:05:19 +0900330 PlatformThread::Sleep(100);
evan@chromium.org37301322009-04-21 10:50:39 +0900331#elif defined(OS_POSIX)
332 // We need to wait at least one second here because the precision of
333 // file creation time is one second.
erikkay@google.com8d133f62009-04-24 00:05:19 +0900334 PlatformThread::Sleep(1500);
evan@chromium.org37301322009-04-21 10:50:39 +0900335#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900336
337 // Establish our cutoff time
erikkay@google.com9ac26762009-04-18 09:42:48 +0900338 base::Time now(base::Time::NowFromSystemTime());
339 EXPECT_EQ(0, file_util::CountFilesCreatedAfter(test_dir_, now));
initial.commit3f4a7322008-07-27 06:49:38 +0900340
341 // Create a new file (that we do want to count)
erikkay@google.com9ac26762009-04-18 09:42:48 +0900342 FilePath new_file_name = test_dir_.Append(FILE_PATH_LITERAL("New File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900343 CreateTextFile(new_file_name, L"Waaaaaaaaaaaaaah.");
344
345 // We should see only the new file.
erikkay@google.com9ac26762009-04-18 09:42:48 +0900346 EXPECT_EQ(1, file_util::CountFilesCreatedAfter(test_dir_, now));
initial.commit3f4a7322008-07-27 06:49:38 +0900347
348 // Delete new file, we should see no files after cutoff now
349 EXPECT_TRUE(file_util::Delete(new_file_name, false));
erikkay@google.com9ac26762009-04-18 09:42:48 +0900350 EXPECT_EQ(0, file_util::CountFilesCreatedAfter(test_dir_, now));
initial.commit3f4a7322008-07-27 06:49:38 +0900351}
352
353// Tests that the Delete function works as expected, especially
354// the recursion flag. Also coincidentally tests PathExists.
355TEST_F(FileUtilTest, Delete) {
356 // Create a file
evanm@google.com874d1672008-10-31 08:54:04 +0900357 FilePath file_name = test_dir_.Append(FILE_PATH_LITERAL("Test File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900358 CreateTextFile(file_name, L"I'm cannon fodder.");
359
360 ASSERT_TRUE(file_util::PathExists(file_name));
361
evanm@google.com874d1672008-10-31 08:54:04 +0900362 FilePath subdir_path = test_dir_.Append(FILE_PATH_LITERAL("Subdirectory"));
363 file_util::CreateDirectory(subdir_path);
initial.commit3f4a7322008-07-27 06:49:38 +0900364
365 ASSERT_TRUE(file_util::PathExists(subdir_path));
366
evanm@google.com874d1672008-10-31 08:54:04 +0900367 FilePath directory_contents = test_dir_;
erikkay@google.com014161d2008-08-16 02:45:13 +0900368#if defined(OS_WIN)
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900369 // TODO(erikkay): see if anyone's actually using this feature of the API
evanm@google.com874d1672008-10-31 08:54:04 +0900370 directory_contents = directory_contents.Append(FILE_PATH_LITERAL("*"));
initial.commit3f4a7322008-07-27 06:49:38 +0900371 // Delete non-recursively and check that only the file is deleted
372 ASSERT_TRUE(file_util::Delete(directory_contents, false));
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900373 EXPECT_FALSE(file_util::PathExists(file_name));
374 EXPECT_TRUE(file_util::PathExists(subdir_path));
375#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900376
377 // Delete recursively and make sure all contents are deleted
378 ASSERT_TRUE(file_util::Delete(directory_contents, true));
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900379 EXPECT_FALSE(file_util::PathExists(file_name));
380 EXPECT_FALSE(file_util::PathExists(subdir_path));
initial.commit3f4a7322008-07-27 06:49:38 +0900381}
382
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +0900383TEST_F(FileUtilTest, MoveNew) {
initial.commit3f4a7322008-07-27 06:49:38 +0900384 // Create a directory
evanm@google.com874d1672008-10-31 08:54:04 +0900385 FilePath dir_name_from =
386 test_dir_.Append(FILE_PATH_LITERAL("Move_From_Subdir"));
387 file_util::CreateDirectory(dir_name_from);
initial.commit3f4a7322008-07-27 06:49:38 +0900388 ASSERT_TRUE(file_util::PathExists(dir_name_from));
389
390 // Create a file under the directory
evanm@google.com874d1672008-10-31 08:54:04 +0900391 FilePath file_name_from =
392 dir_name_from.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900393 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
394 ASSERT_TRUE(file_util::PathExists(file_name_from));
395
396 // Move the directory
evanm@google.com874d1672008-10-31 08:54:04 +0900397 FilePath dir_name_to = test_dir_.Append(FILE_PATH_LITERAL("Move_To_Subdir"));
398 FilePath file_name_to =
399 dir_name_to.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900400
401 ASSERT_FALSE(file_util::PathExists(dir_name_to));
402
403 EXPECT_TRUE(file_util::Move(dir_name_from, dir_name_to));
404
405 // Check everything has been moved.
406 EXPECT_FALSE(file_util::PathExists(dir_name_from));
407 EXPECT_FALSE(file_util::PathExists(file_name_from));
408 EXPECT_TRUE(file_util::PathExists(dir_name_to));
409 EXPECT_TRUE(file_util::PathExists(file_name_to));
410}
411
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +0900412TEST_F(FileUtilTest, MoveExist) {
413 // Create a directory
414 FilePath dir_name_from =
415 test_dir_.Append(FILE_PATH_LITERAL("Move_From_Subdir"));
416 file_util::CreateDirectory(dir_name_from);
417 ASSERT_TRUE(file_util::PathExists(dir_name_from));
418
419 // Create a file under the directory
420 FilePath file_name_from =
421 dir_name_from.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
422 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
423 ASSERT_TRUE(file_util::PathExists(file_name_from));
424
425 // Move the directory
426 FilePath dir_name_exists =
427 test_dir_.Append(FILE_PATH_LITERAL("Destination"));
428
429 FilePath dir_name_to =
430 dir_name_exists.Append(FILE_PATH_LITERAL("Move_To_Subdir"));
431 FilePath file_name_to =
432 dir_name_to.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
433
434 // Create the destination directory.
435 file_util::CreateDirectory(dir_name_exists);
436 ASSERT_TRUE(file_util::PathExists(dir_name_exists));
437
438 EXPECT_TRUE(file_util::Move(dir_name_from, dir_name_to));
439
440 // Check everything has been moved.
441 EXPECT_FALSE(file_util::PathExists(dir_name_from));
442 EXPECT_FALSE(file_util::PathExists(file_name_from));
443 EXPECT_TRUE(file_util::PathExists(dir_name_to));
444 EXPECT_TRUE(file_util::PathExists(file_name_to));
445}
446
447TEST_F(FileUtilTest, CopyDirectoryRecursivelyNew) {
initial.commit3f4a7322008-07-27 06:49:38 +0900448 // Create a directory.
evanm@google.com874d1672008-10-31 08:54:04 +0900449 FilePath dir_name_from =
450 test_dir_.Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
451 file_util::CreateDirectory(dir_name_from);
initial.commit3f4a7322008-07-27 06:49:38 +0900452 ASSERT_TRUE(file_util::PathExists(dir_name_from));
453
454 // Create a file under the directory.
evanm@google.com874d1672008-10-31 08:54:04 +0900455 FilePath file_name_from =
456 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900457 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
458 ASSERT_TRUE(file_util::PathExists(file_name_from));
459
460 // Create a subdirectory.
evanm@google.com874d1672008-10-31 08:54:04 +0900461 FilePath subdir_name_from =
462 dir_name_from.Append(FILE_PATH_LITERAL("Subdir"));
463 file_util::CreateDirectory(subdir_name_from);
initial.commit3f4a7322008-07-27 06:49:38 +0900464 ASSERT_TRUE(file_util::PathExists(subdir_name_from));
465
466 // Create a file under the subdirectory.
evanm@google.com874d1672008-10-31 08:54:04 +0900467 FilePath file_name2_from =
468 subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900469 CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle");
470 ASSERT_TRUE(file_util::PathExists(file_name2_from));
471
472 // Copy the directory recursively.
evanm@google.com874d1672008-10-31 08:54:04 +0900473 FilePath dir_name_to =
474 test_dir_.Append(FILE_PATH_LITERAL("Copy_To_Subdir"));
475 FilePath file_name_to =
476 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
477 FilePath subdir_name_to =
478 dir_name_to.Append(FILE_PATH_LITERAL("Subdir"));
479 FilePath file_name2_to =
480 subdir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900481
482 ASSERT_FALSE(file_util::PathExists(dir_name_to));
483
484 EXPECT_TRUE(file_util::CopyDirectory(dir_name_from, dir_name_to, true));
485
486 // Check everything has been copied.
487 EXPECT_TRUE(file_util::PathExists(dir_name_from));
488 EXPECT_TRUE(file_util::PathExists(file_name_from));
489 EXPECT_TRUE(file_util::PathExists(subdir_name_from));
490 EXPECT_TRUE(file_util::PathExists(file_name2_from));
491 EXPECT_TRUE(file_util::PathExists(dir_name_to));
492 EXPECT_TRUE(file_util::PathExists(file_name_to));
493 EXPECT_TRUE(file_util::PathExists(subdir_name_to));
494 EXPECT_TRUE(file_util::PathExists(file_name2_to));
495}
496
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +0900497TEST_F(FileUtilTest, CopyDirectoryRecursivelyExists) {
498 // Create a directory.
499 FilePath dir_name_from =
500 test_dir_.Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
501 file_util::CreateDirectory(dir_name_from);
502 ASSERT_TRUE(file_util::PathExists(dir_name_from));
503
504 // Create a file under the directory.
505 FilePath file_name_from =
506 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
507 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
508 ASSERT_TRUE(file_util::PathExists(file_name_from));
509
510 // Create a subdirectory.
511 FilePath subdir_name_from =
512 dir_name_from.Append(FILE_PATH_LITERAL("Subdir"));
513 file_util::CreateDirectory(subdir_name_from);
514 ASSERT_TRUE(file_util::PathExists(subdir_name_from));
515
516 // Create a file under the subdirectory.
517 FilePath file_name2_from =
518 subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
519 CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle");
520 ASSERT_TRUE(file_util::PathExists(file_name2_from));
521
522 // Copy the directory recursively.
523 FilePath dir_name_exists =
524 test_dir_.Append(FILE_PATH_LITERAL("Destination"));
525
526 FilePath dir_name_to =
527 dir_name_exists.Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
528 FilePath file_name_to =
529 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
530 FilePath subdir_name_to =
531 dir_name_to.Append(FILE_PATH_LITERAL("Subdir"));
532 FilePath file_name2_to =
533 subdir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
534
535 // Create the destination directory.
536 file_util::CreateDirectory(dir_name_exists);
537 ASSERT_TRUE(file_util::PathExists(dir_name_exists));
538
539 EXPECT_TRUE(file_util::CopyDirectory(dir_name_from, dir_name_exists, true));
540
541 // Check everything has been copied.
542 EXPECT_TRUE(file_util::PathExists(dir_name_from));
543 EXPECT_TRUE(file_util::PathExists(file_name_from));
544 EXPECT_TRUE(file_util::PathExists(subdir_name_from));
545 EXPECT_TRUE(file_util::PathExists(file_name2_from));
546 EXPECT_TRUE(file_util::PathExists(dir_name_to));
547 EXPECT_TRUE(file_util::PathExists(file_name_to));
548 EXPECT_TRUE(file_util::PathExists(subdir_name_to));
549 EXPECT_TRUE(file_util::PathExists(file_name2_to));
550}
551
552TEST_F(FileUtilTest, CopyDirectoryNew) {
initial.commit3f4a7322008-07-27 06:49:38 +0900553 // Create a directory.
evanm@google.com874d1672008-10-31 08:54:04 +0900554 FilePath dir_name_from =
555 test_dir_.Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
556 file_util::CreateDirectory(dir_name_from);
initial.commit3f4a7322008-07-27 06:49:38 +0900557 ASSERT_TRUE(file_util::PathExists(dir_name_from));
558
559 // Create a file under the directory.
evanm@google.com874d1672008-10-31 08:54:04 +0900560 FilePath file_name_from =
561 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900562 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
563 ASSERT_TRUE(file_util::PathExists(file_name_from));
564
565 // Create a subdirectory.
evanm@google.com874d1672008-10-31 08:54:04 +0900566 FilePath subdir_name_from =
567 dir_name_from.Append(FILE_PATH_LITERAL("Subdir"));
568 file_util::CreateDirectory(subdir_name_from);
initial.commit3f4a7322008-07-27 06:49:38 +0900569 ASSERT_TRUE(file_util::PathExists(subdir_name_from));
570
571 // Create a file under the subdirectory.
evanm@google.com874d1672008-10-31 08:54:04 +0900572 FilePath file_name2_from =
573 subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900574 CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle");
575 ASSERT_TRUE(file_util::PathExists(file_name2_from));
576
577 // Copy the directory not recursively.
evanm@google.com874d1672008-10-31 08:54:04 +0900578 FilePath dir_name_to =
579 test_dir_.Append(FILE_PATH_LITERAL("Copy_To_Subdir"));
580 FilePath file_name_to =
581 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
582 FilePath subdir_name_to =
583 dir_name_to.Append(FILE_PATH_LITERAL("Subdir"));
initial.commit3f4a7322008-07-27 06:49:38 +0900584
585 ASSERT_FALSE(file_util::PathExists(dir_name_to));
586
587 EXPECT_TRUE(file_util::CopyDirectory(dir_name_from, dir_name_to, false));
588
589 // Check everything has been copied.
590 EXPECT_TRUE(file_util::PathExists(dir_name_from));
591 EXPECT_TRUE(file_util::PathExists(file_name_from));
592 EXPECT_TRUE(file_util::PathExists(subdir_name_from));
593 EXPECT_TRUE(file_util::PathExists(file_name2_from));
594 EXPECT_TRUE(file_util::PathExists(dir_name_to));
595 EXPECT_TRUE(file_util::PathExists(file_name_to));
596 EXPECT_FALSE(file_util::PathExists(subdir_name_to));
597}
598
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +0900599TEST_F(FileUtilTest, CopyDirectoryExists) {
600 // Create a directory.
601 FilePath dir_name_from =
602 test_dir_.Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
603 file_util::CreateDirectory(dir_name_from);
604 ASSERT_TRUE(file_util::PathExists(dir_name_from));
605
606 // Create a file under the directory.
607 FilePath file_name_from =
608 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
609 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
610 ASSERT_TRUE(file_util::PathExists(file_name_from));
611
612 // Create a subdirectory.
613 FilePath subdir_name_from =
614 dir_name_from.Append(FILE_PATH_LITERAL("Subdir"));
615 file_util::CreateDirectory(subdir_name_from);
616 ASSERT_TRUE(file_util::PathExists(subdir_name_from));
617
618 // Create a file under the subdirectory.
619 FilePath file_name2_from =
620 subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
621 CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle");
622 ASSERT_TRUE(file_util::PathExists(file_name2_from));
623
624 // Copy the directory not recursively.
625 FilePath dir_name_to =
626 test_dir_.Append(FILE_PATH_LITERAL("Copy_To_Subdir"));
627 FilePath file_name_to =
628 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
629 FilePath subdir_name_to =
630 dir_name_to.Append(FILE_PATH_LITERAL("Subdir"));
631
632 // Create the destination directory.
633 file_util::CreateDirectory(dir_name_to);
634 ASSERT_TRUE(file_util::PathExists(dir_name_to));
635
636 EXPECT_TRUE(file_util::CopyDirectory(dir_name_from, dir_name_to, false));
637
638 // Check everything has been copied.
639 EXPECT_TRUE(file_util::PathExists(dir_name_from));
640 EXPECT_TRUE(file_util::PathExists(file_name_from));
641 EXPECT_TRUE(file_util::PathExists(subdir_name_from));
642 EXPECT_TRUE(file_util::PathExists(file_name2_from));
643 EXPECT_TRUE(file_util::PathExists(dir_name_to));
644 EXPECT_TRUE(file_util::PathExists(file_name_to));
645 EXPECT_FALSE(file_util::PathExists(subdir_name_to));
646}
647
initial.commit3f4a7322008-07-27 06:49:38 +0900648TEST_F(FileUtilTest, CopyFile) {
649 // Create a directory
evanm@google.com874d1672008-10-31 08:54:04 +0900650 FilePath dir_name_from =
651 test_dir_.Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
652 file_util::CreateDirectory(dir_name_from);
initial.commit3f4a7322008-07-27 06:49:38 +0900653 ASSERT_TRUE(file_util::PathExists(dir_name_from));
654
655 // Create a file under the directory
evanm@google.com874d1672008-10-31 08:54:04 +0900656 FilePath file_name_from =
657 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900658 const std::wstring file_contents(L"Gooooooooooooooooooooogle");
659 CreateTextFile(file_name_from, file_contents);
660 ASSERT_TRUE(file_util::PathExists(file_name_from));
661
662 // Copy the file.
evanm@google.com874d1672008-10-31 08:54:04 +0900663 FilePath dest_file = dir_name_from.Append(FILE_PATH_LITERAL("DestFile.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900664 ASSERT_TRUE(file_util::CopyFile(file_name_from, dest_file));
mmoss@google.com733df6b2008-09-12 01:09:11 +0900665
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900666 // Copy the file to another location using '..' in the path.
evan@chromium.org1543ad32009-08-27 05:00:14 +0900667 FilePath dest_file2(dir_name_from);
668 dest_file2 = dest_file2.AppendASCII("..");
669 dest_file2 = dest_file2.AppendASCII("DestFile.txt");
670 ASSERT_TRUE(file_util::CopyFile(file_name_from, dest_file2));
671
672 FilePath dest_file2_test(dir_name_from);
673 dest_file2_test = dest_file2_test.DirName();
674 dest_file2_test = dest_file2_test.AppendASCII("DestFile.txt");
initial.commit3f4a7322008-07-27 06:49:38 +0900675
676 // Check everything has been copied.
677 EXPECT_TRUE(file_util::PathExists(file_name_from));
678 EXPECT_TRUE(file_util::PathExists(dest_file));
679 const std::wstring read_contents = ReadTextFile(dest_file);
680 EXPECT_EQ(file_contents, read_contents);
evan@chromium.org1543ad32009-08-27 05:00:14 +0900681 EXPECT_TRUE(file_util::PathExists(dest_file2_test));
682 EXPECT_TRUE(file_util::PathExists(dest_file2));
initial.commit3f4a7322008-07-27 06:49:38 +0900683}
684
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900685// TODO(erikkay): implement
erikkay@google.com014161d2008-08-16 02:45:13 +0900686#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +0900687TEST_F(FileUtilTest, GetFileCreationLocalTime) {
evanm@google.com874d1672008-10-31 08:54:04 +0900688 FilePath file_name = test_dir_.Append(L"Test File.txt");
initial.commit3f4a7322008-07-27 06:49:38 +0900689
690 SYSTEMTIME start_time;
691 GetLocalTime(&start_time);
692 Sleep(100);
693 CreateTextFile(file_name, L"New file!");
694 Sleep(100);
695 SYSTEMTIME end_time;
696 GetLocalTime(&end_time);
697
698 SYSTEMTIME file_creation_time;
evanm@google.com874d1672008-10-31 08:54:04 +0900699 file_util::GetFileCreationLocalTime(file_name.value(), &file_creation_time);
initial.commit3f4a7322008-07-27 06:49:38 +0900700
701 FILETIME start_filetime;
702 SystemTimeToFileTime(&start_time, &start_filetime);
703 FILETIME end_filetime;
704 SystemTimeToFileTime(&end_time, &end_filetime);
705 FILETIME file_creation_filetime;
706 SystemTimeToFileTime(&file_creation_time, &file_creation_filetime);
707
708 EXPECT_EQ(-1, CompareFileTime(&start_filetime, &file_creation_filetime)) <<
709 "start time: " << FileTimeAsUint64(start_filetime) << ", " <<
710 "creation time: " << FileTimeAsUint64(file_creation_filetime);
711
712 EXPECT_EQ(-1, CompareFileTime(&file_creation_filetime, &end_filetime)) <<
713 "creation time: " << FileTimeAsUint64(file_creation_filetime) << ", " <<
714 "end time: " << FileTimeAsUint64(end_filetime);
715
evanm@google.com874d1672008-10-31 08:54:04 +0900716 ASSERT_TRUE(DeleteFile(file_name.value().c_str()));
initial.commit3f4a7322008-07-27 06:49:38 +0900717}
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900718#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900719
erikkay@google.comf2406842008-08-21 00:59:49 +0900720// file_util winds up using autoreleased objects on the Mac, so this needs
evanm@google.com874d1672008-10-31 08:54:04 +0900721// to be a PlatformTest.
erikkay@google.comf2406842008-08-21 00:59:49 +0900722typedef PlatformTest ReadOnlyFileUtilTest;
initial.commit3f4a7322008-07-27 06:49:38 +0900723
erikkay@google.comf2406842008-08-21 00:59:49 +0900724TEST_F(ReadOnlyFileUtilTest, ContentsEqual) {
evanm@google.com874d1672008-10-31 08:54:04 +0900725 FilePath data_dir;
initial.commit3f4a7322008-07-27 06:49:38 +0900726 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &data_dir));
evanm@google.com874d1672008-10-31 08:54:04 +0900727 data_dir = data_dir.Append(FILE_PATH_LITERAL("base"))
728 .Append(FILE_PATH_LITERAL("data"))
729 .Append(FILE_PATH_LITERAL("file_util_unittest"));
initial.commit3f4a7322008-07-27 06:49:38 +0900730 ASSERT_TRUE(file_util::PathExists(data_dir));
731
evanm@google.com874d1672008-10-31 08:54:04 +0900732 FilePath original_file =
733 data_dir.Append(FILE_PATH_LITERAL("original.txt"));
734 FilePath same_file =
735 data_dir.Append(FILE_PATH_LITERAL("same.txt"));
736 FilePath same_length_file =
737 data_dir.Append(FILE_PATH_LITERAL("same_length.txt"));
738 FilePath different_file =
739 data_dir.Append(FILE_PATH_LITERAL("different.txt"));
740 FilePath different_first_file =
741 data_dir.Append(FILE_PATH_LITERAL("different_first.txt"));
742 FilePath different_last_file =
743 data_dir.Append(FILE_PATH_LITERAL("different_last.txt"));
744 FilePath empty1_file =
745 data_dir.Append(FILE_PATH_LITERAL("empty1.txt"));
746 FilePath empty2_file =
747 data_dir.Append(FILE_PATH_LITERAL("empty2.txt"));
748 FilePath shortened_file =
749 data_dir.Append(FILE_PATH_LITERAL("shortened.txt"));
750 FilePath binary_file =
751 data_dir.Append(FILE_PATH_LITERAL("binary_file.bin"));
752 FilePath binary_file_same =
753 data_dir.Append(FILE_PATH_LITERAL("binary_file_same.bin"));
754 FilePath binary_file_diff =
755 data_dir.Append(FILE_PATH_LITERAL("binary_file_diff.bin"));
initial.commit3f4a7322008-07-27 06:49:38 +0900756
757 EXPECT_TRUE(file_util::ContentsEqual(original_file, original_file));
758 EXPECT_TRUE(file_util::ContentsEqual(original_file, same_file));
759 EXPECT_FALSE(file_util::ContentsEqual(original_file, same_length_file));
760 EXPECT_FALSE(file_util::ContentsEqual(original_file, different_file));
761 EXPECT_FALSE(file_util::ContentsEqual(L"bogusname", L"bogusname"));
762 EXPECT_FALSE(file_util::ContentsEqual(original_file, different_first_file));
763 EXPECT_FALSE(file_util::ContentsEqual(original_file, different_last_file));
764 EXPECT_TRUE(file_util::ContentsEqual(empty1_file, empty2_file));
765 EXPECT_FALSE(file_util::ContentsEqual(original_file, shortened_file));
766 EXPECT_FALSE(file_util::ContentsEqual(shortened_file, original_file));
767 EXPECT_TRUE(file_util::ContentsEqual(binary_file, binary_file_same));
768 EXPECT_FALSE(file_util::ContentsEqual(binary_file, binary_file_diff));
769}
770
mark@chromium.org95c9ec92009-06-27 06:17:24 +0900771TEST_F(ReadOnlyFileUtilTest, TextContentsEqual) {
772 FilePath data_dir;
773 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &data_dir));
774 data_dir = data_dir.Append(FILE_PATH_LITERAL("base"))
775 .Append(FILE_PATH_LITERAL("data"))
776 .Append(FILE_PATH_LITERAL("file_util_unittest"));
777 ASSERT_TRUE(file_util::PathExists(data_dir));
778
779 FilePath original_file =
780 data_dir.Append(FILE_PATH_LITERAL("original.txt"));
781 FilePath same_file =
782 data_dir.Append(FILE_PATH_LITERAL("same.txt"));
783 FilePath crlf_file =
784 data_dir.Append(FILE_PATH_LITERAL("crlf.txt"));
785 FilePath shortened_file =
786 data_dir.Append(FILE_PATH_LITERAL("shortened.txt"));
787 FilePath different_file =
788 data_dir.Append(FILE_PATH_LITERAL("different.txt"));
789 FilePath different_first_file =
790 data_dir.Append(FILE_PATH_LITERAL("different_first.txt"));
791 FilePath different_last_file =
792 data_dir.Append(FILE_PATH_LITERAL("different_last.txt"));
793 FilePath first1_file =
794 data_dir.Append(FILE_PATH_LITERAL("first1.txt"));
795 FilePath first2_file =
796 data_dir.Append(FILE_PATH_LITERAL("first2.txt"));
797 FilePath empty1_file =
798 data_dir.Append(FILE_PATH_LITERAL("empty1.txt"));
799 FilePath empty2_file =
800 data_dir.Append(FILE_PATH_LITERAL("empty2.txt"));
801 FilePath blank_line_file =
802 data_dir.Append(FILE_PATH_LITERAL("blank_line.txt"));
803 FilePath blank_line_crlf_file =
804 data_dir.Append(FILE_PATH_LITERAL("blank_line_crlf.txt"));
805
806 EXPECT_TRUE(file_util::TextContentsEqual(original_file, same_file));
807 EXPECT_TRUE(file_util::TextContentsEqual(original_file, crlf_file));
808 EXPECT_FALSE(file_util::TextContentsEqual(original_file, shortened_file));
809 EXPECT_FALSE(file_util::TextContentsEqual(original_file, different_file));
810 EXPECT_FALSE(file_util::TextContentsEqual(original_file,
811 different_first_file));
812 EXPECT_FALSE(file_util::TextContentsEqual(original_file,
813 different_last_file));
814 EXPECT_FALSE(file_util::TextContentsEqual(first1_file, first2_file));
815 EXPECT_TRUE(file_util::TextContentsEqual(empty1_file, empty2_file));
816 EXPECT_FALSE(file_util::TextContentsEqual(original_file, empty1_file));
817 EXPECT_TRUE(file_util::TextContentsEqual(blank_line_file,
818 blank_line_crlf_file));
819}
820
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900821// We don't need equivalent functionality outside of Windows.
erikkay@google.com014161d2008-08-16 02:45:13 +0900822#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +0900823TEST_F(FileUtilTest, ResolveShortcutTest) {
evanm@google.com874d1672008-10-31 08:54:04 +0900824 FilePath target_file = test_dir_.Append(L"Target.txt");
initial.commit3f4a7322008-07-27 06:49:38 +0900825 CreateTextFile(target_file, L"This is the target.");
826
evanm@google.com874d1672008-10-31 08:54:04 +0900827 FilePath link_file = test_dir_.Append(L"Link.lnk");
initial.commit3f4a7322008-07-27 06:49:38 +0900828
829 HRESULT result;
830 IShellLink *shell = NULL;
831 IPersistFile *persist = NULL;
832
833 CoInitialize(NULL);
834 // Temporarily create a shortcut for test
835 result = CoCreateInstance(CLSID_ShellLink, NULL,
836 CLSCTX_INPROC_SERVER, IID_IShellLink,
837 reinterpret_cast<LPVOID*>(&shell));
838 EXPECT_TRUE(SUCCEEDED(result));
839 result = shell->QueryInterface(IID_IPersistFile,
840 reinterpret_cast<LPVOID*>(&persist));
841 EXPECT_TRUE(SUCCEEDED(result));
evanm@google.com874d1672008-10-31 08:54:04 +0900842 result = shell->SetPath(target_file.value().c_str());
initial.commit3f4a7322008-07-27 06:49:38 +0900843 EXPECT_TRUE(SUCCEEDED(result));
844 result = shell->SetDescription(L"ResolveShortcutTest");
845 EXPECT_TRUE(SUCCEEDED(result));
evanm@google.com874d1672008-10-31 08:54:04 +0900846 result = persist->Save(link_file.value().c_str(), TRUE);
initial.commit3f4a7322008-07-27 06:49:38 +0900847 EXPECT_TRUE(SUCCEEDED(result));
848 if (persist)
849 persist->Release();
850 if (shell)
851 shell->Release();
852
853 bool is_solved;
evan@chromium.orga4899042009-08-25 10:51:44 +0900854 is_solved = file_util::ResolveShortcut(&link_file);
initial.commit3f4a7322008-07-27 06:49:38 +0900855 EXPECT_TRUE(is_solved);
856 std::wstring contents;
evan@chromium.orga4899042009-08-25 10:51:44 +0900857 contents = ReadTextFile(link_file);
initial.commit3f4a7322008-07-27 06:49:38 +0900858 EXPECT_EQ(L"This is the target.", contents);
859
ericroman@google.comdbff4f52008-08-19 01:00:38 +0900860 // Cleaning
evanm@google.com874d1672008-10-31 08:54:04 +0900861 DeleteFile(target_file.value().c_str());
evan@chromium.orga4899042009-08-25 10:51:44 +0900862 DeleteFile(link_file.value().c_str());
initial.commit3f4a7322008-07-27 06:49:38 +0900863 CoUninitialize();
864}
865
866TEST_F(FileUtilTest, CreateShortcutTest) {
867 const wchar_t file_contents[] = L"This is another target.";
evanm@google.com874d1672008-10-31 08:54:04 +0900868 FilePath target_file = test_dir_.Append(L"Target1.txt");
initial.commit3f4a7322008-07-27 06:49:38 +0900869 CreateTextFile(target_file, file_contents);
870
evanm@google.com874d1672008-10-31 08:54:04 +0900871 FilePath link_file = test_dir_.Append(L"Link1.lnk");
initial.commit3f4a7322008-07-27 06:49:38 +0900872
873 CoInitialize(NULL);
evanm@google.com874d1672008-10-31 08:54:04 +0900874 EXPECT_TRUE(file_util::CreateShortcutLink(target_file.value().c_str(),
875 link_file.value().c_str(),
initial.commit3f4a7322008-07-27 06:49:38 +0900876 NULL, NULL, NULL, NULL, 0));
evan@chromium.orga4899042009-08-25 10:51:44 +0900877 FilePath resolved_name = link_file;
initial.commit3f4a7322008-07-27 06:49:38 +0900878 EXPECT_TRUE(file_util::ResolveShortcut(&resolved_name));
evan@chromium.orga4899042009-08-25 10:51:44 +0900879 std::wstring read_contents = ReadTextFile(resolved_name);
initial.commit3f4a7322008-07-27 06:49:38 +0900880 EXPECT_EQ(file_contents, read_contents);
881
evanm@google.com874d1672008-10-31 08:54:04 +0900882 DeleteFile(target_file.value().c_str());
883 DeleteFile(link_file.value().c_str());
initial.commit3f4a7322008-07-27 06:49:38 +0900884 CoUninitialize();
885}
huanr@chromium.org7f2c6af2009-03-12 03:37:48 +0900886
887TEST_F(FileUtilTest, CopyAndDeleteDirectoryTest) {
888 // Create a directory
889 FilePath dir_name_from =
890 test_dir_.Append(FILE_PATH_LITERAL("CopyAndDelete_From_Subdir"));
891 file_util::CreateDirectory(dir_name_from);
892 ASSERT_TRUE(file_util::PathExists(dir_name_from));
893
894 // Create a file under the directory
895 FilePath file_name_from =
896 dir_name_from.Append(FILE_PATH_LITERAL("CopyAndDelete_Test_File.txt"));
897 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
898 ASSERT_TRUE(file_util::PathExists(file_name_from));
899
900 // Move the directory by using CopyAndDeleteDirectory
901 FilePath dir_name_to = test_dir_.Append(
902 FILE_PATH_LITERAL("CopyAndDelete_To_Subdir"));
903 FilePath file_name_to =
904 dir_name_to.Append(FILE_PATH_LITERAL("CopyAndDelete_Test_File.txt"));
905
906 ASSERT_FALSE(file_util::PathExists(dir_name_to));
907
908 EXPECT_TRUE(file_util::CopyAndDeleteDirectory(dir_name_from, dir_name_to));
909
910 // Check everything has been moved.
911 EXPECT_FALSE(file_util::PathExists(dir_name_from));
912 EXPECT_FALSE(file_util::PathExists(file_name_from));
913 EXPECT_TRUE(file_util::PathExists(dir_name_to));
914 EXPECT_TRUE(file_util::PathExists(file_name_to));
915}
tkent@chromium.org8da14162009-10-09 16:33:39 +0900916
917TEST_F(FileUtilTest, GetTempDirTest) {
918 static const TCHAR* kTmpKey = _T("TMP");
919 static const TCHAR* kTmpValues[] = {
920 _T(""), _T("C:"), _T("C:\\"), _T("C:\\tmp"), _T("C:\\tmp\\")
921 };
922 // Save the original $TMP.
923 size_t original_tmp_size;
924 TCHAR* original_tmp;
925 ASSERT_EQ(0, ::_tdupenv_s(&original_tmp, &original_tmp_size, kTmpKey));
926 // original_tmp may be NULL.
927
928 for (unsigned int i = 0; i < arraysize(kTmpValues); ++i) {
929 FilePath path;
930 ::_tputenv_s(kTmpKey, kTmpValues[i]);
931 file_util::GetTempDir(&path);
932 EXPECT_TRUE(path.IsAbsolute()) << "$TMP=" << kTmpValues[i] <<
933 " result=" << path.value();
934 }
935
936 // Restore the original $TMP.
937 if (original_tmp) {
938 ::_tputenv_s(kTmpKey, original_tmp);
939 free(original_tmp);
940 } else {
941 ::_tputenv_s(kTmpKey, _T(""));
942 }
943}
944#endif // OS_WIN
initial.commit3f4a7322008-07-27 06:49:38 +0900945
erikkay@chromium.org18f0dde2009-08-19 01:07:55 +0900946TEST_F(FileUtilTest, CreateTemporaryFileTest) {
947 FilePath temp_files[3];
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +0900948 for (int i = 0; i < 3; i++) {
erikkay@chromium.org18f0dde2009-08-19 01:07:55 +0900949 ASSERT_TRUE(file_util::CreateTemporaryFile(&(temp_files[i])));
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +0900950 EXPECT_TRUE(file_util::PathExists(temp_files[i]));
951 EXPECT_FALSE(file_util::DirectoryExists(temp_files[i]));
952 }
953 for (int i = 0; i < 3; i++)
954 EXPECT_FALSE(temp_files[i] == temp_files[(i+1)%3]);
955 for (int i = 0; i < 3; i++)
956 EXPECT_TRUE(file_util::Delete(temp_files[i], false));
957}
958
erikkay@chromium.org18f0dde2009-08-19 01:07:55 +0900959TEST_F(FileUtilTest, CreateAndOpenTemporaryFileTest) {
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +0900960 FilePath names[3];
961 FILE *fps[3];
962 int i;
963
964 // Create; make sure they are open and exist.
965 for (i = 0; i < 3; ++i) {
966 fps[i] = file_util::CreateAndOpenTemporaryFile(&(names[i]));
967 ASSERT_TRUE(fps[i]);
968 EXPECT_TRUE(file_util::PathExists(names[i]));
969 }
970
971 // Make sure all names are unique.
972 for (i = 0; i < 3; ++i) {
973 EXPECT_FALSE(names[i] == names[(i+1)%3]);
974 }
975
976 // Close and delete.
977 for (i = 0; i < 3; ++i) {
978 EXPECT_TRUE(file_util::CloseFile(fps[i]));
979 EXPECT_TRUE(file_util::Delete(names[i], false));
980 }
initial.commit3f4a7322008-07-27 06:49:38 +0900981}
982
983TEST_F(FileUtilTest, CreateNewTempDirectoryTest) {
evan@chromium.org1543ad32009-08-27 05:00:14 +0900984 FilePath temp_dir;
985 ASSERT_TRUE(file_util::CreateNewTempDirectory(FilePath::StringType(),
986 &temp_dir));
mmoss@google.com733df6b2008-09-12 01:09:11 +0900987 EXPECT_TRUE(file_util::PathExists(temp_dir));
988 EXPECT_TRUE(file_util::Delete(temp_dir, false));
initial.commit3f4a7322008-07-27 06:49:38 +0900989}
990
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +0900991TEST_F(FileUtilTest, GetShmemTempDirTest) {
992 FilePath dir;
993 EXPECT_TRUE(file_util::GetShmemTempDir(&dir));
994 EXPECT_TRUE(file_util::DirectoryExists(dir));
995}
996
initial.commit3f4a7322008-07-27 06:49:38 +0900997TEST_F(FileUtilTest, CreateDirectoryTest) {
evanm@google.com874d1672008-10-31 08:54:04 +0900998 FilePath test_root =
999 test_dir_.Append(FILE_PATH_LITERAL("create_directory_test"));
erikkay@google.com014161d2008-08-16 02:45:13 +09001000#if defined(OS_WIN)
evanm@google.com874d1672008-10-31 08:54:04 +09001001 FilePath test_path =
1002 test_root.Append(FILE_PATH_LITERAL("dir\\tree\\likely\\doesnt\\exist\\"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001003#elif defined(OS_POSIX)
evanm@google.com874d1672008-10-31 08:54:04 +09001004 FilePath test_path =
1005 test_root.Append(FILE_PATH_LITERAL("dir/tree/likely/doesnt/exist/"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001006#endif
mmoss@google.com733df6b2008-09-12 01:09:11 +09001007
1008 EXPECT_FALSE(file_util::PathExists(test_path));
1009 EXPECT_TRUE(file_util::CreateDirectory(test_path));
1010 EXPECT_TRUE(file_util::PathExists(test_path));
1011 // CreateDirectory returns true if the DirectoryExists returns true.
1012 EXPECT_TRUE(file_util::CreateDirectory(test_path));
1013
1014 // Doesn't work to create it on top of a non-dir
evanm@google.com874d1672008-10-31 08:54:04 +09001015 test_path = test_path.Append(FILE_PATH_LITERAL("foobar.txt"));
mmoss@google.com733df6b2008-09-12 01:09:11 +09001016 EXPECT_FALSE(file_util::PathExists(test_path));
1017 CreateTextFile(test_path, L"test file");
1018 EXPECT_TRUE(file_util::PathExists(test_path));
1019 EXPECT_FALSE(file_util::CreateDirectory(test_path));
1020
1021 EXPECT_TRUE(file_util::Delete(test_root, true));
1022 EXPECT_FALSE(file_util::PathExists(test_root));
1023 EXPECT_FALSE(file_util::PathExists(test_path));
1024}
1025
1026TEST_F(FileUtilTest, DetectDirectoryTest) {
1027 // Check a directory
evanm@google.com874d1672008-10-31 08:54:04 +09001028 FilePath test_root =
1029 test_dir_.Append(FILE_PATH_LITERAL("detect_directory_test"));
mmoss@google.com733df6b2008-09-12 01:09:11 +09001030 EXPECT_FALSE(file_util::PathExists(test_root));
1031 EXPECT_TRUE(file_util::CreateDirectory(test_root));
1032 EXPECT_TRUE(file_util::PathExists(test_root));
1033 EXPECT_TRUE(file_util::DirectoryExists(test_root));
1034
1035 // Check a file
evanm@google.com874d1672008-10-31 08:54:04 +09001036 FilePath test_path =
1037 test_root.Append(FILE_PATH_LITERAL("foobar.txt"));
mmoss@google.com733df6b2008-09-12 01:09:11 +09001038 EXPECT_FALSE(file_util::PathExists(test_path));
1039 CreateTextFile(test_path, L"test file");
1040 EXPECT_TRUE(file_util::PathExists(test_path));
1041 EXPECT_FALSE(file_util::DirectoryExists(test_path));
1042 EXPECT_TRUE(file_util::Delete(test_path, false));
1043
1044 EXPECT_TRUE(file_util::Delete(test_root, true));
initial.commit3f4a7322008-07-27 06:49:38 +09001045}
1046
initial.commit3f4a7322008-07-27 06:49:38 +09001047static const struct ReplaceExtensionCase {
1048 std::wstring file_name;
estade@chromium.org63343202008-12-05 05:46:06 +09001049 FilePath::StringType extension;
initial.commit3f4a7322008-07-27 06:49:38 +09001050 std::wstring result;
1051} kReplaceExtension[] = {
estade@chromium.org63343202008-12-05 05:46:06 +09001052 {L"", FILE_PATH_LITERAL(""), L""},
1053 {L"", FILE_PATH_LITERAL("txt"), L".txt"},
1054 {L".", FILE_PATH_LITERAL("txt"), L".txt"},
1055 {L".", FILE_PATH_LITERAL(""), L""},
1056 {L"foo.dll", FILE_PATH_LITERAL("txt"), L"foo.txt"},
1057 {L"foo.dll", FILE_PATH_LITERAL(".txt"), L"foo.txt"},
1058 {L"foo", FILE_PATH_LITERAL("txt"), L"foo.txt"},
1059 {L"foo", FILE_PATH_LITERAL(".txt"), L"foo.txt"},
1060 {L"foo.baz.dll", FILE_PATH_LITERAL("txt"), L"foo.baz.txt"},
1061 {L"foo.baz.dll", FILE_PATH_LITERAL(".txt"), L"foo.baz.txt"},
1062 {L"foo.dll", FILE_PATH_LITERAL(""), L"foo"},
1063 {L"foo.dll", FILE_PATH_LITERAL("."), L"foo"},
1064 {L"foo", FILE_PATH_LITERAL(""), L"foo"},
1065 {L"foo", FILE_PATH_LITERAL("."), L"foo"},
1066 {L"foo.baz.dll", FILE_PATH_LITERAL(""), L"foo.baz"},
1067 {L"foo.baz.dll", FILE_PATH_LITERAL("."), L"foo.baz"},
initial.commit3f4a7322008-07-27 06:49:38 +09001068};
1069
1070TEST_F(FileUtilTest, ReplaceExtensionTest) {
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001071 for (unsigned int i = 0; i < arraysize(kReplaceExtension); ++i) {
estade@chromium.org63343202008-12-05 05:46:06 +09001072 FilePath path = FilePath::FromWStringHack(kReplaceExtension[i].file_name);
1073 file_util::ReplaceExtension(&path, kReplaceExtension[i].extension);
1074 EXPECT_EQ(kReplaceExtension[i].result, path.ToWStringHack());
initial.commit3f4a7322008-07-27 06:49:38 +09001075 }
1076}
1077
sky@google.com71e7c6f2008-09-20 02:32:18 +09001078// Make sure ReplaceExtension doesn't replace an extension that occurs as one of
1079// the directory names of the path.
1080TEST_F(FileUtilTest, ReplaceExtensionTestWithPathSeparators) {
estade@chromium.org63343202008-12-05 05:46:06 +09001081 FilePath path;
1082 path = path.Append(FILE_PATH_LITERAL("foo.bar"));
1083 path = path.Append(FILE_PATH_LITERAL("foo"));
sky@google.com71e7c6f2008-09-20 02:32:18 +09001084 // '/foo.bar/foo' with extension '.baz'
estade@chromium.org63343202008-12-05 05:46:06 +09001085 FilePath result_path = path;
1086 file_util::ReplaceExtension(&result_path, FILE_PATH_LITERAL(".baz"));
phajdan.jr@chromium.orgf9908a72009-04-04 02:17:58 +09001087 EXPECT_EQ(path.value() + FILE_PATH_LITERAL(".baz"),
1088 result_path.value());
sky@google.com71e7c6f2008-09-20 02:32:18 +09001089}
1090
initial.commit3f4a7322008-07-27 06:49:38 +09001091TEST_F(FileUtilTest, FileEnumeratorTest) {
1092 // Test an empty directory.
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001093 file_util::FileEnumerator f0(test_dir_, true, FILES_AND_DIRECTORIES);
avi@google.com5cb79352008-12-11 23:55:12 +09001094 EXPECT_EQ(f0.Next().value(), FILE_PATH_LITERAL(""));
1095 EXPECT_EQ(f0.Next().value(), FILE_PATH_LITERAL(""));
initial.commit3f4a7322008-07-27 06:49:38 +09001096
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001097 // Test an empty directory, non-recursively, including "..".
1098 file_util::FileEnumerator f0_dotdot(test_dir_, false,
1099 static_cast<file_util::FileEnumerator::FILE_TYPE>(
1100 FILES_AND_DIRECTORIES | file_util::FileEnumerator::INCLUDE_DOT_DOT));
1101 EXPECT_EQ(test_dir_.Append(FILE_PATH_LITERAL("..")).value(),
1102 f0_dotdot.Next().value());
1103 EXPECT_EQ(FILE_PATH_LITERAL(""),
1104 f0_dotdot.Next().value());
1105
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001106 // create the directories
evanm@google.com874d1672008-10-31 08:54:04 +09001107 FilePath dir1 = test_dir_.Append(FILE_PATH_LITERAL("dir1"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001108 EXPECT_TRUE(file_util::CreateDirectory(dir1));
evanm@google.com874d1672008-10-31 08:54:04 +09001109 FilePath dir2 = test_dir_.Append(FILE_PATH_LITERAL("dir2"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001110 EXPECT_TRUE(file_util::CreateDirectory(dir2));
evanm@google.com874d1672008-10-31 08:54:04 +09001111 FilePath dir2inner = dir2.Append(FILE_PATH_LITERAL("inner"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001112 EXPECT_TRUE(file_util::CreateDirectory(dir2inner));
evanm@google.com874d1672008-10-31 08:54:04 +09001113
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001114 // create the files
evanm@google.com874d1672008-10-31 08:54:04 +09001115 FilePath dir2file = dir2.Append(FILE_PATH_LITERAL("dir2file.txt"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001116 CreateTextFile(dir2file, L"");
evanm@google.com874d1672008-10-31 08:54:04 +09001117 FilePath dir2innerfile = dir2inner.Append(FILE_PATH_LITERAL("innerfile.txt"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001118 CreateTextFile(dir2innerfile, L"");
evanm@google.com874d1672008-10-31 08:54:04 +09001119 FilePath file1 = test_dir_.Append(FILE_PATH_LITERAL("file1.txt"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001120 CreateTextFile(file1, L"");
evanm@google.com874d1672008-10-31 08:54:04 +09001121 FilePath file2_rel =
1122 dir2.Append(FilePath::kParentDirectory)
1123 .Append(FILE_PATH_LITERAL("file2.txt"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001124 CreateTextFile(file2_rel, L"");
evanm@google.com874d1672008-10-31 08:54:04 +09001125 FilePath file2_abs = test_dir_.Append(FILE_PATH_LITERAL("file2.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +09001126
1127 // Only enumerate files.
avi@google.com5cb79352008-12-11 23:55:12 +09001128 file_util::FileEnumerator f1(test_dir_, true,
initial.commit3f4a7322008-07-27 06:49:38 +09001129 file_util::FileEnumerator::FILES);
1130 FindResultCollector c1(f1);
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001131 EXPECT_TRUE(c1.HasFile(file1));
1132 EXPECT_TRUE(c1.HasFile(file2_abs));
1133 EXPECT_TRUE(c1.HasFile(dir2file));
1134 EXPECT_TRUE(c1.HasFile(dir2innerfile));
1135 EXPECT_EQ(c1.size(), 4);
initial.commit3f4a7322008-07-27 06:49:38 +09001136
1137 // Only enumerate directories.
avi@google.com5cb79352008-12-11 23:55:12 +09001138 file_util::FileEnumerator f2(test_dir_, true,
initial.commit3f4a7322008-07-27 06:49:38 +09001139 file_util::FileEnumerator::DIRECTORIES);
1140 FindResultCollector c2(f2);
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001141 EXPECT_TRUE(c2.HasFile(dir1));
1142 EXPECT_TRUE(c2.HasFile(dir2));
1143 EXPECT_TRUE(c2.HasFile(dir2inner));
1144 EXPECT_EQ(c2.size(), 3);
initial.commit3f4a7322008-07-27 06:49:38 +09001145
tim@chromium.org989d0972008-10-16 11:42:45 +09001146 // Only enumerate directories non-recursively.
1147 file_util::FileEnumerator f2_non_recursive(
avi@google.com5cb79352008-12-11 23:55:12 +09001148 test_dir_, false, file_util::FileEnumerator::DIRECTORIES);
tim@chromium.org989d0972008-10-16 11:42:45 +09001149 FindResultCollector c2_non_recursive(f2_non_recursive);
1150 EXPECT_TRUE(c2_non_recursive.HasFile(dir1));
1151 EXPECT_TRUE(c2_non_recursive.HasFile(dir2));
1152 EXPECT_EQ(c2_non_recursive.size(), 2);
1153
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001154 // Only enumerate directories, non-recursively, including "..".
1155 file_util::FileEnumerator f2_dotdot(
1156 test_dir_, false,
1157 static_cast<file_util::FileEnumerator::FILE_TYPE>(
1158 file_util::FileEnumerator::DIRECTORIES |
1159 file_util::FileEnumerator::INCLUDE_DOT_DOT));
1160 FindResultCollector c2_dotdot(f2_dotdot);
1161 EXPECT_TRUE(c2_dotdot.HasFile(dir1));
1162 EXPECT_TRUE(c2_dotdot.HasFile(dir2));
1163 EXPECT_TRUE(c2_dotdot.HasFile(test_dir_.Append(FILE_PATH_LITERAL(".."))));
1164 EXPECT_EQ(c2_dotdot.size(), 3);
1165
initial.commit3f4a7322008-07-27 06:49:38 +09001166 // Enumerate files and directories.
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001167 file_util::FileEnumerator f3(test_dir_, true, FILES_AND_DIRECTORIES);
initial.commit3f4a7322008-07-27 06:49:38 +09001168 FindResultCollector c3(f3);
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001169 EXPECT_TRUE(c3.HasFile(dir1));
1170 EXPECT_TRUE(c3.HasFile(dir2));
1171 EXPECT_TRUE(c3.HasFile(file1));
1172 EXPECT_TRUE(c3.HasFile(file2_abs));
1173 EXPECT_TRUE(c3.HasFile(dir2file));
1174 EXPECT_TRUE(c3.HasFile(dir2inner));
1175 EXPECT_TRUE(c3.HasFile(dir2innerfile));
1176 EXPECT_EQ(c3.size(), 7);
initial.commit3f4a7322008-07-27 06:49:38 +09001177
1178 // Non-recursive operation.
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001179 file_util::FileEnumerator f4(test_dir_, false, FILES_AND_DIRECTORIES);
initial.commit3f4a7322008-07-27 06:49:38 +09001180 FindResultCollector c4(f4);
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001181 EXPECT_TRUE(c4.HasFile(dir2));
1182 EXPECT_TRUE(c4.HasFile(dir2));
1183 EXPECT_TRUE(c4.HasFile(file1));
1184 EXPECT_TRUE(c4.HasFile(file2_abs));
1185 EXPECT_EQ(c4.size(), 4);
initial.commit3f4a7322008-07-27 06:49:38 +09001186
1187 // Enumerate with a pattern.
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001188 file_util::FileEnumerator f5(test_dir_, true, FILES_AND_DIRECTORIES,
avi@google.com5cb79352008-12-11 23:55:12 +09001189 FILE_PATH_LITERAL("dir*"));
initial.commit3f4a7322008-07-27 06:49:38 +09001190 FindResultCollector c5(f5);
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001191 EXPECT_TRUE(c5.HasFile(dir1));
1192 EXPECT_TRUE(c5.HasFile(dir2));
1193 EXPECT_TRUE(c5.HasFile(dir2file));
1194 EXPECT_TRUE(c5.HasFile(dir2inner));
1195 EXPECT_TRUE(c5.HasFile(dir2innerfile));
1196 EXPECT_EQ(c5.size(), 5);
initial.commit3f4a7322008-07-27 06:49:38 +09001197
1198 // Make sure the destructor closes the find handle while in the middle of a
1199 // query to allow TearDown to delete the directory.
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001200 file_util::FileEnumerator f6(test_dir_, true, FILES_AND_DIRECTORIES);
avi@google.com5cb79352008-12-11 23:55:12 +09001201 EXPECT_FALSE(f6.Next().value().empty()); // Should have found something
1202 // (we don't care what).
initial.commit3f4a7322008-07-27 06:49:38 +09001203}
license.botf003cfe2008-08-24 09:55:55 +09001204
aa@chromium.orga4dbdf22009-01-10 07:14:27 +09001205TEST_F(FileUtilTest, Contains) {
thestig@chromium.org4cfbf7a2009-03-11 03:20:44 +09001206 FilePath data_dir = test_dir_.Append(FILE_PATH_LITERAL("FilePathTest"));
aa@chromium.orga4dbdf22009-01-10 07:14:27 +09001207
1208 // Create a fresh, empty copy of this directory.
rvargas@google.com5a0ae3b2009-01-31 10:19:57 +09001209 if (file_util::PathExists(data_dir)) {
1210 ASSERT_TRUE(file_util::Delete(data_dir, true));
1211 }
aa@chromium.orga4dbdf22009-01-10 07:14:27 +09001212 ASSERT_TRUE(file_util::CreateDirectory(data_dir));
1213
1214 FilePath foo(data_dir.Append(FILE_PATH_LITERAL("foo")));
1215 FilePath bar(foo.Append(FILE_PATH_LITERAL("bar.txt")));
1216 FilePath baz(data_dir.Append(FILE_PATH_LITERAL("baz.txt")));
1217 FilePath foobar(data_dir.Append(FILE_PATH_LITERAL("foobar.txt")));
1218
1219 // Annoyingly, the directories must actually exist in order for realpath(),
1220 // which Contains() relies on in posix, to work.
1221 ASSERT_TRUE(file_util::CreateDirectory(foo));
1222 std::string data("hello");
phajdan.jr@chromium.orgf9908a72009-04-04 02:17:58 +09001223 ASSERT_TRUE(file_util::WriteFile(bar, data.c_str(), data.length()));
1224 ASSERT_TRUE(file_util::WriteFile(baz, data.c_str(), data.length()));
1225 ASSERT_TRUE(file_util::WriteFile(foobar, data.c_str(), data.length()));
aa@chromium.orga4dbdf22009-01-10 07:14:27 +09001226
1227 EXPECT_TRUE(file_util::ContainsPath(foo, bar));
1228 EXPECT_FALSE(file_util::ContainsPath(foo, baz));
1229 EXPECT_FALSE(file_util::ContainsPath(foo, foobar));
1230 EXPECT_FALSE(file_util::ContainsPath(foo, foo));
1231
1232// Platform-specific concerns
1233 FilePath foo_caps(data_dir.Append(FILE_PATH_LITERAL("FOO")));
1234#if defined(OS_WIN)
1235 EXPECT_TRUE(file_util::ContainsPath(foo,
1236 foo_caps.Append(FILE_PATH_LITERAL("bar.txt"))));
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +09001237 EXPECT_TRUE(file_util::ContainsPath(foo,
aa@chromium.orga4dbdf22009-01-10 07:14:27 +09001238 FilePath(foo.value() + FILE_PATH_LITERAL("/bar.txt"))));
1239#elif defined(OS_LINUX)
1240 EXPECT_FALSE(file_util::ContainsPath(foo,
1241 foo_caps.Append(FILE_PATH_LITERAL("bar.txt"))));
1242#else
1243 // We can't really do this test on osx since the case-sensitivity of the
1244 // filesystem is configurable.
1245#endif
1246}
1247
mark@chromium.org17684802008-09-10 09:16:28 +09001248} // namespace