2017-07-19 17:51:59 +00:00
|
|
|
require 'json'
|
|
|
|
|
|
|
|
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)
|
2017-10-05 13:59:58 +00:00
|
|
|
@flaky_examples = 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
|
|
|
|
|
2017-10-04 11:01:48 +00:00
|
|
|
flaky_example = suite_flaky_examples.fetch(current_example.uid) { FlakyExample.new(current_example) }
|
|
|
|
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(_)
|
2017-10-05 13:59:58 +00:00
|
|
|
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"
|
2017-10-05 13:59:58 +00:00
|
|
|
Rails.logger.warn JSON.pretty_generate(new_flaky_examples.to_report)
|
2017-07-19 17:51:59 +00:00
|
|
|
|
2017-10-05 13:59:58 +00:00
|
|
|
write_report_file(new_flaky_examples, RspecFlaky::Config.new_flaky_examples_report_path)
|
2017-07-19 17:51:59 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def to_report(examples)
|
|
|
|
Hash[examples.map { |k, ex| [k, ex.to_h] }]
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2017-10-04 11:01:48 +00:00
|
|
|
def init_suite_flaky_examples(suite_flaky_examples_json = nil)
|
|
|
|
unless suite_flaky_examples_json
|
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
|
|
|
|
2017-10-05 13:59:58 +00:00
|
|
|
suite_flaky_examples_json = File.read(RspecFlaky::Config.suite_flaky_examples_report_path)
|
2017-10-04 11:01:48 +00:00
|
|
|
end
|
|
|
|
|
2017-10-05 13:59:58 +00:00
|
|
|
FlakyExamplesCollection.from_json(suite_flaky_examples_json)
|
2017-07-19 17:51:59 +00:00
|
|
|
end
|
|
|
|
|
2017-10-05 13:59:58 +00:00
|
|
|
def write_report_file(examples_collection, file_path)
|
|
|
|
return unless RspecFlaky::Config.generate_report?
|
2017-07-19 17:51:59 +00:00
|
|
|
|
|
|
|
report_path_dir = File.dirname(file_path)
|
|
|
|
FileUtils.mkdir_p(report_path_dir) unless Dir.exist?(report_path_dir)
|
|
|
|
|
2017-10-05 13:59:58 +00:00
|
|
|
File.write(file_path, JSON.pretty_generate(examples_collection.to_report))
|
2017-07-19 17:51:59 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|