Class: RspecPrettyHtmlReporter

Inherits:
RSpec::Core::Formatters::BaseFormatter
  • Object
show all
Includes:
Helpers
Defined in:
lib/rspec_pretty_html_reporter.rb

Constant Summary collapse

DEFAULT_REPORT_PATH =
File.join(Bundler.root, 'reports', Time.now.strftime('%Y%m%d-%H%M%S'))
REPORT_PATH =
ENV['REPORT_PATH'] || DEFAULT_REPORT_PATH
SCREENRECORD_DIR =
File.join(REPORT_PATH, 'screenrecords')
SCREENSHOT_DIR =
File.join(REPORT_PATH, 'screenshots')
RESOURCE_DIR =
File.join(REPORT_PATH, 'resources')

Instance Method Summary collapse

Methods included from Helpers

rename_duplicate_description, rename_duplicate_filename, theme

Constructor Details

#initialize(_io_standard_out) ⇒ RspecPrettyHtmlReporter

Returns a new instance of RspecPrettyHtmlReporter.



25
26
27
28
29
30
31
32
33
# File 'lib/rspec_pretty_html_reporter.rb', line 25

def initialize(_io_standard_out)
  create_reports_dir
  create_screenshots_dir
  create_screenrecords_dir
  copy_resources
  @all_groups = {}
  @group_level = 0
  @theme = Helpers.theme(ENV['THEME'])
end

Instance Method Details

#close(notification) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/rspec_pretty_html_reporter.rb', line 121

def close(notification)
  File.open("#{REPORT_PATH}/overview.html", 'w') do |f|
    @overview = @all_groups

    @passed = @overview.values.map { |g| g[:passed].size }.inject(0) { |sum, i| sum + i }
    @failed = @overview.values.map { |g| g[:failed].size }.inject(0) { |sum, i| sum + i }
    @pending = @overview.values.map { |g| g[:pending].size }.inject(0) { |sum, i| sum + i }

    duration_values = @overview.values.map { |e| e[:duration] }
    @total_duration = duration_values.map { |d| d.to_f.round(5) }
    duration_keys = duration_values.size.times.to_a
    if duration_values.size < 2
      duration_values.unshift(duration_values.first)
      duration_keys = duration_keys << 1
    end

    @durations = duration_keys.zip(duration_values.map { |d| d.to_f.round(5) })
    @summary_duration = duration_values.map do |d|
      d.to_f.round(5)
    end.inject(0) { |sum, i| sum + i }.to_fs(:rounded, precision: 5)
    @total_examples = @passed + @failed + @pending
    template_file = File.read("#{File.dirname(__FILE__)}/../templates/overview.erb")
    f.puts ERB.new(template_file).result(binding)
  end
end

#example_failed(notification) ⇒ Object



57
58
59
60
# File 'lib/rspec_pretty_html_reporter.rb', line 57

def example_failed(notification)
  @group_example_failure_count += 1
  @examples << Example.new(notification.example)
end

#example_group_finished(notification) ⇒ Object



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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/rspec_pretty_html_reporter.rb', line 67

def example_group_finished(notification)
  @group_level -= 1

  if @group_level.zero?
    @filename = Helpers.rename_duplicate_filename("#{REPORT_PATH}/#{notification.group.description.parameterize}.html")

    File.open(@filename, 'w') do |f|
      @passed = @group_example_success_count
      @failed = @group_example_failure_count
      @pending = @group_example_pending_count

      duration_values = @examples.map(&:run_time)

      duration_keys = duration_values.size.times.to_a
      if (duration_values.size < 2) && duration_values.size.positive?
        duration_values.unshift(duration_values.first)
        duration_keys = duration_keys << 1
      end

      @title = notification.group.description
      @durations = duration_keys.zip(duration_values)

      @summary_duration = duration_values.inject(0) { |sum, i| sum + i }.to_fs(:rounded, precision: 5)
      Example.load_spec_comments!(@examples)

      @total_group_examples = @passed + @failed + @pending

      class_map = { passed: 'success', failed: 'danger', pending: 'warning' }
      statuses = @examples.map(&:status)
      @status = if statuses.include?('failed')
                  'failed'
                else
                  (statuses.include?('passed') ? 'passed' : 'pending')
                end
      @spec_description = Helpers.rename_duplicate_description(@all_groups, notification.group.description.parameterize)
      @all_groups[@spec_description] = {
        group: notification.group.description,
        examples: @examples.size,
        status: @status,
        klass: class_map[@status.to_sym],
        passed: statuses.select { |s| s == 'passed' },
        failed: statuses.select { |s| s == 'failed' },
        pending: statuses.select { |s| s == 'pending' },
        duration: @summary_duration
      }
      @example_status = @all_groups[@spec_description][:klass]

      template_file = File.read("#{File.dirname(__FILE__)}/../templates/report.erb")

      f.puts ERB.new(template_file).result(binding)
    end
  end
end

#example_group_started(_notification) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/rspec_pretty_html_reporter.rb', line 35

def example_group_started(_notification)
  if @group_level.zero?
    @example_group = {}
    @examples = []
    @group_example_count = 0
    @group_example_success_count = 0
    @group_example_failure_count = 0
    @group_example_pending_count = 0
  end

  @group_level += 1
end

#example_passed(notification) ⇒ Object



52
53
54
55
# File 'lib/rspec_pretty_html_reporter.rb', line 52

def example_passed(notification)
  @group_example_success_count += 1
  @examples << Example.new(notification.example)
end

#example_pending(notification) ⇒ Object



62
63
64
65
# File 'lib/rspec_pretty_html_reporter.rb', line 62

def example_pending(notification)
  @group_example_pending_count += 1
  @examples << Example.new(notification.example)
end

#example_started(_notification) ⇒ Object



48
49
50
# File 'lib/rspec_pretty_html_reporter.rb', line 48

def example_started(_notification)
  @group_example_count += 1
end