Class: RubyCI::RspecRunFormatter

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

Instance Method Summary collapse

Constructor Details

#initialize(output) ⇒ RspecRunFormatter



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

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 Method Details

#check_heap_live_numObject



65
66
67
# File 'lib/ruby_ci/rspec_run_formatter.rb', line 65

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



90
91
92
93
94
95
96
97
98
99
# File 'lib/ruby_ci/rspec_run_formatter.rb', line 90

def close(null_notification)
  @output.print "Finished rspec run"
  # 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

#example_failed(example_notification) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
# File 'lib/ruby_ci/rspec_run_formatter.rb', line 133

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
  )
  @output.print RSpec::Core::Formatters::ConsoleCodes.wrap('F', :failure)
end

#example_group_finished(group_notification) ⇒ Object



154
155
156
157
158
159
160
161
162
163
# File 'lib/ruby_ci/rspec_run_formatter.rb', line 154

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



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/ruby_ci/rspec_run_formatter.rb', line 101

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



127
128
129
130
131
# File 'lib/ruby_ci/rspec_run_formatter.rb', line 127

def example_passed(example_notification)
   = example_notification.example.
  broadcast_example_finished(serialize_example(, "passed".freeze), example_notification.example)
  @output.print RSpec::Core::Formatters::ConsoleCodes.wrap('.', :success)
end

#example_pending(example_notification) ⇒ Object



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

def example_pending(example_notification)
   = example_notification.example.
  broadcast_example_finished(
    serialize_example(, "pending".freeze),
    example_notification.example
  )
  @output.print RSpec::Core::Formatters::ConsoleCodes.wrap('*', :pending)
end

#example_started(example_notification) ⇒ Object



123
124
125
# File 'lib/ruby_ci/rspec_run_formatter.rb', line 123

def example_started(example_notification)
  @output_before = get_output
end

#passed?Boolean



69
70
71
# File 'lib/ruby_ci/rspec_run_formatter.rb', line 69

def passed?
  !@is_failed
end

#path_with_file(group) ⇒ Object



165
166
167
168
169
# File 'lib/ruby_ci/rspec_run_formatter.rb', line 165

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



46
47
48
# File 'lib/ruby_ci/rspec_run_formatter.rb', line 46

def rspec_runner_index
  ENV["TEST_ENV_NUMBER"]
end

#send_eventsObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/ruby_ci/rspec_run_formatter.rb', line 50

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



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/ruby_ci/rspec_run_formatter.rb', line 73

def start(start_notification)
  @output.print "Starting rspec run"
  # $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



41
42
43
44
# File 'lib/ruby_ci/rspec_run_formatter.rb', line 41

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

#time_nowObject



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

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