11package manager
22
33import (
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
1015func 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\n printf '%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