Skip to content

Commit

Permalink
Dir.glob can find dot dirs, with the correct flags.
Browse files Browse the repository at this point in the history
  • Loading branch information
arvindsv committed Nov 29, 2010
1 parent a5b81ff commit c1e76da
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 12 deletions.
4 changes: 2 additions & 2 deletions lib/fakefs/dir.rb
Expand Up @@ -78,8 +78,8 @@ def self.foreach(dirname, &block)
Dir.open(dirname) { |file| yield file }
end

def self.glob(pattern)
matches_for_pattern = lambda { |matcher| [FileSystem.find(matcher) || []].flatten.map{|e| e.to_s}.sort }
def self.glob(pattern, flags = 0)
matches_for_pattern = lambda { |matcher| [FileSystem.find(matcher, flags) || []].flatten.map{|e| e.to_s}.sort }

if pattern.is_a? Array
return pattern.collect { |matcher| matches_for_pattern.call matcher }.flatten
Expand Down
21 changes: 11 additions & 10 deletions lib/fakefs/file_system.rb
Expand Up @@ -19,11 +19,11 @@ def files
fs.values
end

def find(path)
def find(path, flags = 0)
parts = path_parts(normalize_path(path))
return fs if parts.empty? # '/'

entries = find_recurser(fs, parts).flatten
entries = find_recurser(fs, parts, flags).flatten

case entries.length
when 0 then nil
Expand Down Expand Up @@ -102,7 +102,7 @@ def current_dir

private

def find_recurser(dir, parts)
def find_recurser(dir, parts, flags)
return [] unless dir.respond_to? :[]

pattern , *parts = parts
Expand All @@ -111,14 +111,14 @@ def find_recurser(dir, parts)
case parts
when ['*']
parts = [] # end recursion
directories_under(dir).map do |d|
directories_under(dir, flags).map do |d|
d.values.select{|f| f.is_a?(FakeFile) || is_a_directory_and_matches_flags?(f, 0)}
end.flatten.uniq
when []
parts = [] # end recursion
dir.values.flatten.uniq
else
directories_under(dir)
directories_under(dir, flags)
end
else
dir.reject {|k,v| /\A#{pattern.gsub('?','.').gsub('*', '.*')}\Z/ !~ k }.values
Expand All @@ -127,17 +127,18 @@ def find_recurser(dir, parts)
if parts.empty? # we're done recursing
matches
else
matches.map{|entry| find_recurser(entry, parts) }
matches.map{|entry| find_recurser(entry, parts, flags) }
end
end

def directories_under(dir)
children = dir.values.select{|f| is_a_directory_and_matches_flags?(f, 0)}
([dir] + children + children.map{|c| directories_under(c)}).flatten.uniq
def directories_under(dir, flags)
children = dir.values.select{|f| is_a_directory_and_matches_flags?(f, flags)}
([dir] + children + children.map{|c| directories_under(c, flags)}).flatten.uniq
end

def is_a_directory_and_matches_flags?(entry, flags)
entry.is_a?(FakeDir) && entry.name !~ /^\./
return entry.is_a?(FakeDir) if flags == File::FNM_DOTMATCH
return entry.is_a?(FakeDir) && entry.name !~ /^\./ if flags != File::FNM_DOTMATCH
end
end
end
5 changes: 5 additions & 0 deletions test/fakefs_test.rb
Expand Up @@ -500,6 +500,11 @@ def test_dir_glob_does_not_match_dot_dirs_by_default
assert_equal [], Dir['/one/**/*']
end

def test_dir_glob_can_be_overridden_to_match_dot_dirs
File.open('/one/.dotdir/three.rb', 'w')
assert_equal ['/one/.dotdir/three.rb'], Dir.glob(['/one/**/*'], File::FNM_DOTMATCH)
end

def test_should_report_pos_as_0_when_opening
File.open("/foo", "w") do |f|
f << "foobar"
Expand Down

0 comments on commit c1e76da

Please sign in to comment.