-
Notifications
You must be signed in to change notification settings - Fork 933
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Clarify error when passing a negative int to rollback
[#174107413](https://www.pivotaltracker.com/story/show/174107413)
- Loading branch information
1 parent
2c1edf4
commit 3e3c7fa
Showing
4 changed files
with
109 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}})) | ||
}) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters