Skip to content

Commit

Permalink
fix: support merging v3 pacts
Browse files Browse the repository at this point in the history
  • Loading branch information
bethesque committed Oct 14, 2023
1 parent c894eb8 commit d1e01d0
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 2 deletions.
7 changes: 5 additions & 2 deletions lib/pact_broker/client/merge_pacts.rb
Expand Up @@ -53,14 +53,17 @@ def merge_interactions_or_messages(new_pact, original, additional, key)
end

def almost_duplicate_message(original, new_interaction)
"Two interactions have been found with same description (#{new_interaction[:description].inspect}) and provider state (#{new_interaction[:providerState].inspect}) but a different request or response. " +
"Two interactions have been found with same description (#{new_interaction[:description].inspect}) and provider state (#{(new_interaction[:providerState] || new_interaction[:providerStates]).inspect}) but a different request or response. " +
"Please use a different description or provider state, or hard-code any random data.\n\n" +
original.to_json + "\n\n" + new_interaction.to_json
end

def same_description_and_state? original, additional
original[:description] == additional[:description] &&
original[:providerState] == additional[:providerState]
(
(original[:providerState] && original[:providerState] == additional[:providerState]) ||
(original[:providerStates] && original[:providerStates] == additional[:providerStates])
)
end
end
end
Expand Down
37 changes: 37 additions & 0 deletions spec/fixtures/MyConsumer-MyProvider (1).json
@@ -0,0 +1,37 @@
{
"consumer": {
"name": "MyConsumer"
},
"interactions": [
{
"description": "request",
"providerStates": [
{
"name": "state 2"
}
],
"request": {
"method": "GET",
"path": "/2"
},
"response": {
"status": 200
}
}
],
"metadata": {
"pact-js": {
"version": "11.0.2"
},
"pactRust": {
"ffi": "0.4.0",
"models": "1.0.4"
},
"pactSpecification": {
"version": "3.0.0"
}
},
"provider": {
"name": "MyProvider"
}
}
37 changes: 37 additions & 0 deletions spec/fixtures/MyConsumer-MyProvider.json
@@ -0,0 +1,37 @@
{
"consumer": {
"name": "MyConsumer"
},
"interactions": [
{
"description": "request",
"providerStates": [
{
"name": "state 1"
}
],
"request": {
"method": "GET",
"path": "/1"
},
"response": {
"status": 200
}
}
],
"metadata": {
"pact-js": {
"version": "11.0.2"
},
"pactRust": {
"ffi": "0.4.0",
"models": "1.0.4"
},
"pactSpecification": {
"version": "3.0.0"
}
},
"provider": {
"name": "MyProvider"
}
}
26 changes: 26 additions & 0 deletions spec/lib/pact_broker/client/merge_pacts_spec.rb
Expand Up @@ -166,6 +166,32 @@ module Client
expect(subject).to eq expected_merge
end
end

describe "with v3 pacts" do
let(:pact_hash_1) { JSON.parse(File.read("spec/fixtures/MyConsumer-MyProvider.json"), symbolize_names: true) }
let(:pact_hash_2) { JSON.parse(File.read("spec/fixtures/MyConsumer-MyProvider (1).json"), symbolize_names: true) }
let(:pact_hashes) { [pact_hash_1, pact_hash_2] }

subject { MergePacts.call(pact_hashes) }

context "when there are no conflicts and no duplicates" do
it "adds all the interactions to the merged file" do
expect(subject[:interactions].size).to eq 2
end
end

context "when there is a conflict" do
let(:pact_hash_2) do
hash = JSON.parse(File.read("spec/fixtures/MyConsumer-MyProvider.json"), symbolize_names: true)
hash[:interactions].first[:request][:path] = "/a-different-path"
hash
end

it "raises an error with a message that contains the provider states of the conflicting interactions" do
expect { subject }.to raise_error PactMergeError, /state 1/
end
end
end
end
end
end
Expand Down

0 comments on commit d1e01d0

Please sign in to comment.