Skip to content

Commit

Permalink
Use testify's ErrorAs to simplify tests a bit
Browse files Browse the repository at this point in the history
Should not change (test) behavior, other than perhaps
better test failure output.

Signed-off-by: Miloslav Trmač <[email protected]>
  • Loading branch information
mtrmac committed Nov 25, 2024
1 parent 17f0ec3 commit 4236939
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 4236939

Please sign in to comment.