Module: FakeFS::FileSystem
Overview
FileSystem module
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, dir: nil) ⇒ Object
Finds files/directories using the exact path, without expanding globs.
-
#find_with_glob(path, find_flags = 0, gave_char_class = false, dir: nil) ⇒ Object
Finds files/directories expanding globs.
- #fs ⇒ Object
- #normalize_path(path, dir: nil) ⇒ Object
- #path_parts(path) ⇒ Object
Instance Method Details
#add(path, object = FakeDir.new) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/fakefs/file_system.rb', line 50 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 if object.is_a? FakeDir d[parts.last] ||= object else d[parts.last] = object end end |
#chdir(dir, &blk) ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/fakefs/file_system.rb', line 101 def chdir(dir, &blk) new_dir = find(dir) dir_levels.push dir.to_s if blk raise Errno::ENOENT, dir.to_s unless new_dir raise Errno::ENOTDIR, dir.to_s unless File.directory? new_dir dir_levels.push dir.to_s unless blk yield(dir) if blk ensure dir_levels.pop if blk end |
#clear ⇒ Object
16 17 18 19 |
# File 'lib/fakefs/file_system.rb', line 16 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
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/fakefs/file_system.rb', line 70 def clone(path, target = nil) path = RealFile.(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.read(f) end elsif RealFile.directory?(f) FileUtils.mkdir_p(target_path) end end end |
#current_dir ⇒ Object
131 132 133 |
# File 'lib/fakefs/file_system.rb', line 131 def current_dir find('.') end |
#delete(path) ⇒ Object
95 96 97 98 99 |
# File 'lib/fakefs/file_system.rb', line 95 def delete(path) return unless (node = FileSystem.find(path)) node.delete true end |
#dir_levels ⇒ Object
8 9 10 |
# File 'lib/fakefs/file_system.rb', line 8 def dir_levels @dir_levels ||= ['/'] end |
#files ⇒ Object
21 22 23 |
# File 'lib/fakefs/file_system.rb', line 21 def files fs.entries end |
#find(path, dir: nil) ⇒ Object
Finds files/directories using the exact path, without expanding globs.
26 27 28 29 30 31 |
# File 'lib/fakefs/file_system.rb', line 26 def find(path, dir: nil) parts = path_parts(normalize_path(path, dir: dir)) return fs if parts.empty? # '/' find_recurser(fs, parts) end |
#find_with_glob(path, find_flags = 0, gave_char_class = false, dir: nil) ⇒ Object
Finds files/directories expanding globs.
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/fakefs/file_system.rb', line 34 def find_with_glob(path, find_flags = 0, gave_char_class = false, dir: nil) parts = path_parts(normalize_path(path, dir: dir)) return fs if parts.empty? # '/' entries = Globber.(path).flat_map do |pattern| parts = path_parts(normalize_path(pattern, dir: dir)) find_with_glob_recurser(fs, parts, find_flags, gave_char_class).flatten end case entries.length when 0 then nil when 1 then entries.first else entries end end |
#fs ⇒ Object
12 13 14 |
# File 'lib/fakefs/file_system.rb', line 12 def fs @fs ||= FakeDir.new('/') end |
#normalize_path(path, dir: nil) ⇒ Object
118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/fakefs/file_system.rb', line 118 def normalize_path(path, dir: nil) if Pathname.new(path).absolute? RealFile.(path) else dir ||= dir_levels dir = Array(dir) parts = dir + [path] RealFile.(parts.reduce do |base, part| Pathname(base) + part end.to_s) end end |
#path_parts(path) ⇒ Object
114 115 116 |
# File 'lib/fakefs/file_system.rb', line 114 def path_parts(path) Globber.path_components(path) end |