Skip to content

Commit

Permalink
revises the implementation of Pathname#ascendant_of? (in refinement)
Browse files Browse the repository at this point in the history
  • Loading branch information
fxn committed Nov 11, 2015
1 parent e42a5fd commit 472b8e0
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
Expand Up @@ -79,7 +79,14 @@ class PathHelper
using Module.new {
refine Pathname do
def ascendant_of?(other)
other.to_s =~ /\A#{Regexp.quote(to_s)}#{Pathname::SEPARATOR_PAT}?/
if self != other && other.to_s.start_with?(to_s)
# On Windows each_filename does not include the drive letter,
# but the test above already detects if they differ.
parts = each_filename.to_a
other_parts = other.each_filename.to_a

other_parts[0, parts.length] == parts
end
end
end
}
Expand Down
29 changes: 29 additions & 0 deletions activesupport/test/file_evented_update_checker_test.rb
Expand Up @@ -118,4 +118,33 @@ def pn(path)

assert_equal paths.values_at(0, 2, 4), @ph.filter_out_descendants(paths)
end

test '#filter_out_descendants works on path units' do
paths = %w(
/foo/bar
/foo/barrrr
).map { |path| pn(path) }

assert_equal paths, @ph.filter_out_descendants(paths)
end

test '#filter_out_descendants deals correctly with the root directory' do
paths = %w(
/
/foo
/foo/bar
).map { |path| pn(path) }

assert_equal paths.values_at(0), @ph.filter_out_descendants(paths)
end

test '#filter_out_descendants preserves duplicates' do
paths = %w(
/foo
/foo/bar
/foo
).map { |path| pn(path) }

assert_equal paths.values_at(0, 2), @ph.filter_out_descendants(paths)
end
end

0 comments on commit 472b8e0

Please sign in to comment.