Class: ParallelTestsReport::JsonFormatter

Inherits:
RSpec::Core::Formatters::BaseFormatter
  • Object
show all
Defined in:
lib/parallel_tests_report/json_formatter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(output) ⇒ JsonFormatter

Returns a new instance of JsonFormatter.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/parallel_tests_report/json_formatter.rb', line 15

def initialize(output)
  super
  @output ||= output
  if String === @output
    #open the file given as argument in --out
    FileUtils.mkdir_p(File.dirname(@output))
    # overwrite previous results
    File.open(@output, 'w'){}
    @output = File.open(@output, 'a')
    # close and restart in append mode
  elsif File === @output
    @output.close
    @output = File.open(@output.path, 'a')
  end
  @output_hash = {}

  if ENV['TEST_ENV_NUMBER'].to_i != 0
    @n = ENV['TEST_ENV_NUMBER'].to_i
  else
    @n = 1
  end
end

Instance Attribute Details

#outputObject (readonly)

Returns the value of attribute output.



14
15
16
# File 'lib/parallel_tests_report/json_formatter.rb', line 14

def output
  @output
end

#output_hashObject (readonly)

Returns the value of attribute output_hash.



14
15
16
# File 'lib/parallel_tests_report/json_formatter.rb', line 14

def output_hash
  @output_hash
end

Instance Method Details

#close(_notification) ⇒ Object



47
48
49
50
# File 'lib/parallel_tests_report/json_formatter.rb', line 47

def close(_notification)
  #close the file after all the processes are finished
  @output.close if (IO === @output) & (@output != $stdout)
end

#dump_profile(profile) ⇒ Object



68
69
70
# File 'lib/parallel_tests_report/json_formatter.rb', line 68

def dump_profile(profile)
  dump_profile_slowest_examples(profile)
end

#dump_profile_slowest_examples(profile) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/parallel_tests_report/json_formatter.rb', line 72

def dump_profile_slowest_examples(profile)
  #adds to @output_hash, an array of 20 slowest examples
  lock_output do
    @output_hash[:profile] = {}
    @output_hash[:profile][:examples] = profile.slowest_examples.map do |example|
      format_example(example)
    end
  end
  #write the @output_hash to the file
  output.puts @output_hash.to_json
  output.flush
end

#message(notification) ⇒ Object



38
39
40
# File 'lib/parallel_tests_report/json_formatter.rb', line 38

def message(notification)
  (@output_hash[:messages] ||= []) << notification.message
end

#seed(notification) ⇒ Object



42
43
44
45
# File 'lib/parallel_tests_report/json_formatter.rb', line 42

def seed(notification)
  return unless notification.seed_used?
  @output_hash[:seed] = notification.seed
end

#stop(notification) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/parallel_tests_report/json_formatter.rb', line 52

def stop(notification)
  #adds to @output_hash, an array of examples which run in a particular processor
  @output_hash[:examples] = notification.examples.map do |example|
    format_example(example).tap do |hash|
      e = example.exception
      if e
        hash[:exception] =  {
          :class => e.class.name,
          :message => e.message,
          :backtrace => e.backtrace,
        }
      end
    end
  end
end