Module: FakeFS::FileSystem

Extended by:
FileSystem
Included in:
FileSystem
Defined in:
lib/fakefs/file_system.rb

Overview

FileSystem module

Instance Method Summary collapse

Instance Method Details

#add(path, object = FakeDir.new) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/fakefs/file_system.rb', line 36

def add(path, object = FakeDir.new)
  parts = path_parts(normalize_path(path))

  d = parts[0...-1].reduce(fs) do |dir, part|
    assert_dir dir[part] if dir[part]
    dir[part] ||= FakeDir.new(part, dir)
  end

  assert_dir d
  object.name = parts.last
  object.parent = d
  d[parts.last] ||= object
end

#chdir(dir, &blk) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
# File 'lib/fakefs/file_system.rb', line 82

def chdir(dir, &blk)
  new_dir = find(dir)
  dir_levels.push dir if blk

  fail Errno::ENOENT, dir unless new_dir

  dir_levels.push dir unless blk
  blk.call if blk
ensure
  dir_levels.pop if blk
end

#clearObject



14
15
16
17
# File 'lib/fakefs/file_system.rb', line 14

def clear
  @dir_levels = nil
  @fs = nil
end

#clone(path, target = nil) ⇒ Object

copies directories and files from the real filesystem into our fake one



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/fakefs/file_system.rb', line 52

def clone(path, target = nil)
  path    = RealFile.expand_path(path)
  pattern = File.join(path, '**', '*')
  files   = if RealFile.file?(path)
              [path]
            else
              [path] + RealDir.glob(pattern, RealFile::FNM_DOTMATCH)
            end

  files.each do |f|
    target_path = target ? f.gsub(path, target) : f
    if RealFile.symlink?(f)
      FileUtils.ln_s(RealFile.readlink(f), f)
    elsif RealFile.file?(f)
      FileUtils.mkdir_p(File.dirname(f))
      File.open(target_path, File::WRITE_ONLY) do |g|
        g.print RealFile.open(f) { |h| h.read }
      end
    elsif RealFile.directory?(f)
      FileUtils.mkdir_p(target_path)
    end
  end
end

#current_dirObject



109
110
111
# File 'lib/fakefs/file_system.rb', line 109

def current_dir
  find('.')
end

#delete(path) ⇒ Object



76
77
78
79
80
# File 'lib/fakefs/file_system.rb', line 76

def delete(path)
  return unless (node = FileSystem.find(path))
  node.delete
  true
end

#dir_levelsObject



6
7
8
# File 'lib/fakefs/file_system.rb', line 6

def dir_levels
  @dir_levels ||= ['/']
end

#filesObject



19
20
21
# File 'lib/fakefs/file_system.rb', line 19

def files
  fs.entries
end

#find(path) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/fakefs/file_system.rb', line 23

def find(path)
  parts = path_parts(normalize_path(path))
  return fs if parts.empty? # '/'

  entries = find_recurser(fs, parts).flatten

  case entries.length
  when 0 then nil
  when 1 then entries.first
  else entries
  end
end

#fsObject



10
11
12
# File 'lib/fakefs/file_system.rb', line 10

def fs
  @fs ||= FakeDir.new('/')
end

#normalize_path(path) ⇒ Object



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

def normalize_path(path)
  if Pathname.new(path).absolute?
    RealFile.expand_path(path)
  else
    parts = dir_levels + [path]
    RealFile.expand_path(parts.reduce do |base, part|
                           Pathname(base) + part
                         end.to_s)
  end
end

#path_parts(path) ⇒ Object



94
95
96
# File 'lib/fakefs/file_system.rb', line 94

def path_parts(path)
  drop_root(path.split(File::SEPARATOR)).reject(&:empty?)
end