Class: RSpec::Cheki::SnapFile

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec/cheki/snap_file.rb

Constant Summary collapse

SNAPSHOTS_DIRNAME =

TODO: Should be confugurable

"__snapshots__"
SNAPSHOTS_FILE_EXT =
".yml.snap"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(spec_path) ⇒ SnapFile

Returns a new instance of SnapFile.

Parameters:

  • spec_path (String) —

    The sepc file path



23
24
25
26
# File 'lib/rspec/cheki/snap_file.rb', line 23

def initialize spec_path
  init_paths spec_path
  @snapshots = {}
end

Instance Attribute Details

#dirname ⇒ Object (readonly)

Returns the value of attribute dirname.



20
21
22
# File 'lib/rspec/cheki/snap_file.rb', line 20

def dirname
  @dirname
end

#file_path ⇒ Object (readonly)

Returns the value of attribute file_path.



20
21
22
# File 'lib/rspec/cheki/snap_file.rb', line 20

def file_path
  @file_path
end

#snapshots ⇒ Object (readonly)

Returns the value of attribute snapshots.



20
21
22
# File 'lib/rspec/cheki/snap_file.rb', line 20

def snapshots
  @snapshots
end

#spec_path ⇒ Object (readonly)

Returns the value of attribute spec_path.



20
21
22
# File 'lib/rspec/cheki/snap_file.rb', line 20

def spec_path
  @spec_path
end

Class Method Details

.create(example:) ⇒ RSpec::Cheki::SnapFile

Creata SnapFile instance from example object

Parameters:

  • example (RSpec::Core::Example) —

    The example

Returns:



12
13
14
15
16
17
# 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

Instance Method Details

#create_snapshot(key) ⇒ RSpec::Cheki::Snapshot

Create the snapshot object from a stored snapshots file

Parameters:

  • key (String) —

    The snapshot key

Returns:



45
46
47
48
49
50
51
# 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 ⇒ Object

Create the snapshots directory and the file



59
60
61
62
63
64
# 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? ⇒ boolean

Returns true if the snapshots file has not been existed.

Returns:

  • (boolean) —

    true if the snapshots file has not been existed



54
55
56
# File 'lib/rspec/cheki/snap_file.rb', line 54

def exists?
  File.exist? file_path
end

#load ⇒ Object

Load snapshots file



29
30
31
# File 'lib/rspec/cheki/snap_file.rb', line 29

def load
  @yaml = YAML.load(File.read(file_path)) || {}
end

#save(update: false) ⇒ Object

Save snapshots to file

Parameters:

  • update (boolean) (defaults to: false) —

    Save updated snapshots if true



35
36
37
38
39
40
# 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