Class: RubyCI::RspecFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_ci/rspec_formatter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(output) ⇒ RspecFormatter

Returns a new instance of RspecFormatter.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/ruby_ci/rspec_formatter.rb', line 7

def initialize(output)
  @output = output
  @event_output = {}
  @is_failed = false
  @current_path = []
  @current_path_started_at = []
  @max_heap_live_num = 0
  @dup_stdout = STDOUT.clone
  @events = []

  $stdout = StringIO.new()

  @log_thread = Thread.new do
    loop do
      sleep 10
      check_heap_live_num
      @should_send_events = true
    end
  end
end

Instance Attribute Details

#current_test_keyObject

Returns the value of attribute current_test_key.



5
6
7
# File 'lib/ruby_ci/rspec_formatter.rb', line 5

def current_test_key
  @current_test_key
end

Instance Method Details

#check_heap_live_numObject



56
57
58
# File 'lib/ruby_ci/rspec_formatter.rb', line 56

def check_heap_live_num
  @max_heap_live_num = [@max_heap_live_num, GC.stat[:heap_live_slots] || GC.stat[:heap_live_num]].max
end

#close(null_notification) ⇒ Object



84
85
86
87
88
89
90
91
92
# File 'lib/ruby_ci/rspec_formatter.rb', line 84

def close(null_notification)
  # check_heap_live_num
  msg(:gc_stat, GC.stat.merge(max_heap_live_num: @max_heap_live_num))
  unless running_only_failed? || ENV["EXTRA_SLOWER_RUN"] || running_gem_or_engine?
    msg(:close, {final_output: get_output})
  end
  send_events
  $stdout = @dup_stdout
end

#dump_and_resetObject



186
187
188
189
190
# File 'lib/ruby_ci/rspec_formatter.rb', line 186

def dump_and_reset
  event_output = @event_output
  @event_output = {}
  event_output
end

#example_failed(example_notification) ⇒ Object



125
126
127
128
129
130
131
132
133
134
# File 'lib/ruby_ci/rspec_formatter.rb', line 125

def example_failed(example_notification)
  @example_failed_index += 1
   = example_notification.example.
  fully_formatted = example_notification.fully_formatted(@example_failed_index, ::RSpec::Core::Formatters::ConsoleCodes)
  
  broadcast_example_finished(
    serialize_example(, "failed".freeze, fully_formatted),
    example_notification.example
  )
end

#example_finished(notification) ⇒ Object



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/ruby_ci/rspec_formatter.rb', line 155

def example_finished(notification)
  example =  notification.example
   = example.

  *example_group_ids, example_id = [:scoped_id].split(":")

  file_output = @event_output[current_test_key] ||= {}

  example_group = example_group_ids.reduce(file_output) do |output, scope_id|
    output[scope_id] ||= {}
    output[scope_id]
  end

  example_group[example_id] = {
    run_time: example.execution_result.run_time,
    status: example.execution_result.status
  }

  if example.execution_result.status == :failed
    @is_failed = true
    example_group[example_id][:fully_formatted] =
      notification.fully_formatted(0, ::RSpec::Core::Formatters::ConsoleCodes)
  elsif [:retry_attempts] && [:retry_attempts] > 0
    example_group[example_id][:retry_attempts] = [:retry_attempts]
    example_group[example_id][:fully_formatted] =
      example.set_exception [:retry_exceptions].first.to_s
  end

  example_group[example_id]
end

#example_group_finished(group_notification) ⇒ Object



144
145
146
147
148
149
150
151
152
153
# File 'lib/ruby_ci/rspec_formatter.rb', line 144

def example_group_finished(group_notification)
  run_time = time_now - @current_path_started_at.pop
  if (run_time < 0) || (run_time > 2400)
    run_time = 0.525
  end
    msg(:group_finished, [path_with_file(group_notification.group), {run_time: run_time}])
    # msg(:group_finished, [@current_path.map(&:to_s), {run_time: run_time}])
  @current_path.pop
  @current_path.pop if @current_path.size == 1 # Remove the file_path at the beggining
end

#example_group_started(group_notification) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/ruby_ci/rspec_formatter.rb', line 94

def example_group_started(group_notification)
   = group_notification.group.
  @current_path_started_at << time_now
  
  if @current_path.size == 0
    @example_failed_index = 0
    file_path = [:file_path].gsub("./".freeze, "".freeze)
    file_path = [ENV["DIR_PREFIX"], file_path].join("/") if ENV["DIR_PREFIX"]
    @current_path << file_path
  end
  
  @current_path << id()
  
  msg(:group_started, [
    path_with_file(group_notification.group),
    {
      line_number: [:line_number],
      description: [:description],
    }
  ])
end

#example_passed(example_notification) ⇒ Object



120
121
122
123
# File 'lib/ruby_ci/rspec_formatter.rb', line 120

def example_passed(example_notification)
   = example_notification.example.
  broadcast_example_finished(serialize_example(, "passed".freeze), example_notification.example)
end

#example_pending(example_notification) ⇒ Object



136
137
138
139
140
141
142
# File 'lib/ruby_ci/rspec_formatter.rb', line 136

def example_pending(example_notification)
   = example_notification.example.
  broadcast_example_finished(
    serialize_example(, "pending".freeze),
    example_notification.example
  )
end

#example_started(example_notification) ⇒ Object



116
117
118
# File 'lib/ruby_ci/rspec_formatter.rb', line 116

def example_started(example_notification)
  @output_before = get_output
end

#passed?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/ruby_ci/rspec_formatter.rb', line 60

def passed?
  !@is_failed
end

#path_with_file(group) ⇒ Object



192
193
194
195
196
# File 'lib/ruby_ci/rspec_formatter.rb', line 192

def path_with_file(group)
  file_path = group.parent_groups.last.file_path.gsub("./".freeze, "".freeze)
  
  group.[:scoped_id].split(":").unshift(file_path)
end

#rspec_runner_indexObject



37
38
39
# File 'lib/ruby_ci/rspec_formatter.rb', line 37

def rspec_runner_index
  ENV["TEST_ENV_NUMBER"]
end

#send_eventsObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/ruby_ci/rspec_formatter.rb', line 41

def send_events
  @should_send_events = false
  
  if @events.length > 0
    json_events = {
      build_id: RubyCI.configuration.orig_build_id,
      compressed_data: Base64.strict_encode64(Zlib::Deflate.deflate(JSON.fast_generate(@events), 9)),
    }

    RubyCI.send_events(json_events)

    @events = []
  end
end

#start(start_notification) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/ruby_ci/rspec_formatter.rb', line 68

def start(start_notification)
  # $stderr = $stdout
  
  data = {
    load_time: start_notification.load_time,
    example_count: start_notification.count,
    started_at: time_now.to_s
  }
  
  return if running_only_failed? ||
    running_gem_or_engine? ||
    ENV["EXTRA_SLOWER_RUN"]
  
  msg(:start, data)
end

#time_frozen?Boolean

Returns:

  • (Boolean)


32
33
34
35
# File 'lib/ruby_ci/rspec_formatter.rb', line 32

def time_frozen?
  return unless defined?(Timecop)
  Timecop.frozen?
end

#time_nowObject



28
29
30
# File 'lib/ruby_ci/rspec_formatter.rb', line 28

def time_now
  time_frozen? ? Timecop.return { Time.now } : Time.now
end