Skip to content
This repository
Browse code

Merge pull request #116 from wijet/master

[PATCH] FileUtils.cp and FileUtils.cp_r support array as source
  • Loading branch information...
commit 6d76d306234ce88dbe904aa1f27939e1a7110492 2 parents f0bef26 + f904af6
John Firebaugh authored
70  lib/fakefs/fileutils.rb
@@ -56,50 +56,58 @@ def ln_sf(target, path)
56 56
     end
57 57
 
58 58
     def cp(src, dest)
59  
-      dst_file = FileSystem.find(dest)
60  
-      src_file = FileSystem.find(src)
61  
-
62  
-      if !src_file
63  
-        raise Errno::ENOENT, src
  59
+      if src.is_a?(Array) && !File.directory?(dest)
  60
+        raise Errno::ENOTDIR, dest
64 61
       end
65 62
 
66  
-      if File.directory? src_file
67  
-        raise Errno::EISDIR, src
68  
-      end
  63
+      Array(src).each do |src|
  64
+        dst_file = FileSystem.find(dest)
  65
+        src_file = FileSystem.find(src)
  66
+
  67
+        if !src_file
  68
+          raise Errno::ENOENT, src
  69
+        end
  70
+
  71
+        if File.directory? src_file
  72
+          raise Errno::EISDIR, src
  73
+        end
69 74
 
70  
-      if dst_file && File.directory?(dst_file)
71  
-        FileSystem.add(File.join(dest, src), src_file.entry.clone(dst_file))
72  
-      else
73  
-        FileSystem.delete(dest)
74  
-        FileSystem.add(dest, src_file.entry.clone)
  75
+        if dst_file && File.directory?(dst_file)
  76
+          FileSystem.add(File.join(dest, src), src_file.entry.clone(dst_file))
  77
+        else
  78
+          FileSystem.delete(dest)
  79
+          FileSystem.add(dest, src_file.entry.clone)
  80
+        end
75 81
       end
76 82
     end
77 83
 
78 84
     def cp_r(src, dest)
79  
-      # This error sucks, but it conforms to the original Ruby
80  
-      # method.
81  
-      raise "unknown file type: #{src}" unless dir = FileSystem.find(src)
  85
+      Array(src).each do |src|
  86
+        # This error sucks, but it conforms to the original Ruby
  87
+        # method.
  88
+        raise "unknown file type: #{src}" unless dir = FileSystem.find(src)
82 89
 
83  
-      new_dir = FileSystem.find(dest)
  90
+        new_dir = FileSystem.find(dest)
84 91
 
85  
-      if new_dir && !File.directory?(dest)
86  
-        raise Errno::EEXIST, dest
87  
-      end
  92
+        if new_dir && !File.directory?(dest)
  93
+          raise Errno::EEXIST, dest
  94
+        end
88 95
 
89  
-      if !new_dir && !FileSystem.find(dest+'/../')
90  
-        raise Errno::ENOENT, dest
91  
-      end
  96
+        if !new_dir && !FileSystem.find(dest+'/../')
  97
+          raise Errno::ENOENT, dest
  98
+        end
92 99
 
93  
-      # This last bit is a total abuse and should be thought hard
94  
-      # about and cleaned up.
95  
-      if new_dir
96  
-        if src[-2..-1] == '/.'
97  
-          dir.values.each{|f| new_dir[f.name] = f.clone(new_dir) }
  100
+        # This last bit is a total abuse and should be thought hard
  101
+        # about and cleaned up.
  102
+        if new_dir
  103
+          if src[-2..-1] == '/.'
  104
+            dir.values.each{|f| new_dir[f.name] = f.clone(new_dir) }
  105
+          else
  106
+            new_dir[dir.name] = dir.entry.clone(new_dir)
  107
+          end
98 108
         else
99  
-          new_dir[dir.name] = dir.entry.clone(new_dir)
  109
+          FileSystem.add(dest, dir.entry.clone)
100 110
         end
101  
-      else
102  
-        FileSystem.add(dest, dir.entry.clone)
103 111
       end
104 112
     end
105 113
 
39  test/fakefs_test.rb
@@ -1104,6 +1104,25 @@ def test_cp_file_into_dir
1104 1104
     assert_equal 'bar', File.read('baz/foo')
1105 1105
   end
1106 1106
 
  1107
+  def test_cp_array_of_files_into_directory
  1108
+    File.open('foo', 'w') { |f| f.write 'footext' }
  1109
+    File.open('bar', 'w') { |f| f.write 'bartext' }
  1110
+    FileUtils.mkdir_p 'destdir'
  1111
+    FileUtils.cp(%w(foo bar), 'destdir')
  1112
+
  1113
+    assert_equal 'footext', File.read('destdir/foo')
  1114
+    assert_equal 'bartext', File.read('destdir/bar')
  1115
+  end
  1116
+
  1117
+  def test_cp_fails_on_array_of_files_into_non_directory
  1118
+    File.open('foo', 'w') { |f| f.write 'footext' }
  1119
+
  1120
+    exception = assert_raise(Errno::ENOTDIR) do
  1121
+      FileUtils.cp(%w(foo), 'baz')
  1122
+    end
  1123
+    assert_equal "Not a directory - baz", exception.to_s
  1124
+  end
  1125
+
1107 1126
   def test_cp_overwrites_dest_file
1108 1127
     File.open('foo', 'w') {|f| f.write 'FOO' }
1109 1128
     File.open('bar', 'w') {|f| f.write 'BAR' }
@@ -1161,6 +1180,26 @@ def test_cp_r_handles_copying_directories
1161 1180
     end
1162 1181
   end
1163 1182
 
  1183
+  def test_cp_r_array_of_files
  1184
+    FileUtils.mkdir_p 'subdir'
  1185
+    File.open('foo', 'w') { |f| f.write 'footext' }
  1186
+    File.open('bar', 'w') { |f| f.write 'bartext' }
  1187
+    FileUtils.cp_r(%w(foo bar), 'subdir')
  1188
+
  1189
+    assert_equal 'footext', File.open('subdir/foo') { |f| f.read }
  1190
+    assert_equal 'bartext', File.open('subdir/bar') { |f| f.read }
  1191
+  end
  1192
+
  1193
+  def test_cp_r_array_of_directories
  1194
+    %w(foo bar subdir).each { |d| FileUtils.mkdir_p d }
  1195
+    File.open('foo/baz', 'w') { |f| f.write 'baztext' }
  1196
+    File.open('bar/quux', 'w') { |f| f.write 'quuxtext' }
  1197
+
  1198
+    FileUtils.cp_r(%w(foo bar), 'subdir')
  1199
+    assert_equal 'baztext', File.open('subdir/foo/baz') { |f| f.read }
  1200
+    assert_equal 'quuxtext', File.open('subdir/bar/quux') { |f| f.read }
  1201
+  end
  1202
+
1164 1203
   def test_cp_r_only_copies_into_directories
1165 1204
     FileUtils.mkdir_p 'subdir'
1166 1205
     Dir.chdir('subdir') { File.open('foo', 'w') { |f| f.write 'footext' } }

0 notes on commit 6d76d30

Please sign in to comment.
Something went wrong with that request. Please try again.