Class: SnapshotTesting::Recorder

Inherits:
Object
  • Object
show all
Defined in:
lib/snapshot_testing/recorder.rb

Instance Method Summary collapse

Constructor Details

#initialize(name:, path:, update: SnapshotTesting.update?) ⇒ Recorder

Returns a new instance of Recorder.



5
6
7
8
9
10
11
12
# File 'lib/snapshot_testing/recorder.rb', line 5

def initialize(name:, path:, update: SnapshotTesting.update?)
  @name   = name
  @path   = path
  @update = update
  @visited = []
  @added = 0
  @updated = 0
end

Instance Method Details

#commitObject



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/snapshot_testing/recorder.rb', line 62

def commit
  removed = snapshots.keys - visited
  removed = removed.grep(/^#{name}\s\d+$/)
  removed.each { |key| snapshots.delete(key) }

  write_snapshots(snapshots) if write?
  log(added, :written) unless added.zero?
  log(updated, :updated) unless updated.zero?
  log(removed.length, :removed) if update? && !removed.empty?
  log(removed.length, :obsolete) if !update? && !removed.empty?
end

#record(actual) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/snapshot_testing/recorder.rb', line 44

def record(actual)
  key = "#{name} #{visited.length + 1}"

  self.visited << key

  unless snapshots.key?(key)
    self.added += 1
    self.snapshots[key] = actual
  end

  if update? && actual != snapshots[key]
    self.updated += 1
    self.snapshots[key] = actual
  end

  snapshots[key]
end

#record_file(name, actual) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/snapshot_testing/recorder.rb', line 26

def record_file(name, actual)
  expected = begin
    read(name)
  rescue Errno::ENOENT
    write(name, actual)
    log(1, :written)
    actual
  end

  if update? && actual != expected
    write(name, actual)
    log(1, :updated)
    actual
  else
    expected
  end
end

#snapshot_pathObject



14
15
16
# File 'lib/snapshot_testing/recorder.rb', line 14

def snapshot_path
  File.join(snapshots_path, "#{File.basename(path)}.snap")
end

#snapshotsObject



18
19
20
21
22
23
24
# File 'lib/snapshot_testing/recorder.rb', line 18

def snapshots
  @snapshots ||= begin
    Snapshot.load_file(snapshot_path)
  rescue Errno::ENOENT
    {}
  end
end