diff --git a/lib/fakefs/dir.rb b/lib/fakefs/dir.rb index c9330e6f..fc7580d1 100644 --- a/lib/fakefs/dir.rb +++ b/lib/fakefs/dir.rb @@ -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 diff --git a/lib/fakefs/file_system.rb b/lib/fakefs/file_system.rb index baa6f2bd..253e1e27 100644 --- a/lib/fakefs/file_system.rb +++ b/lib/fakefs/file_system.rb @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/test/fakefs_test.rb b/test/fakefs_test.rb index a5c073e3..44e7234c 100644 --- a/test/fakefs_test.rb +++ b/test/fakefs_test.rb @@ -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"