diff options
author | Rémy Coutable <remy@rymai.me> | 2017-10-05 15:59:58 +0200 |
---|---|---|
committer | Rémy Coutable <remy@rymai.me> | 2017-10-06 12:00:09 +0200 |
commit | da5aa64f06afd687a97cfcb5136c77d668f8990d (patch) | |
tree | 3c113a908ff41f70904cb466f760ed91da886425 /lib/rspec_flaky | |
parent | f286d0978483c912e66257fec299a117b81025c3 (diff) | |
download | gitlab-ce-da5aa64f06afd687a97cfcb5136c77d668f8990d.tar.gz |
Add new RSpecFlaky::FlakyExamplesCollection and RSpecFlaky::Config classes
Signed-off-by: Rémy Coutable <remy@rymai.me>
Diffstat (limited to 'lib/rspec_flaky')
-rw-r--r-- | lib/rspec_flaky/config.rb | 21 | ||||
-rw-r--r-- | lib/rspec_flaky/flaky_examples_collection.rb | 37 | ||||
-rw-r--r-- | lib/rspec_flaky/listener.rb | 46 |
3 files changed, 69 insertions, 35 deletions
diff --git a/lib/rspec_flaky/config.rb b/lib/rspec_flaky/config.rb new file mode 100644 index 00000000000..a17ae55910e --- /dev/null +++ b/lib/rspec_flaky/config.rb @@ -0,0 +1,21 @@ +require 'json' + +module RspecFlaky + class Config + def self.generate_report? + ENV['FLAKY_RSPEC_GENERATE_REPORT'] == 'true' + end + + def self.suite_flaky_examples_report_path + ENV['SUITE_FLAKY_RSPEC_REPORT_PATH'] || Rails.root.join("rspec_flaky/suite-report.json") + end + + def self.flaky_examples_report_path + ENV['FLAKY_RSPEC_REPORT_PATH'] || Rails.root.join("rspec_flaky/report.json") + end + + def self.new_flaky_examples_report_path + ENV['NEW_FLAKY_RSPEC_REPORT_PATH'] || Rails.root.join("rspec_flaky/new-report.json") + end + end +end diff --git a/lib/rspec_flaky/flaky_examples_collection.rb b/lib/rspec_flaky/flaky_examples_collection.rb new file mode 100644 index 00000000000..973c95b0212 --- /dev/null +++ b/lib/rspec_flaky/flaky_examples_collection.rb @@ -0,0 +1,37 @@ +require 'json' + +module RspecFlaky + class FlakyExamplesCollection < SimpleDelegator + def self.from_json(json) + new(JSON.parse(json)) + end + + def initialize(collection = {}) + unless collection.is_a?(Hash) + raise ArgumentError, "`collection` must be a Hash, #{collection.class} given!" + end + + collection_of_flaky_examples = + collection.map do |uid, example| + [ + uid, + example.is_a?(RspecFlaky::FlakyExample) ? example : RspecFlaky::FlakyExample.new(example) + ] + end + + super(Hash[collection_of_flaky_examples]) + end + + def to_report + Hash[map { |uid, example| [uid, example.to_h] }].deep_symbolize_keys + end + + def -(other) + unless other.respond_to?(:key) + raise ArgumentError, "`other` must respond to `#key?`, #{other.class} does not!" + end + + self.class.new(reject { |uid, _| other.key?(uid) }) + end + end +end diff --git a/lib/rspec_flaky/listener.rb b/lib/rspec_flaky/listener.rb index 4752ebe6410..4a5bfec9967 100644 --- a/lib/rspec_flaky/listener.rb +++ b/lib/rspec_flaky/listener.rb @@ -9,7 +9,7 @@ module RspecFlaky attr_reader :suite_flaky_examples, :flaky_examples def initialize(suite_flaky_examples_json = nil) - @flaky_examples = {} + @flaky_examples = FlakyExamplesCollection.new @suite_flaky_examples = init_suite_flaky_examples(suite_flaky_examples_json) end @@ -25,14 +25,14 @@ module RspecFlaky end def dump_summary(_) - write_report_file(flaky_examples, flaky_examples_report_path) + write_report_file(flaky_examples, RspecFlaky::Config.flaky_examples_report_path) - new_flaky_examples = _new_flaky_examples + new_flaky_examples = flaky_examples - suite_flaky_examples if new_flaky_examples.any? Rails.logger.warn "\nNew flaky examples detected:\n" - Rails.logger.warn JSON.pretty_generate(to_report(new_flaky_examples)) + Rails.logger.warn JSON.pretty_generate(new_flaky_examples.to_report) - write_report_file(new_flaky_examples, new_flaky_examples_report_path) + write_report_file(new_flaky_examples, RspecFlaky::Config.new_flaky_examples_report_path) end end @@ -44,45 +44,21 @@ module RspecFlaky def init_suite_flaky_examples(suite_flaky_examples_json = nil) unless suite_flaky_examples_json - return {} unless File.exist?(suite_flaky_examples_report_path) + return {} unless File.exist?(RspecFlaky::Config.suite_flaky_examples_report_path) - suite_flaky_examples_json = File.read(suite_flaky_examples_report_path) + suite_flaky_examples_json = File.read(RspecFlaky::Config.suite_flaky_examples_report_path) end - suite_flaky_examples = JSON.parse(suite_flaky_examples_json) - - Hash[(suite_flaky_examples || {}).map { |k, ex| [k, FlakyExample.new(ex)] }].freeze - end - - def _new_flaky_examples - flaky_examples.reject { |uid, _| already_flaky?(uid) } - end - - def already_flaky?(example_uid) - suite_flaky_examples.key?(example_uid) + FlakyExamplesCollection.from_json(suite_flaky_examples_json) end - def write_report_file(examples, file_path) - return unless ENV['FLAKY_RSPEC_GENERATE_REPORT'] == 'true' + def write_report_file(examples_collection, file_path) + return unless RspecFlaky::Config.generate_report? report_path_dir = File.dirname(file_path) FileUtils.mkdir_p(report_path_dir) unless Dir.exist?(report_path_dir) - File.write(file_path, JSON.pretty_generate(to_report(examples))) - end - - def suite_flaky_examples_report_path - @suite_flaky_examples_report_path ||= ENV['SUITE_FLAKY_RSPEC_REPORT_PATH'] || - Rails.root.join("rspec_flaky/suite-report.json") - end - - def flaky_examples_report_path - @flaky_examples_report_path ||= ENV['FLAKY_RSPEC_REPORT_PATH'] || - Rails.root.join("rspec_flaky/report.json") - end - def new_flaky_examples_report_path - @new_flaky_examples_report_path ||= ENV['NEW_FLAKY_RSPEC_REPORT_PATH'] || - Rails.root.join("rspec_flaky/new-report.json") + File.write(file_path, JSON.pretty_generate(examples_collection.to_report)) end end end |