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

Replace deprecated usages of assert.ErrorType #5312

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
6 changes: 3 additions & 3 deletions cli-plugins/manager/candidate_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package manager

import (
"errors"
"fmt"
"reflect"
"strings"
"testing"

"github.com/spf13/cobra"
"gotest.tools/v3/assert"
"gotest.tools/v3/assert/cmp"
)

type fakeCandidate struct {
Expand Down Expand Up @@ -80,7 +79,8 @@ func TestValidateCandidate(t *testing.T) {
assert.ErrorContains(t, err, tc.err)
case tc.invalid != "":
assert.NilError(t, err)
assert.Assert(t, cmp.ErrorType(p.Err, reflect.TypeOf(&pluginError{})))
var expectedError *pluginError
assert.Check(t, errors.As(p.Err, &expectedError))
Comment on lines +82 to +83
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh! I recall some fun cases where there's no good replacement, or at least errors.As / errors.Is for sure has a bunch of pitfalls; see the discussion I had with the gotest.tools authors on gotestyourself/gotest.tools#272

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed. This works, but the errors.As "pointer-to-pointer" stuff is.. not nice to use.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's really horrible, and very easy to get wrong (in which case, it's even possible to get the unexpected result). I honestly really hate the errors.As / errors.Is handling for that reason.

assert.ErrorContains(t, p.Err, tc.invalid)
default:
assert.NilError(t, err)
Expand Down
3 changes: 1 addition & 2 deletions cli/command/context/remove_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"github.com/docker/cli/cli/config/configfile"
"github.com/docker/docker/errdefs"
"gotest.tools/v3/assert"
is "gotest.tools/v3/assert/cmp"
)

