Skip to content

Commit c52c3b3

Browse files
committed
Test commandName subcommand order
Signed-off-by: Max Proske <max@mproske.com>
1 parent 6ecb8d4 commit c52c3b3

File tree

2 files changed

+52
-2
lines changed

2 files changed

+52
-2
lines changed

cmd/cmdtrace/cmd_span.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,13 +114,14 @@ func wrapRunE(c *cobra.Command, cmdSpan trace.Span, tracingShutdown tracing.Shut
114114
}
115115
}
116116

117-
// commandName returns the path components for a given command.
117+
// commandName returns the path components for a given command,
118+
// in reverse alphabetical order for consistent usage metrics.
118119
//
119120
// The root Compose command and anything before (i.e. "docker")
120121
// are not included.
121122
//
122123
// For example:
123-
// - docker compose alpha watch -> [alpha, watch]
124+
// - docker compose alpha watch -> [watch, alpha]
124125
// - docker-compose up -> [up]
125126
func commandName(cmd *cobra.Command) []string {
126127
var name []string

cmd/cmdtrace/cmd_span_test.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import (
2020
"reflect"
2121
"testing"
2222

23+
commands "github.com/docker/compose/v2/cmd/compose"
24+
"github.com/spf13/cobra"
2325
flag "github.com/spf13/pflag"
2426
)
2527

@@ -61,3 +63,50 @@ func TestGetFlags(t *testing.T) {
6163
})
6264
}
6365
}
66+
67+
func TestCommandName(t *testing.T) {
68+
tests := []struct {
69+
name string
70+
setupCmd func() *cobra.Command
71+
want []string
72+
}{
73+
{
74+
name: "docker compose alpha watch -> [watch, alpha]",
75+
setupCmd: func() *cobra.Command {
76+
dockerCmd := &cobra.Command{Use: "docker"}
77+
composeCmd := &cobra.Command{Use: commands.PluginName}
78+
alphaCmd := &cobra.Command{Use: "alpha"}
79+
watchCmd := &cobra.Command{Use: "watch"}
80+
81+
dockerCmd.AddCommand(composeCmd)
82+
composeCmd.AddCommand(alphaCmd)
83+
alphaCmd.AddCommand(watchCmd)
84+
85+
return watchCmd
86+
},
87+
want: []string{"watch", "alpha"},
88+
},
89+
{
90+
name: "docker-compose up -> [up]",
91+
setupCmd: func() *cobra.Command {
92+
dockerComposeCmd := &cobra.Command{Use: commands.PluginName}
93+
upCmd := &cobra.Command{Use: "up"}
94+
95+
dockerComposeCmd.AddCommand(upCmd)
96+
97+
return upCmd
98+
},
99+
want: []string{"up"},
100+
},
101+
}
102+
103+
for _, tt := range tests {
104+
t.Run(tt.name, func(t *testing.T) {
105+
cmd := tt.setupCmd()
106+
got := commandName(cmd)
107+
if !reflect.DeepEqual(got, tt.want) {
108+
t.Errorf("commandName() = %v, want %v", got, tt.want)
109+
}
110+
})
111+
}
112+
}

0 commit comments

Comments
 (0)