Skip to content

Commit

Permalink
FileUtils.cp and FileUtils.cp_r support array of files/directories as…
Browse files Browse the repository at this point in the history
… source
  • Loading branch information
wijet committed Jan 8, 2012
1 parent 57d1dd3 commit f904af6
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 31 deletions.
70 changes: 39 additions & 31 deletions lib/fakefs/fileutils.rb
Expand Up @@ -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

Expand Down
39 changes: 39 additions & 0 deletions test/fakefs_test.rb
Expand Up @@ -1036,6 +1036,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' }
Expand Down Expand Up @@ -1093,6 +1112,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' } }
Expand Down

0 comments on commit f904af6

Please sign in to comment.