Skip to content

Commit 8e93ef7

Browse files
committed
fix: restore os.Args after plugin completion and fix error return
1 parent 419e5d1 commit 8e93ef7

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

cli-plugins/manager/cobra.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func AddPluginCommandStubs(dockerCLI config.Provider, rootCmd *cobra.Command) (e
4747
flags.SetOutput(nil)
4848
perr := flags.Parse(args)
4949
if perr != nil {
50-
return err
50+
return perr
5151
}
5252
if flags.Changed("help") {
5353
cmd.HelpFunc()(rootCmd, args)
@@ -60,7 +60,11 @@ func AddPluginCommandStubs(dockerCLI config.Provider, rootCmd *cobra.Command) (e
6060
cargs := []string{p.Path, cobra.ShellCompRequestCmd, p.Name} //nolint:prealloc // no need to over-complicate things.
6161
cargs = append(cargs, args...)
6262
cargs = append(cargs, toComplete)
63+
origArgs := os.Args
6364
os.Args = cargs
65+
defer func() {
66+
os.Args = origArgs
67+
}()
6468
runCommand, runErr := PluginRunCommand(dockerCLI, p.Name, cmd)
6569
if runErr != nil {
6670
return nil, cobra.ShellCompDirectiveError

cli-plugins/manager/cobra_test.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
package manager
22

33
import (
4+
"os"
5+
"sync"
46
"testing"
57

8+
"github.com/docker/cli/cli/config/configfile"
9+
"github.com/docker/cli/internal/test"
610
"github.com/spf13/cobra"
711
"gotest.tools/v3/assert"
12+
"gotest.tools/v3/fs"
813
)
914

1015
func TestPluginResourceAttributesEnvvar(t *testing.T) {
@@ -24,3 +29,47 @@ func TestPluginResourceAttributesEnvvar(t *testing.T) {
2429
env = appendPluginResourceAttributesEnvvar(nil, cmd, Plugin{Name: "compose"})
2530
assert.DeepEqual(t, []string{"OTEL_RESOURCE_ATTRIBUTES=a.b.c=foo,docker.cli.cobra.command_path=docker%20compose"}, env)
2631
}
32+
33+
func TestPluginStubRunEReturnsParseError(t *testing.T) {
34+
cmd := preparePluginStubCommand(t, `{"SchemaVersion":"0.1.0","Vendor":"e2e-testing"}`)
35+
36+
err := cmd.RunE(cmd, []string{"--definitely-not-a-real-flag"})
37+
assert.ErrorContains(t, err, "unknown flag: --definitely-not-a-real-flag")
38+
}
39+
40+
func TestPluginStubCompletionRestoresOSArgs(t *testing.T) {
41+
cmd := preparePluginStubCommand(t, `{"SchemaVersion":"0.1.0"}`)
42+
43+
originalArgs := []string{"docker", "image", "ls"}
44+
os.Args = append([]string(nil), originalArgs...)
45+
46+
_, directive := cmd.ValidArgsFunction(cmd, []string{"--all"}, "alp")
47+
assert.Equal(t, directive, cobra.ShellCompDirectiveError)
48+
assert.DeepEqual(t, os.Args, originalArgs)
49+
}
50+
51+
func preparePluginStubCommand(t *testing.T, metadata string) *cobra.Command {
52+
t.Helper()
53+
pluginCommandStubsOnce = sync.Once{}
54+
55+
dir := fs.NewDir(t, t.Name(),
56+
fs.WithFile("docker-testplugin", "#!/bin/sh\nprintf '%s' '"+metadata+"'\n", fs.WithMode(0o777)),
57+
)
58+
t.Cleanup(func() { dir.Remove() })
59+
60+
cli := test.NewFakeCli(nil)
61+
cli.SetConfigFile(&configfile.ConfigFile{
62+
CLIPluginsExtraDirs: []string{dir.Path()},
63+
})
64+
65+
root := &cobra.Command{Use: "docker"}
66+
root.PersistentFlags().Bool("debug", false, "")
67+
68+
err := AddPluginCommandStubs(cli, root)
69+
assert.NilError(t, err)
70+
71+
cmd, _, err := root.Find([]string{"testplugin"})
72+
assert.NilError(t, err)
73+
assert.Assert(t, cmd != nil)
74+
return cmd
75+
}

0 commit comments

Comments
 (0)