2018-01-11 22:19:25 +00:00
|
|
|
package mcfg
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2019-02-05 20:18:17 +00:00
|
|
|
"context"
|
2019-04-04 16:20:40 +00:00
|
|
|
"fmt"
|
2018-08-13 18:17:53 +00:00
|
|
|
"strings"
|
2018-01-11 22:19:25 +00:00
|
|
|
. "testing"
|
2018-08-13 18:17:53 +00:00
|
|
|
"time"
|
2018-01-11 22:19:25 +00:00
|
|
|
|
2018-07-03 00:20:00 +00:00
|
|
|
"github.com/mediocregopher/mediocre-go-lib/mrand"
|
2019-04-03 03:21:16 +00:00
|
|
|
"github.com/mediocregopher/mediocre-go-lib/mtest/massert"
|
2018-08-13 19:03:30 +00:00
|
|
|
"github.com/mediocregopher/mediocre-go-lib/mtest/mchk"
|
2018-01-11 22:19:25 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestSourceCLIHelp(t *T) {
|
2019-02-05 20:18:17 +00:00
|
|
|
ctx := context.Background()
|
2019-02-09 19:08:30 +00:00
|
|
|
ctx, _ = WithInt(ctx, "foo", 5, "Test int param ") // trailing space should be trimmed
|
|
|
|
ctx, _ = WithBool(ctx, "bar", "Test bool param.")
|
|
|
|
ctx, _ = WithString(ctx, "baz", "baz", "Test string param")
|
|
|
|
ctx, _ = WithRequiredString(ctx, "baz2", "")
|
|
|
|
ctx, _ = WithRequiredString(ctx, "baz3", "")
|
2018-01-11 22:19:25 +00:00
|
|
|
src := SourceCLI{}
|
|
|
|
|
|
|
|
buf := new(bytes.Buffer)
|
2019-01-08 19:21:55 +00:00
|
|
|
pM, err := src.cliParams(collectParams(ctx))
|
2018-01-11 22:19:25 +00:00
|
|
|
require.NoError(t, err)
|
2019-04-04 14:57:37 +00:00
|
|
|
new(SourceCLI).printHelp(buf, pM)
|
2018-01-11 22:19:25 +00:00
|
|
|
|
|
|
|
exp := `
|
2019-01-25 03:02:04 +00:00
|
|
|
--baz2 (Required)
|
|
|
|
|
|
|
|
--baz3 (Required)
|
|
|
|
|
2018-01-11 22:19:25 +00:00
|
|
|
--bar (Flag)
|
2019-02-03 00:35:30 +00:00
|
|
|
Test bool param.
|
2018-01-11 22:19:25 +00:00
|
|
|
|
|
|
|
--baz (Default: "baz")
|
2019-02-03 00:35:30 +00:00
|
|
|
Test string param.
|
2018-01-11 22:19:25 +00:00
|
|
|
|
|
|
|
--foo (Default: 5)
|
2019-02-03 00:35:30 +00:00
|
|
|
Test int param.
|
2018-01-11 22:19:25 +00:00
|
|
|
|
|
|
|
`
|
|
|
|
assert.Equal(t, exp, buf.String())
|
|
|
|
}
|
2018-08-13 18:17:53 +00:00
|
|
|
|
2018-08-13 19:03:30 +00:00
|
|
|
func TestSourceCLI(t *T) {
|
|
|
|
type state struct {
|
2018-08-13 23:40:41 +00:00
|
|
|
srcCommonState
|
2019-04-04 14:57:37 +00:00
|
|
|
*SourceCLI
|
2018-08-13 18:17:53 +00:00
|
|
|
}
|
|
|
|
|
2018-08-13 19:03:30 +00:00
|
|
|
type params struct {
|
2018-08-13 23:40:41 +00:00
|
|
|
srcCommonParams
|
|
|
|
nonBoolWEq bool // use equal sign when setting value
|
2018-08-13 18:17:53 +00:00
|
|
|
}
|
|
|
|
|
2018-08-13 19:03:30 +00:00
|
|
|
chk := mchk.Checker{
|
|
|
|
Init: func() mchk.State {
|
|
|
|
var s state
|
2018-08-13 23:40:41 +00:00
|
|
|
s.srcCommonState = newSrcCommonState()
|
2019-04-04 14:57:37 +00:00
|
|
|
s.SourceCLI = &SourceCLI{
|
|
|
|
Args: make([]string, 0, 16),
|
|
|
|
}
|
2018-08-13 18:17:53 +00:00
|
|
|
return s
|
|
|
|
},
|
2018-08-13 19:03:30 +00:00
|
|
|
Next: func(ss mchk.State) mchk.Action {
|
|
|
|
s := ss.(state)
|
|
|
|
var p params
|
2018-08-13 23:40:41 +00:00
|
|
|
p.srcCommonParams = s.srcCommonState.next()
|
|
|
|
// if the param is a bool or unset this won't get used, but w/e
|
2018-08-13 19:03:30 +00:00
|
|
|
p.nonBoolWEq = mrand.Intn(2) == 0
|
|
|
|
return mchk.Action{Params: p}
|
|
|
|
},
|
|
|
|
Apply: func(ss mchk.State, a mchk.Action) (mchk.State, error) {
|
|
|
|
s := ss.(state)
|
|
|
|
p := a.Params.(params)
|
|
|
|
|
2019-01-08 19:21:55 +00:00
|
|
|
s.srcCommonState = s.srcCommonState.applyCtxAndPV(p.srcCommonParams)
|
2018-08-13 19:03:30 +00:00
|
|
|
if !p.unset {
|
|
|
|
arg := cliKeyPrefix
|
|
|
|
if len(p.path) > 0 {
|
|
|
|
arg += strings.Join(p.path, cliKeyJoin) + cliKeyJoin
|
|
|
|
}
|
|
|
|
arg += p.name
|
|
|
|
if !p.isBool {
|
|
|
|
if p.nonBoolWEq {
|
|
|
|
arg += "="
|
|
|
|
} else {
|
|
|
|
s.SourceCLI.Args = append(s.SourceCLI.Args, arg)
|
|
|
|
arg = ""
|
|
|
|
}
|
|
|
|
arg += p.nonBoolVal
|
|
|
|
}
|
|
|
|
s.SourceCLI.Args = append(s.SourceCLI.Args, arg)
|
|
|
|
}
|
|
|
|
|
2018-08-13 23:40:41 +00:00
|
|
|
err := s.srcCommonState.assert(s.SourceCLI)
|
|
|
|
return s, err
|
2018-08-13 18:17:53 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-08-13 23:40:41 +00:00
|
|
|
if err := chk.RunFor(2 * time.Second); err != nil {
|
2018-08-13 18:17:53 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
2019-04-03 03:21:16 +00:00
|
|
|
|
2019-04-04 16:20:40 +00:00
|
|
|
func TestWithCLITail(t *T) {
|
2019-04-03 03:21:16 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
ctx, _ = WithInt(ctx, "foo", 5, "")
|
|
|
|
ctx, _ = WithBool(ctx, "bar", "")
|
|
|
|
|
|
|
|
type testCase struct {
|
|
|
|
args []string
|
|
|
|
expTail []string
|
|
|
|
}
|
|
|
|
|
|
|
|
cases := []testCase{
|
|
|
|
{
|
|
|
|
args: []string{"--foo", "5"},
|
2019-04-04 16:20:40 +00:00
|
|
|
expTail: nil,
|
2019-04-03 03:21:16 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
args: []string{"--foo", "5", "a", "b", "c"},
|
|
|
|
expTail: []string{"a", "b", "c"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
args: []string{"--foo=5", "a", "b", "c"},
|
|
|
|
expTail: []string{"a", "b", "c"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
args: []string{"--foo", "5", "--bar"},
|
2019-04-04 16:20:40 +00:00
|
|
|
expTail: nil,
|
2019-04-03 03:21:16 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
args: []string{"--foo", "5", "--bar", "a", "b", "c"},
|
|
|
|
expTail: []string{"a", "b", "c"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range cases {
|
2019-04-04 16:20:40 +00:00
|
|
|
ctx, tail := WithCLITail(ctx)
|
2019-04-04 16:52:43 +00:00
|
|
|
_, err := Populate(ctx, &SourceCLI{Args: tc.args})
|
2019-04-03 03:21:16 +00:00
|
|
|
massert.Require(t, massert.Comment(massert.All(
|
|
|
|
massert.Nil(err),
|
2019-04-04 16:20:40 +00:00
|
|
|
massert.Equal(tc.expTail, *tail),
|
2019-04-03 03:21:16 +00:00
|
|
|
), "tc: %#v", tc))
|
|
|
|
}
|
|
|
|
}
|
2019-04-04 16:20:40 +00:00
|
|
|
|
|
|
|
func ExampleWithCLITail() {
|
|
|
|
ctx := context.Background()
|
|
|
|
ctx, foo := WithInt(ctx, "foo", 1, "Description of foo.")
|
|
|
|
ctx, tail := WithCLITail(ctx)
|
|
|
|
ctx, bar := WithString(ctx, "bar", "defaultVal", "Description of bar.")
|
|
|
|
|
2019-04-04 16:52:43 +00:00
|
|
|
_, err := Populate(ctx, &SourceCLI{
|
2019-04-04 16:20:40 +00:00
|
|
|
Args: []string{"--foo=100", "BADARG", "--bar", "BAR"},
|
|
|
|
})
|
|
|
|
|
|
|
|
fmt.Printf("err:%v foo:%v bar:%v tail:%#v\n", err, *foo, *bar, *tail)
|
|
|
|
// Output: err:<nil> foo:100 bar:defaultVal tail:[]string{"BADARG", "--bar", "BAR"}
|
|
|
|
}
|