From af2eb63dbe4ac134bcd98a725240f42ca6fdc09a Mon Sep 17 00:00:00 2001 From: Jeff Hodges Date: Sat, 30 May 2009 10:56:06 -0700 Subject: [PATCH] Adding support for chown, chown_R and tests for Dir.glob. --- lib/fakefs.rb | 21 ++++++++++++++++++++- test/fakefs_test.rb | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 1 deletion(-) diff --git a/lib/fakefs.rb b/lib/fakefs.rb index 68e9f7a..ae5d562 100644 --- a/lib/fakefs.rb +++ b/lib/fakefs.rb @@ -33,6 +33,20 @@ module FakeFS FileSystem.delete(src) end end + + def chown(user, group, list, options={}) + list = Array(list) + list.each do |f| + unless File.exists?(f) + raise Errno::ENOENT, f + end + end + list + end + + def chown_R(user, group, list, options={}) + chown(user, group, list, options={}) + end end class File @@ -118,7 +132,12 @@ module FakeFS class Dir def self.glob(pattern) - FileSystem.find(pattern).map { |entry| entry.to_s} + if pattern[-1,1] == '*' + blk = proc { |entry| entry.to_s } + else + blk = proc { |entry| entry[1].parent.to_s } + end + (FileSystem.find(pattern) || []).map(&blk).uniq.sort end def self.[](pattern) diff --git a/test/fakefs_test.rb b/test/fakefs_test.rb index 8f64ebc..a298ade 100644 --- a/test/fakefs_test.rb +++ b/test/fakefs_test.rb @@ -106,4 +106,43 @@ class FakeFSTest < Test::Unit::TestCase assert File.file?(path) end + + def test_can_chown_files + good = 'file.txt' + bad = 'nofile.txt' + File.open(good,'w'){|f| f.write "foo" } + + assert_equal [good], FileUtils.chown('noone', 'nogroup', good, :verbose => true) + assert_raises(Errno::ENOENT) do + FileUtils.chown('noone', 'nogroup', bad, :verbose => true) + end + + assert_equal [good], FileUtils.chown('noone', 'nogroup', good) + assert_raises(Errno::ENOENT) do + FileUtils.chown('noone', 'nogroup', bad) + end + + assert_equal [good], FileUtils.chown('noone', 'nogroup', [good]) + assert_raises(Errno::ENOENT) do + FileUtils.chown('noone', 'nogroup', [good, bad]) + end + end + + def test_can_chown_R_files + FileUtils.mkdir_p '/path/' + File.open('/path/foo', 'w'){|f| f.write 'foo' } + File.open('/path/foobar', 'w'){|f| f.write 'foo' } + resp = FileUtils.chown_R('no', 'no', '/path') + assert_equal ['/path'], resp + end + + def test_dir_globs_paths + FileUtils.mkdir_p '/path' + File.open('/path/foo', 'w'){|f| f.write 'foo' } + File.open('/path/foobar', 'w'){|f| f.write 'foo' } + assert_equal ['/path'], Dir['/path'] + assert_equal ['/path/foo', '/path/foobar'], Dir['/path/*'] + # Unsupported so far. More hackery than I want to work on right now + # assert_equal ['/path'], Dir['/path*'] + end end