Skip to content

Commit b996012

Browse files
committed
test: improve CLI testing
Signed-off-by: Andres Taylor <[email protected]>
1 parent a66bce4 commit b996012

File tree

5 files changed

+69
-13
lines changed

5 files changed

+69
-13
lines changed

go/cmd/keys.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func keysCmd() *cobra.Command {
3535
PreRun: func(cmd *cobra.Command, _ []string) {
3636
csvConfig = csvFlagsToConfig(cmd, *flags)
3737
},
38-
RunE: func(_ *cobra.Command, args []string) error {
38+
RunE: func(c *cobra.Command, args []string) error {
3939
cfg := keys.Config{
4040
FileName: args[0],
4141
}
@@ -46,7 +46,7 @@ func keysCmd() *cobra.Command {
4646
}
4747
cfg.Loader = loader
4848

49-
return keys.Run(cfg)
49+
return keys.Run(c.OutOrStdout(), cfg)
5050
},
5151
}
5252

go/cmd/main_test.go

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
Copyright 2024 The Vitess Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package cmd
18+
19+
import (
20+
"bytes"
21+
"strings"
22+
"testing"
23+
24+
"github.com/stretchr/testify/require"
25+
)
26+
27+
func TestApp(t *testing.T) {
28+
tests := []struct {
29+
args []string
30+
want string
31+
}{
32+
{[]string{"help"}, "Utils tools for testing, running and benchmarking Vitess"},
33+
{[]string{"keys", "../../t/demo.test"}, `"queryStructure"`},
34+
{[]string{"keys", "--help"}, `Runs vexplain keys on all queries of the test file`},
35+
{[]string{"txs", "--help"}, `Analyze transactions on a query log`},
36+
{[]string{"test", "--help"}, `Test the given workload against both Vitess and MySQL`},
37+
{[]string{"trace", "--help"}, `Runs the given workload and does a`},
38+
{[]string{"summarize", "--help"}, `Compares and analyses a trace output`},
39+
{[]string{"dbinfo", "--help"}, `Loads info from the database including row counts`},
40+
{[]string{"planalyze", "--help"}, `Analyze the query plan`},
41+
}
42+
for _, tt := range tests {
43+
t.Run("vt "+strings.Join(tt.args, " "), func(t *testing.T) {
44+
cmd := getRootCmd()
45+
buf := new(bytes.Buffer)
46+
cmd.SetOut(buf)
47+
cmd.SetErr(buf)
48+
cmd.SetArgs(tt.args)
49+
50+
err := cmd.Execute()
51+
52+
out := buf.String()
53+
require.NoError(t, err, out)
54+
require.Contains(t, out, tt.want)
55+
})
56+
}
57+
}

go/cmd/root.go

+8-4
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@ import (
2525
// Execute adds all child commands to the root command and sets flags appropriately.
2626
// This is called by main.main(). It only needs to happen once to the rootCmd.
2727
func Execute() {
28+
err := getRootCmd().Execute()
29+
if err != nil {
30+
os.Exit(1)
31+
}
32+
}
33+
34+
func getRootCmd() *cobra.Command {
2835
// rootCmd represents the base command when called without any subcommands
2936
root := &cobra.Command{
3037
Use: "vt",
@@ -39,8 +46,5 @@ func Execute() {
3946
root.AddCommand(dbinfoCmd())
4047
root.AddCommand(transactionsCmd())
4148
root.AddCommand(planalyzeCmd())
42-
err := root.Execute()
43-
if err != nil {
44-
os.Exit(1)
45-
}
49+
return root
4650
}

go/keys/keys.go

+1-6
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"errors"
2222
"fmt"
2323
"io"
24-
"os"
2524
"sort"
2625

2726
querypb "vitess.io/vitess/go/vt/proto/query"
@@ -73,11 +72,7 @@ type (
7372
}
7473
)
7574

76-
func Run(cfg Config) error {
77-
return run(os.Stdout, cfg)
78-
}
79-
80-
func run(out io.Writer, cfg Config) error {
75+
func Run(out io.Writer, cfg Config) error {
8176
si := &SchemaInfo{
8277
Tables: make(map[string]Columns),
8378
}

go/keys/keys_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func TestKeys(t *testing.T) {
6565
for _, tcase := range cases {
6666
t.Run(tcase.expectedFile, func(t *testing.T) {
6767
sb := &strings.Builder{}
68-
err := run(sb, tcase.cfg)
68+
err := Run(sb, tcase.cfg)
6969
require.NoError(t, err)
7070

7171
out, err := os.ReadFile("../testdata/keys-output/" + tcase.expectedFile)

0 commit comments

Comments
 (0)