github.com/thajeztah/cli@v0.0.0-20240223162942-dc6bfac81a8b/cli/command/plugin/remove_test.go (about)

     1  package plugin
     2  
     3  import (
     4  	"fmt"
     5  	"io"
     6  	"testing"
     7  
     8  	"github.com/docker/cli/internal/test"
     9  	"github.com/docker/docker/api/types"
    10  	"gotest.tools/v3/assert"
    11  	is "gotest.tools/v3/assert/cmp"
    12  )
    13  
    14  func TestRemoveErrors(t *testing.T) {
    15  	testCases := []struct {
    16  		args             []string
    17  		pluginRemoveFunc func(name string, options types.PluginRemoveOptions) error
    18  		expectedError    string
    19  	}{
    20  		{
    21  			args:          []string{},
    22  			expectedError: "requires at least 1 argument",
    23  		},
    24  		{
    25  			args: []string{"plugin-foo"},
    26  			pluginRemoveFunc: func(name string, options types.PluginRemoveOptions) error {
    27  				return fmt.Errorf("Error removing plugin")
    28  			},
    29  			expectedError: "Error removing plugin",
    30  		},
    31  	}
    32  
    33  	for _, tc := range testCases {
    34  		cli := test.NewFakeCli(&fakeClient{
    35  			pluginRemoveFunc: tc.pluginRemoveFunc,
    36  		})
    37  		cmd := newRemoveCommand(cli)
    38  		cmd.SetArgs(tc.args)
    39  		cmd.SetOut(io.Discard)
    40  		assert.ErrorContains(t, cmd.Execute(), tc.expectedError)
    41  	}
    42  }
    43  
    44  func TestRemove(t *testing.T) {
    45  	cli := test.NewFakeCli(&fakeClient{
    46  		pluginRemoveFunc: func(name string, options types.PluginRemoveOptions) error {
    47  			return nil
    48  		},
    49  	})
    50  	cmd := newRemoveCommand(cli)
    51  	cmd.SetArgs([]string{"plugin-foo"})
    52  	assert.NilError(t, cmd.Execute())
    53  	assert.Check(t, is.Equal("plugin-foo\n", cli.OutBuffer().String()))
    54  }
    55  
    56  func TestRemoveWithForceOption(t *testing.T) {
    57  	force := false
    58  	cli := test.NewFakeCli(&fakeClient{
    59  		pluginRemoveFunc: func(name string, options types.PluginRemoveOptions) error {
    60  			force = options.Force
    61  			return nil
    62  		},
    63  	})
    64  	cmd := newRemoveCommand(cli)
    65  	cmd.SetArgs([]string{"plugin-foo"})
    66  	cmd.Flags().Set("force", "true")
    67  	assert.NilError(t, cmd.Execute())
    68  	assert.Check(t, force)
    69  	assert.Check(t, is.Equal("plugin-foo\n", cli.OutBuffer().String()))
    70  }