Class: RSpec::CapturingFormatter::Renderer

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec/capturing_formatter/renderer.rb

Overview

Builds the append-only report and owns captured-line boundaries and destination writes.

Defined Under Namespace

Classes: FailureColorizer

Constant Summary collapse

RESET =
"\e[0m"
COLORS =
{
  green: "\e[32m",
  red: "\e[31m",
  yellow: "\e[33m",
  cyan: "\e[36m",
  gray: "\e[90m",
  pink: "\e[95m",
  bold: "\e[1m"
}.freeze
SOURCE_COLORS =
{
  "stdout" => :gray,
  "stderr" => :yellow,
  "suite stdout" => :gray,
  "suite stderr" => :yellow
}.freeze
BOM_ENCODINGS =
{
  "UTF-16" => "UTF-16BE",
  "UTF-32" => "UTF-32BE"
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(output, configuration, capture_manager: nil, ansi_supported: nil) ⇒ Renderer

Returns a new instance of Renderer.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/rspec/capturing_formatter/renderer.rb', line 32

def initialize(output, configuration, capture_manager: nil, ansi_supported: nil)
  @output = output || StringIO.new
  @configuration = configuration
  @capture_manager = capture_manager
  @ansi_supported = ansi_supported.nil? ? WindowsTerminal.ansi_supported?(@output) : ansi_supported
  @entry_started = false
  @entry_kind = nil
  @entry_path = nil
  @capture_source = nil
  @capture_open = false
  @capture_styled = false
  @sanitizers = {}
  @pending_output = nil
  @bom_output_encoding = nil
  @bom_written = false
end

Instance Attribute Details

#outputObject (readonly)

Returns the value of attribute output.



30
31
32
# File 'lib/rspec/capturing_formatter/renderer.rb', line 30

def output
  @output
end

Instance Method Details

#capture(source, value, encoding = nil, nonblocking: false) ⇒ Object



81
82
83
84
85
86
87
88
89
# File 'lib/rspec/capturing_formatter/renderer.rb', line 81

def capture(source, value, encoding = nil, nonblocking: false)
  if @capture_source != source
    finish_capture
    @capture_source = source
  end

  text = (@sanitizers[source] ||= Sanitizer.new).process(value, encoding)
  emit_captured_text(source, text, nonblocking: nonblocking)
end

#context_started(path, heading: true) ⇒ Object



61
62
63
64
65
66
67
68
69
# File 'lib/rspec/capturing_formatter/renderer.rb', line 61

def context_started(path, heading: true)
  return if !heading && @entry_kind == :context && @entry_path == path

  finish_capture
  begin_entry
  @entry_kind = :context
  @entry_path = path
  line(path, :bold) if heading
end

#example_started(path) ⇒ Object



53
54
55
56
57
58
59
# File 'lib/rspec/capturing_formatter/renderer.rb', line 53

def example_started(path)
  finish_capture
  begin_entry
  @entry_kind = :example
  @entry_path = path
  line(path, :bold)
end

#failure(notification_lines) ⇒ Object



123
124
125
126
127
128
129
# File 'lib/rspec/capturing_formatter/renderer.rb', line 123

def failure(notification_lines)
  finish_capture
  notification_lines.each do |entry|
    value = entry.to_s.chomp
    line(value.empty? ? "" : "  #{value}")
  end
end

#failure_colorizerObject



49
50
51
# File 'lib/rspec/capturing_formatter/renderer.rb', line 49

def failure_colorizer
  FailureColorizer.new(self)
end

#finish_captureObject



201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/rspec/capturing_formatter/renderer.rb', line 201

def finish_capture
  return unless @capture_open || @capture_source || @capture_styled

  sanitizer = @sanitizers[@capture_source]
  trailing = sanitizer&.finish.to_s
  emit_captured_text(@capture_source, trailing) unless trailing.empty?
  if @capture_open
    write_raw(RESET) if color_enabled? || @capture_styled
    write_raw("\n")
  elsif @capture_styled
    # A capture boundary must reset application SGR even when its last line already ended.
    write_raw(RESET)
  end
  @capture_open = false
  @capture_source = nil
  @capture_styled = false
end

#flush_pendingObject



219
220
221
# File 'lib/rspec/capturing_formatter/renderer.rb', line 219

def flush_pending
  flush_pending_output
end

#formatter_color_enabled?Boolean

Returns:

  • (Boolean)


452
453
454
# File 'lib/rspec/capturing_formatter/renderer.rb', line 452

def formatter_color_enabled?
  color_enabled?
end

#message(lines, inside_example: false) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
# File 'lib/rspec/capturing_formatter/renderer.rb', line 91

def message(lines, inside_example: false)
  finish_capture
  unless inside_example && @entry_kind == :example
    begin_entry
    @entry_kind = :rspec
    @entry_path = nil
  end
  lines = lines.to_s.lines
  lines = [""] if lines.empty?
  lines.each { |entry| line("  rspec | #{entry.chomp}") }
end

#pending(reason, location, skipped: false, run_time: nil) ⇒ Object



110
111
112
113
114
115
116
# File 'lib/rspec/capturing_formatter/renderer.rb', line 110

def pending(reason, location, skipped: false, run_time: nil)
  finish_capture
  label, color = status_label(skipped ? :skipped : :pending)
  line("  #{style(label, color)}#{duration_suffix(run_time)}")
  line("  reason | #{reason}") unless reason.to_s.empty?
  line("  rerun  | #{location}") unless location.to_s.empty?
end

#profile(notification) ⇒ Object



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/rspec/capturing_formatter/renderer.rb', line 143

def profile(notification)
  examples = notification.respond_to?(:slowest_examples) ? notification.slowest_examples : []
  examples = Array(examples)

  finish_capture
  begin_entry
  @entry_kind = :profile
  @entry_path = nil
  line("Profile")
  if notification.respond_to?(:slow_duration) && notification.respond_to?(:percentage)
    line("  Slowest examples total #{format_seconds(notification.slow_duration)} (#{notification.percentage}%)")
  end
  examples.each do |example|
    description = example.respond_to?(:full_description) ? example.full_description : example.to_s
    runtime = example.respond_to?(:execution_result) ? example.execution_result.run_time : nil
    location = example.location if example.respond_to?(:location)
    suffix = location.to_s.empty? ? "" : "  #{location}"
    line("  #{format_seconds(runtime)}  #{description}#{suffix}")
  end
  groups = notification.slowest_groups if notification.respond_to?(:slowest_groups)
  unless groups.nil? || groups.empty?
    line("  Slowest example groups")
    groups.each do |location, data|
      total = data[:total_time] if data.respond_to?(:[])
      count = data[:count] if data.respond_to?(:[])
      average = data[:average] if data.respond_to?(:[])
      description = data[:description] if data.respond_to?(:[])
      line("  #{description}") unless description.to_s.empty?
      line(
        "    #{format_seconds(total)} total  #{format_seconds(average)} average  " \
        "#{count} examples  #{location}"
      )
    end
  end
  line("  No examples profiled") if examples.empty? && (groups.nil? || groups.empty?)
end

#rerun_inline(command) ⇒ Object



118
119
120
121
# File 'lib/rspec/capturing_formatter/renderer.rb', line 118

def rerun_inline(command)
  finish_capture
  line("  rerun  | #{command}")
end

#reruns(commands) ⇒ Object



180
181
182
183
184
185
186
187
188
189
# File 'lib/rspec/capturing_formatter/renderer.rb', line 180

def reruns(commands)
  return if commands.empty?

  finish_capture
  begin_entry
  @entry_kind = :reruns
  @entry_path = nil
  line("Failed examples", :red)
  commands.each { |command| line("  #{command}", :red) }
end

#result(status, run_time = nil) ⇒ Object



103
104
105
106
107
108
# File 'lib/rspec/capturing_formatter/renderer.rb', line 103

def result(status, run_time = nil)
  finish_capture
  label, color = status_label(status)
  suffix = duration_suffix(run_time)
  line("  #{style(label, color)}#{suffix}")
end

#seed(seed) ⇒ Object



191
192
193
194
195
196
197
198
199
# File 'lib/rspec/capturing_formatter/renderer.rb', line 191

def seed(seed)
  return if seed.nil?

  finish_capture
  begin_entry
  @entry_kind = :seed
  @entry_path = nil
  line("  Randomized with seed #{seed}")
end

#suite_started(heading: true) ⇒ Object



71
72
73
74
75
76
77
78
79
# File 'lib/rspec/capturing_formatter/renderer.rb', line 71

def suite_started(heading: true)
  return if !heading && @entry_kind == :suite

  finish_capture
  begin_entry
  @entry_kind = :suite
  @entry_path = "RSpec suite"
  line("RSpec suite") if heading
end

#summary(total:, succeeded:, failed:, pending:, duration:, load_time:, errors: 0) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
# File 'lib/rspec/capturing_formatter/renderer.rb', line 131

def summary(total:, succeeded:, failed:, pending:, duration:, load_time:, errors: 0)
  finish_capture
  begin_entry
  @entry_kind = :summary
  @entry_path = nil
  summary_color = failed.to_i.zero? ? :green : :red
  line(style("Summary", :bold))
  line(style("  #{total} total  #{succeeded} succeeded  #{failed} failed  #{pending} pending", summary_color))
  line(style("  Finished in #{format_seconds(duration)}  |  files loaded in #{format_milliseconds(load_time)}", summary_color))
  line(style("  #{errors} errors outside examples", summary_color)) if errors.to_i.positive?
end