From 75f7bfedf8b856b55748b0dfe6e0f630092ca0b1 Mon Sep 17 00:00:00 2001 From: Daniel Nephin Date: Wed, 16 Aug 2017 12:19:53 -0400 Subject: [PATCH] Update image command tests to use the new golden Signed-off-by: Daniel Nephin --- cli/command/image/build_test.go | 2 +- cli/command/image/history_test.go | 9 +++------ cli/command/image/import_test.go | 7 ++----- cli/command/image/inspect_test.go | 14 +++++--------- cli/command/image/list_test.go | 10 +++------- cli/command/image/load_test.go | 11 ++++------- cli/command/image/prune_test.go | 18 ++++++------------ cli/command/image/pull_test.go | 6 ++---- cli/command/image/remove_test.go | 12 +++++------- cli/command/image/save_test.go | 5 ++--- 10 files changed, 33 insertions(+), 61 deletions(-) diff --git a/cli/command/image/build_test.go b/cli/command/image/build_test.go index c874e90965..3664b91b7c 100644 --- a/cli/command/image/build_test.go +++ b/cli/command/image/build_test.go @@ -73,7 +73,7 @@ func TestRunBuildDockerfileFromStdinWithCompress(t *testing.T) { // starting with `github.com/` are special-cased, and the build command attempts // to clone the remote repo. func TestRunBuildFromGitHubSpecialCase(t *testing.T) { - cmd := NewBuildCommand(&command.DockerCli{}) + cmd := NewBuildCommand(test.NewFakeCli(nil)) cmd.SetArgs([]string{"github.com/docker/no-such-repository"}) cmd.SetOutput(ioutil.Discard) err := cmd.Execute() diff --git a/cli/command/image/history_test.go b/cli/command/image/history_test.go index 59c79f573e..7053735aa3 100644 --- a/cli/command/image/history_test.go +++ b/cli/command/image/history_test.go @@ -3,14 +3,13 @@ package image import ( "fmt" "io/ioutil" - "regexp" "testing" "time" "github.com/docker/cli/cli/internal/test" "github.com/docker/docker/api/types/image" "github.com/docker/docker/pkg/testutil" - "github.com/docker/docker/pkg/testutil/golden" + "github.com/gotestyourself/gotestyourself/golden" "github.com/pkg/errors" "github.com/stretchr/testify/assert" ) @@ -96,11 +95,9 @@ func TestNewHistoryCommandSuccess(t *testing.T) { assert.NoError(t, err) actual := cli.OutBuffer().String() if tc.outputRegex == "" { - expected := string(golden.Get(t, []byte(actual), fmt.Sprintf("history-command-success.%s.golden", tc.name))[:]) - testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, expected) + golden.Assert(t, actual, fmt.Sprintf("history-command-success.%s.golden", tc.name)) } else { - match, _ := regexp.MatchString(tc.outputRegex, actual) - assert.True(t, match) + assert.Regexp(t, tc.outputRegex, actual) } } } diff --git a/cli/command/image/import_test.go b/cli/command/image/import_test.go index 6fda4d6cf1..87cc89693c 100644 --- a/cli/command/image/import_test.go +++ b/cli/command/image/import_test.go @@ -1,7 +1,6 @@ package image import ( - "bytes" "io" "io/ioutil" "strings" @@ -36,8 +35,7 @@ func TestNewImportCommandErrors(t *testing.T) { }, } for _, tc := range testCases { - buf := new(bytes.Buffer) - cmd := NewImportCommand(test.NewFakeCliWithOutput(&fakeClient{imageImportFunc: tc.imageImportFunc}, buf)) + cmd := NewImportCommand(test.NewFakeCli(&fakeClient{imageImportFunc: tc.imageImportFunc})) cmd.SetOutput(ioutil.Discard) cmd.SetArgs(tc.args) testutil.ErrorContains(t, cmd.Execute(), tc.expectedError) @@ -91,8 +89,7 @@ func TestNewImportCommandSuccess(t *testing.T) { }, } for _, tc := range testCases { - buf := new(bytes.Buffer) - cmd := NewImportCommand(test.NewFakeCliWithOutput(&fakeClient{imageImportFunc: tc.imageImportFunc}, buf)) + cmd := NewImportCommand(test.NewFakeCli(&fakeClient{imageImportFunc: tc.imageImportFunc})) cmd.SetOutput(ioutil.Discard) cmd.SetArgs(tc.args) assert.NoError(t, cmd.Execute()) diff --git a/cli/command/image/inspect_test.go b/cli/command/image/inspect_test.go index acf80f3782..0bcb65836c 100644 --- a/cli/command/image/inspect_test.go +++ b/cli/command/image/inspect_test.go @@ -1,7 +1,6 @@ package image import ( - "bytes" "fmt" "io/ioutil" "testing" @@ -9,7 +8,7 @@ import ( "github.com/docker/cli/cli/internal/test" "github.com/docker/docker/api/types" "github.com/docker/docker/pkg/testutil" - "github.com/docker/docker/pkg/testutil/golden" + "github.com/gotestyourself/gotestyourself/golden" "github.com/stretchr/testify/assert" ) @@ -26,8 +25,7 @@ func TestNewInspectCommandErrors(t *testing.T) { }, } for _, tc := range testCases { - buf := new(bytes.Buffer) - cmd := newInspectCommand(test.NewFakeCliWithOutput(&fakeClient{}, buf)) + cmd := newInspectCommand(test.NewFakeCli(&fakeClient{})) cmd.SetOutput(ioutil.Discard) cmd.SetArgs(tc.args) testutil.ErrorContains(t, cmd.Execute(), tc.expectedError) @@ -78,15 +76,13 @@ func TestNewInspectCommandSuccess(t *testing.T) { } for _, tc := range testCases { imageInspectInvocationCount = 0 - buf := new(bytes.Buffer) - cmd := newInspectCommand(test.NewFakeCliWithOutput(&fakeClient{imageInspectFunc: tc.imageInspectFunc}, buf)) + cli := test.NewFakeCli(&fakeClient{imageInspectFunc: tc.imageInspectFunc}) + cmd := newInspectCommand(cli) cmd.SetOutput(ioutil.Discard) cmd.SetArgs(tc.args) err := cmd.Execute() assert.NoError(t, err) - actual := buf.String() - expected := string(golden.Get(t, []byte(actual), fmt.Sprintf("inspect-command-success.%s.golden", tc.name))[:]) - testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, expected) + golden.Assert(t, cli.OutBuffer().String(), fmt.Sprintf("inspect-command-success.%s.golden", tc.name)) assert.Equal(t, imageInspectInvocationCount, tc.imageCount) } } diff --git a/cli/command/image/list_test.go b/cli/command/image/list_test.go index 5ae1e47e8d..58b7a71bdb 100644 --- a/cli/command/image/list_test.go +++ b/cli/command/image/list_test.go @@ -1,7 +1,6 @@ package image import ( - "bytes" "fmt" "io/ioutil" "testing" @@ -10,7 +9,7 @@ import ( "github.com/docker/cli/cli/internal/test" "github.com/docker/docker/api/types" "github.com/docker/docker/pkg/testutil" - "github.com/docker/docker/pkg/testutil/golden" + "github.com/gotestyourself/gotestyourself/golden" "github.com/pkg/errors" "github.com/stretchr/testify/assert" ) @@ -80,17 +79,14 @@ func TestNewImagesCommandSuccess(t *testing.T) { }, } for _, tc := range testCases { - buf := new(bytes.Buffer) - cli := test.NewFakeCliWithOutput(&fakeClient{imageListFunc: tc.imageListFunc}, buf) + cli := test.NewFakeCli(&fakeClient{imageListFunc: tc.imageListFunc}) cli.SetConfigFile(&configfile.ConfigFile{ImagesFormat: tc.imageFormat}) cmd := NewImagesCommand(cli) cmd.SetOutput(ioutil.Discard) cmd.SetArgs(tc.args) err := cmd.Execute() assert.NoError(t, err) - actual := buf.String() - expected := string(golden.Get(t, []byte(actual), fmt.Sprintf("list-command-success.%s.golden", tc.name))[:]) - testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, expected) + golden.Assert(t, cli.OutBuffer().String(), fmt.Sprintf("list-command-success.%s.golden", tc.name)) } } diff --git a/cli/command/image/load_test.go b/cli/command/image/load_test.go index 519604d096..bebe40cb07 100644 --- a/cli/command/image/load_test.go +++ b/cli/command/image/load_test.go @@ -1,7 +1,6 @@ package image import ( - "bytes" "fmt" "io" "io/ioutil" @@ -11,7 +10,7 @@ import ( "github.com/docker/cli/cli/internal/test" "github.com/docker/docker/api/types" "github.com/docker/docker/pkg/testutil" - "github.com/docker/docker/pkg/testutil/golden" + "github.com/gotestyourself/gotestyourself/golden" "github.com/pkg/errors" "github.com/stretchr/testify/assert" ) @@ -92,14 +91,12 @@ func TestNewLoadCommandSuccess(t *testing.T) { }, } for _, tc := range testCases { - buf := new(bytes.Buffer) - cmd := NewLoadCommand(test.NewFakeCliWithOutput(&fakeClient{imageLoadFunc: tc.imageLoadFunc}, buf)) + cli := test.NewFakeCli(&fakeClient{imageLoadFunc: tc.imageLoadFunc}) + cmd := NewLoadCommand(cli) cmd.SetOutput(ioutil.Discard) cmd.SetArgs(tc.args) err := cmd.Execute() assert.NoError(t, err) - actual := buf.String() - expected := string(golden.Get(t, []byte(actual), fmt.Sprintf("load-command-success.%s.golden", tc.name))[:]) - testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, expected) + golden.Assert(t, cli.OutBuffer().String(), fmt.Sprintf("load-command-success.%s.golden", tc.name)) } } diff --git a/cli/command/image/prune_test.go b/cli/command/image/prune_test.go index 7f320c7a94..b52601608f 100644 --- a/cli/command/image/prune_test.go +++ b/cli/command/image/prune_test.go @@ -1,7 +1,6 @@ package image import ( - "bytes" "fmt" "io/ioutil" "testing" @@ -10,7 +9,7 @@ import ( "github.com/docker/docker/api/types" "github.com/docker/docker/api/types/filters" "github.com/docker/docker/pkg/testutil" - "github.com/docker/docker/pkg/testutil/golden" + "github.com/gotestyourself/gotestyourself/golden" "github.com/pkg/errors" "github.com/stretchr/testify/assert" ) @@ -37,10 +36,9 @@ func TestNewPruneCommandErrors(t *testing.T) { }, } for _, tc := range testCases { - buf := new(bytes.Buffer) - cmd := NewPruneCommand(test.NewFakeCliWithOutput(&fakeClient{ + cmd := NewPruneCommand(test.NewFakeCli(&fakeClient{ imagesPruneFunc: tc.imagesPruneFunc, - }, buf)) + })) cmd.SetOutput(ioutil.Discard) cmd.SetArgs(tc.args) testutil.ErrorContains(t, cmd.Execute(), tc.expectedError) @@ -85,16 +83,12 @@ func TestNewPruneCommandSuccess(t *testing.T) { }, } for _, tc := range testCases { - buf := new(bytes.Buffer) - cmd := NewPruneCommand(test.NewFakeCliWithOutput(&fakeClient{ - imagesPruneFunc: tc.imagesPruneFunc, - }, buf)) + cli := test.NewFakeCli(&fakeClient{imagesPruneFunc: tc.imagesPruneFunc}) + cmd := NewPruneCommand(cli) cmd.SetOutput(ioutil.Discard) cmd.SetArgs(tc.args) err := cmd.Execute() assert.NoError(t, err) - actual := buf.String() - expected := string(golden.Get(t, []byte(actual), fmt.Sprintf("prune-command-success.%s.golden", tc.name))[:]) - testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, expected) + golden.Assert(t, cli.OutBuffer().String(), fmt.Sprintf("prune-command-success.%s.golden", tc.name)) } } diff --git a/cli/command/image/pull_test.go b/cli/command/image/pull_test.go index 71d258323a..7b24cdbb08 100644 --- a/cli/command/image/pull_test.go +++ b/cli/command/image/pull_test.go @@ -7,7 +7,7 @@ import ( "github.com/docker/cli/cli/internal/test" "github.com/docker/docker/pkg/testutil" - "github.com/docker/docker/pkg/testutil/golden" + "github.com/gotestyourself/gotestyourself/golden" "github.com/stretchr/testify/assert" ) @@ -68,8 +68,6 @@ func TestNewPullCommandSuccess(t *testing.T) { cmd.SetArgs(tc.args) err := cmd.Execute() assert.NoError(t, err) - actual := cli.OutBuffer().String() - expected := string(golden.Get(t, []byte(actual), fmt.Sprintf("pull-command-success.%s.golden", tc.name))[:]) - testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, expected) + golden.Assert(t, cli.OutBuffer().String(), fmt.Sprintf("pull-command-success.%s.golden", tc.name)) } } diff --git a/cli/command/image/remove_test.go b/cli/command/image/remove_test.go index f88bc88520..a813f8f107 100644 --- a/cli/command/image/remove_test.go +++ b/cli/command/image/remove_test.go @@ -8,7 +8,7 @@ import ( "github.com/docker/cli/cli/internal/test" "github.com/docker/docker/api/types" "github.com/docker/docker/pkg/testutil" - "github.com/docker/docker/pkg/testutil/golden" + "github.com/gotestyourself/gotestyourself/golden" "github.com/pkg/errors" "github.com/stretchr/testify/assert" ) @@ -96,16 +96,14 @@ func TestNewRemoveCommandSuccess(t *testing.T) { }, } for _, tc := range testCases { - fakeCli := test.NewFakeCli(&fakeClient{imageRemoveFunc: tc.imageRemoveFunc}) - cmd := NewRemoveCommand(fakeCli) + cli := test.NewFakeCli(&fakeClient{imageRemoveFunc: tc.imageRemoveFunc}) + cmd := NewRemoveCommand(cli) cmd.SetOutput(ioutil.Discard) cmd.SetArgs(tc.args) assert.NoError(t, cmd.Execute()) if tc.expectedErrMsg != "" { - assert.Equal(t, tc.expectedErrMsg, fakeCli.ErrBuffer().String()) + assert.Equal(t, tc.expectedErrMsg, cli.ErrBuffer().String()) } - actual := fakeCli.OutBuffer().String() - expected := string(golden.Get(t, []byte(actual), fmt.Sprintf("remove-command-success.%s.golden", tc.name))[:]) - testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, expected) + golden.Assert(t, cli.OutBuffer().String(), fmt.Sprintf("remove-command-success.%s.golden", tc.name)) } } diff --git a/cli/command/image/save_test.go b/cli/command/image/save_test.go index 84dee142d8..424c2c9a42 100644 --- a/cli/command/image/save_test.go +++ b/cli/command/image/save_test.go @@ -1,7 +1,6 @@ package image import ( - "bytes" "io" "io/ioutil" "os" @@ -90,11 +89,11 @@ func TestNewSaveCommandSuccess(t *testing.T) { }, } for _, tc := range testCases { - cmd := NewSaveCommand(test.NewFakeCliWithOutput(&fakeClient{ + cmd := NewSaveCommand(test.NewFakeCli(&fakeClient{ imageSaveFunc: func(images []string) (io.ReadCloser, error) { return ioutil.NopCloser(strings.NewReader("")), nil }, - }, new(bytes.Buffer))) + })) cmd.SetOutput(ioutil.Discard) cmd.SetArgs(tc.args) assert.NoError(t, cmd.Execute())