2015-07-20 19:18:25 +00:00
|
|
|
package cli
|
2013-07-18 00:00:02 +00:00
|
|
|
|
|
|
|
import (
|
2019-08-06 18:04:51 +00:00
|
|
|
"context"
|
2013-07-19 00:39:42 +00:00
|
|
|
"flag"
|
2016-05-16 14:18:15 +00:00
|
|
|
"sort"
|
2013-07-24 14:35:45 +00:00
|
|
|
"testing"
|
2014-08-02 21:32:32 +00:00
|
|
|
"time"
|
2013-07-18 00:00:02 +00:00
|
|
|
)
|
|
|
|
|
2013-07-20 22:53:25 +00:00
|
|
|
func TestNewContext(t *testing.T) {
|
2013-07-19 01:00:52 +00:00
|
|
|
set := flag.NewFlagSet("test", 0)
|
|
|
|
set.Int("myflag", 12, "doc")
|
2016-06-16 15:13:32 +00:00
|
|
|
set.Int64("myflagInt64", int64(12), "doc")
|
|
|
|
set.Uint("myflagUint", uint(93), "doc")
|
|
|
|
set.Uint64("myflagUint64", uint64(93), "doc")
|
2016-04-28 17:03:25 +00:00
|
|
|
set.Float64("myflag64", float64(17), "doc")
|
2013-07-19 01:00:52 +00:00
|
|
|
globalSet := flag.NewFlagSet("test", 0)
|
|
|
|
globalSet.Int("myflag", 42, "doc")
|
2016-06-16 14:14:28 +00:00
|
|
|
globalSet.Int64("myflagInt64", int64(42), "doc")
|
2016-06-16 15:13:32 +00:00
|
|
|
globalSet.Uint("myflagUint", uint(33), "doc")
|
|
|
|
globalSet.Uint64("myflagUint64", uint64(33), "doc")
|
2016-04-28 17:03:25 +00:00
|
|
|
globalSet.Float64("myflag64", float64(47), "doc")
|
2015-07-20 19:18:25 +00:00
|
|
|
globalCtx := NewContext(nil, globalSet, nil)
|
2016-05-22 19:20:52 +00:00
|
|
|
command := &Command{Name: "mycommand"}
|
2015-07-20 19:18:25 +00:00
|
|
|
c := NewContext(nil, set, globalCtx)
|
2014-03-29 19:02:05 +00:00
|
|
|
c.Command = command
|
2013-07-19 01:00:52 +00:00
|
|
|
expect(t, c.Int("myflag"), 12)
|
2016-06-11 22:41:24 +00:00
|
|
|
expect(t, c.Int64("myflagInt64"), int64(12))
|
2016-06-16 15:13:32 +00:00
|
|
|
expect(t, c.Uint("myflagUint"), uint(93))
|
|
|
|
expect(t, c.Uint64("myflagUint64"), uint64(93))
|
2016-04-28 17:03:25 +00:00
|
|
|
expect(t, c.Float64("myflag64"), float64(17))
|
2014-03-29 19:02:05 +00:00
|
|
|
expect(t, c.Command.Name, "mycommand")
|
2013-07-19 01:00:52 +00:00
|
|
|
}
|
|
|
|
|
2013-07-20 22:53:25 +00:00
|
|
|
func TestContext_Int(t *testing.T) {
|
2013-07-19 00:39:42 +00:00
|
|
|
set := flag.NewFlagSet("test", 0)
|
|
|
|
set.Int("myflag", 12, "doc")
|
2016-07-24 01:53:55 +00:00
|
|
|
parentSet := flag.NewFlagSet("test", 0)
|
|
|
|
parentSet.Int("top-flag", 13, "doc")
|
|
|
|
parentCtx := NewContext(nil, parentSet, nil)
|
|
|
|
c := NewContext(nil, set, parentCtx)
|
2013-07-19 00:39:42 +00:00
|
|
|
expect(t, c.Int("myflag"), 12)
|
2016-07-24 01:53:55 +00:00
|
|
|
expect(t, c.Int("top-flag"), 13)
|
2013-07-18 00:00:02 +00:00
|
|
|
}
|
|
|
|
|
2016-06-11 22:41:24 +00:00
|
|
|
func TestContext_Int64(t *testing.T) {
|
|
|
|
set := flag.NewFlagSet("test", 0)
|
|
|
|
set.Int64("myflagInt64", 12, "doc")
|
2016-07-24 01:53:55 +00:00
|
|
|
parentSet := flag.NewFlagSet("test", 0)
|
|
|
|
parentSet.Int64("top-flag", 13, "doc")
|
|
|
|
parentCtx := NewContext(nil, parentSet, nil)
|
|
|
|
c := NewContext(nil, set, parentCtx)
|
2016-06-11 22:41:24 +00:00
|
|
|
expect(t, c.Int64("myflagInt64"), int64(12))
|
2016-07-24 01:53:55 +00:00
|
|
|
expect(t, c.Int64("top-flag"), int64(13))
|
2016-06-11 22:41:24 +00:00
|
|
|
}
|
|
|
|
|
2016-06-16 15:13:32 +00:00
|
|
|
func TestContext_Uint(t *testing.T) {
|
|
|
|
set := flag.NewFlagSet("test", 0)
|
|
|
|
set.Uint("myflagUint", uint(13), "doc")
|
2016-07-24 01:53:55 +00:00
|
|
|
parentSet := flag.NewFlagSet("test", 0)
|
|
|
|
parentSet.Uint("top-flag", uint(14), "doc")
|
|
|
|
parentCtx := NewContext(nil, parentSet, nil)
|
|
|
|
c := NewContext(nil, set, parentCtx)
|
2016-06-16 15:13:32 +00:00
|
|
|
expect(t, c.Uint("myflagUint"), uint(13))
|
2016-07-24 01:53:55 +00:00
|
|
|
expect(t, c.Uint("top-flag"), uint(14))
|
2016-06-16 15:13:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestContext_Uint64(t *testing.T) {
|
|
|
|
set := flag.NewFlagSet("test", 0)
|
|
|
|
set.Uint64("myflagUint64", uint64(9), "doc")
|
2016-07-24 01:53:55 +00:00
|
|
|
parentSet := flag.NewFlagSet("test", 0)
|
|
|
|
parentSet.Uint64("top-flag", uint64(10), "doc")
|
|
|
|
parentCtx := NewContext(nil, parentSet, nil)
|
|
|
|
c := NewContext(nil, set, parentCtx)
|
2016-06-16 15:13:32 +00:00
|
|
|
expect(t, c.Uint64("myflagUint64"), uint64(9))
|
2016-07-24 01:53:55 +00:00
|
|
|
expect(t, c.Uint64("top-flag"), uint64(10))
|
2016-06-16 15:13:32 +00:00
|
|
|
}
|
|
|
|
|
2016-04-28 17:03:25 +00:00
|
|
|
func TestContext_Float64(t *testing.T) {
|
|
|
|
set := flag.NewFlagSet("test", 0)
|
|
|
|
set.Float64("myflag", float64(17), "doc")
|
2016-07-24 01:53:55 +00:00
|
|
|
parentSet := flag.NewFlagSet("test", 0)
|
|
|
|
parentSet.Float64("top-flag", float64(18), "doc")
|
|
|
|
parentCtx := NewContext(nil, parentSet, nil)
|
|
|
|
c := NewContext(nil, set, parentCtx)
|
2016-04-28 17:03:25 +00:00
|
|
|
expect(t, c.Float64("myflag"), float64(17))
|
2016-07-24 01:53:55 +00:00
|
|
|
expect(t, c.Float64("top-flag"), float64(18))
|
2016-04-28 17:03:25 +00:00
|
|
|
}
|
|
|
|
|
2014-08-02 21:32:32 +00:00
|
|
|
func TestContext_Duration(t *testing.T) {
|
|
|
|
set := flag.NewFlagSet("test", 0)
|
2016-07-24 01:53:55 +00:00
|
|
|
set.Duration("myflag", 12*time.Second, "doc")
|
|
|
|
parentSet := flag.NewFlagSet("test", 0)
|
|
|
|
parentSet.Duration("top-flag", 13*time.Second, "doc")
|
|
|
|
parentCtx := NewContext(nil, parentSet, nil)
|
|
|
|
c := NewContext(nil, set, parentCtx)
|
|
|
|
expect(t, c.Duration("myflag"), 12*time.Second)
|
|
|
|
expect(t, c.Duration("top-flag"), 13*time.Second)
|
2014-08-02 21:32:32 +00:00
|
|
|
}
|
|
|
|
|
2013-07-20 22:53:25 +00:00
|
|
|
func TestContext_String(t *testing.T) {
|
2013-07-19 00:39:42 +00:00
|
|
|
set := flag.NewFlagSet("test", 0)
|
|
|
|
set.String("myflag", "hello world", "doc")
|
2016-07-24 01:53:55 +00:00
|
|
|
parentSet := flag.NewFlagSet("test", 0)
|
|
|
|
parentSet.String("top-flag", "hai veld", "doc")
|
|
|
|
parentCtx := NewContext(nil, parentSet, nil)
|
|
|
|
c := NewContext(nil, set, parentCtx)
|
2013-07-19 00:39:42 +00:00
|
|
|
expect(t, c.String("myflag"), "hello world")
|
2016-07-24 01:53:55 +00:00
|
|
|
expect(t, c.String("top-flag"), "hai veld")
|
2013-07-18 00:00:02 +00:00
|
|
|
}
|
|
|
|
|
2017-09-26 12:38:41 +00:00
|
|
|
func TestContext_Path(t *testing.T) {
|
|
|
|
set := flag.NewFlagSet("test", 0)
|
|
|
|
set.String("path", "path/to/file", "path to file")
|
|
|
|
parentSet := flag.NewFlagSet("test", 0)
|
|
|
|
parentSet.String("top-path", "path/to/top/file", "doc")
|
|
|
|
parentCtx := NewContext(nil, parentSet, nil)
|
|
|
|
c := NewContext(nil, set, parentCtx)
|
|
|
|
expect(t, c.Path("path"), "path/to/file")
|
|
|
|
expect(t, c.Path("top-path"), "path/to/top/file")
|
|
|
|
}
|
|
|
|
|
2013-07-20 22:53:25 +00:00
|
|
|
func TestContext_Bool(t *testing.T) {
|
2013-07-19 00:39:42 +00:00
|
|
|
set := flag.NewFlagSet("test", 0)
|
|
|
|
set.Bool("myflag", false, "doc")
|
2016-07-24 01:53:55 +00:00
|
|
|
parentSet := flag.NewFlagSet("test", 0)
|
|
|
|
parentSet.Bool("top-flag", true, "doc")
|
|
|
|
parentCtx := NewContext(nil, parentSet, nil)
|
|
|
|
c := NewContext(nil, set, parentCtx)
|
2013-07-19 00:39:42 +00:00
|
|
|
expect(t, c.Bool("myflag"), false)
|
2016-07-24 01:53:55 +00:00
|
|
|
expect(t, c.Bool("top-flag"), true)
|
2013-07-18 00:00:02 +00:00
|
|
|
}
|
|
|
|
|
2013-07-20 22:53:25 +00:00
|
|
|
func TestContext_Args(t *testing.T) {
|
2013-07-19 00:39:42 +00:00
|
|
|
set := flag.NewFlagSet("test", 0)
|
|
|
|
set.Bool("myflag", false, "doc")
|
2015-07-20 19:18:25 +00:00
|
|
|
c := NewContext(nil, set, nil)
|
2013-07-19 00:39:42 +00:00
|
|
|
set.Parse([]string{"--myflag", "bat", "baz"})
|
2016-05-22 19:20:52 +00:00
|
|
|
expect(t, c.Args().Len(), 2)
|
2013-07-19 00:39:42 +00:00
|
|
|
expect(t, c.Bool("myflag"), true)
|
2013-07-18 00:00:02 +00:00
|
|
|
}
|
2013-12-18 18:09:16 +00:00
|
|
|
|
2016-02-21 13:57:11 +00:00
|
|
|
func TestContext_NArg(t *testing.T) {
|
|
|
|
set := flag.NewFlagSet("test", 0)
|
|
|
|
set.Bool("myflag", false, "doc")
|
|
|
|
c := NewContext(nil, set, nil)
|
|
|
|
set.Parse([]string{"--myflag", "bat", "baz"})
|
|
|
|
expect(t, c.NArg(), 2)
|
|
|
|
}
|
|
|
|
|
2013-12-18 18:09:16 +00:00
|
|
|
func TestContext_IsSet(t *testing.T) {
|
|
|
|
set := flag.NewFlagSet("test", 0)
|
2016-05-17 07:16:26 +00:00
|
|
|
set.Bool("one-flag", false, "doc")
|
|
|
|
set.Bool("two-flag", false, "doc")
|
|
|
|
set.String("three-flag", "hello world", "doc")
|
|
|
|
parentSet := flag.NewFlagSet("test", 0)
|
|
|
|
parentSet.Bool("top-flag", true, "doc")
|
|
|
|
parentCtx := NewContext(nil, parentSet, nil)
|
|
|
|
ctx := NewContext(nil, set, parentCtx)
|
|
|
|
|
|
|
|
set.Parse([]string{"--one-flag", "--two-flag", "--three-flag", "frob"})
|
|
|
|
parentSet.Parse([]string{"--top-flag"})
|
|
|
|
|
|
|
|
expect(t, ctx.IsSet("one-flag"), true)
|
|
|
|
expect(t, ctx.IsSet("two-flag"), true)
|
|
|
|
expect(t, ctx.IsSet("three-flag"), true)
|
|
|
|
expect(t, ctx.IsSet("top-flag"), true)
|
|
|
|
expect(t, ctx.IsSet("bogus"), false)
|
2014-11-11 20:48:07 +00:00
|
|
|
}
|
|
|
|
|
2015-02-26 21:51:01 +00:00
|
|
|
func TestContext_NumFlags(t *testing.T) {
|
|
|
|
set := flag.NewFlagSet("test", 0)
|
|
|
|
set.Bool("myflag", false, "doc")
|
|
|
|
set.String("otherflag", "hello world", "doc")
|
|
|
|
globalSet := flag.NewFlagSet("test", 0)
|
|
|
|
globalSet.Bool("myflagGlobal", true, "doc")
|
2015-07-20 19:18:25 +00:00
|
|
|
globalCtx := NewContext(nil, globalSet, nil)
|
|
|
|
c := NewContext(nil, set, globalCtx)
|
2015-02-26 21:51:01 +00:00
|
|
|
set.Parse([]string{"--myflag", "--otherflag=foo"})
|
|
|
|
globalSet.Parse([]string{"--myflagGlobal"})
|
|
|
|
expect(t, c.NumFlags(), 2)
|
|
|
|
}
|
2016-04-28 20:05:14 +00:00
|
|
|
|
2015-06-08 20:21:26 +00:00
|
|
|
func TestContext_Set(t *testing.T) {
|
|
|
|
set := flag.NewFlagSet("test", 0)
|
|
|
|
set.Int("int", 5, "an int")
|
2016-04-30 15:25:05 +00:00
|
|
|
c := NewContext(nil, set, nil)
|
2015-06-08 20:21:26 +00:00
|
|
|
|
2017-01-11 12:38:54 +00:00
|
|
|
expect(t, c.IsSet("int"), false)
|
2015-06-08 20:21:26 +00:00
|
|
|
c.Set("int", "1")
|
|
|
|
expect(t, c.Int("int"), 1)
|
2017-01-11 12:38:54 +00:00
|
|
|
expect(t, c.IsSet("int"), true)
|
2015-06-08 20:21:26 +00:00
|
|
|
}
|
2016-04-30 15:46:47 +00:00
|
|
|
|
2016-05-16 14:18:15 +00:00
|
|
|
func TestContext_LocalFlagNames(t *testing.T) {
|
|
|
|
set := flag.NewFlagSet("test", 0)
|
|
|
|
set.Bool("one-flag", false, "doc")
|
|
|
|
set.String("two-flag", "hello world", "doc")
|
2016-05-17 07:16:26 +00:00
|
|
|
parentSet := flag.NewFlagSet("test", 0)
|
|
|
|
parentSet.Bool("top-flag", true, "doc")
|
|
|
|
parentCtx := NewContext(nil, parentSet, nil)
|
|
|
|
ctx := NewContext(nil, set, parentCtx)
|
2016-05-16 14:18:15 +00:00
|
|
|
set.Parse([]string{"--one-flag", "--two-flag=foo"})
|
2016-05-17 07:16:26 +00:00
|
|
|
parentSet.Parse([]string{"--top-flag"})
|
2016-04-30 15:46:47 +00:00
|
|
|
|
2016-05-17 07:16:26 +00:00
|
|
|
actualFlags := ctx.LocalFlagNames()
|
2016-05-16 14:18:15 +00:00
|
|
|
sort.Strings(actualFlags)
|
2016-04-30 15:46:47 +00:00
|
|
|
|
2016-05-16 14:18:15 +00:00
|
|
|
expect(t, actualFlags, []string{"one-flag", "two-flag"})
|
|
|
|
}
|
2016-04-30 15:46:47 +00:00
|
|
|
|
2016-05-16 14:18:15 +00:00
|
|
|
func TestContext_FlagNames(t *testing.T) {
|
|
|
|
set := flag.NewFlagSet("test", 0)
|
|
|
|
set.Bool("one-flag", false, "doc")
|
|
|
|
set.String("two-flag", "hello world", "doc")
|
2016-05-17 07:16:26 +00:00
|
|
|
parentSet := flag.NewFlagSet("test", 0)
|
|
|
|
parentSet.Bool("top-flag", true, "doc")
|
|
|
|
parentCtx := NewContext(nil, parentSet, nil)
|
|
|
|
ctx := NewContext(nil, set, parentCtx)
|
2016-05-16 14:18:15 +00:00
|
|
|
set.Parse([]string{"--one-flag", "--two-flag=foo"})
|
2016-05-17 07:16:26 +00:00
|
|
|
parentSet.Parse([]string{"--top-flag"})
|
2016-04-30 15:46:47 +00:00
|
|
|
|
2016-05-17 07:16:26 +00:00
|
|
|
actualFlags := ctx.FlagNames()
|
2016-05-16 14:18:15 +00:00
|
|
|
sort.Strings(actualFlags)
|
|
|
|
|
|
|
|
expect(t, actualFlags, []string{"one-flag", "top-flag", "two-flag"})
|
2016-04-30 15:46:47 +00:00
|
|
|
}
|
2016-05-17 07:16:26 +00:00
|
|
|
|
|
|
|
func TestContext_Lineage(t *testing.T) {
|
|
|
|
set := flag.NewFlagSet("test", 0)
|
|
|
|
set.Bool("local-flag", false, "doc")
|
|
|
|
parentSet := flag.NewFlagSet("test", 0)
|
|
|
|
parentSet.Bool("top-flag", true, "doc")
|
|
|
|
parentCtx := NewContext(nil, parentSet, nil)
|
|
|
|
ctx := NewContext(nil, set, parentCtx)
|
|
|
|
set.Parse([]string{"--local-flag"})
|
|
|
|
parentSet.Parse([]string{"--top-flag"})
|
|
|
|
|
|
|
|
lineage := ctx.Lineage()
|
|
|
|
expect(t, len(lineage), 2)
|
|
|
|
expect(t, lineage[0], ctx)
|
|
|
|
expect(t, lineage[1], parentCtx)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestContext_lookupFlagSet(t *testing.T) {
|
|
|
|
set := flag.NewFlagSet("test", 0)
|
|
|
|
set.Bool("local-flag", false, "doc")
|
|
|
|
parentSet := flag.NewFlagSet("test", 0)
|
|
|
|
parentSet.Bool("top-flag", true, "doc")
|
|
|
|
parentCtx := NewContext(nil, parentSet, nil)
|
|
|
|
ctx := NewContext(nil, set, parentCtx)
|
|
|
|
set.Parse([]string{"--local-flag"})
|
|
|
|
parentSet.Parse([]string{"--top-flag"})
|
|
|
|
|
|
|
|
fs := lookupFlagSet("top-flag", ctx)
|
|
|
|
expect(t, fs, parentCtx.flagSet)
|
|
|
|
|
|
|
|
fs = lookupFlagSet("local-flag", ctx)
|
|
|
|
expect(t, fs, ctx.flagSet)
|
|
|
|
|
|
|
|
if fs := lookupFlagSet("frob", ctx); fs != nil {
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
}
|
2019-08-06 18:04:51 +00:00
|
|
|
|
|
|
|
// TestContextPropagation tests that
|
|
|
|
// *cli.Context always has a valid
|
|
|
|
// context.Context
|
|
|
|
func TestContextPropagation(t *testing.T) {
|
|
|
|
ctx := NewContext(nil, nil, nil)
|
|
|
|
if ctx.Context == nil {
|
|
|
|
t.Fatal("expected a non nil context when no parent is present")
|
|
|
|
}
|
|
|
|
parent := NewContext(nil, nil, nil)
|
|
|
|
parent.Context = context.WithValue(context.Background(), "key", "val")
|
|
|
|
ctx = NewContext(nil, nil, parent)
|
|
|
|
val := ctx.Value("key")
|
|
|
|
if val == nil {
|
|
|
|
t.Fatal("expected a parent context to be inherited but got nil")
|
|
|
|
}
|
|
|
|
valstr, _ := val.(string)
|
|
|
|
if valstr != "val" {
|
|
|
|
t.Fatalf("expected the context value to be %q but got %q", "val", valstr)
|
|
|
|
}
|
|
|
|
}
|