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
13
# 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
  @removed = 0
end

Instance Method Details

#commitObject



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

def commit
  prune_snapshots if update?
  write_snapshots if write?

  log(added, :written) unless added.zero?
  log(updated, :updated) unless updated.zero?
  log(removed, :removed) unless removed.zero?
  log(obsolete, :obsolete) unless obsolete.zero?
end

#record(actual) ⇒ Object



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

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



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

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



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

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

#snapshotsObject



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

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