Skip to content

Commit 080e47a

Browse files
authored
Merge pull request #113 from thatguysimon/feat/allow-skip-writing-pact
Support not writing interactions to pact file if marked
2 parents e53f1df + d01e846 commit 080e47a

File tree

6 files changed

+92
-4
lines changed

6 files changed

+92
-4
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ log
2929
reports
3030
Gemfile.lock
3131
build
32+
.byebug_history
3233

3334
vendor/bundle/
3435
spec/examples.txt

lib/pact/consumer_contract/consumer_contract_decorator.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ def to_json(options = {})
3232

3333
def sorted_interactions
3434
# Default order: chronological
35-
return consumer_contract.interactions if Pact.configuration.pactfile_write_order == :chronological
35+
return consumer_contract.writable_interactions if Pact.configuration.pactfile_write_order == :chronological
3636
# We are supporting only chronological or alphabetical order
3737
raise NotImplementedError if Pact.configuration.pactfile_write_order != :alphabetical
3838

39-
consumer_contract.interactions.sort{|a, b| sortable_id(a) <=> sortable_id(b)}
39+
consumer_contract.writable_interactions.sort{|a, b| sortable_id(a) <=> sortable_id(b)}
4040
end
4141

4242
def sortable_id interaction

lib/pact/consumer_contract/interaction_decorator.rb

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ def as_json options = {}
1717
hash[:providerState] = interaction.provider_state if interaction.provider_state
1818
hash[:request] = decorate_request.as_json(options)
1919
hash[:response] = decorate_response.as_json(options)
20+
hash[:metadata] = interaction.metadata
2021
fix_all_the_things hash
2122
end
2223

pact-mock_service.gemspec

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ Gem::Specification.new do |gem|
2727
gem.add_runtime_dependency 'json'
2828
gem.add_runtime_dependency 'webrick', '~> 1.3'
2929
gem.add_runtime_dependency 'term-ansicolor', '~> 1.0'
30-
gem.add_runtime_dependency 'pact-support', '~> 1.2', '>= 1.2.1'
30+
gem.add_runtime_dependency 'pact-support', '~> 1.12', '>= 1.12.0'
3131
gem.add_runtime_dependency 'filelock', '~> 1.1'
3232

3333
gem.add_development_dependency 'rack-test', '~> 0.7'

spec/features/write_pact_file_spec.rb

+41
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,47 @@
8282
end
8383
end
8484

85+
context "when the expected interaction is executed but is marked to not be written to the pact file" do
86+
let(:zebra_interaction) do
87+
{
88+
description: "a request for zebras",
89+
provider_state: "zebras exist",
90+
request: {
91+
method: :get,
92+
path: '/zebras',
93+
headers: {'Accept' => 'application/zebra'}
94+
},
95+
response: {
96+
status: 200
97+
},
98+
metadata: {
99+
write_to_pact: false
100+
}
101+
}.to_json
102+
end
103+
104+
before do
105+
post "/interactions", zebra_interaction, admin_headers
106+
end
107+
108+
it "does not include the interaction in the pact file" do
109+
get "/alligators", nil, {'HTTP_ACCEPT' => 'application/alligator'}
110+
get "/giraffes", nil, {'HTTP_ACCEPT' => 'application/giraffe'}
111+
get "/zebras", nil, {'HTTP_ACCEPT' => 'application/zebra'}
112+
post "/pact", pact_details, admin_headers
113+
114+
expect(pact_json['interactions']).to include(
115+
include("description" => "a request for alligators")
116+
)
117+
expect(pact_json['interactions']).to include(
118+
include("description" => "a request for giraffes")
119+
)
120+
expect(pact_json['interactions']).to_not include(
121+
include("description" => "a request for zebras")
122+
)
123+
end
124+
end
125+
85126
context "when no interactions are executed" do
86127
it "does not create the pact file" do
87128
post "/pact", pact_details, admin_headers

spec/lib/pact/consumer_contract/consumer_contract_decorator_spec.rb

+46-1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ module Pact
3434
end
3535

3636
end
37+
3738
describe "as_json" do
3839
context "with multiple interactions" do
3940
let(:desc_2) { 'Desc 1' }
@@ -127,7 +128,51 @@ module Pact
127128
end
128129
end
129130
end
130-
end
131131

132+
context "when an interaction is marked to not be written" do
133+
before do
134+
Pact.configuration.pactfile_write_order = :chronological
135+
end
136+
137+
let(:interaction_1) do
138+
InteractionFactory.create(
139+
provider_state: 'State 1',
140+
description: 'Desc 1',
141+
response: {
142+
status: 201
143+
})
144+
end
145+
let(:interaction_2) do
146+
InteractionFactory.create(
147+
provider_state: 'State 2',
148+
description: 'Desc 2',
149+
response: {
150+
status: 201
151+
},
152+
metadata: {
153+
write_to_pact: true
154+
})
155+
end
156+
let(:interaction_3) do
157+
InteractionFactory.create(
158+
provider_state: 'State 3',
159+
description: 'Desc 3',
160+
response: {
161+
status: 201
162+
},
163+
metadata: {
164+
write_to_pact: false
165+
})
166+
end
167+
let(:interactions) { [interaction_1, interaction_2, interaction_3] }
168+
169+
it "only renders writable interactions" do
170+
expect(subject.as_json[:interactions]).to eq([
171+
InteractionDecorator.new(interaction_1, pact_specification_version: pact_specification_version).as_json,
172+
InteractionDecorator.new(interaction_2, pact_specification_version: pact_specification_version).as_json
173+
])
174+
end
175+
end
176+
end
132177
end
133178
end

0 commit comments

Comments
 (0)