~samwhited/cli

ref: 8da9bdbba125beb76c6f3d3e9be1850cbf634b69 cli/cmd_test.go -rw-r--r-- 3.4 KiB
8da9bdbbSam Whited cli: remove auto help printing 3 years ago
                                                                                
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
// Copyright 2017 The Mellium Contributors.
// Use of this source code is governed by the BSD 2-clause license that can be
// found in the LICENSE file.

package cli_test

import (
	"bytes"
	"flag"
	"fmt"
	"testing"

	"mellium.im/cli"
)

type testCase struct {
	cmd  cli.Command
	name string
	desc string
}

var commandTestCases = [...]testCase{
	0: {},
	1: {cmd: cli.Command{Usage: "name", Description: "desc"}, name: "name", desc: "desc"},
	2: {cmd: cli.Command{Usage: "name [options]", Description: "desc\nlong description"}, name: "name", desc: "desc"},
}

func TestCommand(t *testing.T) {
	b := new(bytes.Buffer)
	for i, tc := range commandTestCases {
		t.Run(fmt.Sprintf("Name/%d", i), func(t *testing.T) {
			if name := tc.cmd.Name(); name != tc.name {
				t.Errorf("Invalid name: want=`%s`, got=`%s`", tc.name, name)
			}
		})
		t.Run(fmt.Sprintf("Description/%d", i), func(t *testing.T) {
			if desc := tc.cmd.ShortDesc(); desc != tc.desc {
				t.Errorf("Invalid description: want=`%s`, got=`%s`", tc.desc, desc)
			}
		})
		t.Run(fmt.Sprintf("Help/%d", i), func(t *testing.T) {
			tc.cmd.Flags = flag.NewFlagSet("testflags", flag.ExitOnError)
			tc.cmd.Flags.String("testflag", "testflagvalue", "usage of a test flag")

			b.Reset()
			tc.cmd.Help(b)
			if tc.cmd.Run != nil && !bytes.Contains(b.Bytes(), []byte(tc.cmd.Usage)) {
				t.Errorf("Expected cmd.Help() output to contain cmd.Usage")
			}
			if !bytes.Contains(b.Bytes(), []byte(tc.cmd.Description)) {
				t.Errorf("Expected cmd.Help() output to contain cmd.Description")
			}
			if !bytes.Contains(b.Bytes(), []byte("testflag")) {
				t.Errorf("Expected cmd.Help() output to contain flag names")
			}
			if !bytes.Contains(b.Bytes(), []byte("testflagvalue")) {
				t.Errorf("Expected cmd.Help() output to contain flag values")
			}
			if !bytes.Contains(b.Bytes(), []byte("usage of a test flag")) {
				t.Errorf("Expected cmd.Help() output to contain flag usage")
			}
		})
	}
}

var csTestCase = [...]struct {
	cs  *cli.Command
	run []string
	err error
}{
	0: {},
	1: {
		cs: &cli.Command{
			Commands: []*cli.Command{
				{Usage: "one [opts]"},
				{Usage: "two [opts]"},
				{Usage: "three [opts]"},
			},
		},
		run: []string{"one"},
		err: cli.ErrNoRun,
	},
	2: {
		cs: &cli.Command{
			Commands: []*cli.Command{
				{Usage: "one [opts]", Flags: func() *flag.FlagSet {
					f := flag.NewFlagSet("one", flag.ExitOnError)
					f.Bool("v", false, "verbose")
					return f
				}()},
				{Usage: "two [opts]"},
				{Usage: "three [opts]"},
			},
		},
		run: []string{"one", "-v"},
		err: cli.ErrNoRun,
	},
	3: {
		cs: &cli.Command{
			Commands: []*cli.Command{
				{Usage: "one [opts]"},
				{Usage: "two [opts]"},
				{Usage: "three [opts]"},
			},
		},
		run: []string{"one", "-v"},
		err: cli.ErrInvalidCmd,
	},
	4: {
		cs: &cli.Command{
			Commands: []*cli.Command{},
		},
		run: []string{"ran"},
		err: cli.ErrInvalidCmd,
	},
}

func TestCommandSet(t *testing.T) {
	for i, tc := range csTestCase {
		t.Run(fmt.Sprintf("Run/%d", i), func(t *testing.T) {
			if err := tc.cs.Exec(tc.run...); err != tc.err {
				t.Errorf("Wrong err: want='%v', got='%v'", tc.err, err)
			}
		})
		if tc.cs != nil {
			t.Run(fmt.Sprintf("Help/%d", i), func(t *testing.T) {
				b := new(bytes.Buffer)
				tc.cs.Help(b)
				for _, cmd := range tc.cs.Commands {
					if !bytes.Contains(b.Bytes(), []byte(cmd.Name())) {
						t.Errorf("Expected commandset help to contain command name: %s", cmd.Name())
					}
				}
			})
		}
	}
}