Adding support for chown, chown_R and tests for Dir.glob.

This commit is contained in:
Jeff Hodges 2009-05-30 10:56:06 -07:00
parent b9a8f4e8ca
commit af2eb63dbe
2 changed files with 59 additions and 1 deletions

View File

@ -33,6 +33,20 @@ module FakeFS
FileSystem.delete(src) FileSystem.delete(src)
end end
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 end
class File class File
@ -118,7 +132,12 @@ module FakeFS
class Dir class Dir
def self.glob(pattern) 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 end
def self.[](pattern) def self.[](pattern)

View File

@ -106,4 +106,43 @@ class FakeFSTest < Test::Unit::TestCase
assert File.file?(path) assert File.file?(path)
end 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 end