2017-07-19 17:51:59 +00:00
|
|
|
require 'json'
|
|
|
|
|
2018-05-31 18:43:47 +00:00
|
|
|
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-04-10 11:22:19 +00:00
|
|
|
|
2017-07-19 17:51:59 +00:00
|
|
|
module RspecFlaky
|
|
|
|
class Listener
|
2017-10-04 11:01:48 +00:00
|
|
|
# - 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-04-10 11:22:19 +00:00
|
|
|
@flaky_examples = RspecFlaky::FlakyExamplesCollection.new
|
2017-10-04 11:01:48 +00:00
|
|
|
@suite_flaky_examples = init_suite_flaky_examples(suite_flaky_examples_json)
|
2017-07-19 17:51:59 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def example_passed(notification)
|
|
|
|
current_example = RspecFlaky::Example.new(notification.example)
|
|
|
|
|
|
|
|
return unless current_example.attempts > 1
|
|
|
|
|
2018-04-10 11:22:19 +00:00
|
|
|
flaky_example = suite_flaky_examples.fetch(current_example.uid) { RspecFlaky::FlakyExample.new(current_example) }
|
2017-10-04 11:01:48 +00:00
|
|
|
flaky_example.update_flakiness!(last_attempts_count: current_example.attempts)
|
|
|
|
|
|
|
|
flaky_examples[current_example.uid] = flaky_example
|
2017-07-19 17:51:59 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def dump_summary(_)
|
2018-04-10 11:22:19 +00:00
|
|
|
RspecFlaky::Report.new(flaky_examples).write(RspecFlaky::Config.flaky_examples_report_path)
|
|
|
|
# write_report_file(flaky_examples, RspecFlaky::Config.flaky_examples_report_path)
|
2017-07-19 17:51:59 +00:00
|
|
|
|
2017-10-05 13:59:58 +00:00
|
|
|
new_flaky_examples = flaky_examples - suite_flaky_examples
|
2017-07-19 17:51:59 +00:00
|
|
|
if new_flaky_examples.any?
|
|
|
|
Rails.logger.warn "\nNew flaky examples detected:\n"
|
2018-04-10 11:22:19 +00:00
|
|
|
Rails.logger.warn JSON.pretty_generate(new_flaky_examples.to_h)
|
2017-07-19 17:51:59 +00:00
|
|
|
|
2018-04-10 11:22:19 +00:00
|
|
|
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)
|
2017-07-19 17:51:59 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2017-10-04 11:01:48 +00:00
|
|
|
def init_suite_flaky_examples(suite_flaky_examples_json = nil)
|
2018-04-10 11:22:19 +00:00
|
|
|
if suite_flaky_examples_json
|
|
|
|
RspecFlaky::Report.load_json(suite_flaky_examples_json).flaky_examples
|
|
|
|
else
|
2017-10-05 13:59:58 +00:00
|
|
|
return {} unless File.exist?(RspecFlaky::Config.suite_flaky_examples_report_path)
|
2017-10-04 11:01:48 +00:00
|
|
|
|
2018-04-10 11:22:19 +00:00
|
|
|
RspecFlaky::Report.load(RspecFlaky::Config.suite_flaky_examples_report_path).flaky_examples
|
2017-10-04 11:01:48 +00:00
|
|
|
end
|
2017-07-19 17:51:59 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|