Class: Crspec::StatusPersistence

Inherits:
Object
  • Object
show all
Defined in:
lib/crspec/status_persistence.rb

Overview

Persists per-example status and timing between runs. Backs slowest-first scheduling and --only-failures. Keyed by the example's stable identity (group description chain + example description).

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ StatusPersistence

Returns a new instance of StatusPersistence.



11
12
13
# File 'lib/crspec/status_persistence.rb', line 11

def initialize(path)
  @path = path
end

Instance Method Details

#loadObject



15
16
17
18
19
20
21
# File 'lib/crspec/status_persistence.rb', line 15

def load
  return {} unless @path && File.exist?(@path)

  JSON.parse(File.read(@path))
rescue JSON::ParserError, Errno::ENOENT
  {}
end

#save(examples) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/crspec/status_persistence.rb', line 23

def save(examples)
  return unless @path

  previous = load
  examples.each do |example|
    next if example.status == :pending

    previous[example.persistence_key] = {
      "status" => example.status.to_s,
      "run_time" => example.execution_time.round(6)
    }
  end

  dir = File.dirname(@path)
  FileUtils.mkdir_p(dir) unless Dir.exist?(dir)
  tmp_path = File.join(dir, ".crspec-status-#{Process.pid}-#{rand(1_000_000)}")
  File.write(tmp_path, JSON.pretty_generate(previous))
  File.rename(tmp_path, @path)
rescue SystemCallError
  File.delete(tmp_path) if tmp_path && File.exist?(tmp_path)
  nil
end