Skip to content

Commit c237d4c

Browse files
committed
Changing one of cli tests to be more explicit (cadence-workflow#7211)
In order to migrate to urfave v3 tests should call .Run explicitely - with command line arguments. There is no cli.NewContext() anymore. I think it is even better to have a whole command-line instead of []string - then one can easily take this usage.
1 parent eb2a6b1 commit c237d4c

File tree

4 files changed

+82
-45
lines changed

4 files changed

+82
-45
lines changed

tools/cli/admin_async_queue_commands_test.go

Lines changed: 14 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,15 @@
2323
package cli
2424

2525
import (
26-
"flag"
2726
"fmt"
2827
"testing"
2928

3029
"github.com/stretchr/testify/assert"
31-
"github.com/urfave/cli/v2"
3230
"go.uber.org/mock/gomock"
3331

3432
"github.com/uber/cadence/client/admin"
3533
"github.com/uber/cadence/common/types"
34+
"github.com/uber/cadence/tools/cli/clitest"
3635
)
3736

3837
func TestAdminGetAsyncWFConfig(t *testing.T) {
@@ -44,7 +43,7 @@ func TestAdminGetAsyncWFConfig(t *testing.T) {
4443
setupMocks func(*admin.MockClient)
4544
expectedError string
4645
expectedStr string
47-
flagDomain string
46+
cmdline string
4847
mockDepsError error
4948
mockContextError error
5049
}{
@@ -64,15 +63,15 @@ func TestAdminGetAsyncWFConfig(t *testing.T) {
6463
},
6564
expectedError: "",
6665
expectedStr: "PredefinedQueueName",
67-
flagDomain: "test-domain",
66+
cmdline: "cadence --domain test-domain admin async-wf-queue get",
6867
},
6968
{
7069
name: "Required flag not present",
7170
setupMocks: func(client *admin.MockClient) {
7271
// No call to the mock admin client is expected
7372
},
7473
expectedError: "Required flag not present:",
75-
flagDomain: "",
74+
cmdline: "cadence admin async-wf-queue get", // --domain is missing
7675
},
7776
{
7877
name: "Config not found (resp.Configuration == nil)",
@@ -85,7 +84,7 @@ func TestAdminGetAsyncWFConfig(t *testing.T) {
8584
Times(1)
8685
},
8786
expectedError: "",
88-
flagDomain: "test-domain",
87+
cmdline: "cadence --domain test-domain admin async-wf-queue get",
8988
},
9089
{
9190
name: "Failed to get async wf config",
@@ -96,7 +95,7 @@ func TestAdminGetAsyncWFConfig(t *testing.T) {
9695
Times(1)
9796
},
9897
expectedError: "Failed to get async wf queue config",
99-
flagDomain: "test-domain",
98+
cmdline: "cadence --domain test-domain admin async-wf-queue get",
10099
},
101100
}
102101

@@ -114,15 +113,7 @@ func TestAdminGetAsyncWFConfig(t *testing.T) {
114113
serverAdminClient: adminClient,
115114
}, WithIOHandler(ioHandler))
116115

117-
// Set up CLI context with flags
118-
set := flag.NewFlagSet("test", 0)
119-
set.String(FlagDomain, tt.flagDomain, "Domain flag")
120-
c := cli.NewContext(app, set, nil)
121-
122-
// Call the function under test
123-
err := AdminGetAsyncWFConfig(c)
124-
125-
// Check the expected outcome
116+
err := clitest.RunCommandLine(t, app, tt.cmdline)
126117
if tt.expectedError != "" {
127118
assert.Error(t, err)
128119
assert.Contains(t, err.Error(), tt.expectedError)
@@ -143,8 +134,7 @@ func TestAdminUpdateAsyncWFConfig(t *testing.T) {
143134
name string
144135
setupMocks func(*admin.MockClient)
145136
expectedError string
146-
flagDomain string
147-
flagJSON string
137+
cmdline string
148138
mockContextError error
149139
unmarshalError error
150140
}{
@@ -157,35 +147,31 @@ func TestAdminUpdateAsyncWFConfig(t *testing.T) {
157147
Times(1)
158148
},
159149
expectedError: "",
160-
flagDomain: "test-domain",
161-
flagJSON: `{"Enabled": true}`,
150+
cmdline: `cadence --domain test-domain admin async-wf-queue update --json '{"Enabled": true}'`,
162151
},
163152
{
164153
name: "Required flag not present for domain",
165154
setupMocks: func(client *admin.MockClient) {
166155
// No call to the mock admin client is expected
167156
},
168157
expectedError: "Required flag not present:",
169-
flagDomain: "",
170-
flagJSON: `{"Enabled": true}`,
158+
cmdline: `cadence admin async-wf-queue update --json '{"Enabled": true}'`, // --domain is missing
171159
},
172160
{
173161
name: "Required flag not present for JSON",
174162
setupMocks: func(client *admin.MockClient) {
175163
// No call to the mock admin client is expected
176164
},
177165
expectedError: "Required flag not present:",
178-
flagDomain: "test-domain",
179-
flagJSON: "",
166+
cmdline: `cadence --domain test-domain admin async-wf-queue update --json ""`, // empty --json flag
180167
},
181168
{
182169
name: "Failed to parse async workflow config",
183170
setupMocks: func(client *admin.MockClient) {
184171
// No call setup for this test case as JSON parsing fails
185172
},
186173
expectedError: "Failed to parse async workflow config",
187-
flagDomain: "test-domain",
188-
flagJSON: `invalid-json`,
174+
cmdline: `cadence --domain test-domain admin async-wf-queue update --json invalid-json`,
189175
unmarshalError: fmt.Errorf("unmarshal error"),
190176
},
191177
{
@@ -197,8 +183,7 @@ func TestAdminUpdateAsyncWFConfig(t *testing.T) {
197183
Times(1)
198184
},
199185
expectedError: "Failed to update async workflow queue config",
200-
flagDomain: "test-domain",
201-
flagJSON: `{"Enabled": true}`,
186+
cmdline: `cadence --domain test-domain admin async-wf-queue update --json '{"Enabled": true}'`,
202187
},
203188
}
204189

