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, _options = nil) ⇒ Object
- .chroot(_string) ⇒ Object
- .delete(string) ⇒ Object (also: rmdir, unlink)
- .each_child(dirname, &_block) ⇒ Object
- .empty?(dirname) ⇒ Boolean
- .entries(dirname, _options = nil) ⇒ Object
- .exist?(path) ⇒ Boolean
- .foreach(dirname, &_block) ⇒ Object
-
.glob(pattern, _flags = 0, flags: _flags, base: nil, sort: true, &block) ⇒ Object
rubocop:disable Lint/UnderscorePrefixedVariableName.
- .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
- #children ⇒ Object
- #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.
15 16 17 18 19 20 21 22 23 |
# File 'lib/fakefs/dir.rb', line 15 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.
9 10 11 |
# File 'lib/fakefs/dir.rb', line 9 def path @path end |
Class Method Details
.[](*pattern) ⇒ Object
71 72 73 |
# File 'lib/fakefs/dir.rb', line 71 def self.[](*pattern) glob pattern end |
._check_for_valid_file(path) ⇒ Object
11 12 13 |
# File 'lib/fakefs/dir.rb', line 11 def self._check_for_valid_file(path) raise Errno::ENOENT, path.to_s unless FileSystem.find(path) end |
.chdir(dir, &blk) ⇒ Object
79 80 81 |
# File 'lib/fakefs/dir.rb', line 79 def self.chdir(dir, &blk) FileSystem.chdir(dir, &blk) end |
.children(dirname, _options = nil) ⇒ Object
100 101 102 |
# File 'lib/fakefs/dir.rb', line 100 def self.children(dirname, = nil) entries(dirname) - ['.', '..'] end |
.chroot(_string) ⇒ Object
83 84 85 |
# File 'lib/fakefs/dir.rb', line 83 def self.chroot(_string) raise NotImplementedError end |
.delete(string) ⇒ Object Also known as: rmdir, unlink
87 88 89 90 91 92 |
# File 'lib/fakefs/dir.rb', line 87 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
104 105 106 107 108 109 110 111 |
# File 'lib/fakefs/dir.rb', line 104 def self.each_child(dirname, &_block) Dir.open(dirname) do |dir| dir.each do |file| next if ['.', '..'].include?(file) yield file end end end |
.empty?(dirname) ⇒ Boolean
113 114 115 116 117 118 119 120 |
# File 'lib/fakefs/dir.rb', line 113 def self.empty?(dirname) _check_for_valid_file(dirname) if File.directory?(dirname) Dir.new(dirname).count <= 2 else false end end |
.entries(dirname, _options = nil) ⇒ Object
94 95 96 97 98 |
# File 'lib/fakefs/dir.rb', line 94 def self.entries(dirname, = nil) _check_for_valid_file(dirname) Dir.new(dirname).map { |file| File.basename(file) } end |
.exist?(path) ⇒ Boolean
75 76 77 |
# File 'lib/fakefs/dir.rb', line 75 def self.exist?(path) File.exist?(path) && File.directory?(path) end |
.foreach(dirname, &_block) ⇒ Object
122 123 124 125 126 127 128 |
# File 'lib/fakefs/dir.rb', line 122 def self.foreach(dirname, &_block) Dir.open(dirname) do |dir| dir.each do |file| yield file end end end |
.glob(pattern, _flags = 0, flags: _flags, base: nil, sort: true, &block) ⇒ Object
rubocop:disable Lint/UnderscorePrefixedVariableName
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/fakefs/dir.rb', line 130 def self.glob(pattern, _flags = 0, flags: _flags, base: nil, sort: true, &block) # rubocop:disable Lint/UnderscorePrefixedVariableName pwd = FileSystem.normalize_path(base || Dir.pwd) matches_for_pattern = lambda do |matcher| matched = [FileSystem.find_with_glob(matcher, flags, true, dir: pwd) || []].flatten.map do |e| pwd_regex = %r{\A#{pwd.gsub('+') { '\+' }}/?} if pwd.match(%r{\A/?\z}) || !e.to_s.match(pwd_regex) e.to_s else e.to_s.match(pwd_regex).post_match end end matched.sort! if sort matched 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
158 159 160 |
# File 'lib/fakefs/dir.rb', line 158 def self.home(user = nil) RealDir.home(user) end |
.mkdir(string, _integer = 0) ⇒ Object
162 163 164 |
# File 'lib/fakefs/dir.rb', line 162 def self.mkdir(string, _integer = 0) FileUtils.mkdir(string) end |
.mktmpdir(prefix_suffix = nil, tmpdir = nil) ⇒ Object
This code has been borrowed from Rubinius
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 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 |
# File 'lib/fakefs/dir.rb', line 241 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
166 167 168 169 170 171 172 173 174 175 |
# File 'lib/fakefs/dir.rb', line 166 def self.open(string, &_block) dir = Dir.new(string) if block_given? result = yield(dir) dir.close result else dir end end |
.pwd ⇒ Object Also known as: getwd
181 182 183 |
# File 'lib/fakefs/dir.rb', line 181 def self.pwd FileSystem.current_dir.to_s end |
.tmpdir ⇒ Object
177 178 179 |
# File 'lib/fakefs/dir.rb', line 177 def self.tmpdir '/tmp' end |
Instance Method Details
#children ⇒ Object
42 43 44 |
# File 'lib/fakefs/dir.rb', line 42 def children each.to_a - ['.', '..'] end |
#close ⇒ Object
25 26 27 28 29 30 |
# File 'lib/fakefs/dir.rb', line 25 def close @open = false @pointer = nil @contents = nil nil end |
#each ⇒ Object
32 33 34 35 36 37 38 39 40 |
# File 'lib/fakefs/dir.rb', line 32 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
236 237 238 |
# File 'lib/fakefs/dir.rb', line 236 def ino @inode.inode_num end |
#pos ⇒ Object
46 47 48 |
# File 'lib/fakefs/dir.rb', line 46 def pos @pointer end |
#pos=(integer) ⇒ Object
50 51 52 |
# File 'lib/fakefs/dir.rb', line 50 def pos=(integer) @pointer = integer end |
#read ⇒ Object
54 55 56 57 58 59 |
# File 'lib/fakefs/dir.rb', line 54 def read raise IOError, 'closed directory' unless @pointer entry = @contents[@pointer] @pointer += 1 entry_to_relative_path(entry) if entry end |
#rewind ⇒ Object
61 62 63 |
# File 'lib/fakefs/dir.rb', line 61 def rewind @pointer = 0 end |
#seek(integer) ⇒ Object
65 66 67 68 69 |
# File 'lib/fakefs/dir.rb', line 65 def seek(integer) raise IOError, 'closed directory' if @pointer.nil? @pointer = integer @contents[integer] end |