Skip to content

Commit

Permalink
fix(can-i-deploy): correct logic for detecting when only the pacticip…
Browse files Browse the repository at this point in the history
…ant name has been specified for a selector
  • Loading branch information
bethesque committed Feb 15, 2023
1 parent 365e585 commit 85bcdb3
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 6 deletions.
7 changes: 6 additions & 1 deletion lib/pact_broker/matrix/resolved_selector.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
require "pact_broker/hash_refinements"

# A selector with the pacticipant id, name, version number, and version id set
# This is created from either specified or inferred data, based on the user's query
# eg.
# can-i-deploy --pacticipant Foo --version 1 (this is a specified selector)
# --to prod (this is used to create inferred selectors)

module PactBroker
module Matrix
class ResolvedSelector < Hash

using PactBroker::HashRefinements

# A version ID of -1 will not match any rows, which is what we want to ensure that
# no matrix rows are returned for a version that does not exist.
NULL_VERSION_ID = -1
Expand Down Expand Up @@ -124,7 +129,7 @@ def most_specific_criterion
end

def only_pacticipant_name_specified?
!!pacticipant_name && !branch && !tag && !latest? && !pacticipant_version_number && !main_branch?
!!pacticipant_name && self[:original_selector].without(:pacticipant_name).none?{ |_key, value| value }
end

def latest_tagged?
Expand Down
12 changes: 8 additions & 4 deletions spec/lib/pact_broker/matrix/deployment_status_summary_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -323,14 +323,16 @@ module Matrix
pacticipant_name: foo.name,
pacticipant_version_number: foo_version.number,
pacticipant_version_id: foo_version.id,
type: :specified
type: :specified,
original_selector: {}
),
ResolvedSelector.new(
pacticipant_id: bar.id,
pacticipant_name: bar.name,
pacticipant_version_number: bar_version.number,
pacticipant_version_id: bar_version.id,
type: :inferred
type: :inferred,
original_selector: {}
)
]
end
Expand All @@ -347,14 +349,16 @@ module Matrix
pacticipant_name: foo.name,
pacticipant_version_number: foo_version.number,
pacticipant_version_id: foo_version.id,
type: :specified
type: :specified,
original_selector: {}
),
ResolvedSelector.new(
pacticipant_id: bar.id,
pacticipant_name: bar.name,
pacticipant_version_number: bar_version.number,
pacticipant_version_id: bar_version.id,
type: :specified
type: :specified,
original_selector: {}
)
]
end
Expand Down
24 changes: 23 additions & 1 deletion spec/lib/pact_broker/matrix/resolved_selector_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,26 @@ module Matrix
let(:one_of_many) { false }
let(:version_values) { {} }

its(:description) { is_expected.to eq "version 123 of Foo" }
context "when it was specified by version number" do
let(:pacticipant_version_number) { "123" }

its(:description) { is_expected.to eq "version 123 of Foo" }
its(:only_pacticipant_name_specified?) { is_expected.to be false }
end

context "when it was specified by tag" do
let(:tag) { "dev" }

its(:description) { is_expected.to eq "a version of Foo with tag dev (123)" }
its(:only_pacticipant_name_specified?) { is_expected.to be false }
end

context "when it was specified by tag and latest" do
let(:tag) { "dev" }
let(:latest) { true }

its(:description) { is_expected.to eq "the latest version of Foo with tag dev (123)" }
its(:only_pacticipant_name_specified?) { is_expected.to be false }
end

context "when it was specified by branch" do
Expand All @@ -58,6 +65,7 @@ module Matrix
let(:one_of_many) { true }

its(:description) { is_expected.to eq "one of the versions of Foo from branch main (123)" }
its(:only_pacticipant_name_specified?) { is_expected.to be false }
end
end

Expand All @@ -66,18 +74,21 @@ module Matrix
let(:latest) { true }

its(:description) { is_expected.to eq "the latest version of Foo from branch main (123)" }
its(:only_pacticipant_name_specified?) { is_expected.to be false }
end

context "when it was specified by environment" do
let(:environment_name) { "test" }

its(:description) { is_expected.to eq "the version of Foo currently in test (123)" }
its(:only_pacticipant_name_specified?) { is_expected.to be false }
end

context "when it was specified by version number" do
let(:pacticipant_version_number) { "123" }

its(:description) { is_expected.to eq "version 123 of Foo" }
its(:only_pacticipant_name_specified?) { is_expected.to be false }
end

context "when specified by main_branch" do
Expand All @@ -91,6 +102,7 @@ module Matrix
let(:one_of_many) { true }

its(:description) { is_expected.to eq "one of the versions of Foo from branch develop (123)" }
its(:only_pacticipant_name_specified?) { is_expected.to be false }
end
end

Expand All @@ -100,6 +112,16 @@ module Matrix
let(:version_values) { { branch_name: "develop" } }

its(:description) { is_expected.to eq "the latest version of Foo from branch develop (123)" }
its(:only_pacticipant_name_specified?) { is_expected.to be false }
end

context "when it was specified by pacticipant name only" do
let(:subject) do
PactBroker::Matrix::ResolvedSelector.for_pacticipant(pacticipant, original_selector, :specified, false)
end

its(:description) { is_expected.to eq "any version of Foo" }
its(:only_pacticipant_name_specified?) { is_expected.to be true }
end
end

Expand Down

0 comments on commit 85bcdb3

Please sign in to comment.