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 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
|
package kongplete
import (
"bytes"
"os"
"strconv"
"strings"
"testing"
"github.com/alecthomas/kong"
"github.com/posener/complete"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
const (
envLine = "COMP_LINE"
envPoint = "COMP_POINT"
)
func TestComplete(t *testing.T) {
type embed struct {
Lion string
}
predictors := map[string]complete.Predictor{
"things": complete.PredictSet("thing1", "thing2"),
"otherthings": complete.PredictSet("otherthing1", "otherthing2"),
}
var cli struct {
Foo struct {
Embedded embed `kong:"embed"`
Bar string `kong:"predictor=things"`
Baz bool
Qux bool `kong:"hidden"`
Rabbit struct{} `kong:"cmd"`
Duck struct{} `kong:"cmd"`
} `kong:"cmd"`
Bar struct {
Tiger string `kong:"arg,predictor=things"`
Bear string `kong:"arg,predictor=otherthings"`
OMG string `kong:"required,enum='oh,my,gizzles'"`
Number int `kong:"required,short=n,enum='1,2,3'"`
BooFlag bool `kong:"name=boofl,short=b"`
} `kong:"cmd"`
Baz struct{} `kong:"cmd,hidden"`
Pos struct {
Cumulative []string `kong:"arg,predictor=things"`
} `kong:"cmd"`
}
for _, td := range []completeTest{
{
parser: kong.Must(&cli),
want: []string{"thing1", "thing2"},
line: "myApp pos thing1 ",
},
{
parser: kong.Must(&cli),
want: []string{"foo", "bar", "pos"},
line: "myApp ",
},
{
parser: kong.Must(&cli),
want: []string{"foo"},
line: "myApp foo",
},
{
parser: kong.Must(&cli),
want: []string{"rabbit", "duck"},
line: "myApp foo ",
},
{
parser: kong.Must(&cli),
want: []string{"rabbit"},
line: "myApp foo r",
},
{
parser: kong.Must(&cli),
want: []string{"--bar", "--baz", "--lion", "--help", "-h"},
line: "myApp foo -",
},
{
parser: kong.Must(&cli),
want: []string{},
line: "myApp foo --lion ",
},
{
parser: kong.Must(&cli),
want: []string{"rabbit", "duck"},
line: "myApp foo --baz ",
},
{
parser: kong.Must(&cli),
want: []string{"--bar", "--baz", "--lion", "--help", "-h"},
line: "myApp foo --baz -",
},
{
parser: kong.Must(&cli),
want: []string{"thing1", "thing2"},
line: "myApp foo --bar ",
},
{
parser: kong.Must(&cli),
want: []string{"thing1", "thing2"},
line: "myApp bar ",
},
{
parser: kong.Must(&cli),
want: []string{"thing1", "thing2"},
line: "myApp bar thing",
},
{
parser: kong.Must(&cli),
want: []string{"otherthing1", "otherthing2"},
line: "myApp bar thing1 ",
},
{
parser: kong.Must(&cli),
want: []string{"oh", "my", "gizzles"},
line: "myApp bar --omg ",
},
{
parser: kong.Must(&cli),
want: []string{"-n", "--number", "--omg", "--help", "-h", "--boofl", "-b"},
line: "myApp bar -",
},
{
parser: kong.Must(&cli),
want: []string{"thing1", "thing2"},
line: "myApp bar -b ",
},
{
parser: kong.Must(&cli),
want: []string{"-n", "--number", "--omg", "--help", "-h", "--boofl", "-b"},
line: "myApp bar -b thing1 -",
},
{
parser: kong.Must(&cli),
want: []string{"oh", "my", "gizzles"},
line: "myApp bar -b thing1 --omg ",
},
{
parser: kong.Must(&cli),
want: []string{"otherthing1", "otherthing2"},
line: "myApp bar -b thing1 --omg gizzles ",
},
} {
name := td.name
if name == "" {
name = td.line
}
t.Run(name, func(t *testing.T) {
options := []Option{WithPredictors(predictors)}
got := runComplete(t, td.parser, td.line, options)
assert.ElementsMatch(t, td.want, got)
})
}
}
func Test_tagPredictor(t *testing.T) {
t.Run("nil", func(t *testing.T) {
got, err := tagPredictor(nil, nil)
assert.NoError(t, err)
assert.Nil(t, got)
})
t.Run("no predictor tag", func(t *testing.T) {
got, err := tagPredictor(testTag{}, nil)
assert.NoError(t, err)
assert.Nil(t, got)
})
t.Run("missing predictor", func(t *testing.T) {
got, err := tagPredictor(testTag{predictorTag: "foo"}, nil)
assert.Error(t, err)
assert.Equal(t, `no predictor with name "foo"`, err.Error())
assert.Nil(t, got)
})
t.Run("existing predictor", func(t *testing.T) {
got, err := tagPredictor(testTag{predictorTag: "foo"}, map[string]complete.Predictor{"foo": complete.PredictAnything})
assert.NoError(t, err)
assert.NotNil(t, got)
})
}
type testTag map[string]string
func (t testTag) Has(k string) bool {
_, ok := t[k]
return ok
}
func (t testTag) Get(k string) string {
return t[k]
}
type completeTest struct {
name string
parser *kong.Kong
want []string
line string
}
func setLineAndPoint(t *testing.T, line string) func() {
t.Helper()
origLine, hasOrigLine := os.LookupEnv(envLine)
origPoint, hasOrigPoint := os.LookupEnv(envPoint)
require.NoError(t, os.Setenv(envLine, line))
require.NoError(t, os.Setenv(envPoint, strconv.Itoa(len(line))))
return func() {
t.Helper()
require.NoError(t, os.Unsetenv(envLine))
require.NoError(t, os.Unsetenv(envPoint))
if hasOrigLine {
require.NoError(t, os.Setenv(envLine, origLine))
}
if hasOrigPoint {
require.NoError(t, os.Setenv(envPoint, origPoint))
}
}
}
func runComplete(t *testing.T, parser *kong.Kong, line string, options []Option) []string {
t.Helper()
options = append(options,
WithErrorHandler(func(err error) {
t.Helper()
assert.NoError(t, err)
}),
WithExitFunc(func(code int) {
t.Helper()
assert.Equal(t, 0, code)
}),
)
cleanup := setLineAndPoint(t, line)
defer cleanup()
var buf bytes.Buffer
if parser != nil {
parser.Stdout = &buf
}
Complete(parser, options...)
return parseOutput(buf.String())
}
func parseOutput(output string) []string {
lines := strings.Split(output, "\n")
options := []string{}
for _, l := range lines {
if l != "" {
options = append(options, l)
}
}
return options
}
|