Skip to content

Commit

Permalink
Migrate to errkit (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
e-sumin authored Dec 2, 2024
1 parent 35a80bf commit 8195145
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 27 deletions.
5 changes: 2 additions & 3 deletions command/arg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,14 @@ import (

"gopkg.in/check.v1"

"github.com/pkg/errors"

"github.com/kanisterio/errkit"
"github.com/kanisterio/safecli"
"github.com/kanisterio/safecli/command"
"github.com/kanisterio/safecli/test"
)

var (
ErrArgument = errors.New("arg error")
ErrArgument = errkit.NewSentinelErr("arg error")
)

// MockArg is a mock implementation of the Applier interface.
Expand Down
6 changes: 3 additions & 3 deletions command/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
package command

import (
"github.com/pkg/errors"
"github.com/kanisterio/errkit"
)

var (
// ErrInvalidArgumentName is returned when the argument name is empty.
ErrInvalidArgumentName = errors.New("argument name is empty")
ErrInvalidArgumentName = errkit.NewSentinelErr("argument name is empty")
// ErrInvalidOptionName is returned when the option name is empty or has no hyphen prefix.
ErrInvalidOptionName = errors.New("option name is empty or has no hyphen prefix")
ErrInvalidOptionName = errkit.NewSentinelErr("option name is empty or has no hyphen prefix")
)
11 changes: 7 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
module github.com/kanisterio/safecli

go 1.20
go 1.21.0

toolchain go1.22.4

require (
github.com/pkg/errors v0.9.1
github.com/kanisterio/errkit v0.0.2
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c
)

require (
github.com/kr/pretty v0.2.1 // indirect
github.com/kr/text v0.1.0 // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/rogpeppe/go-internal v1.9.0 // indirect
)
18 changes: 14 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8=
github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/kanisterio/errkit v0.0.2 h1:3v3HGz9lHIbZR6Jr9qIJpRjaqUX0rsJSLMEQGsMHiUk=
github.com/kanisterio/errkit v0.0.2/go.mod h1:ViQ6kPJ2gTJDEvRytmwde7pzG9/sndObF9BPZoEZixc=
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
7 changes: 2 additions & 5 deletions test/arg_suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@ package test
import (
"gopkg.in/check.v1"

"github.com/pkg/errors"

"github.com/kanisterio/errkit"
"github.com/kanisterio/safecli"

"github.com/kanisterio/safecli/command"
)

Expand Down Expand Up @@ -69,8 +67,7 @@ func (t *ArgumentTest) assertNoError(c *check.C, err error) {

// assertError checks the error against ExpectedErr.
func (t *ArgumentTest) assertError(c *check.C, err error) {
actualErr := errors.Cause(err)
c.Assert(actualErr, check.Equals, t.ExpectedErr, t.comment())
c.Assert(errkit.Is(err, t.ExpectedErr), check.Equals, true, t.comment())
}

// assertErrorMsg checks the error message against ExpectedErrMsg.
Expand Down
15 changes: 7 additions & 8 deletions test/arg_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,9 @@ import (
"strings"
"testing"

"github.com/pkg/errors"

"gopkg.in/check.v1"

"github.com/kanisterio/errkit"
"github.com/kanisterio/safecli"
"github.com/kanisterio/safecli/test"
)
Expand Down Expand Up @@ -113,7 +112,7 @@ func (s *TestRunnerWithConfig) TestArgumentTestEmptyName(c *check.C) {

// TestArgumentTestErrButReturnOK tests the ArgumentTest with an error but ArgumentTest returns no error.
func (s *TestRunnerWithConfig) TestArgumentTestErrButReturnOK(c *check.C) {
err := errors.New("test error")
err := errkit.New("test error")
cat := CustomArgumentTest{
name: "TestArgumentTestErrButReturnOK",
argErr: nil, // no error
Expand All @@ -122,13 +121,13 @@ func (s *TestRunnerWithConfig) TestArgumentTestErrButReturnOK(c *check.C) {
}
res := check.Run(&cat, s.cfg)
out := strings.ReplaceAll(s.out.String(), "\n", "")
c.Assert(out, check.Matches, ".*FAIL:.*CustomArgumentTest\\.Test.*test error.*")
c.Assert(out, check.Matches, ".*FAIL:.*CustomArgumentTest\\.Test.*TestArgumentTestErrButReturnOK")
c.Assert(res.Passed(), check.Equals, false)
}

// TestArgumentTestErr tests the ArgumentTest with an error.
func (s *TestRunnerWithConfig) TestArgumentTestErr(c *check.C) {
err := errors.New("test error")
err := errkit.New("test error")
cat := CustomArgumentTest{
name: "TestArgumentErr",
argErr: err,
Expand All @@ -142,8 +141,8 @@ func (s *TestRunnerWithConfig) TestArgumentTestErr(c *check.C) {

// TestArgumentTestWrapperErr tests the ArgumentTest with a wrapped error.
func (s *TestRunnerWithConfig) TestArgumentTestWrapperErr(c *check.C) {
err := errors.New("test error")
werr := errors.Wrap(err, "wrapper error")
err := errkit.New("test error")
werr := errkit.Wrap(err, "wrapper error")
cat := CustomArgumentTest{
name: "TestArgumentTestWrapperErr",
argErr: werr,
Expand All @@ -156,7 +155,7 @@ func (s *TestRunnerWithConfig) TestArgumentTestWrapperErr(c *check.C) {

// TestArgumentTestUnexpectedErr tests the ArgumentTest with an unexpected error.
func (s *TestRunnerWithConfig) TestArgumentTestUnexpectedErr(c *check.C) {
err := errors.New("test error")
err := errkit.New("test error")
cat := CustomArgumentTest{
name: "TestArgumentUnexpectedErr",
arg: "--test",
Expand Down

0 comments on commit 8195145

Please sign in to comment.