1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
base / files / file_enumerator_unittest.cc [blame]
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/files/file_enumerator.h"
#include <string>
#include <utility>
#include <vector>
#include "base/containers/circular_deque.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/logging.h"
#include "base/strings/string_util.h"
#include "build/build_config.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#if BUILDFLAG(IS_ANDROID)
#include "base/android/content_uri_utils.h"
#include "base/test/android/content_uri_test_utils.h"
#endif
using testing::ElementsAre;
using testing::IsEmpty;
using testing::UnorderedElementsAre;
namespace base {
namespace {
const FilePath::StringType kEmptyPattern;
const std::vector<FileEnumerator::FolderSearchPolicy> kFolderSearchPolicies{
FileEnumerator::FolderSearchPolicy::MATCH_ONLY,
FileEnumerator::FolderSearchPolicy::ALL};
struct TestFile {
TestFile(const FilePath::CharType* file_name, const char* c)
: path(file_name), contents(c) {}
TestFile(const FilePath::CharType* directory,
const FilePath::CharType* file_name,
const char* c)
: path(FilePath(directory).Append(file_name)), contents(c) {}
const FilePath path;
const std::string contents;
File::Info info;
bool found = false;
};
struct TestDirectory {
explicit TestDirectory(const FilePath::CharType* n) : name(n) {}
const FilePath name;
File::Info info;
bool found = false;
};
void CheckModificationTime(const FileEnumerator::FileInfo& actual,
Time expected_last_modified_time) {
#if BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
// On POSIX, GetLastModifiedTime() rounds down to the second, but
// File::GetInfo() does not.
Time::Exploded exploded;
expected_last_modified_time.UTCExplode(&exploded);
exploded.millisecond = 0;
EXPECT_TRUE(Time::FromUTCExploded(exploded, &expected_last_modified_time));
#endif
EXPECT_EQ(actual.GetLastModifiedTime(), expected_last_modified_time);
}
void CheckFileAgainstInfo(const FileEnumerator::FileInfo& actual,
TestFile& expected) {
EXPECT_FALSE(expected.found)
<< "Got " << expected.path.BaseName().value() << " twice";
expected.found = true;
EXPECT_EQ(actual.GetSize(), int64_t(expected.contents.size()));
CheckModificationTime(actual, expected.info.last_modified);
}
void CheckDirectoryAgainstInfo(const FileEnumerator::FileInfo& actual,
TestDirectory& expected) {
EXPECT_FALSE(expected.found) << "Got " << expected.name.value() << " twice";
expected.found = true;
CheckModificationTime(actual, expected.info.last_modified);
}
circular_deque<FilePath> RunEnumerator(
const FilePath& root_path,
bool recursive,
int file_type,
const FilePath::StringType& pattern,
FileEnumerator::FolderSearchPolicy folder_search_policy) {
circular_deque<FilePath> rv;
FileEnumerator enumerator(root_path, recursive, file_type, pattern,
folder_search_policy,
FileEnumerator::ErrorPolicy::IGNORE_ERRORS);
for (auto file = enumerator.Next(); !file.empty(); file = enumerator.Next())
rv.emplace_back(std::move(file));
return rv;
}
bool CreateDummyFile(const FilePath& path) {
return WriteFile(path, byte_span_from_cstring("42"));
}
bool GetFileInfo(const FilePath& file_path, File::Info& info) {
// FLAG_WIN_BACKUP_SEMANTICS: Needed to open directories on Windows.
File f(file_path,
File::FLAG_OPEN | File::FLAG_READ | File::FLAG_WIN_BACKUP_SEMANTICS);
if (!f.IsValid()) {
LOG(ERROR) << "Could not open " << file_path.value() << ": "
<< File::ErrorToString(f.error_details());
return false;
}
if (!f.GetInfo(&info)) {
std::string last_error = File::ErrorToString(File::GetLastFileError());
LOG(ERROR) << "Could not get info about " << file_path.value() << ": "
<< last_error;
return false;
}
return true;
}
void SetUpTestFiles(const ScopedTempDir& temp_dir,
std::vector<TestFile>& files) {
for (TestFile& file : files) {
const FilePath file_path = temp_dir.GetPath().Append(file.path);
ASSERT_TRUE(WriteFile(file_path, file.contents));
ASSERT_TRUE(GetFileInfo(file_path, file.info));
}
}
} // namespace
TEST(FileEnumerator, NotExistingPath) {
const FilePath path = FilePath::FromUTF8Unsafe("some_not_existing_path");
ASSERT_FALSE(PathExists(path));
for (auto policy : kFolderSearchPolicies) {
const auto files = RunEnumerator(
path, true, FileEnumerator::FILES | FileEnumerator::DIRECTORIES,
FILE_PATH_LITERAL(""), policy);
EXPECT_THAT(files, IsEmpty());
}
}
TEST(FileEnumerator, EmptyFolder) {
ScopedTempDir temp_dir;
ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
for (auto policy : kFolderSearchPolicies) {
const auto files =
RunEnumerator(temp_dir.GetPath(), true,
FileEnumerator::FILES | FileEnumerator::DIRECTORIES,
kEmptyPattern, policy);
EXPECT_THAT(files, IsEmpty());
}
}
TEST(FileEnumerator, SingleFileInFolderForFileSearch) {
ScopedTempDir temp_dir;
ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
const FilePath& path = temp_dir.GetPath();
const FilePath file = path.AppendASCII("test.txt");
ASSERT_TRUE(CreateDummyFile(file));
for (auto policy : kFolderSearchPolicies) {
const auto files = RunEnumerator(
temp_dir.GetPath(), true, FileEnumerator::FILES, kEmptyPattern, policy);
EXPECT_THAT(files, ElementsAre(file));
}
}
TEST(FileEnumerator, SingleFileInFolderForDirSearch) {
ScopedTempDir temp_dir;
ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
const FilePath& path = temp_dir.GetPath();
ASSERT_TRUE(CreateDummyFile(path.AppendASCII("test.txt")));
for (auto policy : kFolderSearchPolicies) {
const auto files = RunEnumerator(path, true, FileEnumerator::DIRECTORIES,
kEmptyPattern, policy);
EXPECT_THAT(files, IsEmpty());
}
}
TEST(FileEnumerator, SingleFileInFolderWithFiltering) {
ScopedTempDir temp_dir;
ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
const FilePath& path = temp_dir.GetPath();
const FilePath file = path.AppendASCII("test.txt");
ASSERT_TRUE(CreateDummyFile(file));
for (auto policy : kFolderSearchPolicies) {
auto files = RunEnumerator(path, true, FileEnumerator::FILES,
FILE_PATH_LITERAL("*.txt"), policy);
EXPECT_THAT(files, ElementsAre(file));
files = RunEnumerator(path, true, FileEnumerator::FILES,
FILE_PATH_LITERAL("*.pdf"), policy);
EXPECT_THAT(files, IsEmpty());
}
}
TEST(FileEnumerator, TwoFilesInFolder) {
ScopedTempDir temp_dir;
ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
const FilePath& path = temp_dir.GetPath();
const FilePath foo_txt = path.AppendASCII("foo.txt");
const FilePath bar_txt = path.AppendASCII("bar.txt");
ASSERT_TRUE(CreateDummyFile(foo_txt));
ASSERT_TRUE(CreateDummyFile(bar_txt));
for (auto policy : kFolderSearchPolicies) {
auto files = RunEnumerator(path, true, FileEnumerator::FILES,
FILE_PATH_LITERAL("*.txt"), policy);
EXPECT_THAT(files, UnorderedElementsAre(foo_txt, bar_txt));
files = RunEnumerator(path, true, FileEnumerator::FILES,
FILE_PATH_LITERAL("foo*"), policy);
EXPECT_THAT(files, ElementsAre(foo_txt));
files = RunEnumerator(path, true, FileEnumerator::FILES,
FILE_PATH_LITERAL("*.pdf"), policy);
EXPECT_THAT(files, IsEmpty());
files =
RunEnumerator(path, true, FileEnumerator::FILES, kEmptyPattern, policy);
EXPECT_THAT(files, UnorderedElementsAre(foo_txt, bar_txt));
}
}
TEST(FileEnumerator, SingleFolderInFolderForFileSearch) {
ScopedTempDir temp_dir;
ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
const FilePath& path = temp_dir.GetPath();
ScopedTempDir temp_subdir;
ASSERT_TRUE(temp_subdir.CreateUniqueTempDirUnderPath(path));
for (auto policy : kFolderSearchPolicies) {
const auto files =
RunEnumerator(path, true, FileEnumerator::FILES, kEmptyPattern, policy);
EXPECT_THAT(files, IsEmpty());
}
}
TEST(FileEnumerator, SingleFolderInFolderForDirSearch) {
ScopedTempDir temp_dir;
ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
const FilePath& path = temp_dir.GetPath();
ScopedTempDir temp_subdir;
ASSERT_TRUE(temp_subdir.CreateUniqueTempDirUnderPath(path));
for (auto policy : kFolderSearchPolicies) {
const auto files = RunEnumerator(path, true, FileEnumerator::DIRECTORIES,
kEmptyPattern, policy);
EXPECT_THAT(files, ElementsAre(temp_subdir.GetPath()));
}
}
TEST(FileEnumerator, TwoFoldersInFolder) {
ScopedTempDir temp_dir;
ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
const FilePath& path = temp_dir.GetPath();
const FilePath subdir_foo = path.AppendASCII("foo");
const FilePath subdir_bar = path.AppendASCII("bar");
ASSERT_TRUE(CreateDirectory(subdir_foo));
ASSERT_TRUE(CreateDirectory(subdir_bar));
for (auto policy : kFolderSearchPolicies) {
auto files = RunEnumerator(path, true, FileEnumerator::DIRECTORIES,
kEmptyPattern, policy);
EXPECT_THAT(files, UnorderedElementsAre(subdir_foo, subdir_bar));
files = RunEnumerator(path, true, FileEnumerator::DIRECTORIES,
FILE_PATH_LITERAL("foo"), policy);
EXPECT_THAT(files, ElementsAre(subdir_foo));
}
}
TEST(FileEnumerator, FolderAndFileInFolder) {
ScopedTempDir temp_dir;
ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
const FilePath& path = temp_dir.GetPath();
ScopedTempDir temp_subdir;
ASSERT_TRUE(temp_subdir.CreateUniqueTempDirUnderPath(path));
const FilePath file = path.AppendASCII("test.txt");
ASSERT_TRUE(CreateDummyFile(file));
for (auto policy : kFolderSearchPolicies) {
auto files =
RunEnumerator(path, true, FileEnumerator::FILES, kEmptyPattern, policy);
EXPECT_THAT(files, ElementsAre(file));
files = RunEnumerator(path, true, FileEnumerator::DIRECTORIES,
kEmptyPattern, policy);
EXPECT_THAT(files, ElementsAre(temp_subdir.GetPath()));
files = RunEnumerator(path, true,
FileEnumerator::FILES | FileEnumerator::DIRECTORIES,
kEmptyPattern, policy);
EXPECT_THAT(files, UnorderedElementsAre(file, temp_subdir.GetPath()));
}
}
TEST(FileEnumerator, FilesInParentFolderAlwaysFirst) {
ScopedTempDir temp_dir;
ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
const FilePath& path = temp_dir.GetPath();
ScopedTempDir temp_subdir;
ASSERT_TRUE(temp_subdir.CreateUniqueTempDirUnderPath(path));
const FilePath foo_txt = path.AppendASCII("foo.txt");
const FilePath bar_txt = temp_subdir.GetPath().AppendASCII("bar.txt");
ASSERT_TRUE(CreateDummyFile(foo_txt));
ASSERT_TRUE(CreateDummyFile(bar_txt));
for (auto policy : kFolderSearchPolicies) {
const auto files =
RunEnumerator(path, true, FileEnumerator::FILES, kEmptyPattern, policy);
EXPECT_THAT(files, ElementsAre(foo_txt, bar_txt));
}
}
TEST(FileEnumerator, FileInSubfolder) {
ScopedTempDir temp_dir;
ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
const FilePath subdir = temp_dir.GetPath().AppendASCII("subdir");
ASSERT_TRUE(CreateDirectory(subdir));
const FilePath file = subdir.AppendASCII("test.txt");
ASSERT_TRUE(CreateDummyFile(file));
for (auto policy : kFolderSearchPolicies) {
auto files = RunEnumerator(temp_dir.GetPath(), true, FileEnumerator::FILES,
kEmptyPattern, policy);
EXPECT_THAT(files, ElementsAre(file));
files = RunEnumerator(temp_dir.GetPath(), false, FileEnumerator::FILES,
kEmptyPattern, policy);
EXPECT_THAT(files, IsEmpty());
}
}
TEST(FileEnumerator, FilesInSubfoldersWithFiltering) {
ScopedTempDir temp_dir;
ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
const FilePath test_txt = temp_dir.GetPath().AppendASCII("test.txt");
const FilePath subdir_foo = temp_dir.GetPath().AppendASCII("foo_subdir");
const FilePath subdir_bar = temp_dir.GetPath().AppendASCII("bar_subdir");
const FilePath foo_test = subdir_foo.AppendASCII("test.txt");
const FilePath foo_foo = subdir_foo.AppendASCII("foo.txt");
const FilePath foo_bar = subdir_foo.AppendASCII("bar.txt");
const FilePath bar_test = subdir_bar.AppendASCII("test.txt");
const FilePath bar_foo = subdir_bar.AppendASCII("foo.txt");
const FilePath bar_bar = subdir_bar.AppendASCII("bar.txt");
ASSERT_TRUE(CreateDummyFile(test_txt));
ASSERT_TRUE(CreateDirectory(subdir_foo));
ASSERT_TRUE(CreateDirectory(subdir_bar));
ASSERT_TRUE(CreateDummyFile(foo_test));
ASSERT_TRUE(CreateDummyFile(foo_foo));
ASSERT_TRUE(CreateDummyFile(foo_bar));
ASSERT_TRUE(CreateDummyFile(bar_test));
ASSERT_TRUE(CreateDummyFile(bar_foo));
ASSERT_TRUE(CreateDummyFile(bar_bar));
auto files =
RunEnumerator(temp_dir.GetPath(), true,
FileEnumerator::FILES | FileEnumerator::DIRECTORIES,
FILE_PATH_LITERAL("foo*"),
FileEnumerator::FolderSearchPolicy::MATCH_ONLY);
EXPECT_THAT(files,
UnorderedElementsAre(subdir_foo, foo_test, foo_foo, foo_bar));
files = RunEnumerator(temp_dir.GetPath(), true,
FileEnumerator::FILES | FileEnumerator::DIRECTORIES,
FILE_PATH_LITERAL("foo*"),
FileEnumerator::FolderSearchPolicy::ALL);
EXPECT_THAT(files, UnorderedElementsAre(subdir_foo, foo_foo, bar_foo));
}
TEST(FileEnumerator, InvalidDirectory) {
ScopedTempDir temp_dir;
ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
const FilePath test_file = temp_dir.GetPath().AppendASCII("test_file");
ASSERT_TRUE(CreateDummyFile(test_file));
// Attempt to enumerate entries at a regular file path.
FileEnumerator enumerator(test_file, /*recursive=*/true,
FileEnumerator::FILES, kEmptyPattern,
FileEnumerator::FolderSearchPolicy::ALL,
FileEnumerator::ErrorPolicy::STOP_ENUMERATION);
FilePath path = enumerator.Next();
EXPECT_TRUE(path.empty());
// Slightly different outcomes between Windows and POSIX.
#if BUILDFLAG(IS_WIN)
EXPECT_EQ(File::Error::FILE_ERROR_FAILED, enumerator.GetError());
#else
EXPECT_EQ(File::Error::FILE_ERROR_NOT_A_DIRECTORY, enumerator.GetError());
#endif
}
#if BUILDFLAG(IS_POSIX)
TEST(FileEnumerator, SymLinkLoops) {
ScopedTempDir temp_dir;
ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
const FilePath subdir = temp_dir.GetPath().AppendASCII("subdir");
ASSERT_TRUE(CreateDirectory(subdir));
const FilePath file = subdir.AppendASCII("test.txt");
ASSERT_TRUE(CreateDummyFile(file));
const FilePath link = subdir.AppendASCII("link");
ASSERT_TRUE(CreateSymbolicLink(temp_dir.GetPath(), link));
auto files = RunEnumerator(
temp_dir.GetPath(), true,
FileEnumerator::FILES | FileEnumerator::DIRECTORIES, kEmptyPattern,
FileEnumerator::FolderSearchPolicy::MATCH_ONLY);
EXPECT_THAT(files, UnorderedElementsAre(subdir, link, file));
files = RunEnumerator(subdir, true,
FileEnumerator::FILES | FileEnumerator::DIRECTORIES |
FileEnumerator::SHOW_SYM_LINKS,
kEmptyPattern,
FileEnumerator::FolderSearchPolicy::MATCH_ONLY);
EXPECT_THAT(files, UnorderedElementsAre(link, file));
}
#endif
// Test FileEnumerator::GetInfo() on some files and ensure all the returned
// information is correct.
TEST(FileEnumerator, GetInfo) {
ScopedTempDir temp_dir;
ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
std::vector<TestFile> files = {
TestFile(FILE_PATH_LITERAL("file1"), "First"),
TestFile(FILE_PATH_LITERAL("file2"), "Second"),
TestFile(FILE_PATH_LITERAL("file3"), "Third-third-third")};
SetUpTestFiles(temp_dir, files);
#if BUILDFLAG(IS_ANDROID)
FilePath root_dir =
*base::test::android::GetInMemoryContentTreeUriFromCacheDirDirectory(
temp_dir.GetPath());
#else
FilePath root_dir = temp_dir.GetPath();
#endif
FileEnumerator file_enumerator(
root_dir, false, FileEnumerator::FILES | FileEnumerator::DIRECTORIES);
while (!file_enumerator.Next().empty()) {
auto info = file_enumerator.GetInfo();
bool found = false;
for (TestFile& file : files) {
if (info.GetName() == file.path.BaseName()) {
CheckFileAgainstInfo(info, file);
found = true;
break;
}
}
EXPECT_TRUE(found) << "Got unexpected result " << info.GetName().value();
}
for (const TestFile& file : files) {
EXPECT_TRUE(file.found)
<< "File " << file.path.value() << " was not returned";
}
}
// Test that FileEnumerator::GetInfo() works when searching recursively. It also
// tests that it returns the correct information about directories.
TEST(FileEnumerator, GetInfoRecursive) {
ScopedTempDir temp_dir;
ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
TestDirectory directories[] = {TestDirectory(FILE_PATH_LITERAL("dir1")),
TestDirectory(FILE_PATH_LITERAL("dir2-empty")),
TestDirectory(FILE_PATH_LITERAL("dir3")),
TestDirectory(FILE_PATH_LITERAL("dir4"))};
for (const TestDirectory& dir : directories) {
const FilePath dir_path = temp_dir.GetPath().Append(dir.name);
ASSERT_TRUE(CreateDirectory(dir_path));
}
std::vector<TestFile> files = {
TestFile(FILE_PATH_LITERAL("dir1"), FILE_PATH_LITERAL("file1"), "First"),
TestFile(FILE_PATH_LITERAL("dir1"), FILE_PATH_LITERAL("file2"), "Second"),
TestFile(FILE_PATH_LITERAL("dir3"), FILE_PATH_LITERAL("fileA"),
"Third-third-3"),
TestFile(FILE_PATH_LITERAL("dir4"), FILE_PATH_LITERAL(".file"), "Dot")};
SetUpTestFiles(temp_dir, files);
// Get last-modification times for directories. Must be done after we create
// all the files.
for (TestDirectory& dir : directories) {
const FilePath dir_path = temp_dir.GetPath().Append(dir.name);
ASSERT_TRUE(GetFileInfo(dir_path, dir.info));
}
#if BUILDFLAG(IS_ANDROID)
FilePath root_dir =
*base::test::android::GetInMemoryContentTreeUriFromCacheDirDirectory(
temp_dir.GetPath());
#else
FilePath root_dir = temp_dir.GetPath();
#endif
FileEnumerator file_enumerator(
root_dir, true, FileEnumerator::FILES | FileEnumerator::DIRECTORIES);
while (!file_enumerator.Next().empty()) {
auto info = file_enumerator.GetInfo();
bool found = false;
if (info.IsDirectory()) {
for (TestDirectory& dir : directories) {
if (info.GetName() == dir.name) {
CheckDirectoryAgainstInfo(info, dir);
found = true;
break;
}
}
} else {
for (TestFile& file : files) {
if (info.GetName() == file.path.BaseName()) {
CheckFileAgainstInfo(info, file);
#if BUILDFLAG(IS_ANDROID)
std::string expected =
temp_dir.GetPath().BaseName().Append(file.path.DirName()).value();
EXPECT_EQ(base::JoinString(info.subdirs(), "/"), expected);
#endif
found = true;
break;
}
}
}
EXPECT_TRUE(found) << "Got unexpected result " << info.GetName().value();
}
for (const TestDirectory& dir : directories) {
EXPECT_TRUE(dir.found) << "Directory " << dir.name.value()
<< " was not returned";
}
for (const TestFile& file : files) {
EXPECT_TRUE(file.found)
<< "File " << file.path.value() << " was not returned";
}
}
#if BUILDFLAG(IS_FUCHSIA)
// FileEnumerator::GetInfo does not work correctly with INCLUDE_DOT_DOT.
// https://crbug.com/1106172
#elif BUILDFLAG(IS_WIN)
// Windows has a bug in their handling of ".."; they always report the file
// modification time of the current directory, not the parent directory. This is
// a bug in Windows, not us -- you can see it with the "dir" command (notice
// that the time of . and .. always match). Skip this test.
// https://crbug.com/1119546
#else
// Tests that FileEnumerator::GetInfo() returns the correct info for the ..
// directory.
TEST(FileEnumerator, GetInfoDotDot) {
ScopedTempDir temp_dir;
ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
const FilePath::CharType kSubdir[] = FILE_PATH_LITERAL("subdir");
const FilePath subdir_path = temp_dir.GetPath().Append(kSubdir);
ASSERT_TRUE(CreateDirectory(subdir_path));
std::vector<TestFile> files = {
TestFile(kSubdir, FILE_PATH_LITERAL("file1"), "First"),
TestFile(kSubdir, FILE_PATH_LITERAL("file2"), "Second"),
TestFile(kSubdir, FILE_PATH_LITERAL("file3"), "Third-third-third")};
SetUpTestFiles(temp_dir, files);
TestDirectory dotdot(FILE_PATH_LITERAL(".."));
// test_dir/subdir/.. is just test_dir.
ASSERT_TRUE(GetFileInfo(temp_dir.GetPath(), dotdot.info));
FileEnumerator file_enumerator(subdir_path, false,
FileEnumerator::FILES |
FileEnumerator::DIRECTORIES |
FileEnumerator::INCLUDE_DOT_DOT);
while (!file_enumerator.Next().empty()) {
auto info = file_enumerator.GetInfo();
bool found = false;
if (info.IsDirectory()) {
EXPECT_EQ(info.GetName(), FilePath(FILE_PATH_LITERAL("..")));
CheckDirectoryAgainstInfo(info, dotdot);
found = true;
} else {
for (TestFile& file : files) {
if (info.GetName() == file.path.BaseName()) {
CheckFileAgainstInfo(info, file);
found = true;
break;
}
}
}
EXPECT_TRUE(found) << "Got unexpected result " << info.GetName().value();
}
EXPECT_TRUE(dotdot.found) << "Directory .. was not returned";
for (const TestFile& file : files) {
EXPECT_TRUE(file.found)
<< "File " << file.path.value() << " was not returned";
}
}
#endif // !BUILDFLAG(IS_FUCHSIA) && !BUILDFLAG(IS_WIN)
TEST(FileEnumerator, OnlyName) {
ScopedTempDir temp_dir;
ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
const FilePath& path = temp_dir.GetPath();
// Add a directory and a file.
ScopedTempDir temp_subdir;
ASSERT_TRUE(temp_subdir.CreateUniqueTempDirUnderPath(path));
const FilePath& subdir = temp_subdir.GetPath();
const FilePath dummy_file = path.AppendASCII("a_file.txt");
ASSERT_TRUE(CreateDummyFile(dummy_file));
auto found_paths = RunEnumerator(
path, /*recursive=*/false, FileEnumerator::FileType::NAMES_ONLY,
FilePath::StringType(), FileEnumerator::FolderSearchPolicy::MATCH_ONLY);
EXPECT_THAT(found_paths, UnorderedElementsAre(subdir, dummy_file));
}
struct FileEnumeratorForEachTestCase {
const bool recursive;
const int file_type;
const int expected_invocation_count;
};
class FileEnumeratorForEachTest
: public ::testing::TestWithParam<FileEnumeratorForEachTestCase> {};
INSTANTIATE_TEST_SUITE_P(
FileEnumeratorForEachTestCases,
FileEnumeratorForEachTest,
::testing::ValuesIn(std::vector<FileEnumeratorForEachTestCase>{
{false, FileEnumerator::FILES, 2},
{true, FileEnumerator::FILES, 8},
{false, FileEnumerator::DIRECTORIES, 3},
{true, FileEnumerator::DIRECTORIES, 3},
{false, FileEnumerator::FILES | FileEnumerator::DIRECTORIES, 5},
{true, FileEnumerator::FILES | FileEnumerator::DIRECTORIES, 11},
}));
TEST_P(FileEnumeratorForEachTest, TestCases) {
ScopedTempDir temp_dir;
ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
const FilePath mock_path(temp_dir.GetPath());
// Create a top-level directory, and 3 sub-directories, with 2 files within
// each directory.
for (const FilePath& path :
{mock_path, mock_path.Append(FILE_PATH_LITERAL("1.2.3.4")),
mock_path.Append(FILE_PATH_LITERAL("Download")),
mock_path.Append(FILE_PATH_LITERAL("Install"))}) {
ASSERT_TRUE(CreateDirectory(path));
for (const FilePath::StringType& file_name :
{FILE_PATH_LITERAL("mock.executable"),
FILE_PATH_LITERAL("mock.text")}) {
ASSERT_TRUE(
File(path.Append(file_name), File::FLAG_CREATE | File::FLAG_WRITE)
.IsValid());
}
}
int invocation_count = 0;
FileEnumerator(mock_path, GetParam().recursive, GetParam().file_type)
.ForEach([&invocation_count](const FilePath& item) {
++invocation_count;
if (invocation_count > GetParam().expected_invocation_count) {
ADD_FAILURE() << "Unexpected file/directory found: " << item << ": "
<< invocation_count << ": "
<< GetParam().expected_invocation_count;
}
});
EXPECT_EQ(invocation_count, GetParam().expected_invocation_count);
}
} // namespace base