OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "base/files/file_enumerator.h" | |
6 | |
7 #include <deque> | |
8 #include <utility> | |
9 #include <vector> | |
10 | |
11 #include "base/files/file_path.h" | |
12 #include "base/files/file_util.h" | |
13 #include "base/files/scoped_temp_dir.h" | |
14 #include "testing/gmock/include/gmock/gmock.h" | |
15 #include "testing/gtest/include/gtest/gtest.h" | |
16 | |
17 using testing::ElementsAre; | |
18 using testing::IsEmpty; | |
19 using testing::UnorderedElementsAre; | |
20 | |
21 namespace base { | |
22 namespace { | |
23 | |
24 const FilePath::StringType kEmptyPattern; | |
25 | |
26 const std::vector<FileEnumerator::FolderSearchPolicy> kFolderSearchPolicies{ | |
27 FileEnumerator::FolderSearchPolicy::MATCH_ONLY, | |
28 FileEnumerator::FolderSearchPolicy::ALL}; | |
29 | |
30 std::deque<FilePath> RunEnumerator( | |
31 const FilePath& root_path, | |
32 bool recursive, | |
33 int file_type, | |
34 const FilePath::StringType& pattern, | |
35 FileEnumerator::FolderSearchPolicy folder_search_policy) { | |
36 std::deque<FilePath> rv; | |
37 FileEnumerator enumerator(root_path, recursive, file_type, pattern, | |
38 folder_search_policy); | |
39 for (auto file = enumerator.Next(); !file.empty(); file = enumerator.Next()) | |
40 rv.emplace_back(std::move(file)); | |
41 return rv; | |
42 } | |
43 | |
44 bool CreateFile(const FilePath& path) { | |
45 return WriteFile(path, "42", sizeof("42")) == sizeof("42"); | |
46 } | |
47 | |
48 } // namespace | |
49 | |
50 TEST(FileEnumerator, NotExistingPath) { | |
51 FilePath path; | |
Lei Zhang
2017/06/01 01:37:28
Just initialize this to some dummy value and asser
ivafanas
2017/06/01 08:29:19
Done.
| |
52 { | |
53 ScopedTempDir temp_dir; | |
54 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | |
55 path = temp_dir.GetPath(); | |
56 } | |
57 | |
58 for (auto policy : kFolderSearchPolicies) { | |
59 const auto files = RunEnumerator( | |
60 path, true, FileEnumerator::FILES & FileEnumerator::DIRECTORIES, | |
61 FILE_PATH_LITERAL(""), policy); | |
62 EXPECT_THAT(files, IsEmpty()); | |
63 } | |
64 } | |
65 | |
66 TEST(FileEnumerator, EmptyFolder) { | |
67 ScopedTempDir temp_dir; | |
68 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | |
69 | |
70 for (auto policy : {FileEnumerator::FolderSearchPolicy::MATCH_ONLY, | |
Lei Zhang
2017/06/01 01:37:29
Isn't this kFolderSearchPolicies?
ivafanas
2017/06/01 08:29:19
Done.
| |
71 FileEnumerator::FolderSearchPolicy::ALL}) { | |
72 const auto files = | |
73 RunEnumerator(temp_dir.GetPath(), true, | |
74 FileEnumerator::FILES & FileEnumerator::DIRECTORIES, | |
75 kEmptyPattern, policy); | |
76 EXPECT_THAT(files, IsEmpty()); | |
77 } | |
78 } | |
79 | |
80 TEST(FileEnumerator, SingleFileInFolderForFileSearch) { | |
81 ScopedTempDir temp_dir; | |
82 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | |
83 | |
84 const auto& path = temp_dir.GetPath(); | |
85 const auto filepath = path.AppendASCII("test.txt"); | |
86 ASSERT_TRUE(CreateFile(filepath)); | |
87 | |
88 for (auto policy : kFolderSearchPolicies) { | |
89 const auto files = RunEnumerator( | |
90 temp_dir.GetPath(), true, FileEnumerator::FILES, kEmptyPattern, policy); | |
91 EXPECT_THAT(files, ElementsAre(filepath)); | |
92 } | |
93 } | |
94 | |
95 TEST(FileEnumerator, SingleFileInFolderForDirSearch) { | |
96 ScopedTempDir temp_dir; | |
97 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | |
98 | |
99 const auto& path = temp_dir.GetPath(); | |
100 ASSERT_TRUE(CreateFile(path.AppendASCII("test.txt"))); | |
101 | |
102 for (auto policy : kFolderSearchPolicies) { | |
103 const auto files = RunEnumerator(path, true, FileEnumerator::DIRECTORIES, | |
104 kEmptyPattern, policy); | |
105 EXPECT_THAT(files, IsEmpty()); | |
106 } | |
107 } | |
108 | |
109 TEST(FileEnumerator, SingleFileInFolderWithFiltering) { | |
110 ScopedTempDir temp_dir; | |
111 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | |
112 | |
113 const auto& path = temp_dir.GetPath(); | |
114 const auto filepath = path.AppendASCII("test.txt"); | |
115 ASSERT_TRUE(CreateFile(filepath)); | |
116 | |
117 for (auto policy : kFolderSearchPolicies) { | |
118 auto files = RunEnumerator(path, true, FileEnumerator::FILES, | |
119 FILE_PATH_LITERAL("*.txt"), policy); | |
120 EXPECT_THAT(files, ElementsAre(filepath)); | |
121 | |
122 files = RunEnumerator(path, true, FileEnumerator::FILES, | |
123 FILE_PATH_LITERAL("*.pdf"), policy); | |
124 EXPECT_THAT(files, IsEmpty()); | |
125 } | |
126 } | |
127 | |
128 TEST(FileEnumerator, TwoFilesInFolder) { | |
129 ScopedTempDir temp_dir; | |
130 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | |
131 | |
132 const auto& path = temp_dir.GetPath(); | |
133 const auto filepath_foo = path.AppendASCII("foo.txt"); | |
134 const auto filepath_bar = path.AppendASCII("bar.txt"); | |
135 ASSERT_TRUE(CreateFile(filepath_foo)); | |
136 ASSERT_TRUE(CreateFile(filepath_bar)); | |
137 | |
138 for (auto policy : kFolderSearchPolicies) { | |
139 auto files = RunEnumerator(path, true, FileEnumerator::FILES, | |
140 FILE_PATH_LITERAL("*.txt"), policy); | |
141 EXPECT_THAT(files, UnorderedElementsAre(filepath_foo, filepath_bar)); | |
142 | |
143 files = RunEnumerator(path, true, FileEnumerator::FILES, | |
144 FILE_PATH_LITERAL("foo*"), policy); | |
145 EXPECT_THAT(files, ElementsAre(filepath_foo)); | |
146 | |
147 files = RunEnumerator(path, true, FileEnumerator::FILES, | |
148 FILE_PATH_LITERAL("*.pdf"), policy); | |
149 EXPECT_THAT(files, IsEmpty()); | |
150 | |
151 files = | |
152 RunEnumerator(path, true, FileEnumerator::FILES, kEmptyPattern, policy); | |
153 EXPECT_THAT(files, UnorderedElementsAre(filepath_foo, filepath_bar)); | |
154 } | |
155 } | |
156 | |
157 TEST(FileEnumerator, SingleFolderInFolderForFileSearch) { | |
158 ScopedTempDir temp_dir; | |
159 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | |
160 | |
161 const auto& path = temp_dir.GetPath(); | |
162 | |
163 ScopedTempDir temp_subdir; | |
164 ASSERT_TRUE(temp_subdir.CreateUniqueTempDirUnderPath(path)); | |
165 | |
166 for (auto policy : kFolderSearchPolicies) { | |
167 const auto files = | |
168 RunEnumerator(path, true, FileEnumerator::FILES, kEmptyPattern, policy); | |
169 EXPECT_THAT(files, IsEmpty()); | |
170 } | |
171 } | |
172 | |
173 TEST(FileEnumerator, SingleFolderInFolderForDirSearch) { | |
174 ScopedTempDir temp_dir; | |
175 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | |
176 | |
177 const auto& path = temp_dir.GetPath(); | |
178 | |
179 ScopedTempDir temp_subdir; | |
180 ASSERT_TRUE(temp_subdir.CreateUniqueTempDirUnderPath(path)); | |
181 | |
182 for (auto policy : kFolderSearchPolicies) { | |
183 const auto files = RunEnumerator(path, true, FileEnumerator::DIRECTORIES, | |
184 kEmptyPattern, policy); | |
185 EXPECT_THAT(files, ElementsAre(temp_subdir.GetPath())); | |
186 } | |
187 } | |
188 | |
189 TEST(FileEnumerator, TwoFoldersInFolder) { | |
190 ScopedTempDir temp_dir; | |
191 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | |
192 | |
193 const auto& path = temp_dir.GetPath(); | |
194 | |
195 const auto subdir_foo = path.AppendASCII("foo"); | |
196 const auto subdir_bar = path.AppendASCII("bar"); | |
197 ASSERT_TRUE(CreateDirectory(subdir_foo)); | |
198 ASSERT_TRUE(CreateDirectory(subdir_bar)); | |
199 | |
200 for (auto policy : kFolderSearchPolicies) { | |
201 auto files = RunEnumerator(path, true, FileEnumerator::DIRECTORIES, | |
202 kEmptyPattern, policy); | |
203 EXPECT_THAT(files, UnorderedElementsAre(subdir_foo, subdir_bar)); | |
204 | |
205 files = RunEnumerator(path, true, FileEnumerator::DIRECTORIES, | |
206 FILE_PATH_LITERAL("foo"), policy); | |
207 EXPECT_THAT(files, ElementsAre(subdir_foo)); | |
208 } | |
209 } | |
210 | |
211 TEST(FileEnumerator, FolderAndFileInFolder) { | |
212 ScopedTempDir temp_dir; | |
213 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | |
214 | |
215 const auto& path = temp_dir.GetPath(); | |
216 | |
217 ScopedTempDir temp_subdir; | |
218 ASSERT_TRUE(temp_subdir.CreateUniqueTempDirUnderPath(path)); | |
219 const auto filepath = path.AppendASCII("test.txt"); | |
220 ASSERT_TRUE(CreateFile(filepath)); | |
221 | |
222 for (auto policy : kFolderSearchPolicies) { | |
223 auto files = | |
224 RunEnumerator(path, true, FileEnumerator::FILES, kEmptyPattern, policy); | |
225 EXPECT_THAT(files, ElementsAre(filepath)); | |
226 | |
227 files = RunEnumerator(path, true, FileEnumerator::DIRECTORIES, | |
228 kEmptyPattern, policy); | |
229 EXPECT_THAT(files, ElementsAre(temp_subdir.GetPath())); | |
230 | |
231 files = RunEnumerator(path, true, | |
232 FileEnumerator::FILES | FileEnumerator::DIRECTORIES, | |
233 kEmptyPattern, policy); | |
234 EXPECT_THAT(files, UnorderedElementsAre(filepath, temp_subdir.GetPath())); | |
235 } | |
236 } | |
237 | |
238 TEST(FileEnumerator, FilesInParentFolderAlwaysFirst) { | |
239 ScopedTempDir temp_dir; | |
240 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | |
241 | |
242 const auto& path = temp_dir.GetPath(); | |
243 | |
244 ScopedTempDir temp_subdir; | |
245 ASSERT_TRUE(temp_subdir.CreateUniqueTempDirUnderPath(path)); | |
246 const auto filepath_foo = path.AppendASCII("foo.txt"); | |
247 const auto filepath_bar = temp_subdir.GetPath().AppendASCII("bar.txt"); | |
248 ASSERT_TRUE(CreateFile(filepath_foo)); | |
249 ASSERT_TRUE(CreateFile(filepath_bar)); | |
250 | |
251 for (auto policy : kFolderSearchPolicies) { | |
252 const auto files = | |
253 RunEnumerator(path, true, FileEnumerator::FILES, kEmptyPattern, policy); | |
254 EXPECT_THAT(files, ElementsAre(filepath_foo, filepath_bar)); | |
255 } | |
256 } | |
257 | |
258 TEST(FileEnumerator, FileInSubfolder) { | |
259 ScopedTempDir temp_dir; | |
260 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | |
261 | |
262 const auto subdir = temp_dir.GetPath().AppendASCII("subdir"); | |
263 ASSERT_TRUE(CreateDirectory(subdir)); | |
264 | |
265 const auto filepath = subdir.AppendASCII("test.txt"); | |
266 ASSERT_TRUE(CreateFile(filepath)); | |
267 | |
268 for (auto policy : kFolderSearchPolicies) { | |
269 auto files = RunEnumerator(temp_dir.GetPath(), true, FileEnumerator::FILES, | |
270 kEmptyPattern, policy); | |
271 EXPECT_THAT(files, ElementsAre(filepath)); | |
272 | |
273 files = RunEnumerator(temp_dir.GetPath(), false, FileEnumerator::FILES, | |
274 kEmptyPattern, policy); | |
275 EXPECT_THAT(files, IsEmpty()); | |
276 } | |
277 } | |
278 | |
279 TEST(FileEnumerator, FilesInSubfoldersWithFiltering) { | |
280 ScopedTempDir temp_dir; | |
281 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | |
282 | |
283 const auto filepath_test = temp_dir.GetPath().AppendASCII("test.txt"); | |
284 const auto subdir_foo = temp_dir.GetPath().AppendASCII("foo_subdir"); | |
285 const auto subdir_bar = temp_dir.GetPath().AppendASCII("bar_subdir"); | |
286 const auto filepath_foo_test = subdir_foo.AppendASCII("test.txt"); | |
287 const auto filepath_bar_test = subdir_bar.AppendASCII("test.txt"); | |
288 const auto filepath_foo_foo = subdir_foo.AppendASCII("foo.txt"); | |
289 const auto filepath_bar_foo = subdir_bar.AppendASCII("foo.txt"); | |
290 const auto filepath_foo_bar = subdir_foo.AppendASCII("bar.txt"); | |
291 const auto filepath_bar_bar = subdir_bar.AppendASCII("bar.txt"); | |
292 ASSERT_TRUE(CreateFile(filepath_test)); | |
293 ASSERT_TRUE(CreateDirectory(subdir_foo)); | |
294 ASSERT_TRUE(CreateDirectory(subdir_bar)); | |
295 ASSERT_TRUE(CreateFile(filepath_foo_test)); | |
296 ASSERT_TRUE(CreateFile(filepath_bar_test)); | |
297 ASSERT_TRUE(CreateFile(filepath_foo_foo)); | |
298 ASSERT_TRUE(CreateFile(filepath_bar_foo)); | |
299 ASSERT_TRUE(CreateFile(filepath_foo_bar)); | |
300 ASSERT_TRUE(CreateFile(filepath_bar_bar)); | |
301 | |
302 auto files = | |
303 RunEnumerator(temp_dir.GetPath(), true, | |
304 FileEnumerator::FILES | FileEnumerator::DIRECTORIES, | |
305 FILE_PATH_LITERAL("foo*"), | |
306 FileEnumerator::FolderSearchPolicy::MATCH_ONLY); | |
307 EXPECT_THAT(files, UnorderedElementsAre(subdir_foo, filepath_foo_test, | |
308 filepath_foo_foo, filepath_foo_bar)); | |
309 | |
310 files = RunEnumerator(temp_dir.GetPath(), true, | |
311 FileEnumerator::FILES | FileEnumerator::DIRECTORIES, | |
312 FILE_PATH_LITERAL("foo*"), | |
313 FileEnumerator::FolderSearchPolicy::ALL); | |
314 EXPECT_THAT(files, UnorderedElementsAre(subdir_foo, filepath_foo_foo, | |
315 filepath_bar_foo)); | |
316 } | |
317 | |
318 } // namespace base | |
OLD | NEW |