Skip to content

Commit

Permalink
Merge pull request #2641 from mtrmac/testify-benefits
Browse files Browse the repository at this point in the history
Use testify's ErrorAs to simplify tests a bit
  • Loading branch information
vrothberg authored Nov 26, 2024
2 parents b58c235 + 65cdd65 commit 07fb5bd
Showing 1 changed file with 6 additions and 13 deletions.
19 changes: 6 additions & 13 deletions docker/errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package docker
import (
"bufio"
"bytes"
"errors"
"net/http"
"testing"

Expand Down Expand Up @@ -109,8 +108,7 @@ func TestRegistryHTTPResponseToError(t *testing.T) {
errorCode: &errcode.ErrorCodeUnknown,
fn: func(t *testing.T, err error) {
var e errcode.Error
ok := errors.As(err, &e)
require.True(t, ok)
require.ErrorAs(t, err, &e)
// Note: (skopeo inspect) is checking for this errcode.Error value
assert.Equal(t, errcode.Error{
Code: errcode.ErrorCodeUnknown, // The NOT_FOUND value is not defined, and turns into Unknown
Expand Down Expand Up @@ -140,8 +138,7 @@ func TestRegistryHTTPResponseToError(t *testing.T) {
errorCode: &errcode.ErrorCodeUnknown,
fn: func(t *testing.T, err error) {
var e errcode.Error
ok := errors.As(err, &e)
require.True(t, ok)
require.ErrorAs(t, err, &e)
// isManifestUnknownError is checking for this
assert.Equal(t, errcode.Error{
Code: errcode.ErrorCodeUnknown, // The 404 value is not defined, and turns into Unknown
Expand All @@ -166,8 +163,7 @@ func TestRegistryHTTPResponseToError(t *testing.T) {
unwrappedErrorPtr: &unwrappedUnexpectedHTTPResponseError,
fn: func(t *testing.T, err error) {
var e *unexpectedHTTPResponseError
ok := errors.As(err, &e)
require.True(t, ok)
require.ErrorAs(t, err, &e)
// isManifestUnknownError is checking for this
assert.Equal(t, 404, e.StatusCode)
assert.Equal(t, []byte("Not found\r"), e.Response)
Expand All @@ -191,8 +187,7 @@ func TestRegistryHTTPResponseToError(t *testing.T) {
errorCode: &errcode.ErrorCodeUnknown,
fn: func(t *testing.T, err error) {
var e errcode.Error
ok := errors.As(err, &e)
require.True(t, ok)
require.ErrorAs(t, err, &e)
// isManifestUnknownError is checking for this
assert.Equal(t, errcode.Error{
Code: errcode.ErrorCodeUnknown, // The NOT_FOUND value is not defined, and turns into Unknown
Expand All @@ -212,13 +207,11 @@ func TestRegistryHTTPResponseToError(t *testing.T) {
assert.IsType(t, c.errorType, err, c.name)
}
if c.unwrappedErrorPtr != nil {
found := errors.As(err, c.unwrappedErrorPtr)
assert.True(t, found, c.name)
assert.ErrorAs(t, err, c.unwrappedErrorPtr, c.name)
}
if c.errorCode != nil {
var ec errcode.ErrorCoder
ok := errors.As(err, &ec)
require.True(t, ok, c.name)
require.ErrorAs(t, err, &ec, c.name)
assert.Equal(t, *c.errorCode, ec.ErrorCode(), c.name)
}
if c.fn != nil {
Expand Down

0 comments on commit 07fb5bd

Please sign in to comment.