Class: Chartspec::Formatter

Inherits:
RSpec::Core::Formatters::ProgressFormatter
  • Object
show all
Defined in:
lib/chartspec.rb

Instance Method Summary collapse

Constructor Details

#initialize(output) ⇒ Formatter

Returns a new instance of Formatter.



23
24
25
26
27
28
29
# File 'lib/chartspec.rb', line 23

def initialize(output)
  super(output)
  @db = SQLite3::Database.new( ENV["CHARTSPEC_DB"] || "tmp/chartspec.sqlite3" )
  @title = ENV["CHARTSPEC_TITLE"]
  @name = ENV["CHARTSPEC_NAME"]
  @db.execute( "CREATE TABLE IF NOT EXISTS specs(id INTEGER PRIMARY KEY, file TEXT, name TEXT, duration NUMERIC, measured_at DATETIME);" )
end

Instance Method Details

#dump_summary(summary) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/chartspec.rb', line 59

def dump_summary(summary)
  output.puts summary.fully_formatted
  @output_hash[:summary] = {
    :duration => summary.duration,
    :example_count => summary.example_count,
    :failure_count => summary.failure_count,
    :pending_count => summary.pending_count
  }
  @output_hash[:summary_line] = summary.totals_line
  
  specs_history = [].tap do |cd|
    @db.execute( "select file, name, duration, measured_at from specs where measured_at > ?", (Time.now - ((ENV['CHARTSPEC_HISTORY_HOURS'] || 2)*3600)).to_i).each do |row|
      cd << {
        file: row[0],
        name: row[1], 
        duration: row[2], 
        measured_at: row[3]
      }
    end
  end
  
  @chart_data = {}.tap do |spec_summary|
    specs_history.group_by{ |x| x[:name] }.each do |name, specs|
      measures = []
      specs.each do |spec|
        measures << [Time.at(spec[:measured_at]), spec[:duration], spec[:name]]
      end
      spec_summary[name] = measures
    end
  end

  @chartspec_root = File.expand_path("../../", __FILE__)
  template = ERB.new File.new(File.expand_path("../../templates/chartspec.html.erb", __FILE__)).read, nil, "%"
  template.result(binding)

  File.open(ENV["CHARTSPEC_HTML"] || "tmp/chartspec.html", "w") do |file|
    file.puts template.result(binding)
  end
end

#example_failed(failure) ⇒ Object



116
117
118
119
120
121
122
# File 'lib/chartspec.rb', line 116

def example_failed(failure)
  super
  @failed_examples << failure.example
  unless @header_red
    @header_red = true
  end
end

#example_group_started(notification) ⇒ Object



99
100
101
102
103
# File 'lib/chartspec.rb', line 99

def example_group_started(notification)
  super
  @example_group_red = false
  @example_group_number += 1
end

#example_passed(passed) ⇒ Object



112
113
114
# File 'lib/chartspec.rb', line 112

def example_passed(passed)
  super
end

#example_pending(pending) ⇒ Object



124
125
126
# File 'lib/chartspec.rb', line 124

def example_pending(pending)
  super
end

#example_started(_notification) ⇒ Object



108
109
110
# File 'lib/chartspec.rb', line 108

def example_started(_notification)
  @example_number += 1
end

#start(notification) ⇒ Object



31
32
33
34
35
36
37
38
# File 'lib/chartspec.rb', line 31

def start(notification)
  super
  @failed_examples = []
  @example_group_number = 0
  @example_number = 0
  @header_red = false
  @output_hash = {}
end

#start_dump(_notification) ⇒ Object



105
106
# File 'lib/chartspec.rb', line 105

def start_dump(_notification)
end

#stop(notification) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/chartspec.rb', line 40

def stop(notification)
  @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,
        }
      else
        @db.execute("INSERT INTO specs(file, name, duration, measured_at) VALUES (?, ?, ?, ?)", [
          example.[:file_path], example.full_description, example.execution_result.run_time, Time.now.to_i
        ]) if example.[:chart]
      end
    end
  end
end