Skip to content

Commit 533faa1

Browse files
committed
fix: correctly calculate exit code when a mix of pending and non pending pacts are verified
Closes: pact-foundation#223
1 parent aa9e7cb commit 533faa1

File tree

6 files changed

+89
-16
lines changed

6 files changed

+89
-16
lines changed

lib/pact/provider/pact_source.rb

+4
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ def pact_hash
2020
@pact_hash ||= JSON.load(pact_json, nil, { max_nesting: 50 })
2121
end
2222

23+
def pending?
24+
uri.metadata[:pending]
25+
end
26+
2327
def hal_entity
2428
http_client_keys = [:username, :password, :token]
2529
http_client_options = uri.options.reject{ |k, _| !http_client_keys.include?(k) }

lib/pact/provider/pact_spec_runner.rb

+4-11
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
require 'pact/provider/rspec/pact_broker_formatter'
1212
require 'pact/provider/rspec/json_formatter'
1313
require 'pact/provider/rspec'
14+
require 'pact/provider/rspec/calculate_exit_code'
1415

1516
module Pact
1617
module Provider
@@ -79,6 +80,7 @@ def configure_rspec
7980
executing_with_ruby = executing_with_ruby?
8081

8182
config.after(:suite) do | suite |
83+
Pact.provider_world.failed_examples = suite.reporter.failed_examples
8284
Pact::Provider::Help::Write.call(Pact.provider_world.pact_sources) if executing_with_ruby
8385
end
8486
end
@@ -90,7 +92,8 @@ def run_specs
9092
::RSpec::Core::CommandLine.new(NoConfigurationOptions.new)
9193
.run(::RSpec.configuration.output_stream, ::RSpec.configuration.error_stream)
9294
end
93-
pending_mode? ? 0 : exit_code
95+
96+
Pact::Provider::RSpec::CalculateExitCode.call(pact_sources, Pact.provider_world.failed_examples)
9497
end
9598

9699
def rspec_runner_options
@@ -147,8 +150,6 @@ def configure_output
147150
end
148151

149152
::RSpec.configuration.full_backtrace = @options[:full_backtrace]
150-
151-
::RSpec.configuration.failure_color = :yellow if pending_mode?
152153
end
153154

154155
def ordered_pact_json(pact_json)
@@ -159,20 +160,12 @@ def ordered_pact_json(pact_json)
159160
consumer_contract.to_json
160161
end
161162

162-
def all_pacts_pending?
163-
pact_urls.all?{ | pact_url| pact_url.metadata[:pending] }
164-
end
165-
166163
def class_exists? name
167164
Kernel.const_get name
168165
rescue NameError
169166
false
170167
end
171168

172-
def pending_mode?
173-
(all_pacts_pending? || options[:ignore_failures])
174-
end
175-
176169
def executing_with_ruby?
177170
ENV['PACT_EXECUTING_LANGUAGE'] == 'ruby'
178171
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
module Pact
2+
module Provider
3+
module RSpec
4+
module CalculateExitCode
5+
def self.call(pact_sources, failed_examples)
6+
any_non_pending_failures = pact_sources.any? do |pact_source|
7+
if pact_source.pending?
8+
nil
9+
else
10+
failed_examples.select { |e| e.metadata[:pact_source] == pact_source }.any?
11+
end
12+
end
13+
any_non_pending_failures ? 1 : 0
14+
end
15+
end
16+
end
17+
end
18+
end

lib/pact/provider/rspec/formatter_rspec_3.rb

+6-4
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,14 @@ def print_rerun_commands summary
9696
interaction_rerun_commands(pending_interaction_examples(summary)).each do | message |
9797
output.puts(message)
9898
end
99+
set_rspec_failure_color(:red)
99100
end
100101

101-
set_rspec_failure_color(:red)
102-
output.puts("\nFailed interactions:\n\n")
103-
interaction_rerun_commands(failed_interaction_examples(summary)).each do | message |
104-
output.puts(message)
102+
if failed_interactions_count(summary) > 0
103+
output.puts("\nFailed interactions:\n\n")
104+
interaction_rerun_commands(failed_interaction_examples(summary)).each do | message |
105+
output.puts(message)
106+
end
105107
end
106108
end
107109

lib/pact/provider/world.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def self.clear_provider_world
1414
module Provider
1515
class World
1616

17-
attr_accessor :pact_sources
17+
attr_accessor :pact_sources, :failed_examples
1818

1919
def provider_states
2020
@provider_states_proxy ||= Pact::Provider::State::ProviderStateProxy.new
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
require 'pact/provider/rspec/calculate_exit_code'
2+
3+
module Pact
4+
module Provider
5+
module RSpec
6+
module CalculateExitCode
7+
describe ".call" do
8+
let(:pact_source_1) { double('pact_source_1', pending?: pending_1) }
9+
let(:pending_1) { nil }
10+
let(:pact_source_2) { double('pact_source_2', pending?: pending_2) }
11+
let(:pending_2) { nil }
12+
let(:pact_source_3) { double('pact_source_3', pending?: pending_3) }
13+
let(:pending_3) { nil }
14+
let(:pact_sources) { [pact_source_1, pact_source_2, pact_source_3]}
15+
16+
let(:failed_examples) { [ example_1, example_2, example_3 ] }
17+
let(:example_1) { double('example_1', metadata: { pact_source: pact_source_1 }) }
18+
let(:example_2) { double('example_2', metadata: { pact_source: pact_source_1 }) }
19+
let(:example_3) { double('example_3', metadata: { pact_source: pact_source_2 }) }
20+
21+
subject { CalculateExitCode.call(pact_sources, failed_examples ) }
22+
23+
context "when all pacts are pending" do
24+
let(:pending_1) { true }
25+
let(:pending_2) { true }
26+
let(:pending_3) { true }
27+
28+
it "returns 0" do
29+
expect(subject).to eq 0
30+
end
31+
end
32+
33+
context "when a non pending pact has no failures" do
34+
let(:pending_1) { true }
35+
let(:pending_2) { true }
36+
let(:pending_3) { false }
37+
38+
it "returns 0" do
39+
expect(subject).to eq 0
40+
end
41+
end
42+
43+
context "when a non pending pact no failures" do
44+
let(:pending_1) { true }
45+
let(:pending_2) { false }
46+
let(:pending_3) { false }
47+
48+
it "returns 1" do
49+
expect(subject).to eq 1
50+
end
51+
end
52+
end
53+
end
54+
end
55+
end
56+
end

0 commit comments

Comments
 (0)