Page MenuHomePhabricator

Allow `Filesystem::walkToRoot` to walk to a given root directory
ClosedPublic

Authored by joshuaspence on Jun 8 2015, 10:18 AM.
Tags
None
Referenced Files
Unknown Object (File)
Wed, Nov 20, 9:21 PM
Unknown Object (File)
Sun, Nov 17, 12:22 AM
Unknown Object (File)
Oct 24 2024, 10:03 PM
Unknown Object (File)
Oct 23 2024, 2:26 PM
Unknown Object (File)
Oct 22 2024, 5:26 PM
Unknown Object (File)
Oct 22 2024, 2:43 PM
Unknown Object (File)
Oct 19 2024, 2:35 PM
Unknown Object (File)
Oct 17 2024, 6:42 AM
Subscribers

Details

Summary

Allow Filesystem::walkToRoot to walk to a given root directory. See D13185#139299.

Test Plan

Added unit tests.

Diff Detail

Repository
rPHU libphutil
Lint
Lint Not Applicable
Unit
Tests Not Applicable

Event Timeline

joshuaspence retitled this revision from to Allow `Filesystem::walkToRoot` to walk to a given root directory.
joshuaspence updated this object.
joshuaspence edited the test plan for this revision. (Show Details)
joshuaspence added a reviewer: epriestley.
epriestley edited edge metadata.
epriestley added inline comments.
src/filesystem/Filesystem.php
762

This will incorrectly remove directories named 0, 0.0, etc.

src/filesystem/__tests__/FilesystemTestCase.php
108

Three other useful cases might be:

  • Directory named 0.
  • Root and path identical.
  • Root not an ancestor of path.
This revision is now accepted and ready to land.Jun 8 2015, 8:45 PM

Yeah, I wanted to add something like this:

if (!self::isDescendant($path, $root)) {
  return array();
}

But this was problematic for my testing purposes because Filesystem::isDescendant($path, $root) requires $root and $path to actually exist. Would you be open to changing this method to be more general?

What do you think about creating some actual directories inside __tests__/data/ or similar and checking those?

Makes it slightly harder to get the expected values right for stuff above the library, but that seems surmountable to me.

Personally, I find the behavior of Filesystem::isDescendant to be non-obvious and so I think fixing that is the most correct solution. Adding directories in the __tests__/data directory is less obtrussive however.

@epriestley, I haven't had time to extend this further to check for "path is not a descendent of root". Is this fine to push as is?

joshuaspence edited edge metadata.

Add a bunch of test coverage

One possible issue, rest of it looks great to me.

src/filesystem/Filesystem.php
766

This is probably wrong on Windows -- there won't be a leading directory separator on $root, right?

Maybe cleaner would be:

while ($parts) {
  // compute the next thing
  // add it to the list
  // if we just added the root, break out of the loop
  // array_pop parts
}
joshuaspence marked 3 inline comments as done.
  • Improve Windows compatibility
  • Add some additional test cases
This revision was automatically updated to reflect the committed changes.