Permalink
Browse files

Merge pull request #116 from wijet/master

[PATCH] FileUtils.cp and FileUtils.cp_r support array as source
  • Loading branch information...
2 parents f0bef26 + f904af6 commit 6d76d306234ce88dbe904aa1f27939e1a7110492 @jfirebaugh jfirebaugh committed Apr 17, 2012
Showing with 78 additions and 31 deletions.
  1. +39 −31 lib/fakefs/fileutils.rb
  2. +39 −0 test/fakefs_test.rb
@@ -56,50 +56,58 @@ def ln_sf(target, path)
end
def cp(src, dest)
- dst_file = FileSystem.find(dest)
- src_file = FileSystem.find(src)
-
- if !src_file
- raise Errno::ENOENT, src
+ if src.is_a?(Array) && !File.directory?(dest)
+ raise Errno::ENOTDIR, dest
end
- if File.directory? src_file
- raise Errno::EISDIR, src
- end
+ Array(src).each do |src|
+ dst_file = FileSystem.find(dest)
+ src_file = FileSystem.find(src)
+
+ if !src_file
+ raise Errno::ENOENT, src
+ end
+
+ if File.directory? src_file
+ raise Errno::EISDIR, src
+ end
- if dst_file && File.directory?(dst_file)
- FileSystem.add(File.join(dest, src), src_file.entry.clone(dst_file))
- else
- FileSystem.delete(dest)
- FileSystem.add(dest, src_file.entry.clone)
+ if dst_file && File.directory?(dst_file)
+ FileSystem.add(File.join(dest, src), src_file.entry.clone(dst_file))
+ else
+ FileSystem.delete(dest)
+ FileSystem.add(dest, src_file.entry.clone)
+ end
end
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)
+ Array(src).each do |src|
+ # 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)
+ new_dir = FileSystem.find(dest)
- if new_dir && !File.directory?(dest)
- raise Errno::EEXIST, dest
- end
+ if new_dir && !File.directory?(dest)
+ raise Errno::EEXIST, dest
+ end
- if !new_dir && !FileSystem.find(dest+'/../')
- raise Errno::ENOENT, dest
- end
+ if !new_dir && !FileSystem.find(dest+'/../')
+ raise Errno::ENOENT, dest
+ end
- # This last bit is a total abuse and should be thought hard
- # about and cleaned up.
- if new_dir
- if src[-2..-1] == '/.'
- dir.values.each{|f| new_dir[f.name] = f.clone(new_dir) }
+ # This last bit is a total abuse and should be thought hard
+ # about and cleaned up.
+ if new_dir
+ if src[-2..-1] == '/.'
+ dir.values.each{|f| new_dir[f.name] = f.clone(new_dir) }
+ else
+ new_dir[dir.name] = dir.entry.clone(new_dir)
+ end
else
- new_dir[dir.name] = dir.entry.clone(new_dir)
+ FileSystem.add(dest, dir.entry.clone)
end
- else
- FileSystem.add(dest, dir.entry.clone)
end
end
View
@@ -1104,6 +1104,25 @@ def test_cp_file_into_dir
assert_equal 'bar', File.read('baz/foo')
end
+ def test_cp_array_of_files_into_directory
+ File.open('foo', 'w') { |f| f.write 'footext' }
+ File.open('bar', 'w') { |f| f.write 'bartext' }
+ FileUtils.mkdir_p 'destdir'
+ FileUtils.cp(%w(foo bar), 'destdir')
+
+ assert_equal 'footext', File.read('destdir/foo')
+ assert_equal 'bartext', File.read('destdir/bar')
+ end
+
+ def test_cp_fails_on_array_of_files_into_non_directory
+ File.open('foo', 'w') { |f| f.write 'footext' }
+
+ exception = assert_raise(Errno::ENOTDIR) do
+ FileUtils.cp(%w(foo), 'baz')
+ end
+ assert_equal "Not a directory - baz", exception.to_s
+ end
+
def test_cp_overwrites_dest_file
File.open('foo', 'w') {|f| f.write 'FOO' }
File.open('bar', 'w') {|f| f.write 'BAR' }
@@ -1161,6 +1180,26 @@ def test_cp_r_handles_copying_directories
end
end
+ def test_cp_r_array_of_files
+ FileUtils.mkdir_p 'subdir'
+ File.open('foo', 'w') { |f| f.write 'footext' }
+ File.open('bar', 'w') { |f| f.write 'bartext' }
+ FileUtils.cp_r(%w(foo bar), 'subdir')
+
+ assert_equal 'footext', File.open('subdir/foo') { |f| f.read }
+ assert_equal 'bartext', File.open('subdir/bar') { |f| f.read }
+ end
+
+ def test_cp_r_array_of_directories
+ %w(foo bar subdir).each { |d| FileUtils.mkdir_p d }
+ File.open('foo/baz', 'w') { |f| f.write 'baztext' }
+ File.open('bar/quux', 'w') { |f| f.write 'quuxtext' }
+
+ FileUtils.cp_r(%w(foo bar), 'subdir')
+ assert_equal 'baztext', File.open('subdir/foo/baz') { |f| f.read }
+ assert_equal 'quuxtext', File.open('subdir/bar/quux') { |f| f.read }
+ end
+
def test_cp_r_only_copies_into_directories
FileUtils.mkdir_p 'subdir'
Dir.chdir('subdir') { File.open('foo', 'w') { |f| f.write 'footext' } }

0 comments on commit 6d76d30

Please sign in to comment.