Module: FakeFS::FileSystem
Instance Method Summary collapse
- #add(path, object = FakeDir.new) ⇒ Object
- #chdir(dir, &blk) ⇒ Object
- #clear ⇒ Object
-
#clone(path, target = nil) ⇒ Object
copies directories and files from the real filesystem into our fake one.
- #current_dir ⇒ Object
- #delete(path) ⇒ Object
- #dir_levels ⇒ Object
- #files ⇒ Object
- #find(path) ⇒ Object
- #fs ⇒ Object
- #normalize_path(path) ⇒ Object
- #path_parts(path) ⇒ Object
Instance Method Details
#add(path, object = FakeDir.new) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/fakefs/file_system.rb', line 35 def add(path, object=FakeDir.new) parts = path_parts(normalize_path(path)) d = parts[0...-1].inject(fs) do |dir, part| dir[part] ||= FakeDir.new(part, dir) end object.name = parts.last object.parent = d d[parts.last] ||= object end |
#chdir(dir, &blk) ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/fakefs/file_system.rb', line 76 def chdir(dir, &blk) new_dir = find(dir) dir_levels.push dir if blk raise Errno::ENOENT, dir unless new_dir dir_levels.push dir if !blk blk.call if blk ensure dir_levels.pop if blk end |
#clear ⇒ Object
13 14 15 16 |
# File 'lib/fakefs/file_system.rb', line 13 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
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/fakefs/file_system.rb', line 49 def clone(path, target = nil) path = File.(path) pattern = File.join(path, '**', '*') files = RealFile.file?(path) ? [path] : [path] + RealDir.glob(pattern, RealFile::FNM_DOTMATCH) files.each do |f| target_path = target ? f.gsub(path, target) : f if 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) elsif RealFile.symlink?(f) FileUtils.ln_s() end end end |
#current_dir ⇒ Object
101 102 103 |
# File 'lib/fakefs/file_system.rb', line 101 def current_dir find(normalize_path('.')) end |
#delete(path) ⇒ Object
69 70 71 72 73 74 |
# File 'lib/fakefs/file_system.rb', line 69 def delete(path) if node = FileSystem.find(path) node.delete true end end |
#dir_levels ⇒ Object
5 6 7 |
# File 'lib/fakefs/file_system.rb', line 5 def dir_levels @dir_levels ||= [] end |
#files ⇒ Object
18 19 20 |
# File 'lib/fakefs/file_system.rb', line 18 def files fs.entries end |
#find(path) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/fakefs/file_system.rb', line 22 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 |
#normalize_path(path) ⇒ Object
92 93 94 95 96 97 98 99 |
# File 'lib/fakefs/file_system.rb', line 92 def normalize_path(path) if Pathname.new(path).absolute? File.(path) else parts = dir_levels + [path] File.(File.join(*parts)) end end |
#path_parts(path) ⇒ Object
88 89 90 |
# File 'lib/fakefs/file_system.rb', line 88 def path_parts(path) drop_root(path.split(File::SEPARATOR)).reject { |part| part.empty? } end |