class Path::Backend::Mock
rubocop:disable Metrics/ClassLength
Attributes
cwd[W]
Set current working directory.
homes[RW]
umask[R]
user[R]
Public Class Methods
new()
click to toggle source
@!group Internal Methods
# File lib/rubypath/backend/mock.rb, line 25 def initialize @user = 'root' @homes = {'root' => '/root'} @cwd = '/root' @umask = 0o022 end
Public Instance Methods
atime(path)
click to toggle source
# File lib/rubypath/backend/mock.rb, line 131 def atime(path) lookup!(path).atime end
atime=(path, time)
click to toggle source
# File lib/rubypath/backend/mock.rb, line 135 def atime=(path, time) lookup!(path).atime = time end
current_user=(user)
click to toggle source
Set user that owns the current process.
# File lib/rubypath/backend/mock.rb, line 12 def current_user=(user) @user = user.to_s end
directory?(path)
click to toggle source
# File lib/rubypath/backend/mock.rb, line 59 def directory?(path) lookup(path).is_a?(Dir) end
entries(path)
click to toggle source
# File lib/rubypath/backend/mock.rb, line 151 def entries(path) node = lookup_dir! path node.children.map(&:name) + %w[. ..] end
exists?(path)
click to toggle source
# File lib/rubypath/backend/mock.rb, line 63 def exists?(path) lookup(path) ? true : false end
expand_path(path, base = getwd)
click to toggle source
@!group Backend
Operations
# File lib/rubypath/backend/mock.rb, line 40 def expand_path(path, base = getwd) # rubocop:disable RegexpMatch if %r{^~(?<name>[^/]*)(/(?<rest>.*))?$} =~ path ::File.expand_path rest.to_s, home(name.empty? ? user : name) else ::File.expand_path(path, base) end end
Also aliased as: expand
file?(path)
click to toggle source
# File lib/rubypath/backend/mock.rb, line 55 def file?(path) lookup(path).is_a?(File) end
getwd()
click to toggle source
rubocop:enable all
# File lib/rubypath/backend/mock.rb, line 51 def getwd @cwd ||= '/' end
glob(pattern, flags = 0)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# File lib/rubypath/backend/mock.rb, line 225 def lookup(path) root.lookup to_lookup path end
lookup!(path)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# File lib/rubypath/backend/mock.rb, line 255 def lookup_parent!(path) node = lookup ::File.dirname expand 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)
click to toggle source
# 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)
click to toggle source
# File lib/rubypath/backend/mock.rb, line 82 def mkpath(path) path = expand_path(path) ::Pathname.new(path).descend do |p| mkdir(p.to_s) end end
mode(path)
click to toggle source
# File lib/rubypath/backend/mock.rb, line 168 def mode(path) lookup!(path).mode end
mtime(path)
click to toggle source
# File lib/rubypath/backend/mock.rb, line 123 def mtime(path) lookup!(path).mtime end
mtime=(path, time)
click to toggle source
# File lib/rubypath/backend/mock.rb, line 127 def mtime=(path, time) lookup!(path).mtime = time end
read(path, *args)
click to toggle source
# 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)
click to toggle source
# 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
Also aliased as: safe_rmtree
rmtree!(path)
click to toggle source
# 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
Also aliased as: safe_rmtree!
root()
click to toggle source
Return root node.
# File lib/rubypath/backend/mock.rb, line 216 def root @root ||= Dir.new(self, '') end
to_lookup(path)
click to toggle source
# File lib/rubypath/backend/mock.rb, line 220 def to_lookup(path) path = expand path path.sub(%r{^/+}, '') end
touch(path)
click to toggle source
# 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
umask=(mask)
click to toggle source
# File lib/rubypath/backend/mock.rb, line 164 def umask=(mask) @umask = Integer(mask) end
unlink(path)
click to toggle source
# 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)
click to toggle source
# 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