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
- .chroot(_string) ⇒ Object
- .delete(string) ⇒ Object (also: rmdir, unlink)
- .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(&_block) ⇒ Object
-
#initialize(string) ⇒ Dir
constructor
A new instance of Dir.
- #pos ⇒ Object
- #pos=(integer) ⇒ Object
- #read ⇒ Object
- #rewind ⇒ Object
- #seek(integer) ⇒ Object
Constructor Details
#initialize(string) ⇒ Dir
Returns a new instance of Dir.
11 12 13 14 15 16 17 18 |
# File 'lib/fakefs/dir.rb', line 11 def initialize(string) self.class._check_for_valid_file(string) @path = FileSystem.normalize_path(string) @open = true @pointer = 0 @contents = ['.', '..'] + FileSystem.find(@path).entries end |
Instance Attribute Details
#path ⇒ Object (readonly)
Returns the value of attribute path.
5 6 7 |
# File 'lib/fakefs/dir.rb', line 5 def path @path end |
Class Method Details
.[](*pattern) ⇒ Object
64 65 66 |
# File 'lib/fakefs/dir.rb', line 64 def self.[](*pattern) glob pattern end |
._check_for_valid_file(path) ⇒ Object
7 8 9 |
# File 'lib/fakefs/dir.rb', line 7 def self._check_for_valid_file(path) fail Errno::ENOENT, path unless FileSystem.find(path) end |
.chdir(dir, &blk) ⇒ Object
72 73 74 |
# File 'lib/fakefs/dir.rb', line 72 def self.chdir(dir, &blk) FileSystem.chdir(dir, &blk) end |
.chroot(_string) ⇒ Object
76 77 78 |
# File 'lib/fakefs/dir.rb', line 76 def self.chroot(_string) fail NotImplementedError end |
.delete(string) ⇒ Object Also known as: rmdir, unlink
80 81 82 83 84 85 |
# File 'lib/fakefs/dir.rb', line 80 def self.delete(string) _check_for_valid_file(string) fail Errno::ENOTEMPTY, string unless FileSystem.find(string).empty? FileSystem.delete(string) end |
.entries(dirname, _opts = {}) ⇒ Object
87 88 89 90 91 |
# File 'lib/fakefs/dir.rb', line 87 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?
68 69 70 |
# File 'lib/fakefs/dir.rb', line 68 def self.exists?(path) File.exist?(path) && File.directory?(path) end |
.foreach(dirname, &_block) ⇒ Object
93 94 95 |
# File 'lib/fakefs/dir.rb', line 93 def self.foreach(dirname, &_block) Dir.open(dirname) { |file| yield file } end |
.glob(pattern, _flags = 0, &block) ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/fakefs/dir.rb', line 97 def self.glob(pattern, _flags = 0, &block) matches_for_pattern = lambda do |matcher| [FileSystem.find(matcher) || []].flatten.map do |e| if Dir.pwd.match(/\A\/?\z/) || !e.to_s.match(/\A#{Dir.pwd}\/?/) e.to_s else e.to_s.match(/\A#{Dir.pwd}\/?/).post_match end end.sort end if pattern.is_a? Array files = pattern.map do |matcher| matches_for_pattern.call matcher end.flatten else files = matches_for_pattern.call pattern end block_given? ? files.each { |file| block.call(file) } : files end |
.home(user = nil) ⇒ Object
121 122 123 |
# File 'lib/fakefs/dir.rb', line 121 def self.home(user = nil) RealDir.home(user) end |
.mkdir(string, _integer = 0) ⇒ Object
126 127 128 |
# File 'lib/fakefs/dir.rb', line 126 def self.mkdir(string, _integer = 0) FileUtils.mkdir(string) end |
.mktmpdir(prefix_suffix = nil, tmpdir = nil) ⇒ Object
This code has been borrowed from Rubinius
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 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 |
# File 'lib/fakefs/dir.rb', line 204 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 fail ArgumentError, "unexpected prefix_suffix: #{prefix_suffix.inspect}" end t = Time.now.strftime('%Y%m%d') n = nil begin path = "#{tmpdir}/#{prefix}#{t}-#{$PID}-#{rand(0x100000000).to_s(36)}" path << "-#{n}" if n path << suffix mkdir(path, 0700) 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
130 131 132 133 134 135 136 |
# File 'lib/fakefs/dir.rb', line 130 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
142 143 144 |
# File 'lib/fakefs/dir.rb', line 142 def self.pwd FileSystem.current_dir.to_s end |
.tmpdir ⇒ Object
138 139 140 |
# File 'lib/fakefs/dir.rb', line 138 def self.tmpdir '/tmp' end |
Instance Method Details
#close ⇒ Object
20 21 22 23 24 25 |
# File 'lib/fakefs/dir.rb', line 20 def close @open = false @pointer = nil @contents = nil nil end |
#each(&_block) ⇒ Object
27 28 29 30 31 |
# File 'lib/fakefs/dir.rb', line 27 def each(&_block) while (f = read) yield f end end |
#pos ⇒ Object
33 34 35 |
# File 'lib/fakefs/dir.rb', line 33 def pos @pointer end |
#pos=(integer) ⇒ Object
37 38 39 |
# File 'lib/fakefs/dir.rb', line 37 def pos=(integer) @pointer = integer end |
#read ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/fakefs/dir.rb', line 41 def read fail IOError, 'closed directory' if @pointer.nil? n = @contents[@pointer] @pointer += 1 return unless n if n.to_s[0, path.size + 1] == path + '/' n.to_s[path.size + 1..-1] else n.to_s end end |
#rewind ⇒ Object
54 55 56 |
# File 'lib/fakefs/dir.rb', line 54 def rewind @pointer = 0 end |
#seek(integer) ⇒ Object
58 59 60 61 62 |
# File 'lib/fakefs/dir.rb', line 58 def seek(integer) fail IOError, 'closed directory' if @pointer.nil? @pointer = integer @contents[integer] end |