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

Use testify's ErrorAs to simplify tests a bit #2641

Merged
merged 2 commits into from
Nov 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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