From 07ff80440e35cbfc38d746318f8e28635632e050 Mon Sep 17 00:00:00 2001 From: Beth Skurrie Date: Tue, 17 May 2022 12:02:42 +1000 Subject: [PATCH] feat(matrix): add support for selectors specified by branch and environment name when reporting that a version does not exist --- .../api/decorators/reason_decorator.rb | 16 +----- lib/pact_broker/matrix/resolved_selector.rb | 19 +++++++ .../api/decorators/reason_decorator_spec.rb | 8 +++ .../matrix/resolved_selector_spec.rb | 57 +++++++++++++++++++ 4 files changed, 85 insertions(+), 15 deletions(-) create mode 100644 spec/lib/pact_broker/matrix/resolved_selector_spec.rb diff --git a/lib/pact_broker/api/decorators/reason_decorator.rb b/lib/pact_broker/api/decorators/reason_decorator.rb index 2e72c3aa3..66d1fe22a 100644 --- a/lib/pact_broker/api/decorators/reason_decorator.rb +++ b/lib/pact_broker/api/decorators/reason_decorator.rb @@ -30,7 +30,7 @@ def reason_text when PactBroker::Matrix::PactNotVerifiedByRequiredProviderVersion "There is no verified pact between #{reason.consumer_selector.description} and #{reason.provider_selector.description}" when PactBroker::Matrix::SpecifiedVersionDoesNotExist - version_does_not_exist_description(reason.selector) + reason.selector.version_does_not_exist_description when PactBroker::Matrix::VerificationFailed "The verification for the pact between #{reason.consumer_selector.description} and #{reason.provider_selector.description} failed" when PactBroker::Matrix::NoDependenciesMissing @@ -53,20 +53,6 @@ def reason_text end end - def version_does_not_exist_description selector - if selector.version_does_not_exist? - if selector.tag - "No version with tag #{selector.tag} exists for #{selector.pacticipant_name}" - elsif selector.pacticipant_version_number - "No pacts or verifications have been published for version #{selector.pacticipant_version_number} of #{selector.pacticipant_name}" - else - "No pacts or verifications have been published for #{selector.pacticipant_name}" - end - else - "" - end - end - # TODO move this somewhere else def interaction_description(interaction) if interaction["providerState"] && interaction["providerState"] != "" diff --git a/lib/pact_broker/matrix/resolved_selector.rb b/lib/pact_broker/matrix/resolved_selector.rb index 404f4fe84..df435d9a1 100644 --- a/lib/pact_broker/matrix/resolved_selector.rb +++ b/lib/pact_broker/matrix/resolved_selector.rb @@ -99,6 +99,7 @@ def tag self[:tag] end + # @return [String] the name of the branch def branch self[:branch] end @@ -208,6 +209,24 @@ def description end end # rubocop: enable Metrics/CyclomaticComplexity + + def version_does_not_exist_description + if version_does_not_exist? + if tag + "No version with tag #{tag} exists for #{pacticipant_name}" + elsif branch + "No version of #{pacticipant_name} from branch #{branch} exists" + elsif environment_name + "No version of #{pacticipant_name} is currently recorded as deployed or released in environment #{environment_name}" + elsif pacticipant_version_number + "No pacts or verifications have been published for version #{pacticipant_version_number} of #{pacticipant_name}" + else + "No pacts or verifications have been published for #{pacticipant_name}" + end + else + "" + end + end end end end diff --git a/spec/lib/pact_broker/api/decorators/reason_decorator_spec.rb b/spec/lib/pact_broker/api/decorators/reason_decorator_spec.rb index 89dde6aa7..824fb12ba 100644 --- a/spec/lib/pact_broker/api/decorators/reason_decorator_spec.rb +++ b/spec/lib/pact_broker/api/decorators/reason_decorator_spec.rb @@ -1,5 +1,6 @@ require "pact_broker/api/decorators/reason_decorator" require "pact_broker/matrix/reason" +require "pact_broker/matrix/resolved_selector" module PactBroker module Api @@ -88,6 +89,13 @@ module Decorators its(:to_s) { is_expected.to start_with "WARN: It is recommended to specify the version number" } end + + context "when the reason is PactBroker::Matrix::SpecifiedVersionDoesNotExist" do + let(:reason) { PactBroker::Matrix::SpecifiedVersionDoesNotExist.new(selector) } + let(:selector) { instance_double("PactBroker::Matrix::ResolvedSelector", version_does_not_exist_description: "version_does_not_exist_description") } + + its(:to_s) { is_expected.to eq "version_does_not_exist_description" } + end end end end diff --git a/spec/lib/pact_broker/matrix/resolved_selector_spec.rb b/spec/lib/pact_broker/matrix/resolved_selector_spec.rb new file mode 100644 index 000000000..af67fb871 --- /dev/null +++ b/spec/lib/pact_broker/matrix/resolved_selector_spec.rb @@ -0,0 +1,57 @@ +require "pact_broker/matrix/resolved_selector" + +module PactBroker + module Matrix + describe ResolvedSelector do + describe "#version_does_not_exist_description" do + let(:subject) do + PactBroker::Matrix::ResolvedSelector.for_pacticipant_and_non_existing_version(pacticipant, original_selector, :specified, false) + end + + let(:original_selector) do + { + pacticipant_name: pacticipant_name, + tag: tag, + branch: branch, + environment_name: environment_name, + pacticipant_version_number: pacticipant_version_number, + } + end + + let(:pacticipant) { double("pacticipant", name: pacticipant_name, id: 1)} + + let(:pacticipant_name) { "Foo" } + let(:tag) { nil } + let(:branch) { nil } + let(:environment_name) { nil } + let(:pacticipant_version_number) { nil } + + its(:version_does_not_exist_description) { is_expected.to eq "No pacts or verifications have been published for Foo" } + + context "when it was specified by tag" do + let(:tag) { "dev" } + + its(:version_does_not_exist_description) { is_expected.to eq "No version with tag dev exists for Foo" } + end + + context "when it was specified by branch" do + let(:branch) { "main" } + + its(:version_does_not_exist_description) { is_expected.to eq "No version of Foo from branch main exists" } + end + + context "when it was specified by environment" do + let(:environment_name) { "test" } + + its(:version_does_not_exist_description) { is_expected.to eq "No version of Foo is currently recorded as deployed or released in environment test" } + end + + context "when it was specified by verison number" do + let(:pacticipant_version_number) { "1" } + + its(:version_does_not_exist_description) { is_expected.to eq "No pacts or verifications have been published for version 1 of Foo" } + end + end + end + end +end