Class: Path::Backend::Mock
- Inherits:
-
Object
- Object
- Path::Backend::Mock
- Defined in:
- lib/rubypath/backend/mock.rb
Overview
rubocop:disable Metrics/ClassLength
Defined Under Namespace
Virtual File System Configuration collapse
-
#cwd ⇒ Object
writeonly
Set current working directory.
Backend Operations collapse
-
#umask ⇒ Object
Returns the value of attribute umask.
Instance Attribute Summary collapse
-
#homes ⇒ Object
Returns the value of attribute homes.
-
#user ⇒ Object
readonly
Returns the value of attribute user.
Virtual File System Configuration collapse
-
#current_user=(user) ⇒ Object
Set user that owns the current process.
Internal Methods collapse
- #home(user) ⇒ Object
-
#initialize ⇒ Mock
constructor
A new instance of Mock.
Backend Operations collapse
- #atime(path) ⇒ Object
- #atime=(path, time) ⇒ Object
- #directory?(path) ⇒ Boolean
- #entries(path) ⇒ Object
- #exists?(path) ⇒ Boolean
- #expand_path(path, base = getwd) ⇒ Object (also: #expand)
- #file?(path) ⇒ Boolean
-
#getwd ⇒ Object
rubocop:enable all.
- #glob(pattern, flags = 0) ⇒ Object
- #mkdir(path) ⇒ Object
- #mkpath(path) ⇒ Object
- #mode(path) ⇒ Object
- #mtime(path) ⇒ Object
- #mtime=(path, time) ⇒ Object
- #read(path, *args) ⇒ Object
- #rmtree(path) ⇒ Object (also: #safe_rmtree)
- #rmtree!(path) ⇒ Object (also: #safe_rmtree!)
- #touch(path) ⇒ Object
-
#unlink(path) ⇒ Object
rubocop:disable MethodLength.
-
#write(path, content, *args) ⇒ Object
rubocop:disable AbcSize, MethodLength.
Internal Virtual File System collapse
- #lookup(path) ⇒ Object
- #lookup!(path) ⇒ Object
- #lookup_dir!(path) ⇒ Object
- #lookup_file!(path) ⇒ Object
- #lookup_parent!(path) ⇒ Object
-
#root ⇒ Object
Return root node.
- #to_lookup(path) ⇒ Object
Constructor Details
#initialize ⇒ Mock
Returns a new instance of Mock.
25 26 27 28 29 30 |
# File 'lib/rubypath/backend/mock.rb', line 25 def initialize @user = 'root' @homes = {'root' => '/root'} @cwd = '/root' @umask = 0o022 end |
Instance Attribute Details
#cwd=(value) ⇒ Object (writeonly)
Set current working directory.
21 22 23 |
# File 'lib/rubypath/backend/mock.rb', line 21 def cwd=(value) @cwd = value end |
#homes ⇒ Object
Returns the value of attribute homes.
7 8 9 |
# File 'lib/rubypath/backend/mock.rb', line 7 def homes @homes end |
#umask ⇒ Object
Returns the value of attribute umask.
162 163 164 |
# File 'lib/rubypath/backend/mock.rb', line 162 def umask @umask end |
#user ⇒ Object (readonly)
Returns the value of attribute user.
7 8 9 |
# File 'lib/rubypath/backend/mock.rb', line 7 def user @user end |
Instance Method Details
#atime(path) ⇒ Object
131 132 133 |
# File 'lib/rubypath/backend/mock.rb', line 131 def atime(path) lookup!(path).atime end |
#atime=(path, time) ⇒ Object
135 136 137 |
# File 'lib/rubypath/backend/mock.rb', line 135 def atime=(path, time) lookup!(path).atime = time end |
#current_user=(user) ⇒ Object
Set user that owns the current process.
12 13 14 |
# File 'lib/rubypath/backend/mock.rb', line 12 def current_user=(user) @user = user.to_s end |
#directory?(path) ⇒ Boolean
59 60 61 |
# File 'lib/rubypath/backend/mock.rb', line 59 def directory?(path) lookup(path).is_a?(Dir) end |
#entries(path) ⇒ Object
151 152 153 154 |
# File 'lib/rubypath/backend/mock.rb', line 151 def entries(path) node = lookup_dir! path node.children.map(&:name) + %w[. ..] end |
#exists?(path) ⇒ Boolean
63 64 65 |
# File 'lib/rubypath/backend/mock.rb', line 63 def exists?(path) lookup(path) ? true : false end |
#expand_path(path, base = getwd) ⇒ Object Also known as: expand
40 41 42 43 44 45 46 47 |
# File 'lib/rubypath/backend/mock.rb', line 40 def (path, base = getwd) # rubocop:disable RegexpMatch if %r{^~(?<name>[^/]*)(/(?<rest>.*))?$} =~ path ::File. rest.to_s, home(name.empty? ? user : name) else ::File.(path, base) end end |
#file?(path) ⇒ Boolean
55 56 57 |
# File 'lib/rubypath/backend/mock.rb', line 55 def file?(path) lookup(path).is_a?(File) end |
#getwd ⇒ Object
rubocop:enable all
51 52 53 |
# File 'lib/rubypath/backend/mock.rb', line 51 def getwd @cwd ||= '/' end |
#glob(pattern, flags = 0) ⇒ Object
156 157 158 159 160 |
# File 'lib/rubypath/backend/mock.rb', line 156 def glob(pattern, flags = 0) root.all.select do |node| ::File.fnmatch pattern, node.path, (flags | ::File::FNM_PATHNAME) end end |
#home(user) ⇒ Object
32 33 34 35 36 |
# File 'lib/rubypath/backend/mock.rb', line 32 def home(user) homes.fetch(user) do raise ArgumentError.new "user #{user} doesn't exist" end end |
#lookup(path) ⇒ Object
225 226 227 |
# File 'lib/rubypath/backend/mock.rb', line 225 def lookup(path) root.lookup to_lookup path end |
#lookup!(path) ⇒ Object
229 230 231 232 233 234 |
# File 'lib/rubypath/backend/mock.rb', line 229 def lookup!(path) node = lookup path return node if node raise Errno::ENOENT.new path end |
#lookup_dir!(path) ⇒ Object
248 249 250 251 252 253 |
# File 'lib/rubypath/backend/mock.rb', line 248 def lookup_dir!(path) node = lookup! path return node if node.is_a?(Dir) raise Errno::ENOENT.new path end |
#lookup_file!(path) ⇒ Object
236 237 238 239 240 241 242 243 244 245 246 |
# File 'lib/rubypath/backend/mock.rb', line 236 def lookup_file!(path) node = lookup! path case node when File node when Dir raise Errno::EISDIR.new path else raise ArgumentError.new "NOT A FILE: #{path}" end end |
#lookup_parent!(path) ⇒ Object
255 256 257 258 259 260 261 262 263 264 265 |
# File 'lib/rubypath/backend/mock.rb', line 255 def lookup_parent!(path) node = lookup ::File.dirname path if node && node.is_a?(Dir) node elsif node raise Errno::ENOTDIR.new path else raise Errno::ENOENT.new path end end |
#mkdir(path) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/rubypath/backend/mock.rb', line 67 def mkdir(path) return if path.to_s == '/' node = lookup_parent! path dir = node.lookup ::File.basename path return if dir.is_a?(Dir) if dir.nil? node.add Dir.new(self, ::File.basename(path)) else raise ArgumentError.new \ "Node #{dir.path} exists and is no directory." end end |
#mkpath(path) ⇒ Object
82 83 84 85 86 87 |
# File 'lib/rubypath/backend/mock.rb', line 82 def mkpath(path) path = (path) ::Pathname.new(path).descend do |p| mkdir(p.to_s) end end |
#mode(path) ⇒ Object
168 169 170 |
# File 'lib/rubypath/backend/mock.rb', line 168 def mode(path) lookup!(path).mode end |
#mtime(path) ⇒ Object
123 124 125 |
# File 'lib/rubypath/backend/mock.rb', line 123 def mtime(path) lookup!(path).mtime end |
#mtime=(path, time) ⇒ Object
127 128 129 |
# File 'lib/rubypath/backend/mock.rb', line 127 def mtime=(path, time) lookup!(path).mtime = time end |
#read(path, *args) ⇒ Object
139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/rubypath/backend/mock.rb', line 139 def read(path, *args) file = lookup_file!(path) file.atime = Time.now content = file.content if args[0] length = args[0].to_i offset = args[1] ? args[1].to_i : 0 content = content.slice(offset, length) end content end |
#rmtree(path) ⇒ Object Also known as: safe_rmtree
187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/rubypath/backend/mock.rb', line 187 def rmtree(path) node = lookup path case node when Dir, File lookup_parent!(path).children.delete(node) when nil nil else raise ArgumentError.new "Unknown node #{node.inspect} for #rmtree." end end |
#rmtree!(path) ⇒ Object Also known as: safe_rmtree!
200 201 202 203 204 205 206 207 208 209 210 |
# File 'lib/rubypath/backend/mock.rb', line 200 def rmtree!(path) node = lookup path case node when Dir, File lookup_parent!(path).children.delete(node) when nil raise Errno::ENOENT.new path else raise ArgumentError.new "Unknown node #{node.inspect} for #rmtree." end end |
#root ⇒ Object
Return root node.
216 217 218 |
# File 'lib/rubypath/backend/mock.rb', line 216 def root @root ||= Dir.new(self, '') end |
#to_lookup(path) ⇒ Object
220 221 222 223 |
# File 'lib/rubypath/backend/mock.rb', line 220 def to_lookup(path) path = path path.sub(%r{^/+}, '') end |
#touch(path) ⇒ Object
89 90 91 92 93 94 95 96 97 |
# File 'lib/rubypath/backend/mock.rb', line 89 def touch(path) node = lookup_parent! path file = node.lookup ::File.basename path if file file.mtime = Time.now else node.add File.new(self, ::File.basename(path)) end end |
#unlink(path) ⇒ Object
rubocop:disable MethodLength
172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/rubypath/backend/mock.rb', line 172 def unlink(path) # rubocop:disable MethodLength node = lookup_parent!(path) file = node.lookup ::File.basename path case file when Dir raise Errno::EISDIR.new path when File node.children.delete(file) when nil raise Errno::ENOENT.new path else raise ArgumentError.new "Unknown node #{node.inspect} for #unlink." end end |
#write(path, content, *args) ⇒ Object
rubocop:disable AbcSize, MethodLength
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/rubypath/backend/mock.rb', line 99 def write(path, content, *args) # rubocop:disable AbcSize, MethodLength node = lookup_parent! path file = node.lookup ::File.basename(path) unless file file = File.new self, ::File.basename(path) node.add file end case file when File if args.empty? file.content = String.new(content) else offset = args[0].to_i file.content[offset, content.length] = content end file.mtime = Time.now when Dir raise Errno::EISDIR.new path else raise ArgumentError.new end end |