Class: Path::Backend::Mock

Inherits:
Object
  • Object
show all
Defined in:
lib/rubypath/backend/mock.rb

Overview

rubocop:disable Metrics/ClassLength

Defined Under Namespace

Classes: Dir, File, Node

Virtual File System Configuration collapse

Backend Operations collapse

Instance Attribute Summary collapse

Virtual File System Configuration collapse

Internal Methods collapse

Backend Operations collapse

Internal Virtual File System collapse

Constructor Details

#initializeMock

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

#homesObject

Returns the value of attribute homes.



7
8
9
# File 'lib/rubypath/backend/mock.rb', line 7

def homes
  @homes
end

#umaskObject

Returns the value of attribute umask.



162
163
164
# File 'lib/rubypath/backend/mock.rb', line 162

def umask
  @umask
end

#userObject (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

Returns:

  • (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

Returns:

  • (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 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

#file?(path) ⇒ Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/rubypath/backend/mock.rb', line 55

def file?(path)
  lookup(path).is_a?(File)
end

#getwdObject

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 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) ⇒ 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 = expand_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

#rootObject

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 = expand 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

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