Always use errors.Cause() for testing error values

This commit is contained in:
Alexander Neumann 2016-08-29 19:18:57 +02:00
parent c55b6ee544
commit b06845c545
24 changed files with 70 additions and 44 deletions

View file

@ -13,6 +13,8 @@ import (
"strings"
"time"
"github.com/pkg/errors"
"golang.org/x/crypto/ssh/terminal"
)
@ -222,7 +224,7 @@ func (cmd CmdBackup) newArchiveStdinProgress() *restic.Progress {
func filterExisting(items []string) (result []string, err error) {
for _, item := range items {
_, err := fs.Lstat(item)
if err != nil && os.IsNotExist(err) {
if err != nil && os.IsNotExist(errors.Cause(err)) {
continue
}

View file

@ -7,6 +7,8 @@ import (
"os"
"restic"
"github.com/pkg/errors"
resticfs "restic/fs"
"restic/fuse"
@ -56,7 +58,7 @@ func (cmd CmdMount) Execute(args []string) error {
}
mountpoint := args[0]
if _, err := resticfs.Stat(mountpoint); os.IsNotExist(err) {
if _, err := resticfs.Stat(mountpoint); os.IsNotExist(errors.Cause(err)) {
cmd.global.Verbosef("Mountpoint %s doesn't exist, creating it\n", mountpoint)
err = resticfs.Mkdir(mountpoint, os.ModeDir|0700)
if err != nil {

View file

@ -19,6 +19,7 @@ import (
"restic/repository"
"github.com/jessevdk/go-flags"
"github.com/pkg/errors"
"golang.org/x/crypto/ssh/terminal"
)
@ -183,7 +184,7 @@ func readPassword(in io.Reader) (password string, err error) {
n, err := io.ReadFull(in, buf)
buf = buf[:n]
if err != nil && err != io.ErrUnexpectedEOF {
if err != nil && errors.Cause(err) != io.ErrUnexpectedEOF {
return "", err
}

View file

@ -10,6 +10,8 @@ import (
"testing"
"time"
"github.com/pkg/errors"
"restic"
"restic/backend"
"restic/repository"
@ -125,7 +127,7 @@ func TestMount(t *testing.T) {
datafile := filepath.Join("testdata", "backup-data.tar.gz")
fd, err := os.Open(datafile)
if os.IsNotExist(err) {
if os.IsNotExist(errors.Cause(err)) {
t.Skipf("unable to find data file %q, skipping", datafile)
return
}

View file

@ -16,6 +16,8 @@ import (
"testing"
"time"
"github.com/pkg/errors"
"restic/backend"
"restic/debug"
"restic/filter"
@ -142,7 +144,7 @@ func TestBackup(t *testing.T) {
withTestEnvironment(t, func(env *testEnvironment, global GlobalOptions) {
datafile := filepath.Join("testdata", "backup-data.tar.gz")
fd, err := os.Open(datafile)
if os.IsNotExist(err) {
if os.IsNotExist(errors.Cause(err)) {
t.Skipf("unable to find data file %q, skipping", datafile)
return
}
@ -204,7 +206,7 @@ func TestBackupNonExistingFile(t *testing.T) {
withTestEnvironment(t, func(env *testEnvironment, global GlobalOptions) {
datafile := filepath.Join("testdata", "backup-data.tar.gz")
fd, err := os.Open(datafile)
if os.IsNotExist(err) {
if os.IsNotExist(errors.Cause(err)) {
t.Skipf("unable to find data file %q, skipping", datafile)
return
}
@ -232,7 +234,7 @@ func TestBackupMissingFile1(t *testing.T) {
withTestEnvironment(t, func(env *testEnvironment, global GlobalOptions) {
datafile := filepath.Join("testdata", "backup-data.tar.gz")
fd, err := os.Open(datafile)
if os.IsNotExist(err) {
if os.IsNotExist(errors.Cause(err)) {
t.Skipf("unable to find data file %q, skipping", datafile)
return
}
@ -270,7 +272,7 @@ func TestBackupMissingFile2(t *testing.T) {
withTestEnvironment(t, func(env *testEnvironment, global GlobalOptions) {
datafile := filepath.Join("testdata", "backup-data.tar.gz")
fd, err := os.Open(datafile)
if os.IsNotExist(err) {
if os.IsNotExist(errors.Cause(err)) {
t.Skipf("unable to find data file %q, skipping", datafile)
return
}
@ -308,7 +310,7 @@ func TestBackupDirectoryError(t *testing.T) {
withTestEnvironment(t, func(env *testEnvironment, global GlobalOptions) {
datafile := filepath.Join("testdata", "backup-data.tar.gz")
fd, err := os.Open(datafile)
if os.IsNotExist(err) {
if os.IsNotExist(errors.Cause(err)) {
t.Skipf("unable to find data file %q, skipping", datafile)
return
}
@ -625,7 +627,7 @@ func TestRestoreFilter(t *testing.T) {
if ok, _ := filter.Match(pat, filepath.Base(test.name)); !ok {
OK(t, err)
} else {
Assert(t, os.IsNotExist(err),
Assert(t, os.IsNotExist(errors.Cause(err)),
"expected %v to not exist in restore step %v, but it exists, err %v", test.name, i+1, err)
}
}
@ -673,15 +675,15 @@ func TestRestoreLatest(t *testing.T) {
cmdRestoreLatest(t, global, filepath.Join(env.base, "restore1"), []string{filepath.Dir(p1)}, "")
OK(t, testFileSize(p1rAbs, int64(102)))
if _, err := os.Stat(p2rAbs); os.IsNotExist(err) {
Assert(t, os.IsNotExist(err),
if _, err := os.Stat(p2rAbs); os.IsNotExist(errors.Cause(err)) {
Assert(t, os.IsNotExist(errors.Cause(err)),
"expected %v to not exist in restore, but it exists, err %v", p2rAbs, err)
}
cmdRestoreLatest(t, global, filepath.Join(env.base, "restore2"), []string{filepath.Dir(p2)}, "")
OK(t, testFileSize(p2rAbs, int64(103)))
if _, err := os.Stat(p1rAbs); os.IsNotExist(err) {
Assert(t, os.IsNotExist(err),
if _, err := os.Stat(p1rAbs); os.IsNotExist(errors.Cause(err)) {
Assert(t, os.IsNotExist(errors.Cause(err)),
"expected %v to not exist in restore, but it exists, err %v", p1rAbs, err)
}

View file

@ -9,6 +9,7 @@ import (
"restic/repository"
"time"
"github.com/pkg/errors"
"github.com/restic/chunker"
)
@ -48,7 +49,7 @@ func ArchiveReader(repo *repository.Repository, p *Progress, rd io.Reader, name
for {
chunk, err := chnker.Next(getBuf())
if err == io.EOF {
if errors.Cause(err) == io.EOF {
break
}

View file

@ -225,7 +225,7 @@ func (arch *Archiver) SaveFile(p *Progress, node *Node) error {
for {
chunk, err := chnker.Next(getBuf())
if err == io.EOF {
if errors.Cause(err) == io.EOF {
break
}

View file

@ -14,6 +14,7 @@ import (
"restic/repository"
. "restic/test"
"github.com/pkg/errors"
"github.com/restic/chunker"
)
@ -31,7 +32,7 @@ func benchmarkChunkEncrypt(b testing.TB, buf, buf2 []byte, rd Rdr, key *crypto.K
for {
chunk, err := ch.Next(buf)
if err == io.EOF {
if errors.Cause(err) == io.EOF {
break
}
@ -69,7 +70,7 @@ func benchmarkChunkEncryptP(b *testing.PB, buf []byte, rd Rdr, key *crypto.Key)
for {
chunk, err := ch.Next(buf)
if err == io.EOF {
if errors.Cause(err) == io.EOF {
break
}
@ -292,7 +293,7 @@ func getRandomData(seed int, size int) []chunker.Chunk {
for {
c, err := chunker.Next(nil)
if err == io.EOF {
if errors.Cause(err) == io.EOF {
break
}
chunks = append(chunks, c)

View file

@ -232,7 +232,7 @@ func (b *Local) Test(t backend.Type, name string) (bool, error) {
debug.Log("backend.local.Test", "Test %v %v", t, name)
_, err := fs.Stat(filename(b.p, t, name))
if err != nil {
if os.IsNotExist(err) {
if os.IsNotExist(errors.Cause(err)) {
return false, nil
}
return false, err

View file

@ -125,7 +125,7 @@ func (be s3) Load(h backend.Handle, p []byte, off int64) (n int, err error) {
// return an error if the offset is beyond the end of the file
if off > info.Size {
return 0, io.EOF
return 0, errors.Wrap(io.EOF, "")
}
var nextError error
@ -141,7 +141,7 @@ func (be s3) Load(h backend.Handle, p []byte, off int64) (n int, err error) {
}
n, err = obj.ReadAt(p, off)
if int64(n) == info.Size-off && err == io.EOF {
if int64(n) == info.Size-off && errors.Cause(err) == io.EOF {
err = nil
}

View file

@ -429,7 +429,7 @@ func (r *SFTP) Test(t backend.Type, name string) (bool, error) {
}
_, err := r.c.Lstat(r.filename(t, name))
if os.IsNotExist(err) {
if os.IsNotExist(errors.Cause(err)) {
return false, nil
}

View file

@ -6,6 +6,8 @@ import (
"path/filepath"
"strings"
"github.com/pkg/errors"
"restic/backend"
"restic/backend/sftp"
"restic/backend/test"
@ -37,7 +39,7 @@ func init() {
for _, dir := range strings.Split(TestSFTPPath, ":") {
testpath := filepath.Join(dir, "sftp-server")
_, err := os.Stat(testpath)
if !os.IsNotExist(err) {
if !os.IsNotExist(errors.Cause(err)) {
sftpserver = testpath
break
}

View file

@ -10,6 +10,8 @@ import (
"sort"
"testing"
"github.com/pkg/errors"
"restic/backend"
. "restic/test"
)
@ -223,7 +225,7 @@ func TestLoad(t testing.TB) {
// if we requested data beyond the end of the file, require
// ErrUnexpectedEOF error
if l > len(d) {
if err != io.ErrUnexpectedEOF {
if errors.Cause(err) != io.ErrUnexpectedEOF {
t.Errorf("Load(%d, %d) did not return io.ErrUnexpectedEOF", len(buf), int64(o))
}
err = nil
@ -270,7 +272,7 @@ func TestLoad(t testing.TB) {
// if we requested data beyond the end of the file, require
// ErrUnexpectedEOF error
if l > len(d) {
if err != io.ErrUnexpectedEOF {
if errors.Cause(err) != io.ErrUnexpectedEOF {
t.Errorf("Load(%d, %d) did not return io.ErrUnexpectedEOF", len(buf), int64(o))
continue
}
@ -303,7 +305,7 @@ func TestLoad(t testing.TB) {
t.Errorf("wrong length for larger buffer returned, want %d, got %d", length, n)
}
if err != io.ErrUnexpectedEOF {
if errors.Cause(err) != io.ErrUnexpectedEOF {
t.Errorf("wrong error returned for larger buffer: want io.ErrUnexpectedEOF, got %#v", err)
}
@ -337,7 +339,7 @@ func TestLoadNegativeOffset(t testing.TB) {
// if we requested data beyond the end of the file, require
// ErrUnexpectedEOF error
if len(buf) > -o {
if err != io.ErrUnexpectedEOF {
if errors.Cause(err) != io.ErrUnexpectedEOF {
t.Errorf("Load(%d, %d) did not return io.ErrUnexpectedEOF", len(buf), o)
continue
}

View file

@ -1,6 +1,10 @@
package backend
import "io"
import (
"io"
"github.com/pkg/errors"
)
// LoadAll reads all data stored in the backend for the handle. The buffer buf
// is resized to accomodate all data in the blob. Errors returned by be.Load()
@ -17,7 +21,7 @@ func LoadAll(be Backend, h Handle, buf []byte) ([]byte, error) {
}
n, err := be.Load(h, buf, 0)
if err == io.ErrUnexpectedEOF {
if errors.Cause(err) == io.ErrUnexpectedEOF {
err = nil
}
buf = buf[:n]

View file

@ -48,7 +48,7 @@ func (c *Cache) Has(t backend.Type, subtype string, id backend.ID) (bool, error)
defer fd.Close()
if err != nil {
if os.IsNotExist(err) {
if os.IsNotExist(errors.Cause(err)) {
debug.Log("Cache.Has", "test for file %v: not cached", filename)
return false, nil
}
@ -106,7 +106,7 @@ func (c *Cache) purge(t backend.Type, subtype string, id backend.ID) error {
err = fs.Remove(filename)
debug.Log("Cache.purge", "Remove file %v: %v", filename, err)
if err != nil && os.IsNotExist(err) {
if err != nil && os.IsNotExist(errors.Cause(err)) {
return nil
}
@ -160,7 +160,7 @@ func (c *Cache) list(t backend.Type) ([]cacheEntry, error) {
fd, err := fs.Open(dir)
if err != nil {
if os.IsNotExist(err) {
if os.IsNotExist(errors.Cause(err)) {
return []cacheEntry{}, nil
}
return nil, err
@ -231,7 +231,7 @@ func getWindowsCacheDir() (string, error) {
cachedir = filepath.Join(cachedir, "restic")
fi, err := fs.Stat(cachedir)
if os.IsNotExist(err) {
if os.IsNotExist(errors.Cause(err)) {
err = fs.MkdirAll(cachedir, 0700)
if err != nil {
return "", err
@ -268,7 +268,7 @@ func getXDGCacheDir() (string, error) {
}
fi, err := fs.Stat(cachedir)
if os.IsNotExist(err) {
if os.IsNotExist(errors.Cause(err)) {
err = fs.MkdirAll(cachedir, 0700)
if err != nil {
return "", err

View file

@ -85,7 +85,7 @@ func (c *Checker) LoadIndex() (hints []error, errs []error) {
worker := func(id backend.ID, done <-chan struct{}) error {
debug.Log("LoadIndex", "worker got index %v", id)
idx, err := repository.LoadIndexWithDecoder(c.repo, id, repository.DecodeIndex)
if err == repository.ErrOldIndexFormat {
if errors.Cause(err) == repository.ErrOldIndexFormat {
debug.Log("LoadIndex", "index %v has old format", id.Str())
hints = append(hints, ErrOldIndexFormat{id})

View file

@ -14,6 +14,8 @@ import (
"strings"
"sync"
"time"
"github.com/pkg/errors"
)
type process struct {
@ -59,7 +61,7 @@ func initDebugLogger() {
}
}
if err != nil && os.IsNotExist(err) {
if err != nil && os.IsNotExist(errors.Cause(err)) {
f, err = fs.OpenFile(debugfile, os.O_WRONLY|os.O_CREATE, 0600)
}

View file

@ -6,13 +6,15 @@ import (
"os"
"syscall"
"github.com/pkg/errors"
"golang.org/x/sys/unix"
)
// Open opens a file for reading, without updating the atime and without caching data on read.
func Open(name string) (File, error) {
file, err := os.OpenFile(name, os.O_RDONLY|syscall.O_NOATIME, 0)
if os.IsPermission(err) {
if os.IsPermission(errors.Cause(err)) {
file, err = os.OpenFile(name, os.O_RDONLY, 0)
}
return &nonCachingFile{File: file}, err

View file

@ -296,7 +296,7 @@ func List(k *crypto.Key, rd io.ReaderAt, size int64) (entries []Blob, err error)
for {
e := headerEntry{}
err = binary.Read(hdrRd, binary.LittleEndian, &e)
if err == io.EOF {
if errors.Cause(err) == io.EOF {
break
}

View file

@ -126,7 +126,7 @@ func SearchKey(s *Repository, password string, maxKeys int) (*Key, error) {
debug.Log("SearchKey", "key %v returned error %v", name[:12], err)
// ErrUnauthenticated means the password is wrong, try the next key
if err == crypto.ErrUnauthenticated {
if errors.Cause(err) == crypto.ErrUnauthenticated {
continue
}

View file

@ -7,6 +7,8 @@ import (
"restic/crypto"
"restic/debug"
"restic/pack"
"github.com/pkg/errors"
)
// Repack takes a list of packs together with a list of blobs contained in
@ -22,7 +24,7 @@ func Repack(repo *Repository, packs backend.IDSet, keepBlobs pack.BlobSet) (err
h := backend.Handle{Type: backend.Data, Name: packID.String()}
l, err := repo.Backend().Load(h, buf[:cap(buf)], 0)
if err == io.ErrUnexpectedEOF {
if errors.Cause(err) == io.ErrUnexpectedEOF {
err = nil
buf = buf[:l]
}

View file

@ -403,7 +403,7 @@ func LoadIndex(repo *Repository, id backend.ID) (*Index, error) {
return idx, nil
}
if err == ErrOldIndexFormat {
if errors.Cause(err) == ErrOldIndexFormat {
fmt.Fprintf(os.Stderr, "index %v has old format\n", id.Str())
return LoadIndexWithDecoder(repo, id, DecodeOldIndex)
}

View file

@ -95,7 +95,7 @@ func (res *Restorer) restoreNodeTo(node *Node, dir string, dst string) error {
}
// Did it fail because of ENOENT?
if err != nil && os.IsNotExist(err) {
if err != nil && os.IsNotExist(errors.Cause(err)) {
debug.Log("Restorer.restoreNodeTo", "create intermediate paths")
// Create parent directories and retry

View file

@ -11,6 +11,7 @@ import (
"testing"
"time"
"github.com/pkg/errors"
"github.com/restic/chunker"
)
@ -34,7 +35,7 @@ func (fs fakeFileSystem) saveFile(rd io.Reader) (blobs backend.IDs) {
for {
chunk, err := ch.Next(getBuf())
if err == io.EOF {
if errors.Cause(err) == io.EOF {
break
}