Skip to content

Commit

Permalink
chore: tidy up consumer version selectors
Browse files Browse the repository at this point in the history
  • Loading branch information
mefellows committed Aug 9, 2021
1 parent 99c243f commit 936315b
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 24 deletions.
2 changes: 1 addition & 1 deletion command/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/spf13/cobra"
)

var version = "v1.5.3"
var version = "v1.6.3"
var cliToolsVersion = "1.82.3"
var versionCmd = &cobra.Command{
Use: "version",
Expand Down
49 changes: 32 additions & 17 deletions types/consumer_version_selector.go
Original file line number Diff line number Diff line change
@@ -1,35 +1,50 @@
package types

import "fmt"
import (
"fmt"
"log"
)

// ConsumerVersionSelector are the way we specify which pacticipants and
// versions we want to use when configuring verifications
// See https://docs.pact.io/selectors for more
type ConsumerVersionSelector struct {
Pacticipant string `json:"pacticipant"`
Tag string `json:"tag"`
Version string `json:"version"`
Latest bool `json:"latest"`
All bool `json:"all"`
Consumer string `json:"consumer"`
DeployedOrReleased bool `json:"deployedOrReleased"`
Deployed bool `json:"deployed"`
Released bool `json:"released"`
Environment string `json:"environment"`
Pacticipant string `json:"-"` // Deprecated
All bool `json:"-"` // Deprecated
Version string `json:"-"` // Deprecated
Tag string `json:"tag,omitempty"`
FallbackTag string `json:"fallbackTag,omitempty"`
Latest bool `json:"latest,omitempty"`
Consumer string `json:"consumer,omitempty"`
DeployedOrReleased bool `json:"deployedOrReleased,omitempty"`
Deployed bool `json:"deployed,omitempty"`
Released bool `json:"released,omitempty"`
Environment string `json:"environment,omitempty"`
MainBranch bool `json:"mainBranch,omitempty"`
Branch string `json:"branch,omitempty"`
}

// Validate the selector configuration
func (c *ConsumerVersionSelector) Validate() error {
if c.All && c.Pacticipant == "" {
return fmt.Errorf("must provide a Pacticpant")
if c.All && c.Latest {
return fmt.Errorf("cannot select both All and Latest")
}

if c.Pacticipant != "" && c.Tag == "" {
return fmt.Errorf("must provide at least a Tag if Pacticpant specified")
if c.All {
c.Latest = false
}

if c.All && c.Latest {
return fmt.Errorf("cannot select both All and Latest")
if c.Pacticipant != "" && c.Consumer != "" {
return fmt.Errorf("cannot select deprecated field 'Pacticipant' along with Consumer, use only 'Consumer'")
}

if c.Pacticipant != "" {
c.Consumer = c.Pacticipant
log.Println("[WARN] 'Pacticipant' is deprecated, please use 'Consumer'. 'Consumer' has been automatically set to", c.Pacticipant)
}

if c.Version != "" {
log.Println("[WARN] 'Version' is deprecated and has no effect")
}

return nil
Expand Down
9 changes: 5 additions & 4 deletions types/consumer_version_selector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ func TestConsumerVersionSelectorValidate(t *testing.T) {
err bool
}{
{name: "no pacticipant", selector: ConsumerVersionSelector{}, err: false},
{name: "no pacticipant and all set", selector: ConsumerVersionSelector{All: true}, err: true},
{name: "all and latest set", selector: ConsumerVersionSelector{All: true}, err: true},
{name: "pacticipant only", selector: ConsumerVersionSelector{Pacticipant: "foo"}, err: true},
{name: "no pacticipant and all set", selector: ConsumerVersionSelector{All: true}, err: false},
{name: "all and latest set", selector: ConsumerVersionSelector{All: true, Latest: true}, err: true},
{name: "pacticipant only", selector: ConsumerVersionSelector{Pacticipant: "foo"}, err: false},
{name: "pacticipant and tag", selector: ConsumerVersionSelector{Pacticipant: "foo", Tag: "foo"}, err: false},
{name: "pacticipant, tag and all set", selector: ConsumerVersionSelector{Pacticipant: "foo", Tag: "foo", All: true}, err: false},
{name: "pacticipant, tag, consumer", selector: ConsumerVersionSelector{Pacticipant: "foo", Tag: "foo", Consumer: "bar"}, err: false},
{name: "pacticipant and consumer", selector: ConsumerVersionSelector{Pacticipant: "foo", Tag: "foo", Consumer: "bar"}, err: true},
{name: "pacticipant, tag, consumer", selector: ConsumerVersionSelector{Pacticipant: "foo", Tag: "foo"}, err: false},
{name: "pacticipant, tag, deployedOrReleased", selector: ConsumerVersionSelector{Pacticipant: "foo", Tag: "foo", DeployedOrReleased: true}, err: false},
{name: "pacticipant, tag, deployed", selector: ConsumerVersionSelector{Pacticipant: "foo", Tag: "foo", Deployed: true}, err: false},
{name: "pacticipant, tag, released", selector: ConsumerVersionSelector{Pacticipant: "foo", Tag: "foo", Released: true}, err: false},
Expand Down
4 changes: 2 additions & 2 deletions types/verify_request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,10 @@ func TestVerifyRequestValidate(t *testing.T) {
ProviderBaseURL: "http://localhost:8080",
ConsumerVersionSelectors: []ConsumerVersionSelector{ConsumerVersionSelector{}},
}, err: false},
{name: "pacticipant only", request: VerifyRequest{
{name: "consumer only", request: VerifyRequest{
PactURLs: []string{"http://localhost:1234/path/to/pact"},
ProviderBaseURL: "http://localhost:8080",
ConsumerVersionSelectors: []ConsumerVersionSelector{ConsumerVersionSelector{Pacticipant: "foo", Tag: "test"}},
ConsumerVersionSelectors: []ConsumerVersionSelector{ConsumerVersionSelector{Consumer: "foo", Tag: "test"}},
}, err: false},
}
for _, tt := range tests {
Expand Down

0 comments on commit 936315b

Please sign in to comment.