Skip to content

Commit

Permalink
Clarify error when passing a negative int to rollback
Browse files Browse the repository at this point in the history
  • Loading branch information
nickjameswebb committed Oct 2, 2020
1 parent 2c1edf4 commit 3e3c7fa
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 6 deletions.
34 changes: 34 additions & 0 deletions command/flag/revision.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package flag

import (
"code.cloudfoundry.org/cli/types"
flags "github.com/jessevdk/go-flags"
)

type Revision struct {
types.NullInt
}

func (i *Revision) UnmarshalFlag(val string) error {
err := i.ParseStringValue(val)
if err != nil {
return &flags.Error{
Type: flags.ErrRequired,
Message: "invalid argument for flag '--revision' (expected int > 0)",
}
}
if i.Value < 1 {
if i.Value == 0 && i.IsSet == false {
return nil
}
return &flags.Error{
Type: flags.ErrRequired,
Message: "invalid argument for flag '--revision' (expected int > 0)",
}
}
return nil
}

func (i *Revision) IsValidValue(val string) error {
return i.UnmarshalFlag(val)
}
68 changes: 68 additions & 0 deletions command/flag/revision_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package flag_test

import (
. "code.cloudfoundry.org/cli/command/flag"
"code.cloudfoundry.org/cli/types"
flags "github.com/jessevdk/go-flags"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)

var _ = Describe("Revision", func() {
var revision Revision

BeforeEach(func() {
revision = Revision{}
})

Describe("UnmarshalFlag", func() {
When("the empty string is provided", func() {
It("sets IsSet to false", func() {
err := revision.IsValidValue("")
Expect(err).ToNot(HaveOccurred())
Expect(revision).To(Equal(Revision{NullInt: types.NullInt{Value: 0, IsSet: false}}))
})
})

When("an invalid integer is provided", func() {
It("returns an error", func() {
err := revision.IsValidValue("abcdef")
Expect(err).To(MatchError(&flags.Error{
Type: flags.ErrRequired,
Message: "invalid argument for flag '--revision' (expected int > 0)",
}))
Expect(revision).To(Equal(Revision{NullInt: types.NullInt{Value: 0, IsSet: false}}))
})
})

When("a negative integer is provided", func() {
It("returns an error", func() {
err := revision.IsValidValue("-10")
Expect(err).To(MatchError(&flags.Error{
Type: flags.ErrRequired,
Message: "invalid argument for flag '--revision' (expected int > 0)",
}))
Expect(revision).To(Equal(Revision{NullInt: types.NullInt{Value: -10, IsSet: true}}))
})
})

When("0 is provided", func() {
It("returns an error", func() {
err := revision.IsValidValue("0")
Expect(err).To(MatchError(&flags.Error{
Type: flags.ErrRequired,
Message: "invalid argument for flag '--revision' (expected int > 0)",
}))
Expect(revision).To(Equal(Revision{NullInt: types.NullInt{Value: 0, IsSet: true}}))
})
})

When("a valid integer is provided", func() {
It("stores the integer and sets IsSet to true", func() {
err := revision.IsValidValue("1")
Expect(err).ToNot(HaveOccurred())
Expect(revision).To(Equal(Revision{NullInt: types.NullInt{Value: 1, IsSet: true}}))
})
})
})
})
10 changes: 5 additions & 5 deletions command/v7/rollback_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ import (
type RollbackCommand struct {
BaseCommand

Force bool `short:"f" description:"Force rollback without confirmation"`
RequiredArgs flag.AppName `positional-args:"yes"`
Version flag.PositiveInteger `long:"revision" required:"true" description:"Roll back to the given app revision"`
relatedCommands interface{} `related_commands:"revisions"`
usage interface{} `usage:"CF_NAME rollback APP_NAME [--revision REVISION_NUMBER] [-f]"`
Force bool `short:"f" description:"Force rollback without confirmation"`
RequiredArgs flag.AppName `positional-args:"yes"`
Version flag.Revision `long:"revision" required:"true" description:"Roll back to the given app revision"`
relatedCommands interface{} `related_commands:"revisions"`
usage interface{} `usage:"CF_NAME rollback APP_NAME [--revision REVISION_NUMBER] [-f]"`

LogCacheClient sharedaction.LogCacheClient
Stager shared.AppStager
Expand Down
3 changes: 2 additions & 1 deletion command/v7/rollback_command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"code.cloudfoundry.org/cli/command/v7/shared/sharedfakes"
"code.cloudfoundry.org/cli/command/v7/v7fakes"
"code.cloudfoundry.org/cli/resources"
"code.cloudfoundry.org/cli/types"
"code.cloudfoundry.org/cli/util/configv3"
"code.cloudfoundry.org/cli/util/ui"
. "github.com/onsi/ginkgo"
Expand Down Expand Up @@ -144,7 +145,7 @@ var _ = Describe("rollback Command", func() {

When("the first revision is set as the rollback target", func() {
BeforeEach(func() {
cmd.Version = flag.PositiveInteger{Value: 1}
cmd.Version = flag.Revision{NullInt: types.NullInt{Value: 1, IsSet: true}}
})

When("the app has at least one revision", func() {
Expand Down

0 comments on commit 3e3c7fa

Please sign in to comment.