Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove pkg/errors dependency #230

Merged
merged 1 commit into from Apr 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 3 additions & 4 deletions assert/cmd/gty-migrate-from-testify/main.go
Expand Up @@ -13,7 +13,6 @@ import (
"path/filepath"
"strings"

"github.com/pkg/errors"
"github.com/spf13/pflag"
"golang.org/x/tools/go/packages"
"golang.org/x/tools/imports"
Expand Down Expand Up @@ -93,7 +92,7 @@ func run(opts options) error {
fset := token.NewFileSet()
pkgs, err := loadPackages(opts, fset)
if err != nil {
return errors.Wrapf(err, "failed to load program")
return fmt.Errorf("failed to load program: %w", err)
}

debugf("package count: %d", len(pkgs))
Expand Down Expand Up @@ -122,11 +121,11 @@ func run(opts options) error {

raw, err := formatFile(m)
if err != nil {
return errors.Wrapf(err, "failed to format %s", filename)
return fmt.Errorf("failed to format %s: %w", filename, err)
}

if err := ioutil.WriteFile(absFilename, raw, 0); err != nil {
return errors.Wrapf(err, "failed to write file %s", filename)
return fmt.Errorf("failed to write file %s: %w", filename, err)
}
}
}
Expand Down
38 changes: 34 additions & 4 deletions assert/cmp/compare_test.go
@@ -1,6 +1,7 @@
package cmp

import (
"errors"
"fmt"
"go/ast"
"io"
Expand All @@ -11,7 +12,6 @@ import (
"testing"

"github.com/google/go-cmp/cmp"
"github.com/pkg/errors"
)

func TestDeepEqual(t *testing.T) {
Expand Down Expand Up @@ -315,16 +315,46 @@ func TestEqual_PointersNotEqual(t *testing.T) {
assertFailureTemplate(t, res, args, expected)
}

// errorWithCause mimics the error formatting of github.com/pkg/errors
type errorWithCause struct {
msg string
cause error
}

func (e errorWithCause) Error() string {
return fmt.Sprintf("%v with cause: %v", e.msg, e.cause)
}

func (e errorWithCause) Cause() error {
return e.cause
}

func (e errorWithCause) Format(s fmt.State, verb rune) {
switch verb {
case 'v':
if s.Flag('+') {
fmt.Fprintf(s, "%+v", e.Cause())
fmt.Fprint(s, "\nstack trace")
return
}
fallthrough
case 's':
io.WriteString(s, e.Error())
case 'q':
fmt.Fprintf(s, "%q", e.Error())
}
}

func TestError(t *testing.T) {
result := Error(nil, "the error message")()
assertFailure(t, result, "expected an error, got nil")

// A Wrapped error also includes the stack
result = Error(errors.Wrap(errors.New("other"), "wrapped"), "the error message")()
result = Error(errorWithCause{cause: errors.New("other"), msg: "wrapped"}, "the error message")()
assertFailureHasPrefix(t, result,
`expected error "the error message", got "wrapped: other"
`expected error "the error message", got "wrapped with cause: other"
other
gotest.tools`)
stack trace`)

msg := "the message"
result = Error(errors.New(msg), msg)()
Expand Down
4 changes: 2 additions & 2 deletions fs/manifest.go
@@ -1,12 +1,12 @@
package fs

import (
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"

"github.com/pkg/errors"
"gotest.tools/v3/assert"
)

Expand Down Expand Up @@ -75,7 +75,7 @@ func manifestFromDir(path string) (Manifest, error) {
case err != nil:
return Manifest{}, err
case !info.IsDir():
return Manifest{}, errors.Errorf("path %s must be a directory", path)
return Manifest{}, fmt.Errorf("path %s must be a directory", path)
}

directory, err := newDirectory(path, info)
Expand Down
8 changes: 4 additions & 4 deletions fs/ops.go
Expand Up @@ -2,14 +2,14 @@ package fs

import (
"bytes"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"
"time"

"github.com/pkg/errors"
"gotest.tools/v3/assert"
)

Expand Down Expand Up @@ -137,7 +137,7 @@ func WithFiles(files map[string]string) PathOp {
func FromDir(source string) PathOp {
return func(path Path) error {
if _, ok := path.(manifestDirectory); ok {
return errors.New("use manifest.FromDir")
return fmt.Errorf("use manifest.FromDir")
}
return copyDirectory(source, path.Path())
}
Expand Down Expand Up @@ -257,7 +257,7 @@ func WithSymlink(path, target string) PathOp {
func WithHardlink(path, target string) PathOp {
return func(root Path) error {
if _, ok := root.(manifestDirectory); ok {
return errors.New("WithHardlink not implemented for manifests")
return fmt.Errorf("WithHardlink not implemented for manifests")
}
return os.Link(filepath.Join(root.Path(), target), filepath.Join(root.Path(), path))
}
Expand All @@ -268,7 +268,7 @@ func WithHardlink(path, target string) PathOp {
func WithTimestamps(atime, mtime time.Time) PathOp {
return func(root Path) error {
if _, ok := root.(manifestDirectory); ok {
return errors.New("WithTimestamp not implemented for manifests")
return fmt.Errorf("WithTimestamp not implemented for manifests")
}
return os.Chtimes(root.Path(), atime, mtime)
}
Expand Down
1 change: 0 additions & 1 deletion go.mod
Expand Up @@ -4,7 +4,6 @@ go 1.13

require (
github.com/google/go-cmp v0.5.5
github.com/pkg/errors v0.9.1
github.com/spf13/pflag v1.0.3
golang.org/x/sys v0.0.0-20210119212857-b64e53b001e4
golang.org/x/tools v0.1.0
Expand Down
2 changes: 0 additions & 2 deletions go.sum
@@ -1,7 +1,5 @@
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/spf13/pflag v1.0.3 h1:zPAT6CGy6wXeQ7NtTnaTerfKOsV6V6F8agHXFiazDkg=
github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
Expand Down
4 changes: 2 additions & 2 deletions icmd/exitcode.go
@@ -1,9 +1,9 @@
package icmd

import (
"fmt"
"syscall"

"github.com/pkg/errors"
exec "golang.org/x/sys/execabs"
)

Expand All @@ -15,7 +15,7 @@ func getExitCode(err error) (int, error) {
return procExit.ExitStatus(), nil
}
}
return 0, errors.Wrap(err, "failed to get exit code")
return 0, fmt.Errorf("failed to get exit code: %w", err)
}

func processExitCode(err error) (exitCode int) {
Expand Down
7 changes: 3 additions & 4 deletions internal/source/defers.go
@@ -1,10 +1,9 @@
package source

import (
"fmt"
"go/ast"
"go/token"

"github.com/pkg/errors"
)

func scanToDeferLine(fileset *token.FileSet, node ast.Node, lineNum int) ast.Node {
Expand All @@ -29,11 +28,11 @@ func guessDefer(node ast.Node) (ast.Node, error) {
defers := collectDefers(node)
switch len(defers) {
case 0:
return nil, errors.New("failed to expression in defer")
return nil, fmt.Errorf("failed to expression in defer")
case 1:
return defers[0].Call, nil
default:
return nil, errors.Errorf(
return nil, fmt.Errorf(
"ambiguous call expression: multiple (%d) defers in call block",
len(defers))
}
Expand Down
7 changes: 3 additions & 4 deletions internal/source/source.go
Expand Up @@ -2,6 +2,7 @@ package source // import "gotest.tools/v3/internal/source"

import (
"bytes"
"errors"
"fmt"
"go/ast"
"go/format"
Expand All @@ -11,8 +12,6 @@ import (
"runtime"
"strconv"
"strings"

"github.com/pkg/errors"
)

const baseStackIndex = 1
Expand Down Expand Up @@ -52,7 +51,7 @@ func getNodeAtLine(filename string, lineNum int) (ast.Node, error) {
fileset := token.NewFileSet()
astFile, err := parser.ParseFile(fileset, filename, nil, parser.AllErrors)
if err != nil {
return nil, errors.Wrapf(err, "failed to parse source file: %s", filename)
return nil, fmt.Errorf("failed to parse source file %s: %w", filename, err)
}

if node := scanToLine(fileset, astFile, lineNum); node != nil {
Expand All @@ -64,7 +63,7 @@ func getNodeAtLine(filename string, lineNum int) (ast.Node, error) {
return node, err
}
}
return nil, errors.Errorf(
return nil, fmt.Errorf(
"failed to find an expression on line %d in %s", lineNum, filename)
}

Expand Down
4 changes: 2 additions & 2 deletions poll/example_test.go
@@ -1,9 +1,9 @@
package poll_test

import (
"fmt"
"time"

"github.com/pkg/errors"
"gotest.tools/v3/poll"
)

Expand All @@ -19,7 +19,7 @@ func ExampleWaitOn() {
check := func(t poll.LogT) poll.Result {
actual, err := numOfProcesses()
if err != nil {
return poll.Error(errors.Wrap(err, "failed to get number of processes"))
return poll.Error(fmt.Errorf("failed to get number of processes: %w", err))
}
if actual == desired {
return poll.Success()
Expand Down
3 changes: 1 addition & 2 deletions poll/poll_test.go
Expand Up @@ -5,7 +5,6 @@ import (
"testing"
"time"

"github.com/pkg/errors"
"gotest.tools/v3/assert"
"gotest.tools/v3/assert/cmp"
)
Expand Down Expand Up @@ -67,7 +66,7 @@ func TestWaitOnWithCheckError(t *testing.T) {
fakeT := &fakeT{}

check := func(t LogT) Result {
return Error(errors.New("broke"))
return Error(fmt.Errorf("broke"))
}

assert.Assert(t, cmp.Panics(func() { WaitOn(fakeT, check) }))
Expand Down