Пример #1
0
func TestContext_Args(t *testing.T) {
	set := flag.NewFlagSet("test", 0)
	set.Bool("myflag", false, "doc")
	c := cli.NewContext(nil, set, set)
	set.Parse([]string{"--myflag", "bat", "baz"})
	expect(t, len(c.Args()), 2)
	expect(t, c.Bool("myflag"), true)
}
Пример #2
0
func TestContext_IsSet(t *testing.T) {
	set := flag.NewFlagSet("test", 0)
	set.Bool("myflag", false, "doc")
	set.String("otherflag", "hello world", "doc")
	c := cli.NewContext(nil, set, set)
	set.Parse([]string{"--myflag", "bat", "baz"})
	expect(t, c.IsSet("myflag"), true)
	expect(t, c.IsSet("otherflag"), false)
	expect(t, c.IsSet("bogusflag"), false)
}
Пример #3
0
func TestNewContext(t *testing.T) {
	set := flag.NewFlagSet("test", 0)
	set.Int("myflag", 12, "doc")
	globalSet := flag.NewFlagSet("test", 0)
	globalSet.Int("myflag", 42, "doc")
	command := cli.Command{Name: "mycommand"}
	c := cli.NewContext(nil, set, globalSet)
	c.Command = command
	expect(t, c.Int("myflag"), 12)
	expect(t, c.GlobalInt("myflag"), 42)
	expect(t, c.Command.Name, "mycommand")
}
Пример #4
0
func TestAppVersionPrinter(t *testing.T) {
	oldPrinter := cli.VersionPrinter
	defer func() {
		cli.VersionPrinter = oldPrinter
	}()

	var wasCalled = false
	cli.VersionPrinter = func(c *cli.Context) {
		wasCalled = true
	}

	app := cli.NewApp()
	ctx := cli.NewContext(app, nil, nil)
	cli.ShowVersion(ctx)

	if wasCalled == false {
		t.Errorf("Version printer expected to be called, but was not")
	}
}
Пример #5
0
func TestCommandDoNotIgnoreFlags(t *testing.T) {
	app := cli.NewApp()
	set := flag.NewFlagSet("test", 0)
	test := []string{"blah", "blah", "-break"}
	set.Parse(test)

	c := cli.NewContext(app, set, set)

	command := cli.Command{
		Name:        "test-cmd",
		ShortName:   "tc",
		Usage:       "this is for testing",
		Description: "testing",
		Action:      func(_ *cli.Context) {},
	}
	err := command.Run(c)

	expect(t, err.Error(), "flag provided but not defined: -break")
}
Пример #6
0
func TestCommandIgnoreFlags(t *testing.T) {
	app := cli.NewApp()
	set := flag.NewFlagSet("test", 0)
	test := []string{"blah", "blah"}
	set.Parse(test)

	c := cli.NewContext(app, set, set)

	command := cli.Command{
		Name:            "test-cmd",
		ShortName:       "tc",
		Usage:           "this is for testing",
		Description:     "testing",
		Action:          func(_ *cli.Context) {},
		SkipFlagParsing: true,
	}
	err := command.Run(c)

	expect(t, err, nil)
}
Пример #7
0
func TestDaemonRun(t *testing.T) {
	bootstrap := true
	debug := true
	iface := "auto"

	set := flag.NewFlagSet("test", 0)
	set.Bool("debug", debug, "")
	set.String("iface", iface, "")
	set.Bool("bootstrap", bootstrap, "")
	c := cli.NewContext(nil, set, set)

	daemon := NewDaemon()
	go daemon.Run(c)

	// Wait for subsystems to come up
	time.Sleep(3 * time.Second)

	if daemon.bootstrapNode != bootstrap {
		t.Fatalf("bootstrap value is incorrect")
	}

	if log.GetLevel() != log.DebugLevel {
		t.Fatalf("incorrect log level")
	}

	_, err := net.InterfaceByName(defaultBridgeName)
	if err != nil {
		t.Fatal("bridge not created")
	}

	_, err = GetNetwork("default")
	if err != nil {
		t.Fatal("default network not created")
	}

}
Пример #8
0
func TestContext_BoolT(t *testing.T) {
	set := flag.NewFlagSet("test", 0)
	set.Bool("myflag", true, "doc")
	c := cli.NewContext(nil, set, set)
	expect(t, c.BoolT("myflag"), true)
}
Пример #9
0
func TestContext_String(t *testing.T) {
	set := flag.NewFlagSet("test", 0)
	set.String("myflag", "hello world", "doc")
	c := cli.NewContext(nil, set, set)
	expect(t, c.String("myflag"), "hello world")
}
Пример #10
0
func TestContext_Duration(t *testing.T) {
	set := flag.NewFlagSet("test", 0)
	set.Duration("myflag", time.Duration(12*time.Second), "doc")
	c := cli.NewContext(nil, set, set)
	expect(t, c.Duration("myflag"), time.Duration(12*time.Second))
}
Пример #11
0
func TestContext_Int(t *testing.T) {
	set := flag.NewFlagSet("test", 0)
	set.Int("myflag", 12, "doc")
	c := cli.NewContext(nil, set, set)
	expect(t, c.Int("myflag"), 12)
}