From 39129db3d24d65e776f35713ad21e27385c25757 Mon Sep 17 00:00:00 2001 From: Beth Skurrie Date: Thu, 12 Aug 2021 08:07:22 +1000 Subject: [PATCH] chore: sort resolved selectors deterministically --- lib/pact_broker/pacts/selected_pact.rb | 2 +- lib/pact_broker/pacts/selector.rb | 9 +++++++++ lib/pact_broker/pacts/selectors.rb | 4 ++++ .../pact_broker/pacts/verifiable_pact_messages_spec.rb | 2 +- 4 files changed, 15 insertions(+), 2 deletions(-) diff --git a/lib/pact_broker/pacts/selected_pact.rb b/lib/pact_broker/pacts/selected_pact.rb index a1b6466f9..fb0f22c53 100644 --- a/lib/pact_broker/pacts/selected_pact.rb +++ b/lib/pact_broker/pacts/selected_pact.rb @@ -13,7 +13,7 @@ def initialize(pact, selectors) def self.merge(selected_pacts) latest_selected_pact = selected_pacts.sort_by(&:consumer_version_order).last - selectors = selected_pacts.collect(&:selectors).reduce(&:+) + selectors = selected_pacts.collect(&:selectors).reduce(&:+).sort SelectedPact.new(latest_selected_pact.pact, selectors) end diff --git a/lib/pact_broker/pacts/selector.rb b/lib/pact_broker/pacts/selector.rb index 2817a9025..8354808c8 100644 --- a/lib/pact_broker/pacts/selector.rb +++ b/lib/pact_broker/pacts/selector.rb @@ -338,6 +338,15 @@ def environment def == other super && consumer_version == other.consumer_version end + + def <=> other + comparison = super + if comparison == 0 + consumer_version.order <=> other.consumer_version.order + else + comparison + end + end end end end diff --git a/lib/pact_broker/pacts/selectors.rb b/lib/pact_broker/pacts/selectors.rb index c06a3874e..08992006c 100644 --- a/lib/pact_broker/pacts/selectors.rb +++ b/lib/pact_broker/pacts/selectors.rb @@ -43,6 +43,10 @@ def + other Selectors.new(super) end + def sort + Selectors.new(super) + end + def overall_latest? any?(&:overall_latest?) end diff --git a/spec/lib/pact_broker/pacts/verifiable_pact_messages_spec.rb b/spec/lib/pact_broker/pacts/verifiable_pact_messages_spec.rb index a6d8c394c..01136f895 100644 --- a/spec/lib/pact_broker/pacts/verifiable_pact_messages_spec.rb +++ b/spec/lib/pact_broker/pacts/verifiable_pact_messages_spec.rb @@ -26,7 +26,7 @@ module Pacts provider_branch: provider_branch ) end - let(:consumer_version) { double("version", number: "1234" )} + let(:consumer_version) { double("version", number: "1234", order: 1) } let(:environment) { instance_double("PactBroker::Deployments::Environment", name: "test") } let(:test_environment) { environment } let(:prod_environment) { instance_double("PactBroker::Deployments::Environment", name: "prod") }