Class: FakeFS::Dir
Overview
FakeFs Dir class
Defined Under Namespace
Modules: Tmpname
Instance Attribute Summary collapse
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Class Method Summary collapse
- .[](*pattern) ⇒ Object
- ._check_for_valid_file(path) ⇒ Object
- .chdir(dir, &blk) ⇒ Object
- .children(dirname, opts = {}) ⇒ Object
- .chroot(_string) ⇒ Object
- .delete(string) ⇒ Object (also: rmdir, unlink)
- .each_child(dirname, &_block) ⇒ Object
- .empty?(dirname) ⇒ Boolean
- .entries(dirname, _opts = {}) ⇒ Object
- .exists?(path) ⇒ Boolean (also: exist?)
- .foreach(dirname, &_block) ⇒ Object
- .glob(pattern, flags = 0, &block) ⇒ Object
- .home(user = nil) ⇒ Object
- .mkdir(string, _integer = 0) ⇒ Object
-
.mktmpdir(prefix_suffix = nil, tmpdir = nil) ⇒ Object
This code has been borrowed from Rubinius.
- .open(string, &_block) ⇒ Object
- .pwd ⇒ Object (also: getwd)
- .tmpdir ⇒ Object
Instance Method Summary collapse
- #close ⇒ Object
- #each ⇒ Object
-
#initialize(string) ⇒ Dir
constructor
A new instance of Dir.
- #ino ⇒ Object
- #pos ⇒ Object
- #pos=(integer) ⇒ Object
- #read ⇒ Object
- #rewind ⇒ Object
- #seek(integer) ⇒ Object
Constructor Details
#initialize(string) ⇒ Dir
Returns a new instance of Dir.
13 14 15 16 17 18 19 20 21 |
# File 'lib/fakefs/dir.rb', line 13 def initialize(string) self.class._check_for_valid_file(string) @path = FileSystem.normalize_path(string) @open = true @pointer = 0 @contents = ['.', '..'] + FileSystem.find(@path).entries @inode = FakeInode.new(self) end |
Instance Attribute Details
#path ⇒ Object (readonly)
Returns the value of attribute path.
7 8 9 |
# File 'lib/fakefs/dir.rb', line 7 def path @path end |
Class Method Details
.[](*pattern) ⇒ Object
65 66 67 |
# File 'lib/fakefs/dir.rb', line 65 def self.[](*pattern) glob pattern end |
._check_for_valid_file(path) ⇒ Object
9 10 11 |
# File 'lib/fakefs/dir.rb', line 9 def self._check_for_valid_file(path) raise Errno::ENOENT, path.to_s unless FileSystem.find(path) end |
.chdir(dir, &blk) ⇒ Object
73 74 75 |
# File 'lib/fakefs/dir.rb', line 73 def self.chdir(dir, &blk) FileSystem.chdir(dir, &blk) end |
.children(dirname, opts = {}) ⇒ Object
94 95 96 |
# File 'lib/fakefs/dir.rb', line 94 def self.children(dirname, opts = {}) entries(dirname, **opts) - ['.', '..'] end |
.chroot(_string) ⇒ Object
77 78 79 |
# File 'lib/fakefs/dir.rb', line 77 def self.chroot(_string) raise NotImplementedError end |
.delete(string) ⇒ Object Also known as: rmdir, unlink
81 82 83 84 85 86 |
# File 'lib/fakefs/dir.rb', line 81 def self.delete(string) _check_for_valid_file(string) raise Errno::ENOTEMPTY, string.to_s unless FileSystem.find(string).empty? FileSystem.delete(string) end |
.each_child(dirname, &_block) ⇒ Object
98 99 100 101 102 103 |
# File 'lib/fakefs/dir.rb', line 98 def self.each_child(dirname, &_block) Dir.open(dirname) do |file| next if ['.', '..'].include?(file) yield file end end |
.empty?(dirname) ⇒ Boolean
106 107 108 109 110 111 112 113 |
# File 'lib/fakefs/dir.rb', line 106 def self.empty?(dirname) _check_for_valid_file(dirname) if File.directory?(dirname) Dir.new(dirname).count <= 2 else false end end |
.entries(dirname, _opts = {}) ⇒ Object
88 89 90 91 92 |
# File 'lib/fakefs/dir.rb', line 88 def self.entries(dirname, _opts = {}) _check_for_valid_file(dirname) Dir.new(dirname).map { |file| File.basename(file) } end |
.exists?(path) ⇒ Boolean Also known as: exist?
69 70 71 |
# File 'lib/fakefs/dir.rb', line 69 def self.exists?(path) File.exist?(path) && File.directory?(path) end |
.foreach(dirname, &_block) ⇒ Object
116 117 118 |
# File 'lib/fakefs/dir.rb', line 116 def self.foreach(dirname, &_block) Dir.open(dirname) { |file| yield file } end |
.glob(pattern, flags = 0, &block) ⇒ Object
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/fakefs/dir.rb', line 120 def self.glob(pattern, flags = 0, &block) matches_for_pattern = lambda do |matcher| [FileSystem.find(matcher, flags, true) || []].flatten.map do |e| if Dir.pwd.match(%r{\A/?\z}) || !e.to_s.match(%r{\A#{Dir.pwd}/?}) e.to_s else e.to_s.match(%r{\A#{Dir.pwd}/?}).post_match end end.sort end files = if pattern.is_a?(Array) pattern.map do |matcher| matches_for_pattern.call matcher end.flatten else matches_for_pattern.call pattern end block_given? ? files.each { |file| block.call(file) } : files end |
.home(user = nil) ⇒ Object
144 145 146 |
# File 'lib/fakefs/dir.rb', line 144 def self.home(user = nil) RealDir.home(user) end |
.mkdir(string, _integer = 0) ⇒ Object
148 149 150 |
# File 'lib/fakefs/dir.rb', line 148 def self.mkdir(string, _integer = 0) FileUtils.mkdir(string) end |
.mktmpdir(prefix_suffix = nil, tmpdir = nil) ⇒ Object
This code has been borrowed from Rubinius
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 |
# File 'lib/fakefs/dir.rb', line 222 def self.mktmpdir(prefix_suffix = nil, tmpdir = nil) case prefix_suffix when nil prefix = 'd' suffix = '' when String prefix = prefix_suffix suffix = '' when Array prefix = prefix_suffix[0] suffix = prefix_suffix[1] else raise ArgumentError, "unexpected prefix_suffix: #{prefix_suffix.inspect}" end t = Time.now.strftime('%Y%m%d') n = nil begin path = "#{tmpdir}/#{prefix}#{t}-#{$$}-#{rand(0x100000000).to_s(36)}" path << "-#{n}" if n path << suffix mkdir(path, 0o700) rescue Errno::EEXIST n ||= 0 n += 1 retry end if block_given? begin yield path ensure require 'fileutils' # This here was using FileUtils.remove_entry_secure instead of just # .rm_r. However, the security concerns that apply to # .rm_r/.remove_entry_secure shouldn't apply to a test fake # filesystem. :^) FileUtils.rm_r path end else path end end |
.open(string, &_block) ⇒ Object
152 153 154 155 156 157 158 |
# File 'lib/fakefs/dir.rb', line 152 def self.open(string, &_block) if block_given? Dir.new(string).each { |file| yield(file) } else Dir.new(string) end end |
.pwd ⇒ Object Also known as: getwd
164 165 166 |
# File 'lib/fakefs/dir.rb', line 164 def self.pwd FileSystem.current_dir.to_s end |
.tmpdir ⇒ Object
160 161 162 |
# File 'lib/fakefs/dir.rb', line 160 def self.tmpdir '/tmp' end |
Instance Method Details
#close ⇒ Object
23 24 25 26 27 28 |
# File 'lib/fakefs/dir.rb', line 23 def close @open = false @pointer = nil @contents = nil nil end |
#each ⇒ Object
30 31 32 33 34 35 36 37 38 |
# File 'lib/fakefs/dir.rb', line 30 def each if block_given? while (f = read) yield f end else @contents.map { |entry| entry_to_relative_path(entry) }.each end end |
#ino ⇒ Object
217 218 219 |
# File 'lib/fakefs/dir.rb', line 217 def ino @inode.inode_num end |
#pos ⇒ Object
40 41 42 |
# File 'lib/fakefs/dir.rb', line 40 def pos @pointer end |
#pos=(integer) ⇒ Object
44 45 46 |
# File 'lib/fakefs/dir.rb', line 44 def pos=(integer) @pointer = integer end |
#read ⇒ Object
48 49 50 51 52 53 |
# File 'lib/fakefs/dir.rb', line 48 def read raise IOError, 'closed directory' unless @pointer entry = @contents[@pointer] @pointer += 1 entry_to_relative_path(entry) if entry end |
#rewind ⇒ Object
55 56 57 |
# File 'lib/fakefs/dir.rb', line 55 def rewind @pointer = 0 end |
#seek(integer) ⇒ Object
59 60 61 62 63 |
# File 'lib/fakefs/dir.rb', line 59 def seek(integer) raise IOError, 'closed directory' if @pointer.nil? @pointer = integer @contents[integer] end |