Skip to content

Commit

Permalink
Fix FileUtils.cp behavior.
Browse files Browse the repository at this point in the history
  • Loading branch information
nakajima committed Jun 11, 2009
1 parent 3537c43 commit bf6f121
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
11 changes: 6 additions & 5 deletions lib/fakefs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,6 @@ 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
Expand All @@ -41,7 +37,12 @@ def cp(src, dest)
raise Errno::EISDIR, src
end

FileSystem.add(dest, src_file.entry.clone)
if dst_file and File.directory?(dst_file)
FileSystem.add(File.join(dest, src), src_file.entry.clone)
else
FileSystem.delete(dest)
FileSystem.add(dest, src_file.entry.clone)
end
end

def cp_r(src, dest)
Expand Down
15 changes: 11 additions & 4 deletions test/fakefs_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -304,13 +304,20 @@ def test_cp_actually_works
assert_equal 'bar', File.read('baz')
end

def test_cp_fails_on_dest_exists
def test_cp_file_into_dir
File.open('foo', 'w') {|f| f.write 'bar' }
FileUtils.mkdir_p 'baz'

assert_raise Errno::EEXIST do
FileUtils.cp('foo', 'baz')
end
FileUtils.cp('foo', 'baz')
assert_equal 'bar', File.read('baz/foo')
end

def test_cp_overwrites_dest_file
File.open('foo', 'w') {|f| f.write 'FOO' }
File.open('bar', 'w') {|f| f.write 'BAR' }

FileUtils.cp('foo', 'bar')
assert_equal 'FOO', File.read('bar')
end

def test_cp_fails_on_no_source
Expand Down

0 comments on commit bf6f121

Please sign in to comment.