Skip to content

Commit e99276d

Browse files
committed
feat(consumer): only update pact if test suite passes
1 parent 004fb11 commit e99276d

File tree

13 files changed

+174
-76
lines changed

13 files changed

+174
-76
lines changed

lib/pact/message/consumer/configuration/message_builder.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
require 'pact/message/consumer/consumer_contract_builder'
22
require 'pact/message/consumer/consumer_contract_builders'
3-
# require 'pact/consumer/world'
3+
require 'pact/message/consumer/world'
44

55
module Pact
66
module Message
@@ -58,7 +58,8 @@ def create_consumer_contract_builders_method consumer_contract_builder
5858
Pact::Message::Consumer::ConsumerContractBuilders.send(:define_method, @name.to_sym) do
5959
consumer_contract_builder
6060
end
61-
# Pact.consumer_world.add_consumer_contract_builder consumer_contract_builder
61+
62+
Pact::Message.consumer_world.add_consumer_contract_builder consumer_contract_builder
6263
end
6364
end
6465
end

lib/pact/message/consumer/consumer_contract_builder.rb

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,27 +22,32 @@ def is_expected_to_send(description)
2222
end
2323

2424
def send_message_string
25-
yield contents.reified_contents_string if block_given?
25+
if block_given?
26+
yield contents.reified_contents_string
27+
end
2628
end
2729

2830
def handle_interaction_fully_defined(interaction)
2931
@contents = interaction.contents
3032
@contents_string = interaction.contents.to_s
3133
@interactions << interaction
3234
@interaction_builder = nil
33-
# TODO pull these from pact config
34-
Pact::Message::Consumer::UpdatePact.call(interaction, "./spec/pacts", consumer_name, provider_name, "2.0.0")
35+
3536
end
3637

3738
def verify example_description
3839
#
3940
# TODO check that message was actually yielded
4041
end
4142

43+
def write_pact
44+
Pact::Message::Consumer::UpdatePact.call(interactions, "./spec/pacts", consumer_name, provider_name, "2.0.0")
45+
end
46+
4247
private
4348

4449
attr_writer :interaction_builder
45-
attr_accessor :consumer_name, :provider_name, :consumer_contract_details, :contents
50+
attr_accessor :consumer_name, :provider_name, :consumer_contract_details, :contents, :interactions
4651

4752
def interaction_builder
4853
@interaction_builder ||=

lib/pact/message/consumer/rspec.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,15 @@ module RSpec
2020
RSpec.configure do |config|
2121
config.include Pact::Message::Consumer::RSpec, :pact => :message
2222

23-
config.after :each, :pact => true do | example |
23+
config.before :each, :pact => :message do | example |
24+
hooks.before_each Pact::RSpec.full_description(example)
25+
end
26+
27+
config.after :each, :pact => :message do | example |
2428
hooks.after_each Pact::RSpec.full_description(example)
2529
end
30+
31+
config.after :all do
32+
hooks.after_suite
33+
end
2634
end

lib/pact/message/consumer/spec_hooks.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,24 @@
1+
require 'pact/message/consumer/world'
2+
13
module Pact
24
module Message
35
module Consumer
46
class SpecHooks
7+
def before_each example_description
8+
Pact::Message.consumer_world.register_pact_example_ran
9+
end
10+
511
def after_each example_description
612
Pact.configuration.message_provider_verifications.each do | message_provider_verification |
713
message_provider_verification.call example_description
814
end
915
end
16+
17+
def after_suite
18+
if Pact::Message.consumer_world.any_pact_examples_ran?
19+
Pact::Message.consumer_world.consumer_contract_builders.each(&:write_pact)
20+
end
21+
end
1022
end
1123
end
1224
end

lib/pact/message/consumer/update_pact.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,23 @@ module Message
66
module Consumer
77
class UpdatePact
88

9-
def initialize message, pact_dir, consumer_name, provider_name, pact_specification_version
9+
def initialize messages, pact_dir, consumer_name, provider_name, pact_specification_version
1010
@pact_dir = pact_dir
11-
@message = message
11+
@messages = messages
1212
@consumer_name = consumer_name
1313
@provider_name = provider_name
1414
@pact_specification_version = pact_specification_version
1515
end
1616

