Skip to content

Commit a61163b

Browse files
committed
Fix commandName to return subcommands in CLI order
Signed-off-by: Max Proske <max@mproske.com>
1 parent 6ecb8d4 commit a61163b

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
@@ -20,7 +20,6 @@ import (
2020
"context"
2121
"errors"
2222
"fmt"
23-
"sort"
2423
"strings"
2524
"time"
2625

@@ -130,7 +129,9 @@ func commandName(cmd *cobra.Command) []string {
130129
}
131130
name = append(name, c.Name())
132131
}
133-
sort.Sort(sort.Reverse(sort.StringSlice(name)))
132+
for i, j := 0, len(name)-1; i < j; i, j = i+1, j-1 {
133+
name[i], name[j] = name[j], name[i]
134+
}
134135
return name
135136
}
136137

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 -> [alpha, watch]",
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{"alpha", "watch"},
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)