Add junit formatter.

This commit is contained in:
Rob Christie 2020-12-12 01:41:14 -07:00 committed by Kyle Rosenbaum
parent 6388ca51ec
commit 4e4fc462db
5 changed files with 109 additions and 0 deletions

View File

@ -11,6 +11,7 @@ end
require_relative "rspecq/formatters/example_count_recorder"
require_relative "rspecq/formatters/failure_recorder"
require_relative "rspecq/formatters/job_timing_recorder"
require_relative "rspecq/formatters/junit_formatter"
require_relative "rspecq/formatters/worker_heartbeat_recorder"
require_relative "rspecq/configuration"

View File

@ -0,0 +1,74 @@
require "rspec_junit_formatter"
require "pry-byebug"
module RSpecQ
module Formatters
# Persists status of examples, so that we can run a single Reporter.
class JUnitFormatter < RSpecJUnitFormatter
# def initialize(queue, job, max_requeues)
def initialize(queue, job, max_requeues, job_index)
@queue = queue
@job = job
@max_requeues = max_requeues
@requeued_examples =[]
path = "test_results/results-#{job_index}.xml"
RSpec::Support::DirectoryMaker.mkdir_p(File.dirname(path))
output_file = File.new(path, "w")
super(output_file)
end
def example_passed(notification)
log_notification("example_passed", notification)
end
def example_failed(notification)
# if it is requeued, store the notification
log_notification("example_failed", notification)
if @queue.requeueable_job?(notification.example.id, @max_requeues)
puts "IGNORE from dump... getting requeued"
@requeued_examples << notification.example
else
puts "FAILED - print it in dump"
end
end
def start(notification)
log_notification("start", notification)
super
end
def stop(notification)
log_notification("stop", notification)
super
end
def dump_summary(notification)
log_notification("dump_summary", notification)
super
end
private
def log_notification(event, notification)
puts "============"
puts event
puts notification.inspect
puts "============"
end
def example_count
@summary_notification.example_count - @requeued_examples.size
end
def failure_count
@summary_notification.failure_count - @requeued_examples.size
end
def examples
@examples_notification.notifications.reject do |example_notification|
@requeued_examples.include?(example_notification.example)
end
end
end
end
end

View File

@ -71,6 +71,19 @@ module RSpecQ
return true
LUA
REQUEUEABLE_JOB = <<~LUA.freeze
local key_requeues = KEYS[1]
local job = ARGV[1]
local max_requeues = ARGV[2]
local requeued_times = redis.call('hget', key_requeues, job)
if requeued_times and requeued_times >= max_requeues then
return nil
end
redis.call('hincrby', key_requeues, job, 1)
return true
LUA
STATUS_INITIALIZING = "initializing".freeze
STATUS_READY = "ready".freeze
@ -167,6 +180,16 @@ module RSpecQ
"--reproduction #{jobs.join(' ')}"
end
def requeueable_job?(job, max_requeues)
return false if max_requeues.zero?
@redis.eval(
REQUEUEABLE_JOB,
keys: [key_requeues_formatter_stats],
argv: [job, max_requeues]
)
end
def record_example_failure(example_id, message)
@redis.hset(key_failures, example_id, message)
end
@ -347,6 +370,10 @@ module RSpecQ
key("requeues")
end
def key_requeues_formatter_stats
key("requeues_formatter_stats")
end
# The total number of examples, those that were requeued.
#
# redis: STRING<integer>

View File

@ -88,6 +88,8 @@ module RSpecQ
RSpec::Core::Formatters.register(Formatters::ExampleCountRecorder, :dump_summary)
RSpec::Core::Formatters.register(Formatters::FailureRecorder, :example_failed, :message)
RSpec::Core::Formatters.register(Formatters::WorkerHeartbeatRecorder, :example_finished)
RSpec::Core::Formatters.register(Formatters::JUnitFormatter, :example_failed, :example_passed, :start, :stop, :dump_summary)
end
def work
@ -126,6 +128,8 @@ module RSpecQ
RSpec.configuration.seed = seed
RSpec.configuration.backtrace_formatter.filter_gem("rspecq")
RSpec.configuration.add_formatter(Formatters::FailureRecorder.new(queue, job, max_requeues, @worker_id))
RSpec.configuration.add_formatter(Formatters::JUnitFormatter.new(queue, job, max_requeues, idx))
RSpec.configuration.add_formatter(Formatters::FailureRecorder.new(queue, job, max_requeues))
RSpec.configuration.add_formatter(Formatters::ExampleCountRecorder.new(queue))
RSpec.configuration.add_formatter(Formatters::WorkerHeartbeatRecorder.new(self))
@ -140,6 +144,8 @@ module RSpecQ
add_suite_index_to_output_filename(opts, idx)
end
puts RSpec::Core::Formatters::Loader.formatters.inspect
_result = RSpec::Core::Runner.new(opts).run($stderr, $stdout)
queue.acknowledge_job(job)

View File

@ -20,6 +20,7 @@ Gem::Specification.new do |s|
s.add_dependency "redis"
s.add_dependency "sentry-raven"
s.add_dependency "rspec_junit_formatter"
s.add_development_dependency "minitest"
s.add_development_dependency "pry-byebug"