Class: Karst::Spec::Reporter

Inherits:
Object
  • Object
show all
Defined in:
lib/karst/spec/reporter.rb

Overview

Collects ExampleObservation instances as the suite runs and serializes them to one deterministic JSON artifact on disk. Never touches the host application's database: this is the only persistence Reporter performs.

Instance Method Summary collapse

Constructor Details

#initializeReporter

Returns a new instance of Reporter.



12
13
14
15
# File 'lib/karst/spec/reporter.rb', line 12

def initialize
  @examples = []
  @mutex = Mutex.new
end

Instance Method Details

#record(example_observation) ⇒ Object



17
18
19
20
# File 'lib/karst/spec/reporter.rb', line 17

def record(example_observation)
  @mutex.synchronize { @examples << example_observation }
  self
end

#to_aObject



22
23
24
# File 'lib/karst/spec/reporter.rb', line 22

def to_a
  @mutex.synchronize { @examples.dup }
end

#write(path) ⇒ Object

Ordered by file path then line number, independent of RSpec run order (--order random reshuffles examples but must not reshuffle the artifact), so two runs of an unchanged suite produce identical JSON.



29
30
31
32
33
34
35
36
# File 'lib/karst/spec/reporter.rb', line 29

def write(path)
  browser_facing = @mutex.synchronize { @examples.select(&:browser_facing?) }
  ordered = browser_facing.sort_by { |example| [example.file_path.to_s, example.line_number.to_i] }

  FileUtils.mkdir_p(File.dirname(path))
  File.write(path, "#{JSON.pretty_generate(ordered.map { |example| serialize(example) })}\n")
  path
end