From f839391c0348b02fb569da25961d653eee25b890 Mon Sep 17 00:00:00 2001 From: Beth Skurrie Date: Wed, 16 Sep 2020 10:18:07 +1000 Subject: [PATCH] feat: split pending and failed rerun commands into separate sections --- lib/pact/provider/rspec/formatter_rspec_3.rb | 63 ++++++++++++++----- .../provider/rspec/formatter_rspec_3_spec.rb | 10 +-- 2 files changed, 55 insertions(+), 18 deletions(-) diff --git a/lib/pact/provider/rspec/formatter_rspec_3.rb b/lib/pact/provider/rspec/formatter_rspec_3.rb index b0b36fc1..68c46984 100644 --- a/lib/pact/provider/rspec/formatter_rspec_3.rb +++ b/lib/pact/provider/rspec/formatter_rspec_3.rb @@ -54,11 +54,15 @@ def dump_summary(summary) private def interactions_count(summary) - summary.examples.collect{ |e| e.metadata[:pact_interaction_example_description] }.uniq.size + summary.examples.collect{ |e| interaction_unique_key(e) }.uniq.size end def failed_interactions_count(summary) - summary.failed_examples.collect{ |e| e.metadata[:pact_interaction_example_description] }.uniq.size + failed_interaction_examples(summary).size + end + + def pending_interactions_count(summary) + pending_interaction_examples(summary).size end def ignore_failures?(summary) @@ -66,16 +70,14 @@ def ignore_failures?(summary) end def failure_title summary - if ignore_failures?(summary) - "#{failed_interactions_count(summary)} pending" - else - ::RSpec::Core::Formatters::Helpers.pluralize(failed_interactions_count(summary), "failure") - end + ::RSpec::Core::Formatters::Helpers.pluralize(failed_interactions_count(summary), "failure") end def totals_line summary line = ::RSpec::Core::Formatters::Helpers.pluralize(interactions_count(summary), "interaction") line << ", " << failure_title(summary) + pending_count = pending_interactions_count(summary) + line << ", " << "#{pending_count} pending" if pending_count > 0 line end @@ -88,12 +90,17 @@ def color_for_summary summary end def print_rerun_commands summary - if ignore_failures?(summary) + if pending_interactions_count(summary) > 0 + set_rspec_failure_color(:yellow) output.puts("\nPending interactions: (Failures listed here are expected and do not affect your suite's status)\n\n") - else - output.puts("\nFailed interactions:\n\n") + interaction_rerun_commands(pending_interaction_examples(summary)).each do | message | + output.puts(message) + end end - interaction_rerun_commands(summary).each do | message | + + set_rspec_failure_color(:red) + output.puts("\nFailed interactions:\n\n") + interaction_rerun_commands(failed_interaction_examples(summary)).each do | message | output.puts(message) end end @@ -104,10 +111,34 @@ def print_missing_provider_states end end - def interaction_rerun_commands summary - summary.failed_examples.collect do |example| + def pending_interaction_examples(summary) + one_failed_example_per_interaction(summary).select do | example | + example.metadata[:pactfile_uri].metadata[:pending] + end + end + + def failed_interaction_examples(summary) + one_failed_example_per_interaction(summary).select do | example | + !example.metadata[:pactfile_uri].metadata[:pending] + end + end + + def one_failed_example_per_interaction(summary) + summary.failed_examples.group_by{| e| interaction_unique_key(e)}.values.collect(&:first) + end + + def interaction_rerun_commands examples + examples.collect do |example| interaction_rerun_command_for example - end.uniq.compact + end.compact + end + + def interaction_unique_key(example) + # pending is just to make the counting easier, it isn't required for the unique key + { + pactfile_uri: example.metadata[:pactfile_uri], + index: example.metadata[:pact_interaction].index, + } end def interaction_rerun_command_for example @@ -156,6 +187,10 @@ def colorizer def executing_with_ruby? ENV['PACT_EXECUTING_LANGUAGE'] == 'ruby' end + + def set_rspec_failure_color color + ::RSpec.configuration.failure_color = color + end end end end diff --git a/spec/lib/pact/provider/rspec/formatter_rspec_3_spec.rb b/spec/lib/pact/provider/rspec/formatter_rspec_3_spec.rb index 3194335d..791bfbf7 100644 --- a/spec/lib/pact/provider/rspec/formatter_rspec_3_spec.rb +++ b/spec/lib/pact/provider/rspec/formatter_rspec_3_spec.rb @@ -11,8 +11,9 @@ module RSpec describe Formatter do let(:interaction) { InteractionFactory.create 'provider_state' => 'a state', 'description' => 'a description', '_id' => id, 'index' => 2 } + let(:interaction_2) { InteractionFactory.create 'provider_state' => 'a state', 'description' => 'a description 2', '_id' => "#{id}2", 'index' => 3 } let(:id) { nil } - let(:pactfile_uri) { 'pact_file_uri' } + let(:pactfile_uri) { Pact::Provider::PactURI.new('pact_file_uri') } let(:description) { 'an interaction' } let(:pact_json) { {some: 'pact json'}.to_json } let(:metadata) do @@ -21,10 +22,10 @@ module RSpec pactfile_uri: pactfile_uri, pact_interaction_example_description: description, pact_json: pact_json, - pact_ignore_failures: ignore_failures + pact_ignore_failures: ignore_failures, } end - let(:metadata_2) { metadata.merge(pact_interaction_example_description: 'another interaction')} + let(:metadata_2) { metadata.merge(pact_interaction: interaction_2)} let(:example) { double("Example", metadata: metadata) } let(:example_2) { double("Example", metadata: metadata_2) } let(:failed_examples) { [example, example] } @@ -51,6 +52,7 @@ module RSpec allow(Pact::Provider::Help::PromptText).to receive(:call).and_return("some help") allow(subject).to receive(:failed_examples).and_return(failed_examples) allow(Pact.provider_world.provider_states).to receive(:missing_provider_states).and_return(missing_provider_states) + allow(subject).to receive(:set_rspec_failure_color) subject.dump_summary summary end @@ -141,7 +143,7 @@ module RSpec end context "when ignore_failures is true" do - let(:ignore_failures) { true } + let(:pactfile_uri) { Pact::Provider::PactURI.new('pact_file_uri', {}, { pending: true}) } it "reports failures as pending" do expect(output_result).to include("1 pending")