Module: FakeFS::FileUtils
Overview
FileUtils module
Instance Method Summary collapse
- #cd(dir, &block) ⇒ Object (also: #chdir)
- #chmod(mode, list, _options = {}) ⇒ Object
- #chmod_R(mode, list, _options = {}) ⇒ Object
- #chown(user, group, list, _options = {}) ⇒ Object
- #chown_R(user, group, list, _options = {}) ⇒ Object
- #compare_file(file1, file2) ⇒ Object (also: #cmp, #identical?)
- #copy_file(src, dest, _preserve = false, _dereference = true) ⇒ Object
- #cp(src, dest, options = {}) ⇒ Object (also: #copy)
- #cp_r(src, dest, options = {}) ⇒ Object
- #ln_s(target, path, options = {}) ⇒ Object (also: #symlink)
- #ln_sf(target, path) ⇒ Object
- #mkdir(list, _ignored_options = {}) ⇒ Object
- #mkdir_p(list, _options = {}) ⇒ Object (also: #mkpath, #makedirs)
- #mv(src, dest, options = {}) ⇒ Object (also: #move)
- #rm(list, options = {}) ⇒ Object (also: #rm_r, #rm_f, #remove)
- #rm_rf(list, options = {}) ⇒ Object (also: #rmtree, #remove_entry_secure)
- #rmdir(list, _options = {}) ⇒ Object
- #touch(list, options = {}) ⇒ Object
Instance Method Details
#cd(dir, &block) ⇒ Object Also known as: chdir
273 274 275 |
# File 'lib/fakefs/fileutils.rb', line 273 def cd(dir, &block) FileSystem.chdir(dir, &block) end |
#chmod(mode, list, _options = {}) ⇒ Object
232 233 234 235 236 237 238 239 240 241 242 |
# File 'lib/fakefs/fileutils.rb', line 232 def chmod(mode, list, = {}) list = Array(list) list.each do |f| if File.exist?(f) File.chmod(mode, f) else fail Errno::ENOENT, f end end list end |
#chmod_R(mode, list, _options = {}) ⇒ Object
244 245 246 247 248 249 250 251 252 253 |
# File 'lib/fakefs/fileutils.rb', line 244 def chmod_R(mode, list, = {}) list = Array(list) list.each do |file| chmod(mode, file) [FileSystem.find("#{file}/**/**")].flatten.each do |f| chmod(mode, f.to_s) end end list end |
#chown(user, group, list, _options = {}) ⇒ Object
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 |
# File 'lib/fakefs/fileutils.rb', line 197 def chown(user, group, list, = {}) list = Array(list) list.each do |f| if File.exist?(f) uid = if user user.to_s.match(/[0-9]+/) ? user.to_i : Etc.getpwnam(user).uid else nil end gid = if group group.to_s.match(/[0-9]+/) ? group.to_i : Etc.getgrnam(group).gid else nil end File.chown(uid, gid, f) else fail Errno::ENOENT, f end end list end |
#chown_R(user, group, list, _options = {}) ⇒ Object
221 222 223 224 225 226 227 228 229 230 |
# File 'lib/fakefs/fileutils.rb', line 221 def chown_R(user, group, list, = {}) list = Array(list) list.each do |file| chown(user, group, file) [FileSystem.find("#{file}/**/**")].flatten.each do |f| chown(user, group, f.to_s) end end list end |
#compare_file(file1, file2) ⇒ Object Also known as: cmp, identical?
278 279 280 281 |
# File 'lib/fakefs/fileutils.rb', line 278 def compare_file(file1, file2) # we do a strict comparison of both files content File.readlines(file1) == File.readlines(file2) end |
#copy_file(src, dest, _preserve = false, _dereference = true) ⇒ Object
126 127 128 129 |
# File 'lib/fakefs/fileutils.rb', line 126 def copy_file(src, dest, _preserve = false, _dereference = true) # Not a perfect match, but similar to what regular FileUtils does. cp(src, dest) end |
#cp(src, dest, options = {}) ⇒ Object Also known as: copy
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/fakefs/fileutils.rb', line 95 def cp(src, dest, = {}) fail Errno::ENOTDIR, dest if src.is_a?(Array) && !File.directory?(dest) # handle `verbose' flag RealFileUtils.cp src, dest, .merge(noop: true) # handle `noop' flag return if [:noop] Array(src).each do |source| dst_file = FileSystem.find(dest) src_file = FileSystem.find(source) fail Errno::ENOENT, source unless src_file fail Errno::EISDIR, source if File.directory? src_file if dst_file && File.directory?(dst_file) FileSystem.add( File.join( dest, File.basename(source)), src_file.entry.clone(dst_file)) else FileSystem.delete(dest) FileSystem.add(dest, src_file.entry.clone) end end nil end |
#cp_r(src, dest, options = {}) ⇒ Object
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/fakefs/fileutils.rb', line 131 def cp_r(src, dest, = {}) # handle `verbose' flag RealFileUtils.cp_r src, dest, .merge(noop: true) # handle `noop' flag return if [:noop] Array(src).each do |source| # This error sucks, but it conforms to the original Ruby # method. fail "unknown file type: #{source}" unless (dir = FileSystem.find(source)) new_dir = FileSystem.find(dest) fail Errno::EEXIST, dest if new_dir && !File.directory?(dest) fail Errno::ENOENT, dest if !new_dir && !FileSystem.find(dest + '/../') # This last bit is a total abuse and should be thought hard # about and cleaned up. if new_dir if src[-2..-1] == '/.' dir.entries.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 nil end |
#ln_s(target, path, options = {}) ⇒ Object Also known as: symlink
77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/fakefs/fileutils.rb', line 77 def ln_s(target, path, = {}) = { force: false }.merge() fail(Errno::EEXIST, path) if FileSystem.find(path) && ![:force] FileSystem.delete(path) if ![:force] && !Dir.exist?(File.dirname(path)) fail Errno::ENOENT, path end FileSystem.add(path, FakeSymlink.new(target)) end |
#ln_sf(target, path) ⇒ Object
89 90 91 |
# File 'lib/fakefs/fileutils.rb', line 89 def ln_sf(target, path) ln_s(target, path, force: true) end |
#mkdir(list, _ignored_options = {}) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/fakefs/fileutils.rb', line 35 def mkdir(list, = {}) list = [list] unless list.is_a?(Array) list.each do |path| parent = path.split('/') parent.pop fail Errno::ENOENT, path unless parent.join == '' || parent.join == '.' || FileSystem.find(parent.join('/')) fail Errno::EEXIST, path if FileSystem.find(path) FileSystem.add(path, FakeDir.new) end end |
#mkdir_p(list, _options = {}) ⇒ Object Also known as: mkpath, makedirs
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/fakefs/fileutils.rb', line 6 def mkdir_p(list, = {}) list = [list] unless list.is_a?(Array) list.each do |path| # FileSystem.add call adds all the necessary parent directories but # can't set their mode. Thus, we have to collect created directories # here and set the mode later. if [:mode] created_dirs = [] dir = path until Dir.exists?(dir) created_dirs << dir dir = File.dirname(dir) end end FileSystem.add(path, FakeDir.new) if [:mode] created_dirs.each do |dir| File.chmod([:mode], dir) end end end end |
#mv(src, dest, options = {}) ⇒ Object Also known as: move
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/fakefs/fileutils.rb', line 164 def mv(src, dest, = {}) # handle `verbose' flag RealFileUtils.mv src, dest, .merge(noop: true) # handle `noop' flag return if [:noop] Array(src).each do |path| if (target = FileSystem.find(path)) dest_path = if File.directory?(dest) File.join(dest, File.basename(path)) else dest end if File.directory?(dest_path) fail Errno::EEXIST, dest_path unless [:force] elsif File.directory?(File.dirname(dest_path)) FileSystem.delete(dest_path) FileSystem.add(dest_path, target.entry.clone) FileSystem.delete(path) else fail Errno::ENOENT, dest_path unless [:force] end else fail Errno::ENOENT, path end end nil end |
#rm(list, options = {}) ⇒ Object Also known as: rm_r, rm_f, remove
60 61 62 63 64 65 |
# File 'lib/fakefs/fileutils.rb', line 60 def rm(list, = {}) Array(list).each do |path| FileSystem.delete(path) || (![:force] && fail(Errno::ENOENT, path)) end end |
#rm_rf(list, options = {}) ⇒ Object Also known as: rmtree, remove_entry_secure
70 71 72 |
# File 'lib/fakefs/fileutils.rb', line 70 def rm_rf(list, = {}) rm_r(list, .merge(:force => true)) end |
#rmdir(list, _options = {}) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/fakefs/fileutils.rb', line 47 def rmdir(list, = {}) list = [list] unless list.is_a?(Array) list.each do |l| parent = l.split('/') parent.pop fail Errno::ENOENT, l unless parent.join == '' || FileSystem.find(parent.join('/')) fail Errno::ENOENT, l unless FileSystem.find(l) fail Errno::ENOTEMPTY, l unless FileSystem.find(l).empty? rm(l) end end |
#touch(list, options = {}) ⇒ Object
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 |
# File 'lib/fakefs/fileutils.rb', line 255 def touch(list, = {}) Array(list).each do |f| if (fs = FileSystem.find(f)) now = Time.now fs.mtime = [:mtime] || now fs.atime = now else file = File.open(f, 'w') file.close if (mtime = [:mtime]) fs = FileSystem.find(f) fs.mtime = mtime end end end end |