add FileUtils.cp & tests

This commit is contained in:
Chris Wanstrath 2009-06-03 17:53:07 -07:00
parent e051533cd4
commit 3537c43cbe
2 changed files with 65 additions and 5 deletions

View File

@ -25,6 +25,25 @@ module FakeFS
FileSystem.add(path, MockSymlink.new(target))
end
def cp(src, dest)
dst_file = FileSystem.find(dest)
src_file = FileSystem.find(src)
if dst_file
raise Errno::EEXIST, dest
end
if !src_file
raise Errno::ENOENT, src
end
if File.directory? src_file
raise Errno::EISDIR, src
end
FileSystem.add(dest, src_file.entry.clone)
end
def cp_r(src, dest)
# This error sucks, but it conforms to the original Ruby
# method.
@ -89,16 +108,28 @@ module FakeFS
end
def self.directory?(path)
if path.respond_to? :entry
path.entry.is_a? MockDir
else
FileSystem.find(path).entry.is_a? MockDir
end
end
def self.symlink?(path)
if path.respond_to? :entry
path.is_a? MockSymlink
else
FileSystem.find(path).is_a? MockSymlink
end
end
def self.file?(path)
if path.respond_to? :entry
path.entry.is_a? MockFile
else
FileSystem.find(path).entry.is_a? MockFile
end
end
def self.expand_path(*args)
RealFile.expand_path(*args)

View File

@ -298,6 +298,35 @@ class FakeFSTest < Test::Unit::TestCase
assert_equal 'bar', File.open('baz'){|f| f.read }
end
def test_cp_actually_works
File.open('foo', 'w') {|f| f.write 'bar' }
FileUtils.cp('foo', 'baz')
assert_equal 'bar', File.read('baz')
end
def test_cp_fails_on_dest_exists
File.open('foo', 'w') {|f| f.write 'bar' }
FileUtils.mkdir_p 'baz'
assert_raise Errno::EEXIST do
FileUtils.cp('foo', 'baz')
end
end
def test_cp_fails_on_no_source
assert_raise Errno::ENOENT do
FileUtils.cp('foo', 'baz')
end
end
def test_cp_fails_on_directory_copy
FileUtils.mkdir_p 'baz'
assert_raise Errno::EISDIR do
FileUtils.cp('baz', 'bar')
end
end
def test_cp_r_doesnt_tangle_files_together
File.open('foo', 'w') {|f| f.write 'bar' }
FileUtils.cp_r('foo', 'baz')