Class: Hiiro::Effects::NullFilesystem

Inherits:
Object
  • Object
show all
Defined in:
lib/hiiro/effects.rb

Overview

In-memory filesystem double for tests. Does not touch the real filesystem. Usage:

fs = NullFilesystem.new
manager = Hiiro::TodoManager.new(fs: fs)
manager.save
assert_includes fs.writes, TodoManager::TODO_FILE

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeNullFilesystem

Returns a new instance of NullFilesystem.



104
105
106
107
108
109
# File 'lib/hiiro/effects.rb', line 104

def initialize
  @store   = {}   # path → content
  @writes  = []
  @deletes = []
  @renames = []
end

Instance Attribute Details

#deletesObject (readonly)

Returns the value of attribute deletes.



102
103
104
# File 'lib/hiiro/effects.rb', line 102

def deletes
  @deletes
end

#renamesObject (readonly)

Returns the value of attribute renames.



102
103
104
# File 'lib/hiiro/effects.rb', line 102

def renames
  @renames
end

#writesObject (readonly)

Returns the value of attribute writes.



102
103
104
# File 'lib/hiiro/effects.rb', line 102

def writes
  @writes
end

Instance Method Details

#content_at(path) ⇒ Object

Retrieve what was written to path (for assertions).



139
140
141
# File 'lib/hiiro/effects.rb', line 139

def content_at(path)
  @store[path]
end

#exist?(path) ⇒ Boolean

Returns:

  • (Boolean)


120
# File 'lib/hiiro/effects.rb', line 120

def exist?(path)  = @store.key?(path)

#glob(pattern) ⇒ Object



122
# File 'lib/hiiro/effects.rb', line 122

def glob(pattern) = @store.keys.select { |k| File.fnmatch(pattern, k) }

#mkdir_p(path) ⇒ Object



121
# File 'lib/hiiro/effects.rb', line 121

def mkdir_p(path) = nil

#mv(src, dst) ⇒ Object



129
130
131
132
# File 'lib/hiiro/effects.rb', line 129

def mv(src, dst)
  @store[dst] = @store.delete(src)
  @renames << [src, dst]
end

#read(path) ⇒ Object



116
117
118
# File 'lib/hiiro/effects.rb', line 116

def read(path)
  @store.fetch(path) { raise Errno::ENOENT, path }
end

#rename(src, dst) ⇒ Object



134
135
136
# File 'lib/hiiro/effects.rb', line 134

def rename(src, dst)
  mv(src, dst)
end

#reset!Object

Reset state between tests.



144
145
146
147
148
149
# File 'lib/hiiro/effects.rb', line 144

def reset!
  @store   = {}
  @writes  = []
  @deletes = []
  @renames = []
end

#rm(path) ⇒ Object



124
125
126
127
# File 'lib/hiiro/effects.rb', line 124

def rm(path)
  @deletes << path
  @store.delete(path)
end

#write(path, content) ⇒ Object



111
112
113
114
# File 'lib/hiiro/effects.rb', line 111

def write(path, content)
  @writes << path
  @store[path] = content
end