Skip to content

Commit

Permalink
fix(pacts for verification): do not de-duplicate pacts with the same …
Browse files Browse the repository at this point in the history
…content but different consumers

Fixes: #548
  • Loading branch information
bethesque committed Mar 16, 2022
1 parent 4803b99 commit ae3bb54
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 2 deletions.
6 changes: 5 additions & 1 deletion lib/pact_broker/pacts/verifiable_pact.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def self.create_for_wip_for_provider_tags(pact, selectors, pending_provider_tags

def self.deduplicate(verifiable_pacts)
verifiable_pacts
.group_by { | verifiable_pact | verifiable_pact.pact_version_sha }
.group_by { | verifiable_pact | [verifiable_pact.consumer_name, verifiable_pact.pact_version_sha] }
.values
.collect { | verifiable_pact | verifiable_pact.reduce(&:+) }
end
Expand All @@ -51,6 +51,10 @@ def + other
raise PactBroker::Error.new("Can't merge two verifiable pacts with different provider_branch")
end

if consumer_name != other.consumer_name
raise PactBroker::Error.new("Can't merge two verifiable pacts with different consumer names")
end

latest_pact = [self, other].sort_by(&:consumer_version_order).last.__getobj__()

VerifiablePact.new(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ module Pacts
.create_pact_with_hierarchy("foo", "1", "bar", json_content)
.create_consumer_version_tag("feat-1")
.add_day
.create_pact_with_hierarchy("meep", "2", "bar", json_content)
.create_pact_with_hierarchy("foo", "2", "bar", json_content)
.create_consumer_version_tag("feat-2")
end

Expand Down
40 changes: 40 additions & 0 deletions spec/lib/pact_broker/pacts/verifiable_pact_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
require "pact_broker/pacts/verifiable_pact"

module PactBroker
module Pacts
describe VerifiablePact do
describe "deduplicate" do
let(:pact_1) { double("pact 1", consumer_name: "A", pact_version_sha: "1", consumer_version: double("consumer version", order: 1)) }
let(:pact_2) { double("pact 2", consumer_name: "B", pact_version_sha: "1", consumer_version: double("consumer version", order: 1)) }

let(:verifiable_pact_1) do
VerifiablePact.new(pact_1, ["selectors1"], false, [], [], "main", false)
end

let(:verifiable_pact_2) do
VerifiablePact.new(pact_2, ["selectors2"], false, [], [], "main", false)
end

subject { VerifiablePact.deduplicate([verifiable_pact_1, verifiable_pact_2]) }

context "when the pact sha matches and the consumer name matches" do
let(:pact_2) { double("pact 2", consumer_name: "A", pact_version_sha: "1", consumer_version: double("consumer version", order: 2)) }

it "merges the two verifiable pacts" do
expect(subject.size).to eq 1
end

it "merges the selectors" do
expect(subject.first.selectors).to eq ["selectors1", "selectors2"]
end
end

context "when the pact sha matches and the consumer name does not match" do
it "does not merge the two verifiable pacts" do
expect(subject.size).to eq 2
end
end
end
end
end
end

0 comments on commit ae3bb54

Please sign in to comment.