17-
def self.call(message, pact_dir, consumer_name, provider_name, pact_specification_version)
18-
new(message, pact_dir, consumer_name, provider_name, pact_specification_version).call
17+
def self.call(messages, pact_dir, consumer_name, provider_name, pact_specification_version)
18+
new(messages, pact_dir, consumer_name, provider_name, pact_specification_version).call
1919
end
2020

2121
def call
2222
details = {
2323
consumer: {name: consumer_name},
2424
provider: {name: provider_name},
25-
interactions: [message],
25+
interactions: [*messages],
2626
pactfile_write_mode: :update,
2727
pact_dir: pact_dir,
2828
pact_specification_version: pact_specification_version,
@@ -36,7 +36,7 @@ def call
3636

3737
private
3838

39-
attr_reader :message, :pact_dir, :consumer_name, :provider_name, :pact_specification_version
39+
attr_reader :messages, :pact_dir, :consumer_name, :provider_name, :pact_specification_version
4040
end
4141
end
4242
end

lib/pact/message/consumer/world.rb

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
module Pact
2+
module Message
3+
def self.consumer_world
4+
@consumer_world ||= Consumer::World.new
5+
end
6+
7+
# internal api, for testing only
8+
def self.clear_consumer_world
9+
@consumer_world = nil
10+
end
11+
12+
module Consumer
13+
class World
14+
def initialize
15+
@any_pact_examples_ran = false
16+
end
17+
18+
def consumer_contract_builders
19+
@consumer_contract_builders ||= []
20+
end
21+
22+
def add_consumer_contract_builder consumer_contract_builder
23+
consumer_contract_builders << consumer_contract_builder
24+
end
25+
26+
def register_pact_example_ran
27+
@any_pact_examples_ran = true
28+
end
29+
30+
def any_pact_examples_ran?
31+
@any_pact_examples_ran
32+
end
33+
end
34+
end
35+
end
36+
end

lib/pact/pact-message.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
require 'pact/message'

spec/features/create_message_pact_spec.rb

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,4 @@ def call(content_string)
6262

6363
expect(message_handler.output_stream.string).to eq ("Hello John")
6464
end
65-
66-
it "merges the message into the pact file" do
67-
pact_hash = JSON.parse(File.read(ZOO_PACT_FILE_PATH), symbolize_names: true)
68-
expect(pact_hash[:consumer][:name]).to eq "Zoo Consumer"
69-
expect(pact_hash[:provider][:name]).to eq "Zoo Provider"
70-
expect(pact_hash[:messages].size).to eq 2
71-
end
7265
end

spec/features/create_message_pact_with_failure_spec.rb renamed to spec/features/create_message_pact_with_failure_test.rb

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,22 +33,17 @@ def call(content_string)
3333

3434
let(:message_handler) { MessageHandler.new }
3535

36-
it "allows a consumer to test that it can handle a message example correctly", pact: :message, pending: true do
36+
it "allows a consumer to test that it can handle a message example correctly", pact: :message do
3737
bar_producer
3838
.given("there is an alligator named Mary")
3939
.is_expected_to_send("an alligator message")
4040
.with_metadata(type: 'animal')
4141
.with_content(name: "Mary")
4242

43-
bar_producer.send_message do | content_string |
43+
bar_producer.send_message_string do | content_string |
4444
message_handler.call(content_string)
4545
end
4646

4747
expect(message_handler.output_stream.string).to eq ("Hello The Wrong Name")
4848
end
49-
50-
51-
it "merges the message into the pact file", pending: true do
52-
expect(File.exist?(FOO_BAR_PACT_FILE_PATH)).to be false
53-
end
5449
end

spec/lib/pact/message/consumer/interaction_decorator_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ module Consumer
1111
provider_states: [double('Pact::ProviderState', as_json: provider_state_json)],
1212
contents: double('Pact::ConsumerContract::Message::Contents', contents: contents_object),
1313
metadata: { content_type: 'foo/bar' }
14-
)
14+
)
1515
end
1616
let(:contents_object) do
1717
{ 'foo' => Pact.like('bar') }

0 commit comments

Comments
 (0)