diff --git a/lib/fakefs/file_system.rb b/lib/fakefs/file_system.rb index 2f2f357..678526c 100644 --- a/lib/fakefs/file_system.rb +++ b/lib/fakefs/file_system.rb @@ -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 diff --git a/test/fakefs_test.rb b/test/fakefs_test.rb index e7df561..a38c1c5 100644 --- a/test/fakefs_test.rb +++ b/test/fakefs_test.rb @@ -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