-
-
Notifications
You must be signed in to change notification settings - Fork 10
fix(filesystem): make the walker over a document or archive steerable #714
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
e660dbf
fix(filesystem): make the walker over a document or archive steerable
andiwand fea9e38
fix(filesystem): keep a directory entry that arrives after its children
andiwand 528207c
docs(filesystem): trim the walker comments
andiwand 1224275
refactor(filesystem): order the walker's map by the algorithm itself
andiwand File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,160 @@ | ||
| #include <odr/internal/common/filesystem.hpp> | ||
|
|
||
| #include <odr/internal/abstract/filesystem.hpp> | ||
| #include <odr/internal/common/file.hpp> | ||
| #include <odr/internal/common/path.hpp> | ||
|
|
||
| #include <gtest/gtest.h> | ||
|
|
||
| #include <memory> | ||
| #include <string> | ||
| #include <vector> | ||
|
|
||
| using namespace odr::internal; | ||
|
|
||
| namespace { | ||
|
|
||
| /// The paths a walk over `paths` visits, in order. | ||
| std::vector<std::string> walk(const abstract::ReadableFilesystem &filesystem, | ||
| const AbsPath &root) { | ||
| std::vector<std::string> result; | ||
| for (const auto walker = filesystem.file_walker(root); !walker->end(); | ||
| walker->next()) { | ||
| result.push_back(walker->path().string()); | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| VirtualFilesystem | ||
| filesystem_of(const std::vector<std::string> &files, | ||
| const std::vector<std::string> &directories = {}) { | ||
| VirtualFilesystem result; | ||
| for (const std::string &directory : directories) { | ||
| result.create_directory(AbsPath(directory)); | ||
| } | ||
| for (const std::string &file : files) { | ||
| result.copy(std::make_shared<MemoryFile>(std::string()), AbsPath(file)); | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| } // namespace | ||
|
|
||
| TEST(VirtualFilesystem, walker_reports_the_depth_of_every_entry) { | ||
| const VirtualFilesystem filesystem = | ||
| filesystem_of({"/mimetype", "/a/b.txt", "/a/c/d.txt"}); | ||
|
|
||
| const auto walker = filesystem.file_walker(AbsPath("/")); | ||
|
|
||
| EXPECT_EQ(walker->path().string(), "/a/b.txt"); | ||
| EXPECT_EQ(walker->depth(), 1); | ||
| walker->next(); | ||
| EXPECT_EQ(walker->path().string(), "/a/c/d.txt"); | ||
| EXPECT_EQ(walker->depth(), 2); | ||
| walker->next(); | ||
| EXPECT_EQ(walker->path().string(), "/mimetype"); | ||
| EXPECT_EQ(walker->depth(), 0); | ||
| } | ||
|
|
||
| TEST(VirtualFilesystem, walker_depth_is_relative_to_the_walked_root) { | ||
| const VirtualFilesystem filesystem = filesystem_of({"/a/c/d.txt"}); | ||
|
|
||
| const auto walker = filesystem.file_walker(AbsPath("/a")); | ||
|
|
||
| EXPECT_EQ(walker->depth(), 1); | ||
| } | ||
|
|
||
| TEST(VirtualFilesystem, flat_next_skips_the_subtree_and_terminates) { | ||
| const VirtualFilesystem filesystem = | ||
| filesystem_of({"/a/b.txt", "/a/c/d.txt", "/e.txt"}); | ||
|
|
||
| const auto walker = filesystem.file_walker(AbsPath("/")); | ||
|
|
||
| EXPECT_EQ(walker->path().string(), "/a/b.txt"); | ||
| walker->flat_next(); | ||
| EXPECT_EQ(walker->path().string(), "/a/c/d.txt"); | ||
| walker->flat_next(); | ||
| EXPECT_EQ(walker->path().string(), "/e.txt"); | ||
| walker->flat_next(); | ||
| EXPECT_TRUE(walker->end()); | ||
| } | ||
|
|
||
| /// The loop flat_next() exists for; it never terminated before. | ||
| TEST(VirtualFilesystem, flat_next_over_a_directory_skips_what_is_under_it) { | ||
| const VirtualFilesystem filesystem = | ||
| filesystem_of({"/a/b.txt", "/a/c.txt", "/e.txt"}, {"/a"}); | ||
|
|
||
| const auto walker = filesystem.file_walker(AbsPath("/")); | ||
|
|
||
| EXPECT_EQ(walker->path().string(), "/a"); | ||
| EXPECT_TRUE(walker->is_directory()); | ||
| walker->flat_next(); | ||
| EXPECT_EQ(walker->path().string(), "/e.txt"); | ||
| } | ||
|
|
||
| /// "/a-b" sorts between "/a" and "/a/b" by string. | ||
| TEST(VirtualFilesystem, flat_next_skips_a_subtree_a_sibling_sorts_into) { | ||
| const VirtualFilesystem filesystem = | ||
| filesystem_of({"/a/b.txt", "/a-b.txt", "/e.txt"}, {"/a"}); | ||
|
|
||
| EXPECT_EQ(walk(filesystem, AbsPath("/")), | ||
| (std::vector<std::string>{"/a", "/a/b.txt", "/a-b.txt", "/e.txt"})); | ||
|
|
||
| const auto walker = filesystem.file_walker(AbsPath("/")); | ||
| walker->flat_next(); | ||
| EXPECT_EQ(walker->path().string(), "/a-b.txt"); | ||
| } | ||
|
|
||
| TEST(VirtualFilesystem, pop_leaves_the_directory) { | ||
| const VirtualFilesystem filesystem = | ||
| filesystem_of({"/a/b.txt", "/a/c.txt", "/e.txt"}); | ||
|
|
||
| const auto walker = filesystem.file_walker(AbsPath("/")); | ||
|
|
||
| EXPECT_EQ(walker->path().string(), "/a/b.txt"); | ||
| walker->pop(); | ||
| EXPECT_EQ(walker->path().string(), "/e.txt"); | ||
| } | ||
|
|
||
| TEST(VirtualFilesystem, pop_at_depth_zero_ends_the_walk) { | ||
| const VirtualFilesystem filesystem = filesystem_of({"/a.txt", "/b.txt"}); | ||
|
|
||
| const auto walker = filesystem.file_walker(AbsPath("/")); | ||
|
|
||
| EXPECT_EQ(walker->depth(), 0); | ||
| walker->pop(); | ||
| EXPECT_TRUE(walker->end()); | ||
| } | ||
|
|
||
| /// An archive names its entries in its own order. A directory arriving after | ||
| /// what it holds is still an entry of its own, or `flat_next()` never sees it. | ||
| TEST(VirtualFilesystem, | ||
| a_directory_entry_survives_arriving_after_its_children) { | ||
| VirtualFilesystem filesystem; | ||
| filesystem.copy(std::make_shared<MemoryFile>(std::string()), | ||
| AbsPath("/a/b.txt")); | ||
| EXPECT_TRUE(filesystem.create_directory(AbsPath("/a"))); | ||
|
|
||
| EXPECT_EQ(walk(filesystem, AbsPath("/")), | ||
| (std::vector<std::string>{"/a", "/a/b.txt"})); | ||
|
|
||
| const auto walker = filesystem.file_walker(AbsPath("/")); | ||
| EXPECT_TRUE(walker->is_directory()); | ||
| walker->flat_next(); | ||
| EXPECT_TRUE(walker->end()); | ||
| } | ||
|
|
||
| TEST(VirtualFilesystem, an_intermediate_directory_is_a_directory) { | ||
| const VirtualFilesystem filesystem = filesystem_of({"/a/c/d.txt"}); | ||
|
|
||
| EXPECT_TRUE(filesystem.exists(AbsPath("/"))); | ||
| EXPECT_TRUE(filesystem.is_directory(AbsPath("/"))); | ||
| EXPECT_TRUE(filesystem.exists(AbsPath("/a"))); | ||
| EXPECT_TRUE(filesystem.is_directory(AbsPath("/a"))); | ||
| EXPECT_TRUE(filesystem.exists(AbsPath("/a/c"))); | ||
| EXPECT_TRUE(filesystem.is_directory(AbsPath("/a/c"))); | ||
|
|
||
| EXPECT_FALSE(filesystem.is_file(AbsPath("/a"))); | ||
| EXPECT_FALSE(filesystem.exists(AbsPath("/b"))); | ||
| EXPECT_FALSE(filesystem.is_directory(AbsPath("/a/c/d.txt"))); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When an archive lists
a/b.txtbefore its explicita/entry, the file makesis_directory("/a")return true here, so the latercreate_directory("/a")call returns without adding it.ZipArchive::as_filesystem()inserts entries in archive order, meaning the resulting walker omits/afor this valid ordering and callers cannot encounter or prune that directory withflat_next(). Materialize inferred directories in the walker or otherwise retain explicit directory entries regardless of insertion order.Useful? React with πΒ / π.