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 <aaron.lehmann@docker.com>
This commit is contained in:
Aaron Lehmann 2017-04-13 15:45:37 -07:00
parent db8f631c1f
commit 6029def4cb
3 changed files with 10 additions and 10 deletions

View File

@ -5,7 +5,7 @@ package main
import ( import (
"testing" "testing"
"github.com/docker/docker/pkg/testutil/assert" "github.com/stretchr/testify/assert"
) )
func TestDaemonCommand(t *testing.T) { func TestDaemonCommand(t *testing.T) {
@ -13,5 +13,5 @@ func TestDaemonCommand(t *testing.T) {
cmd.SetArgs([]string{"--version"}) cmd.SetArgs([]string{"--version"})
err := cmd.Execute() err := cmd.Execute()
assert.Error(t, err, "Please run `dockerd`") assert.EqualError(t, err, "Please run `dockerd`")
} }

View File

@ -5,8 +5,8 @@ package main
import ( import (
"testing" "testing"
"github.com/docker/docker/pkg/testutil/assert"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
) )
func stubRun(cmd *cobra.Command, args []string) error { func stubRun(cmd *cobra.Command, args []string) error {
@ -18,7 +18,7 @@ func TestDaemonCommandHelp(t *testing.T) {
cmd.RunE = stubRun cmd.RunE = stubRun
cmd.SetArgs([]string{"--help"}) cmd.SetArgs([]string{"--help"})
err := cmd.Execute() err := cmd.Execute()
assert.NilError(t, err) assert.NoError(t, err)
} }
func TestDaemonCommand(t *testing.T) { func TestDaemonCommand(t *testing.T) {
@ -26,5 +26,5 @@ func TestDaemonCommand(t *testing.T) {
cmd.RunE = stubRun cmd.RunE = stubRun
cmd.SetArgs([]string{"--containerd", "/foo"}) cmd.SetArgs([]string{"--containerd", "/foo"})
err := cmd.Execute() err := cmd.Execute()
assert.NilError(t, err) assert.NoError(t, err)
} }

View File

@ -8,7 +8,7 @@ import (
"github.com/Sirupsen/logrus" "github.com/Sirupsen/logrus"
"github.com/docker/docker/cli/command" "github.com/docker/docker/cli/command"
"github.com/docker/docker/cli/debug" "github.com/docker/docker/cli/debug"
"github.com/docker/docker/pkg/testutil/assert" "github.com/stretchr/testify/assert"
) )
func TestClientDebugEnabled(t *testing.T) { func TestClientDebugEnabled(t *testing.T) {
@ -18,9 +18,9 @@ func TestClientDebugEnabled(t *testing.T) {
cmd.Flags().Set("debug", "true") cmd.Flags().Set("debug", "true")
err := cmd.PersistentPreRunE(cmd, []string{}) err := cmd.PersistentPreRunE(cmd, []string{})
assert.NilError(t, err) assert.NoError(t, err)
assert.Equal(t, os.Getenv("DEBUG"), "1") assert.Equal(t, "1", os.Getenv("DEBUG"))
assert.Equal(t, logrus.GetLevel(), logrus.DebugLevel) assert.Equal(t, logrus.DebugLevel, logrus.GetLevel())
} }
func TestExitStatusForInvalidSubcommandWithHelpFlag(t *testing.T) { func TestExitStatusForInvalidSubcommandWithHelpFlag(t *testing.T) {
@ -28,5 +28,5 @@ func TestExitStatusForInvalidSubcommandWithHelpFlag(t *testing.T) {
cmd := newDockerCommand(command.NewDockerCli(os.Stdin, discard, discard)) cmd := newDockerCommand(command.NewDockerCli(os.Stdin, discard, discard))
cmd.SetArgs([]string{"help", "invalid"}) cmd.SetArgs([]string{"help", "invalid"})
err := cmd.Execute() err := cmd.Execute()
assert.Error(t, err, "unknown help topic: invalid") assert.EqualError(t, err, "unknown help topic: invalid")
} }