Skip to content

Commit

Permalink
added support for recursive globbing
Browse files Browse the repository at this point in the history
FakeDir.glob now handles recursive globs (i.e. '**') like the standard
lib Dir.  Previously they were treated identically to single wild cards
(i.e. '*')

Resolves issue #16
http://github.com/defunkt/fakefs/issues#issue/16
  • Loading branch information
samg committed Oct 29, 2009
1 parent 01b0421 commit 1c6825f
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
20 changes: 19 additions & 1 deletion lib/fakefs/file_system.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,31 @@ def find_recurser(dir, parts)
return [] unless dir.respond_to? :[]

pattern , *parts = parts
matches = dir.reject {|k,v| /\A#{pattern.gsub('?','.').gsub('*', '.*')}\Z/ !~ k }.values
matches = case pattern
when '**'
case parts
when ['*'], []
parts = [] # end recursion
directories_under(dir).map do |d|
d.values.select{|f| f.is_a? FakeFile }
end.flatten.uniq
else
directories_under(dir)
end
else
dir.reject {|k,v| /\A#{pattern.gsub('?','.').gsub('*', '.*')}\Z/ !~ k }.values
end

if parts.empty? # we're done recursing
matches
else
matches.map{|entry| find_recurser(entry, parts) }
end
end

def directories_under(dir)
children = dir.values.select{|f| f.is_a? FakeDir}
([dir] + children + children.map{|c| directories_under(c)}).flatten.uniq
end
end
end
15 changes: 15 additions & 0 deletions test/fakefs_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,21 @@ def test_dir_glob_handles_root
#assert_equal ['/'], Dir['/']
end

def test_dir_glob_handles_recursive_globs
File.open('/one/two/three/four.rb', 'w')
File.open('/one/five.rb', 'w')
assert_equal ['/one/five.rb', '/one/two/three/four.rb'], Dir['/one/**/*.rb']
assert_equal ['/one/two'], Dir['/one/**/two']
assert_equal ['/one/two/three'], Dir['/one/**/three']
end

def test_dir_recursive_glob_ending_in_wildcards_only_returns_files
File.open('/one/two/three/four.rb', 'w')
File.open('/one/five.rb', 'w')
assert_equal ['/one/five.rb', '/one/two/three/four.rb'], Dir['/one/**/*']
assert_equal ['/one/five.rb', '/one/two/three/four.rb'], Dir['/one/**']
end

def test_chdir_changes_directories_like_a_boss
# I know memes!
FileUtils.mkdir_p '/path'
Expand Down

0 comments on commit 1c6825f

Please sign in to comment.