Skip to content

Commit

Permalink
feat: split pending and failed rerun commands into separate sections
Browse files Browse the repository at this point in the history
  • Loading branch information
bethesque committed Sep 16, 2020
1 parent 15ec231 commit f839391
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 18 deletions.
63 changes: 49 additions & 14 deletions lib/pact/provider/rspec/formatter_rspec_3.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,28 +54,30 @@ 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)
summary.failed_examples.any?{ |e| e.metadata[:pact_ignore_failures] }
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

Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
10 changes: 6 additions & 4 deletions spec/lib/pact/provider/rspec/formatter_rspec_3_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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] }
Expand All @@ -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

Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit f839391

Please sign in to comment.