Module: FakeFS::FileUtils

Extended by:
FileUtils
Included in:
FileUtils
Defined in:
lib/fakefs/fileutils.rb

Instance Method Summary collapse

Instance Method Details

#cd(dir) ⇒ Object Also known as: chdir



135
136
137
# File 'lib/fakefs/fileutils.rb', line 135

def cd(dir)
  FileSystem.chdir(dir)
end

#chown(user, group, list, options = {}) ⇒ Object



109
110
111
112
113
114
115
116
117
# File 'lib/fakefs/fileutils.rb', line 109

def chown(user, group, list, options={})
  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



119
120
121
# File 'lib/fakefs/fileutils.rb', line 119

def chown_R(user, group, list, options={})
  chown(user, group, list, options={})
end

#cp(src, dest) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/fakefs/fileutils.rb', line 49

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



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/fakefs/fileutils.rb', line 69

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, options = {}) ⇒ Object



40
41
42
43
44
# File 'lib/fakefs/fileutils.rb', line 40

def ln_s(target, path, options = {})
  options = { :force => false }.merge(options)
  (FileSystem.find(path) and !options[:force]) ? raise(Errno::EEXIST, path) : FileSystem.delete(path)
  FileSystem.add(path, FakeSymlink.new(target))
end

#ln_sf(target, path) ⇒ Object



45
46
47
# File 'lib/fakefs/fileutils.rb', line 45

def ln_sf(target, path)
  ln_s(target, path, { :force => true })
end

#mkdir(path) ⇒ Object

Raises:

  • (Errno::ENOENT)


10
11
12
13
14
15
16
# File 'lib/fakefs/fileutils.rb', line 10

def mkdir(path)
  parent = path.split('/')
  parent.pop
  raise Errno::ENOENT, "No such file or directory - #{path}" unless parent.join == "" || FileSystem.find(parent.join('/'))
  raise Errno::EEXIST, "File exists - #{path}" if FileSystem.find(path)
  FileSystem.add(path, FakeDir.new)
end

#mkdir_p(path, options = {}) ⇒ Object Also known as: mkpath



5
6
7
# File 'lib/fakefs/fileutils.rb', line 5

def mkdir_p(path, options = {})
  FileSystem.add(path, FakeDir.new)
end

#mv(src, dest, options = {}) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
# File 'lib/fakefs/fileutils.rb', line 97

def mv(src, dest, options={})
  Array(src).each do |path|
    if target = FileSystem.find(path)
      dest_path = File.directory?(dest) ? File.join(dest, File.basename(path)) : dest
      FileSystem.add(dest_path, target.entry.clone)
      FileSystem.delete(path)
    else
      raise Errno::ENOENT, src
    end
  end
end

#rm(list, options = {}) ⇒ Object Also known as: rm_rf, rm_r, rm_f



30
31
32
33
34
# File 'lib/fakefs/fileutils.rb', line 30

def rm(list, options = {})
  Array(list).each do |path|
    FileSystem.delete(path)
  end
end

#rmdir(list, options = {}) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/fakefs/fileutils.rb', line 18

def rmdir(list, options = {})
  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



123
124
125
126
127
128
129
130
131
132
133
# File 'lib/fakefs/fileutils.rb', line 123

def touch(list, options={})
  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