debian-mirror-gitlab/lib/rspec_flaky/listener.rb

64 lines
2.3 KiB
Ruby
Raw Normal View History

2018-12-13 13:39:08 +05:30
# frozen_string_literal: true
2018-03-17 18:26:18 +05:30
require 'json'
2018-11-08 19:23:39 +05:30
require_dependency 'rspec_flaky/config'
require_dependency 'rspec_flaky/example'
require_dependency 'rspec_flaky/flaky_example'
require_dependency 'rspec_flaky/flaky_examples_collection'
require_dependency 'rspec_flaky/report'
2018-10-15 14:42:47 +05:30
2018-03-17 18:26:18 +05:30
module RspecFlaky
class Listener
# - suite_flaky_examples: contains all the currently tracked flacky example
# for the whole RSpec suite
# - flaky_examples: contains the examples detected as flaky during the
# current RSpec run
attr_reader :suite_flaky_examples, :flaky_examples
def initialize(suite_flaky_examples_json = nil)
2018-10-15 14:42:47 +05:30
@flaky_examples = RspecFlaky::FlakyExamplesCollection.new
2018-03-17 18:26:18 +05:30
@suite_flaky_examples = init_suite_flaky_examples(suite_flaky_examples_json)
end
def example_passed(notification)
current_example = RspecFlaky::Example.new(notification.example)
return unless current_example.attempts > 1
2018-10-15 14:42:47 +05:30
flaky_example = suite_flaky_examples.fetch(current_example.uid) { RspecFlaky::FlakyExample.new(current_example) }
2018-03-17 18:26:18 +05:30
flaky_example.update_flakiness!(last_attempts_count: current_example.attempts)
flaky_examples[current_example.uid] = flaky_example
end
2019-09-30 21:07:59 +05:30
# rubocop:disable Gitlab/RailsLogger
2018-03-17 18:26:18 +05:30
def dump_summary(_)
2018-10-15 14:42:47 +05:30
RspecFlaky::Report.new(flaky_examples).write(RspecFlaky::Config.flaky_examples_report_path)
# write_report_file(flaky_examples, RspecFlaky::Config.flaky_examples_report_path)
2018-03-17 18:26:18 +05:30
new_flaky_examples = flaky_examples - suite_flaky_examples
if new_flaky_examples.any?
Rails.logger.warn "\nNew flaky examples detected:\n"
2020-05-24 23:13:21 +05:30
Rails.logger.warn Gitlab::Json.pretty_generate(new_flaky_examples.to_h)
2018-03-17 18:26:18 +05:30
2018-10-15 14:42:47 +05:30
RspecFlaky::Report.new(new_flaky_examples).write(RspecFlaky::Config.new_flaky_examples_report_path)
# write_report_file(new_flaky_examples, RspecFlaky::Config.new_flaky_examples_report_path)
2018-03-17 18:26:18 +05:30
end
end
2019-09-30 21:07:59 +05:30
# rubocop:enable Gitlab/RailsLogger
2018-03-17 18:26:18 +05:30
private
def init_suite_flaky_examples(suite_flaky_examples_json = nil)
2018-10-15 14:42:47 +05:30
if suite_flaky_examples_json
RspecFlaky::Report.load_json(suite_flaky_examples_json).flaky_examples
else
2018-03-17 18:26:18 +05:30
return {} unless File.exist?(RspecFlaky::Config.suite_flaky_examples_report_path)
2018-10-15 14:42:47 +05:30
RspecFlaky::Report.load(RspecFlaky::Config.suite_flaky_examples_report_path).flaky_examples
2018-03-17 18:26:18 +05:30
end
end
end
end