func TestRemove(t *testing.T) {
Expand All @@ -18,7 +17,7 @@ func TestRemove(t *testing.T) {
_, err := cli.ContextStore().GetMetadata("current")
assert.NilError(t, err)
_, err = cli.ContextStore().GetMetadata("other")
assert.Check(t, is.ErrorType(err, errdefs.IsNotFound))
assert.Check(t, errdefs.IsNotFound(err))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To keep the current (more informative) output of is.ErrorType, we should probably use the 3rd argument of assert.Check to write a custom error message (wanted %T, actual %T or something along those lines), but it will definitely make the code a lot more lengthy/verbose 😞

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh, yeah I just saw you mention the nicer output (I definitely wondered why we were using Errortype(err, errdefs.IsXxx)) in the thread with the gotest.tools folks.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we want to continue using the is.ErrorType, I think Daniel was open to un-deprecating it (IIUC, reason for deprecating was mostly cleaning up ("stdlib now has utilities for it, and the alternative can work, so let's deprecate"). We can open a PR to do so, and see if they're ok with it.

}

func TestRemoveNotAContext(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion cli/command/context/use_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func TestUse(t *testing.T) {
func TestUseNoExist(t *testing.T) {
cli := makeFakeCli(t)
err := newUseCommand(cli).RunE(nil, []string{"test"})
assert.Check(t, is.ErrorType(err, errdefs.IsNotFound))
assert.Check(t, errdefs.IsNotFound(err))
}

// TestUseDefaultWithoutConfigFile verifies that the CLI does not create
Expand Down
7 changes: 3 additions & 4 deletions cli/command/defaultcontextstore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"github.com/docker/docker/errdefs"
"github.com/docker/go-connections/tlsconfig"
"gotest.tools/v3/assert"
is "gotest.tools/v3/assert/cmp"
"gotest.tools/v3/golden"
)

Expand Down Expand Up @@ -158,21 +157,21 @@ func TestErrCreateDefault(t *testing.T) {
Metadata: testContext{Bar: "baz"},
Name: "default",
})
assert.Check(t, is.ErrorType(err, errdefs.IsInvalidParameter))
assert.Check(t, errdefs.IsInvalidParameter(err))
assert.Error(t, err, "default context cannot be created nor updated")
}

func TestErrRemoveDefault(t *testing.T) {
meta := testDefaultMetadata()
s := testStore(t, meta, store.ContextTLSData{})
err := s.Remove("default")
assert.Check(t, is.ErrorType(err, errdefs.IsInvalidParameter))
assert.Check(t, errdefs.IsInvalidParameter(err))
assert.Error(t, err, "default context cannot be removed")
}

func TestErrTLSDataError(t *testing.T) {
meta := testDefaultMetadata()
s := testStore(t, meta, store.ContextTLSData{})
_, err := s.GetTLSData("default", "noop", "noop")
assert.Check(t, is.ErrorType(err, errdefs.IsNotFound))
assert.Check(t, errdefs.IsNotFound(err))
}
4 changes: 3 additions & 1 deletion cli/compose/loader/loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package loader

import (
"bytes"
"errors"
"os"
"runtime"
"sort"
Expand Down Expand Up @@ -878,7 +879,8 @@ services:
service: foo
`)

assert.ErrorType(t, err, &ForbiddenPropertiesError{})
var expectedErr *ForbiddenPropertiesError
assert.Check(t, errors.As(err, &expectedErr))

props := err.(*ForbiddenPropertiesError).Properties
assert.Check(t, is.Len(props, 2))
Expand Down
4 changes: 3 additions & 1 deletion cli/compose/template/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package template

import (
"errors"
"fmt"
"testing"

Expand Down Expand Up @@ -128,7 +129,8 @@ func TestMandatoryVariableErrors(t *testing.T) {
for _, tc := range testCases {
_, err := Substitute(tc.template, defaultMapping)
assert.Check(t, is.ErrorContains(err, tc.expectedError))
assert.Check(t, is.ErrorType(err, &InvalidTemplateError{}))
var expectedError *InvalidTemplateError
assert.Check(t, errors.As(err, &expectedError))
}
}

Expand Down
4 changes: 2 additions & 2 deletions cli/context/store/metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func testMetadata(name string) Metadata {
func TestMetadataGetNotExisting(t *testing.T) {
testee := metadataStore{root: t.TempDir(), config: testCfg}
_, err := testee.get("noexist")
assert.ErrorType(t, err, errdefs.IsNotFound)
assert.Check(t, errdefs.IsNotFound(err))
}

func TestMetadataCreateGetRemove(t *testing.T) {
Expand Down Expand Up @@ -60,7 +60,7 @@ func TestMetadataCreateGetRemove(t *testing.T) {
assert.NilError(t, testee.remove("test-context"))
assert.NilError(t, testee.remove("test-context")) // support duplicate remove
_, err = testee.get("test-context")
assert.ErrorType(t, err, errdefs.IsNotFound)
assert.Check(t, errdefs.IsNotFound(err))
}

func TestMetadataRespectJsonAnnotation(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions cli/context/store/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func TestRemove(t *testing.T) {
}))
assert.NilError(t, s.Remove("source"))
_, err = s.GetMetadata("source")
assert.Check(t, is.ErrorType(err, errdefs.IsNotFound))
assert.Check(t, errdefs.IsNotFound(err))
f, err := s.ListTLSFiles("source")
assert.NilError(t, err)
assert.Equal(t, 0, len(f))
Expand All @@ -122,7 +122,7 @@ func TestListEmptyStore(t *testing.T) {
func TestErrHasCorrectContext(t *testing.T) {
_, err := New(t.TempDir(), testCfg).GetMetadata("no-exists")
assert.ErrorContains(t, err, "no-exists")
assert.Check(t, is.ErrorType(err, errdefs.IsNotFound))
assert.Check(t, errdefs.IsNotFound(err))
}

func TestDetectImportContentType(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions cli/context/store/tlsstore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func TestTlsCreateUpdateGetRemove(t *testing.T) {
const contextName = "test-ctx"

_, err := testee.getData(contextName, "test-ep", "test-data")
assert.ErrorType(t, err, errdefs.IsNotFound)
assert.Check(t, errdefs.IsNotFound(err))

err = testee.createOrUpdate(contextName, "test-ep", "test-data", []byte("data"))
assert.NilError(t, err)
Expand All @@ -29,7 +29,7 @@ func TestTlsCreateUpdateGetRemove(t *testing.T) {
err = testee.removeEndpoint(contextName, "test-ep")
assert.NilError(t, err)
_, err = testee.getData(contextName, "test-ep", "test-data")
assert.ErrorType(t, err, errdefs.IsNotFound)
assert.Check(t, errdefs.IsNotFound(err))
}

func TestTlsListAndBatchRemove(t *testing.T) {
Expand Down
Loading