support globbing to files and partial path part matches

This commit is contained in:
Matt Freels 2009-07-28 00:25:31 -07:00
parent 3b6eb053de
commit 76002ea431
2 changed files with 20 additions and 8 deletions

View File

@ -21,16 +21,26 @@ module FakeFS
def find(path)
parts = path_parts(normalize_path(path))
target = parts[0...-1].inject(fs) do |dir, part|
dir[part] || {}
entries = find_recurser(fs, parts).flatten
case entries.length
when 0 then nil
when 1 then entries.first
else entries
end
end
case parts.last
when '*'
target.values
def find_recurser(dir, parts)
return [] unless dir.respond_to? :[]
pattern , *parts = parts
matches = dir.reject {|k,v| /\A#{pattern.gsub('*', '.*')}\Z/ !~ k }.values
if parts.empty? # we're done recursing
matches
else
target[parts.last]
matches.map{|entry| find_recurser(entry, parts) }
end
end

View File

@ -203,9 +203,11 @@ class FakeFSTest < Test::Unit::TestCase
assert_equal %w( /path/bar /path/bar2 /path/foo /path/foobar ), Dir['/path/*']
assert_equal ['/path/bar/baz'], Dir['/path/bar/*']
assert_equal ['/path/foo'], Dir['/path/foo']
# Unsupported so far. More hackery than I want to work on right now
# assert_equal ['/path'], Dir['/path*']
assert_equal ['/path/foo', '/path/foobar'], Dir['/path/foo*']
assert_equal ['/path'], Dir['/path*']
end
def test_chdir_changes_directories_like_a_boss