From 66c27ff31b9b958bf78eda62cd017ded1fc43495 Mon Sep 17 00:00:00 2001 From: Mariusz Pietrzyk Date: Sun, 26 Dec 2010 01:05:26 +0100 Subject: [PATCH] FakeFS::Dir.glob works with a given block. Closes #39. Closes #55. --- lib/fakefs/dir.rb | 5 +++-- test/fakefs_test.rb | 10 ++++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/lib/fakefs/dir.rb b/lib/fakefs/dir.rb index c407928..32c5234 100644 --- a/lib/fakefs/dir.rb +++ b/lib/fakefs/dir.rb @@ -78,8 +78,9 @@ module FakeFS Dir.open(dirname) { |file| yield file } end - def self.glob(pattern) - [FileSystem.find(pattern) || []].flatten.map{|e| e.to_s}.sort + def self.glob(pattern, &block) + files = [FileSystem.find(pattern) || []].flatten.map(&:to_s).sort + block_given? ? files.each { |file| block.call(file) } : files end def self.mkdir(string, integer = 0) diff --git a/test/fakefs_test.rb b/test/fakefs_test.rb index 0948ce7..f041baf 100644 --- a/test/fakefs_test.rb +++ b/test/fakefs_test.rb @@ -588,6 +588,16 @@ class FakeFSTest < Test::Unit::TestCase assert_equal ['/one/five.rb', '/one/two'], Dir['/one/**'] end + def test_dir_glob_with_block + FileUtils.touch('foo') + FileUtils.touch('bar') + + yielded = [] + Dir.glob('*') { |file| yielded << file } + + assert_equal 2, yielded.size + end + def test_should_report_pos_as_0_when_opening File.open("/foo", "w") do |f| f << "foobar"