From ae3bb541f2a4e78615655bd059cee9ec338e1d88 Mon Sep 17 00:00:00 2001 From: Beth Skurrie Date: Wed, 16 Mar 2022 14:17:31 +1100 Subject: [PATCH] fix(pacts for verification): do not de-duplicate pacts with the same content but different consumers Fixes: https://github.com/pact-foundation/pact_broker/issues/548 --- lib/pact_broker/pacts/verifiable_pact.rb | 6 ++- ...ind_wip_pact_versions_for_provider_spec.rb | 2 +- .../pact_broker/pacts/verifiable_pact_spec.rb | 40 +++++++++++++++++++ 3 files changed, 46 insertions(+), 2 deletions(-) diff --git a/lib/pact_broker/pacts/verifiable_pact.rb b/lib/pact_broker/pacts/verifiable_pact.rb index 5e9a7ce0a..e72a70fb7 100644 --- a/lib/pact_broker/pacts/verifiable_pact.rb +++ b/lib/pact_broker/pacts/verifiable_pact.rb @@ -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 @@ -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( diff --git a/spec/lib/pact_broker/pacts/repository_find_wip_pact_versions_for_provider_spec.rb b/spec/lib/pact_broker/pacts/repository_find_wip_pact_versions_for_provider_spec.rb index 8b431cfa7..adb2e4151 100644 --- a/spec/lib/pact_broker/pacts/repository_find_wip_pact_versions_for_provider_spec.rb +++ b/spec/lib/pact_broker/pacts/repository_find_wip_pact_versions_for_provider_spec.rb @@ -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 diff --git a/spec/lib/pact_broker/pacts/verifiable_pact_spec.rb b/spec/lib/pact_broker/pacts/verifiable_pact_spec.rb index e69de29bb..5c7702d50 100644 --- a/spec/lib/pact_broker/pacts/verifiable_pact_spec.rb +++ b/spec/lib/pact_broker/pacts/verifiable_pact_spec.rb @@ -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