@@ -215,23 +200,13 @@ func TestAdminUpdateAsyncWFConfig(t *testing.T) {
215200
serverAdminClient: adminClient,
216201
})
217202

218-
// Set up CLI context with flags
219-
set := flag.NewFlagSet("test", 0)
220-
set.String(FlagDomain, tt.flagDomain, "Domain flag")
221-
set.String(FlagJSON, tt.flagJSON, "JSON flag")
222-
c := cli.NewContext(app, set, nil)
223-
224-
// Call the function under test
225-
err := AdminUpdateAsyncWFConfig(c)
226-
227-
// Check the expected outcome
203+
err := clitest.RunCommandLine(t, app, tt.cmdline)
228204
if tt.expectedError != "" {
229205
assert.Error(t, err)
230206
assert.Contains(t, err.Error(), tt.expectedError)
231207
} else {
232208
assert.NoError(t, err)
233209
}
234-
235210
})
236211
}
237212
}

tools/cli/app_test.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,10 @@ import (
2929
"testing"
3030
"time"
3131

32-
"github.com/flynn/go-shlex"
3332
"github.com/olekukonko/tablewriter"
3433
"github.com/olivere/elastic"
3534
"github.com/pborman/uuid"
3635
"github.com/stretchr/testify/assert"
37-
"github.com/stretchr/testify/require"
3836
"github.com/stretchr/testify/suite"
3937
"github.com/urfave/cli/v2"
4038
"go.uber.org/mock/gomock"
@@ -44,6 +42,7 @@ import (
4442
"github.com/uber/cadence/common"
4543
"github.com/uber/cadence/common/config"
4644
"github.com/uber/cadence/common/types"
45+
"github.com/uber/cadence/tools/cli/clitest"
4746
)
4847

4948
type (
@@ -159,10 +158,7 @@ func (s *cliAppSuite) runTestCase(tt testcase) {
159158
tt.mock()
160159
}
161160

162-
args, err := shlex.Split(tt.command)
163-
require.NoError(s.T(), err)
164-
err = s.app.Run(args)
165-
161+
err := clitest.RunCommandLine(s.T(), s.app, tt.command)
166162
if tt.err != "" {
167163
assert.ErrorContains(s.T(), err, tt.err)
168164
} else {

tools/cli/clitest/runner.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package clitest
2+
3+
import (
4+
"testing"
5+
6+
"github.com/flynn/go-shlex"
7+
"github.com/stretchr/testify/require"
8+
"github.com/urfave/cli/v2"
9+
)
10+
11+
// RunCommandLine runs cmdline (a whole line like you'd type it in Shell)
12+
func RunCommandLine(t *testing.T, app *cli.App, cmdline string) error {
13+
t.Helper()
14+
15+
args, err := shlex.Split(cmdline)
16+
require.NoError(t, err)
17+
return app.Run(args)
18+
}

tools/cli/clitest/runner_test.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package clitest
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
"github.com/stretchr/testify/require"
9+
"github.com/urfave/cli/v2"
10+
)
11+
12+
func TestRunCommandLine(t *testing.T) {
13+
var output string
14+
15+
testApp := &cli.App{
16+
Name: "devmngr",
17+
Commands: []*cli.Command{
18+
{
19+
Name: "upgrade",
20+
Flags: []cli.Flag{
21+
&cli.StringFlag{Name: "device"},
22+
&cli.BoolFlag{Name: "enabled"},
23+
&cli.StringFlag{Name: "reason"},
24+
},
25+
Action: func(c *cli.Context) error {
26+
output = fmt.Sprintf(
27+
"upgrading device: \"%s\"; enabled: %v; reason: \"%s\"",
28+
c.String("device"),
29+
c.Bool("enabled"),
30+
c.String("reason"),
31+
)
32+
return nil
33+
},
34+
},
35+
},
36+
}
37+
38+
err := RunCommandLine(
39+
t,
40+
testApp,
41+
"devmngr upgrade --device video-card --enabled --reason \"Doom: The Dark Ages is released\"",
42+
)
43+
require.NoError(t, err)
44+
assert.Equal(t,
45+
"upgrading device: \"video-card\"; enabled: true; reason: \"Doom: The Dark Ages is released\"",
46+
output,
47+
)
48+
}

0 commit comments

Comments
 (0)