From 6029def4cb2c003d08666924c7af21fae4719d40 Mon Sep 17 00:00:00 2001 From: Aaron Lehmann Date: Thu, 13 Apr 2017 15:45:37 -0700 Subject: [PATCH] Remove pkg/testutil/assert in favor of testify I noticed that we're using a homegrown package for assertions. The functions are extremely similar to testify, but with enough slight differences to be confusing (for example, Equal takes its arguments in a different order). We already vendor testify, and it's used in a few places by tests. I also found some problems with pkg/testutil/assert. For example, the NotNil function seems to be broken. It checks the argument against "nil", which only works for an interface. If you pass in a nil map or slice, the equality check will fail. In the interest of avoiding NIH, I'm proposing replacing pkg/testutil/assert with testify. The test code looks almost the same, but we avoid the confusion of having two similar but slightly different assertion packages, and having to maintain our own package instead of using a commonly-used one. In the process, I found a few places where the tests should halt if an assertion fails, so I've made those cases (that I noticed) use "require" instead of "assert", and I've vendored the "require" package from testify alongside the already-present "assert" package. Signed-off-by: Aaron Lehmann --- daemon_none_test.go | 4 ++-- daemon_unit_test.go | 6 +++--- docker_test.go | 10 +++++----- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/daemon_none_test.go b/daemon_none_test.go index bd42add986..af0fcfd670 100644 --- a/daemon_none_test.go +++ b/daemon_none_test.go @@ -5,7 +5,7 @@ package main import ( "testing" - "github.com/docker/docker/pkg/testutil/assert" + "github.com/stretchr/testify/assert" ) func TestDaemonCommand(t *testing.T) { @@ -13,5 +13,5 @@ func TestDaemonCommand(t *testing.T) { cmd.SetArgs([]string{"--version"}) err := cmd.Execute() - assert.Error(t, err, "Please run `dockerd`") + assert.EqualError(t, err, "Please run `dockerd`") } diff --git a/daemon_unit_test.go b/daemon_unit_test.go index 26348a8843..ffd8a5e2f5 100644 --- a/daemon_unit_test.go +++ b/daemon_unit_test.go @@ -5,8 +5,8 @@ package main import ( "testing" - "github.com/docker/docker/pkg/testutil/assert" "github.com/spf13/cobra" + "github.com/stretchr/testify/assert" ) func stubRun(cmd *cobra.Command, args []string) error { @@ -18,7 +18,7 @@ func TestDaemonCommandHelp(t *testing.T) { cmd.RunE = stubRun cmd.SetArgs([]string{"--help"}) err := cmd.Execute() - assert.NilError(t, err) + assert.NoError(t, err) } func TestDaemonCommand(t *testing.T) { @@ -26,5 +26,5 @@ func TestDaemonCommand(t *testing.T) { cmd.RunE = stubRun cmd.SetArgs([]string{"--containerd", "/foo"}) err := cmd.Execute() - assert.NilError(t, err) + assert.NoError(t, err) } diff --git a/docker_test.go b/docker_test.go index f8a5297ed4..88afb41e9e 100644 --- a/docker_test.go +++ b/docker_test.go @@ -8,7 +8,7 @@ import ( "github.com/Sirupsen/logrus" "github.com/docker/docker/cli/command" "github.com/docker/docker/cli/debug" - "github.com/docker/docker/pkg/testutil/assert" + "github.com/stretchr/testify/assert" ) func TestClientDebugEnabled(t *testing.T) { @@ -18,9 +18,9 @@ func TestClientDebugEnabled(t *testing.T) { cmd.Flags().Set("debug", "true") err := cmd.PersistentPreRunE(cmd, []string{}) - assert.NilError(t, err) - assert.Equal(t, os.Getenv("DEBUG"), "1") - assert.Equal(t, logrus.GetLevel(), logrus.DebugLevel) + assert.NoError(t, err) + assert.Equal(t, "1", os.Getenv("DEBUG")) + assert.Equal(t, logrus.DebugLevel, logrus.GetLevel()) } func TestExitStatusForInvalidSubcommandWithHelpFlag(t *testing.T) { @@ -28,5 +28,5 @@ func TestExitStatusForInvalidSubcommandWithHelpFlag(t *testing.T) { cmd := newDockerCommand(command.NewDockerCli(os.Stdin, discard, discard)) cmd.SetArgs([]string{"help", "invalid"}) err := cmd.Execute() - assert.Error(t, err, "unknown help topic: invalid") + assert.EqualError(t, err, "unknown help topic: invalid") }