-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathecdysis_test.go
224 lines (187 loc) · 7.53 KB
/
ecdysis_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
// Copyright © 2024 Meroxa, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//go:generate mockgen -source=ecdysis_test.go -destination=behavioral_mock_test.go -package=ecdysis -typed
package ecdysis
import (
"context"
"fmt"
"log/slog"
"testing"
"time"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/spf13/cobra"
"go.uber.org/mock/gomock"
)
type testCmd struct {
flagLongFoo string
}
var (
_ CommandWithDocs = (*testCmd)(nil)
_ CommandWithAliases = (*testCmd)(nil)
_ CommandWithFlags = (*testCmd)(nil)
_ CommandWithSubCommands = (*testCmd)(nil)
)
func (c *testCmd) Usage() string {
return "cmd1"
}
func (c *testCmd) Aliases() []string {
return []string{"foo", "bar"}
}
func (c *testCmd) Docs() Docs {
return Docs{
Short: "short-foo",
Long: "long-bar",
Example: "example-baz",
}
}
func (c *testCmd) Flags() []Flag {
return []Flag{
{Long: "long-foo", Short: "l", Usage: "test flag", Required: false, Persistent: false, Ptr: &c.flagLongFoo},
}
}
func (c *testCmd) SubCommands() []Command {
return []Command{
&subCmd{},
}
}
type subCmd struct{}
func (c *subCmd) Usage() string {
return "subCmd"
}
func TestBuildCobraCommand_Structural(t *testing.T) {
ecdysis := New()
cmd := &testCmd{}
want := &cobra.Command{
Use: "cmd1",
Aliases: []string{"foo", "bar"},
Short: "short-foo",
Long: "long-bar",
Example: "example-baz",
}
want.Flags().StringVarP(&cmd.flagLongFoo, "long-foo", "l", "", "test flag")
want.AddCommand(&cobra.Command{Use: "subCmd"})
got := ecdysis.MustBuildCobraCommand(cmd)
// Since we can't compare functions, we ignore RunE (coming from `buildCommandEvent`)
got.RunE = nil
// Since we can't compare functions, we ignore PostRunE (coming from `buildCommandAutoUpdate`)
got.PostRunE = nil
if v := cmp.Diff(got, want, cmpopts.IgnoreUnexported(cobra.Command{})); v != "" {
t.Fatal(v)
}
}
// BehavioralTestCommand is an interface out of which a mock will be generated.
type BehavioralTestCommand interface {
CommandWithArgs
CommandWithLogger
CommandWithExecute
}
func TestBuildCobraCommand_Behavioral(t *testing.T) {
ctx := context.Background()
ctrl := gomock.NewController(t)
cmd := NewMockBehavioralTestCommand(ctrl)
ctx = context.WithValue(ctx, cobraCmdCtxKey{}, cmd)
wantLogger := slog.New(slog.NewTextHandler(nil, nil))
ecdysis := New(WithDecorators(CommandWithLoggerDecorator{Logger: wantLogger}))
// When building we only expect Usage and Logger to be called.
call := cmd.EXPECT().Usage().Return("mock").Call
call = cmd.EXPECT().Logger(wantLogger).After(call)
got := ecdysis.MustBuildCobraCommand(cmd)
// Set up the remaining expectations before executing the command.
call = cmd.EXPECT().Args(gomock.Any()).Return(nil).After(call)
cmd.EXPECT().Execute(gomock.AssignableToTypeOf(ctx)).Return(nil).After(call)
err := got.ExecuteContext(ctx)
if err != nil {
t.Fatalf("not expected error, got %q", err.Error())
}
}
type testCmdWithFlags struct {
flag1 string
flag2 int
flag3 int8
flag4 int16
flag5 int32
flag6 int64
flag7 float32
flag8 float64
flag9 bool
flag10 time.Duration
flag11 []bool
flag12 []float32
flag13 []float64
flag14 []int32
flag15 []int64
flag16 []int
flag17 []string
}
var _ CommandWithFlags = (*testCmdWithFlags)(nil)
func (t *testCmdWithFlags) Usage() string {
return "testCmdWithFlags"
}
func (t *testCmdWithFlags) Flags() []Flag {
return []Flag{
{Long: "flag1", Short: "a", Usage: "flag1 usage", Required: true, Persistent: false, Ptr: &t.flag1},
{Long: "flag2", Short: "b", Usage: "flag2 usage", Required: false, Persistent: true, Ptr: &t.flag2},
{Long: "flag3", Short: "c", Usage: "flag3 usage", Required: true, Persistent: false, Ptr: &t.flag3},
{Long: "flag4", Short: "d", Usage: "flag4 usage", Required: false, Persistent: true, Ptr: &t.flag4},
{Long: "flag5", Short: "e", Usage: "flag5 usage", Required: true, Persistent: false, Ptr: &t.flag5},
{Long: "flag6", Short: "f", Usage: "flag6 usage", Required: false, Persistent: true, Ptr: &t.flag6},
{Long: "flag7", Short: "g", Usage: "flag7 usage", Required: true, Persistent: false, Ptr: &t.flag7},
{Long: "flag8", Short: "h", Usage: "flag8 usage", Required: false, Persistent: true, Ptr: &t.flag8},
{Long: "flag9", Short: "i", Usage: "flag9 usage", Required: true, Persistent: false, Ptr: &t.flag9},
{Long: "flag10", Short: "j", Usage: "flag10 usage", Required: false, Persistent: true, Ptr: &t.flag10},
{Long: "flag11", Short: "k", Usage: "flag11 usage", Required: true, Persistent: false, Ptr: &t.flag11},
{Long: "flag12", Short: "l", Usage: "flag12 usage", Required: false, Persistent: true, Ptr: &t.flag12},
{Long: "flag13", Short: "m", Usage: "flag13 usage", Required: true, Persistent: false, Ptr: &t.flag13},
{Long: "flag14", Short: "n", Usage: "flag14 usage", Required: false, Persistent: true, Ptr: &t.flag14},
{Long: "flag15", Short: "o", Usage: "flag15 usage", Required: true, Persistent: false, Ptr: &t.flag15},
{Long: "flag16", Short: "p", Usage: "flag16 usage", Required: false, Persistent: true, Ptr: &t.flag16},
{Long: "flag17", Short: "q", Usage: "flag17 usage", Required: true, Persistent: false, Ptr: &t.flag17},
}
}
func TestBuildCommandWithFlags(t *testing.T) {
ecdysis := New()
cmd := &testCmdWithFlags{}
want := &cobra.Command{Use: "testCmdWithFlags"}
want.Flags().StringVarP(&cmd.flag1, "flag1", "a", "", "flag1 usage")
want.PersistentFlags().IntVarP(&cmd.flag2, "flag2", "b", 0, "flag2 usage")
want.Flags().Int8VarP(&cmd.flag3, "flag3", "c", 0, "flag3 usage")
want.PersistentFlags().Int16VarP(&cmd.flag4, "flag4", "d", 0, "flag4 usage")
want.Flags().Int32VarP(&cmd.flag5, "flag5", "e", 0, "flag5 usage")
want.PersistentFlags().Int64VarP(&cmd.flag6, "flag6", "f", 0, "flag6 usage")
want.Flags().Float32VarP(&cmd.flag7, "flag7", "g", 0, "flag7 usage")
want.PersistentFlags().Float64VarP(&cmd.flag8, "flag8", "h", 0, "flag8 usage")
want.Flags().BoolVarP(&cmd.flag9, "flag9", "i", false, "flag9 usage")
want.PersistentFlags().DurationVarP(&cmd.flag10, "flag10", "j", 0, "flag10 usage")
want.Flags().BoolSliceVarP(&cmd.flag11, "flag11", "k", nil, "flag11 usage")
want.PersistentFlags().Float32SliceVarP(&cmd.flag12, "flag12", "l", nil, "flag12 usage")
want.Flags().Float64SliceVarP(&cmd.flag13, "flag13", "m", nil, "flag13 usage")
want.PersistentFlags().Int32SliceVarP(&cmd.flag14, "flag14", "n", nil, "flag14 usage")
want.Flags().Int64SliceVarP(&cmd.flag15, "flag15", "o", nil, "flag15 usage")
want.PersistentFlags().IntSliceVarP(&cmd.flag16, "flag16", "p", nil, "flag16 usage")
want.Flags().StringSliceVarP(&cmd.flag17, "flag17", "q", nil, "flag17 usage")
for i := 1; i <= 17; i++ {
if i%2 == 1 {
_ = want.MarkFlagRequired(fmt.Sprintf("flag%d", i))
}
}
got := ecdysis.MustBuildCobraCommand(cmd)
// Since we can't compare functions, we ignore RunE (coming from `buildCommandEvent`)
got.RunE = nil
// Since we can't compare functions, we ignore PostRunE (coming from `buildCommandAutoUpdate`)
got.PostRunE = nil
if v := cmp.Diff(got, want, cmpopts.IgnoreUnexported(cobra.Command{})); v != "" {
t.Fatal(v)
}
}