github.com/thajeztah/cli@v0.0.0-20240223162942-dc6bfac81a8b/cli/command/plugin/enable_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 TestPluginEnableErrors(t *testing.T) {
    15  	testCases := []struct {
    16  		args             []string
    17  		flags            map[string]string
    18  		pluginEnableFunc func(name string, options types.PluginEnableOptions) error
    19  		expectedError    string
    20  	}{
    21  		{
    22  			args:          []string{},
    23  			expectedError: "requires exactly 1 argument",
    24  		},
    25  		{
    26  			args:          []string{"too-many", "arguments"},
    27  			expectedError: "requires exactly 1 argument",
    28  		},
    29  		{
    30  			args: []string{"plugin-foo"},
    31  			pluginEnableFunc: func(name string, options types.PluginEnableOptions) error {
    32  				return fmt.Errorf("failed to enable plugin")
    33  			},
    34  			expectedError: "failed to enable plugin",
    35  		},
    36  		{
    37  			args: []string{"plugin-foo"},
    38  			flags: map[string]string{
    39  				"timeout": "-1",
    40  			},
    41  			expectedError: "negative timeout -1 is invalid",
    42  		},
    43  	}
    44  
    45  	for _, tc := range testCases {
    46  		cmd := newEnableCommand(
    47  			test.NewFakeCli(&fakeClient{
    48  				pluginEnableFunc: tc.pluginEnableFunc,
    49  			}))
    50  		cmd.SetArgs(tc.args)
    51  		for key, value := range tc.flags {
    52  			cmd.Flags().Set(key, value)
    53  		}
    54  		cmd.SetOut(io.Discard)
    55  		assert.ErrorContains(t, cmd.Execute(), tc.expectedError)
    56  	}
    57  }
    58  
    59  func TestPluginEnable(t *testing.T) {
    60  	cli := test.NewFakeCli(&fakeClient{
    61  		pluginEnableFunc: func(name string, options types.PluginEnableOptions) error {
    62  			return nil
    63  		},
    64  	})
    65  
    66  	cmd := newEnableCommand(cli)
    67  	cmd.SetArgs([]string{"plugin-foo"})
    68  	assert.NilError(t, cmd.Execute())
    69  	assert.Check(t, is.Equal("plugin-foo\n", cli.OutBuffer().String()))
    70  }