Module: FakeFS::FileUtils
Instance Method Summary collapse
- #cd(dir) ⇒ Object (also: #chdir)
- #chown(user, group, list, options = {}) ⇒ Object
- #chown_R(user, group, list, options = {}) ⇒ Object
- #cp(src, dest) ⇒ Object
- #cp_r(src, dest) ⇒ Object
- #ln_s(target, path) ⇒ Object
- #mkdir_p(path) ⇒ Object (also: #mkpath)
- #mv(src, dest) ⇒ Object
- #rm(path) ⇒ Object (also: #rm_rf, #rm_r)
- #rmdir(list, options = {}) ⇒ Object
- #touch(list, options = {}) ⇒ Object
Instance Method Details
#cd(dir) ⇒ Object Also known as: chdir
117 118 119 |
# File 'lib/fakefs/fileutils.rb', line 117 def cd(dir) FileSystem.chdir(dir) end |
#chown(user, group, list, options = {}) ⇒ Object
91 92 93 94 95 96 97 98 99 |
# File 'lib/fakefs/fileutils.rb', line 91 def chown(user, group, list, ={}) list = Array(list) list.each do |f| unless File.exists?(f) raise Errno::ENOENT, f end end list end |
#chown_R(user, group, list, options = {}) ⇒ Object
101 102 103 |
# File 'lib/fakefs/fileutils.rb', line 101 def chown_R(user, group, list, ={}) chown(user, group, list, ={}) end |
#cp(src, dest) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/fakefs/fileutils.rb', line 34 def cp(src, dest) 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) end end |
#cp_r(src, dest) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/fakefs/fileutils.rb', line 54 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 # 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 FileSystem.add(dest, dir.entry.clone) end end |
#ln_s(target, path) ⇒ Object
29 30 31 32 |
# File 'lib/fakefs/fileutils.rb', line 29 def ln_s(target, path) raise Errno::EEXIST, path if FileSystem.find(path) FileSystem.add(path, FakeSymlink.new(target)) end |
#mkdir_p(path) ⇒ Object Also known as: mkpath
5 6 7 |
# File 'lib/fakefs/fileutils.rb', line 5 def mkdir_p(path) FileSystem.add(path, FakeDir.new) end |
#mv(src, dest) ⇒ Object
82 83 84 85 86 87 88 89 |
# File 'lib/fakefs/fileutils.rb', line 82 def mv(src, dest) if target = FileSystem.find(src) FileSystem.add(dest, target.entry.clone) FileSystem.delete(src) else raise Errno::ENOENT, src end end |
#rm(path) ⇒ Object Also known as: rm_rf, rm_r
22 23 24 |
# File 'lib/fakefs/fileutils.rb', line 22 def rm(path) FileSystem.delete(path) end |
#rmdir(list, options = {}) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/fakefs/fileutils.rb', line 10 def rmdir(list, = {}) list = [ list ] unless list.is_a?(Array) list.each do |l| parent = l.split('/') parent.pop raise Errno::ENOENT, "No such file or directory - #{l}" unless parent.join == "" || FileSystem.find(parent.join('/')) raise Errno::ENOENT, l unless FileSystem.find(l) raise Errno::ENOTEMPTY, l unless FileSystem.find(l).values.empty? rm(l) end end |
#touch(list, options = {}) ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/fakefs/fileutils.rb', line 105 def touch(list, ={}) Array(list).each do |f| directory = File.dirname(f) # FIXME this explicit check for '.' shouldn't need to happen if File.exists?(directory) || directory == '.' FileSystem.add(f, FakeFile.new) else raise Errno::ENOENT, f end end end |