From 3537c43cbe4ea8cafc47896cc2ef018d59741e06 Mon Sep 17 00:00:00 2001 From: Chris Wanstrath Date: Wed, 3 Jun 2009 17:53:07 -0700 Subject: [PATCH] add FileUtils.cp & tests --- lib/fakefs.rb | 41 ++++++++++++++++++++++++++++++++++++----- test/fakefs_test.rb | 29 +++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+), 5 deletions(-) diff --git a/lib/fakefs.rb b/lib/fakefs.rb index 1a0942f..dab6eed 100644 --- a/lib/fakefs.rb +++ b/lib/fakefs.rb @@ -25,17 +25,36 @@ 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. raise "unknown file type: #{src}" unless dir = FileSystem.find(src) - + new_dir = FileSystem.find(dest) if new_dir && !File.directory?(dest) raise Errno::EEXIST, dest end - + if !new_dir && !FileSystem.find(dest+'/../') raise Errno::ENOENT, dest end @@ -89,15 +108,27 @@ module FakeFS end def self.directory?(path) - FileSystem.find(path).entry.is_a? MockDir + if path.respond_to? :entry + path.entry.is_a? MockDir + else + FileSystem.find(path).entry.is_a? MockDir + end end def self.symlink?(path) - FileSystem.find(path).is_a? MockSymlink + if path.respond_to? :entry + path.is_a? MockSymlink + else + FileSystem.find(path).is_a? MockSymlink + end end def self.file?(path) - FileSystem.find(path).entry.is_a? MockFile + 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) diff --git a/test/fakefs_test.rb b/test/fakefs_test.rb index 744f1dd..4301572 100644 --- a/test/fakefs_test.rb +++ b/test/fakefs_test.rb @@ -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')