From 1cbea4a179737b0402558d1dec926fa198c43d38 Mon Sep 17 00:00:00 2001 From: Christian Ang Date: Fri, 22 Sep 2017 13:54:48 -0700 Subject: [PATCH] Drive out errors [#151229598] Signed-off-by: Slawek Ligus --- cmd/compare.go | 4 ++-- cmd/compare_test.go | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/cmd/compare.go b/cmd/compare.go index 7ef4de0..d7eb45d 100644 --- a/cmd/compare.go +++ b/cmd/compare.go @@ -27,12 +27,12 @@ func NewCompare(stdout, stderr io.Writer) Compare { func (c Compare) Execute(args []string) error { v1, err := semverMake(args[0]) if err != nil { - panic(err) + return err } v2, err := semverMake(args[1]) if err != nil { - panic(err) + return err } c.stdout.Write([]byte(fmt.Sprintf("%d\n", semverCompare(v1, v2)))) diff --git a/cmd/compare_test.go b/cmd/compare_test.go index 1105416..dff0e74 100644 --- a/cmd/compare_test.go +++ b/cmd/compare_test.go @@ -2,6 +2,7 @@ package cmd_test import ( "bytes" + "errors" "github.com/blang/semver" "github.com/christianang/semver-cli/cmd" @@ -53,5 +54,23 @@ var _ = Describe("Compare", func() { Expect(fakeSemver.MakeArgsForCall(1)).To(Equal("1.12.5")) Expect(stdout.String()).To(Equal("9999\n")) }) + + Context("when an error occurs", func() { + Context("when the first semver is invalid", func() { + It("returns an error", func() { + fakeSemver.MakeReturnsOnCall(0, semver.Version{}, errors.New("failed to make")) + err := command.Execute([]string{"1.x.x.x", "1.12.5"}) + Expect(err).To(MatchError("failed to make")) + }) + }) + + Context("when the first semver is invalid", func() { + It("returns an error", func() { + fakeSemver.MakeReturnsOnCall(1, semver.Version{}, errors.New("failed to make")) + err := command.Execute([]string{"1.x.x.x", "1.12.5"}) + Expect(err).To(MatchError("failed to make")) + }) + }) + }) }) })