class RSpec::Cheki::SnapFile

Constants

SNAPSHOTS_DIRNAME

TODO: Should be confugurable

SNAPSHOTS_FILE_EXT

Attributes

dirname[R]
file_path[R]
snapshots[R]
spec_path[R]

Public Class Methods

create(example:) click to toggle source

Creata SnapFile instance from example object @param [RSpec::Core::Example] example The example @return [RSpec::Cheki::SnapFile] The SnapFile object

# File lib/rspec/cheki/snap_file.rb, line 12
def create(example:)
  SnapFile.new(example.file_path).tap do |file|
    file.create_snapshots_file
    file.load
  end
end
new(spec_path) click to toggle source

@param [String] spec_path The sepc file path

# File lib/rspec/cheki/snap_file.rb, line 23
def initialize spec_path
  init_paths spec_path
  @snapshots = {}
end

Public Instance Methods

create_snapshot(key) click to toggle source

Create the snapshot object from a stored snapshots file @param [String] key The snapshot key @return [RSpec::Cheki::Snapshot] snapshot The snapshot

# File lib/rspec/cheki/snap_file.rb, line 45
def create_snapshot key
  fail SnapshotsFileNotLoadedError if @yaml.nil?
  Snapshot.new(key).tap do |s|
    s.expected = @yaml[s.key] if @yaml.key? s.key
    snapshots[s.key] = s
  end
end
create_snapshots_file() click to toggle source

Create the snapshots directory and the file

# File lib/rspec/cheki/snap_file.rb, line 59
def create_snapshots_file
  unless exists?
    FileUtils.mkdir dirname unless File.exist? dirname
    FileUtils.touch file_path
  end
end
exists?() click to toggle source

@return [boolean] true if the snapshots file has not been existed

# File lib/rspec/cheki/snap_file.rb, line 54
def exists?
  File.exist? file_path
end
load() click to toggle source

Load snapshots file

# File lib/rspec/cheki/snap_file.rb, line 29
def load
  @yaml = YAML.load(File.read(file_path)) || {}
end
save(update: false) click to toggle source

Save snapshots to file @param update [boolean] Save updated snapshots if true

# File lib/rspec/cheki/snap_file.rb, line 35
def save(update: false)
  snapshots.each do |key, s|
    @yaml[key] = (s.new? || (update && s.needs_update?)) ? s.actual : s.expected
  end
  File.write(file_path, YAML.dump(@yaml))
end

Private Instance Methods

init_paths(spec_rel_path) click to toggle source
# File lib/rspec/cheki/snap_file.rb, line 68
def init_paths spec_rel_path
  @spec_path = File.expand_path spec_rel_path
  basename = "#{File.basename spec_path}#{SNAPSHOTS_FILE_EXT}"
  @dirname = File.join(File.dirname(spec_path), SNAPSHOTS_DIRNAME)
  @file_path = File.join(dirname, basename)
end