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") globalCtx := cli.NewContext(nil, globalSet, nil) c := cli.NewContext(nil, set, globalCtx) set.Parse([]string{"--myflag", "--otherflag=foo"}) globalSet.Parse([]string{"--myflagGlobal"}) expect(t, c.NumFlags(), 2) }
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") globalCtx := cli.NewContext(nil, globalSet, nil) command := cli.Command{Name: "mycommand"} c := cli.NewContext(nil, set, globalCtx) c.Command = command expect(t, c.Int("myflag"), 12) expect(t, c.GlobalInt("myflag"), 42) expect(t, c.Command.Name, "mycommand") }
func TestContext_IsSet(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") globalCtx := cli.NewContext(nil, globalSet, nil) c := cli.NewContext(nil, set, globalCtx) set.Parse([]string{"--myflag", "bat", "baz"}) globalSet.Parse([]string{"--myflagGlobal", "bat", "baz"}) expect(t, c.IsSet("myflag"), true) expect(t, c.IsSet("otherflag"), false) expect(t, c.IsSet("bogusflag"), false) expect(t, c.IsSet("myflagGlobal"), false) }
func TestUploadHandleFlags(t *testing.T) { app := cli.NewApp() flagset := flag.NewFlagSet("flags", 1) flagset.String("name", "", "") flagset.String("file", "", "") flagset.String("public-key", "", "") flagset.Set("name", "keypair1Name") flagset.Set("public-key", "ssh public key data here") c := cli.NewContext(app, flagset, nil) cmd := &commandUpload{ Ctx: &handler.Context{ CLIContext: c, }, } expected := &handler.Resource{ Params: ¶msUpload{ opts: &osKeypairs.CreateOpts{ Name: "keypair1Name", PublicKey: "ssh public key data here", }, }, } actual := &handler.Resource{} err := cmd.HandleFlags(actual) th.AssertNoErr(t, err) th.AssertDeepEquals(t, *expected.Params.(*paramsUpload).opts, *actual.Params.(*paramsUpload).opts) }
func TestCreateHandleFlags(t *testing.T) { app := cli.NewApp() flagset := flag.NewFlagSet("flags", 1) flagset.String("metadata", "", "") flagset.String("container-read", "", "") flagset.String("container-write", "", "") flagset.Set("metadata", "key=val,foo=bar") c := cli.NewContext(app, flagset, nil) cmd := &commandCreate{ Ctx: &handler.Context{ CLIContext: c, }, } expected := &handler.Resource{ Params: ¶msCreate{ opts: containers.CreateOpts{ Metadata: map[string]string{ "key": "val", "foo": "bar", }, }, }, } actual := &handler.Resource{} err := cmd.HandleFlags(actual) th.AssertNoErr(t, err) th.AssertDeepEquals(t, expected.Params.(*paramsCreate).opts, actual.Params.(*paramsCreate).opts) }
func TestDeleteExecute(t *testing.T) { th.SetupHTTP() defer th.TeardownHTTP() th.Mux.HandleFunc("/container1", func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusNoContent) }) app := cli.NewApp() flagset := flag.NewFlagSet("flags", 1) flagset.String("name", "", "") flagset.Set("name", "container1") c := cli.NewContext(app, flagset, nil) cmd := &commandDelete{ Ctx: &handler.Context{ ServiceClient: client.ServiceClient(), CLIContext: c, }, } actual := &handler.Resource{ Params: ¶msDelete{ container: "container1", }, } cmd.Execute(actual) th.AssertNoErr(t, actual.Err) }
func TestListHandleFlags(t *testing.T) { app := cli.NewApp() flagset := flag.NewFlagSet("flags", 1) flagset.String("image", "", "") flagset.String("flavor", "", "") flagset.String("name", "", "") flagset.String("status", "", "") flagset.String("marker", "", "") flagset.Set("image", "13ba-75c0-4483-acf9") flagset.Set("flavor", "1234-b95f-ac5b-cd23") flagset.Set("name", "server*") flagset.Set("status", "AVAILABLE") flagset.Set("marker", "1fd3-4f9f-44df-1b5c") c := cli.NewContext(app, flagset, nil) cmd := &commandList{ Ctx: &handler.Context{ CLIContext: c, }, } expected := &handler.Resource{ Params: ¶msList{ opts: &osServers.ListOpts{ Image: "13ba-75c0-4483-acf9", Flavor: "1234-b95f-ac5b-cd23", Name: "server*", Status: "AVAILABLE", Marker: "1fd3-4f9f-44df-1b5c", }, }, } actual := &handler.Resource{} err := cmd.HandleFlags(actual) th.AssertNoErr(t, err) th.AssertDeepEquals(t, *expected.Params.(*paramsList).opts, *actual.Params.(*paramsList).opts) }
func TestGenerateExecute(t *testing.T) { th.SetupHTTP() defer th.TeardownHTTP() th.Mux.HandleFunc("/os-keypairs", func(w http.ResponseWriter, r *http.Request) { w.Header().Add("Content-Type", "application/json") fmt.Fprintf(w, `{"keypair":{}}`) }) app := cli.NewApp() flagset := flag.NewFlagSet("flags", 1) flagset.Bool("json", false, "") c := cli.NewContext(app, flagset, nil) cmd := &commandGenerate{ Ctx: &handler.Context{ CLIContext: c, ServiceClient: client.ServiceClient(), }, } actual := &handler.Resource{ Params: ¶msGenerate{ opts: &osKeypairs.CreateOpts{ Name: "keypair1Name", }, }, } cmd.Execute(actual) th.AssertNoErr(t, actual.Err) }
func TestGetHandleSingle(t *testing.T) { th.SetupHTTP() defer th.TeardownHTTP() th.Mux.HandleFunc("/servers/detail", func(w http.ResponseWriter, r *http.Request) { w.Header().Add("Content-Type", "application/json") fmt.Fprintf(w, `{"servers":[{"ID":"server1","Name":"server1Name"}]}`) }) app := cli.NewApp() flagset := flag.NewFlagSet("flags", 1) flagset.String("name", "", "") flagset.Set("name", "server1Name") c := cli.NewContext(app, flagset, nil) cmd := &commandGet{ Ctx: &handler.Context{ CLIContext: c, ServiceClient: client.ServiceClient(), }, } expected := &handler.Resource{ Params: ¶msGet{ server: "server1", }, } actual := &handler.Resource{ Params: ¶msGet{}, } err := cmd.HandleSingle(actual) th.AssertNoErr(t, err) th.AssertEquals(t, expected.Params.(*paramsGet).server, actual.Params.(*paramsGet).server) }
func TestGenerateHandleSingle(t *testing.T) { app := cli.NewApp() flagset := flag.NewFlagSet("flags", 1) flagset.String("name", "", "") flagset.Set("name", "keypair1Name") c := cli.NewContext(app, flagset, nil) cmd := &commandGenerate{ Ctx: &handler.Context{ CLIContext: c, }, } expected := &handler.Resource{ Params: ¶msGenerate{ opts: &osKeypairs.CreateOpts{ Name: "keypair1Name", }, }, } actual := &handler.Resource{ Params: ¶msGenerate{ opts: &osKeypairs.CreateOpts{}, }, } err := cmd.HandleSingle(actual) th.AssertNoErr(t, err) th.AssertEquals(t, *expected.Params.(*paramsGenerate).opts, *actual.Params.(*paramsGenerate).opts) }
func TestContext_Args(t *testing.T) { set := flag.NewFlagSet("test", 0) set.Bool("myflag", false, "doc") c := cli.NewContext(nil, set, nil) set.Parse([]string{"--myflag", "bat", "baz"}) expect(t, len(c.Args()), 2) expect(t, c.Bool("myflag"), true) }
func TestDeleteContext(t *testing.T) { app := cli.NewApp() flagset := flag.NewFlagSet("flags", 1) c := cli.NewContext(app, flagset, nil) cmd := &commandDelete{ Ctx: &handler.Context{ CLIContext: c, }, } expected := cmd.Ctx actual := cmd.Context() th.AssertDeepEquals(t, expected, actual) }
func Test_ShowAppHelp_NoAuthor(t *testing.T) { output := new(bytes.Buffer) app := cli.NewApp() app.Writer = output c := cli.NewContext(app, nil, nil) cli.ShowAppHelp(c) if bytes.Index(output.Bytes(), []byte("AUTHOR(S):")) != -1 { t.Errorf("expected\n%snot to include %s", output.String(), "AUTHOR(S):") } }
func TestCreateHandleFlags(t *testing.T) { app := cli.NewApp() flagset := flag.NewFlagSet("flags", 1) flagset.String("image-id", "", "") flagset.String("flavor-id", "", "") flagset.String("security-groups", "", "") flagset.String("networks", "", "") flagset.String("metadata", "", "") flagset.String("admin-pass", "", "") flagset.String("keypair", "", "") flagset.Set("image-id", "13ba-75c0-4483-acf9") flagset.Set("flavor-id", "1234-b95f-ac5b-cd23") flagset.Set("security-groups", "sg1,sg2,sg3") flagset.Set("networks", "1111-2222-3333-4444,5555-7777-8888-9999") flagset.Set("metadata", "img=foo,flavor=bar") flagset.Set("admin-pass", "secret") flagset.Set("keypair", "kp1") c := cli.NewContext(app, flagset, nil) cmd := &commandCreate{ Ctx: &handler.Context{ CLIContext: c, }, } expected := &handler.Resource{ Params: ¶msCreate{ opts: &servers.CreateOpts{ ImageRef: "13ba-75c0-4483-acf9", FlavorRef: "1234-b95f-ac5b-cd23", SecurityGroups: []string{"sg1", "sg2", "sg3"}, Networks: []osServers.Network{ { UUID: "1111-2222-3333-4444", }, { UUID: "5555-7777-8888-9999", }, }, Metadata: map[string]string{ "img": "foo", "flavor": "bar", }, AdminPass: "******", KeyPair: "kp1", }, }, } actual := &handler.Resource{} err := cmd.HandleFlags(actual) th.AssertNoErr(t, err) th.AssertDeepEquals(t, *expected.Params.(*paramsCreate).opts, *actual.Params.(*paramsCreate).opts) }
func TestRebuildHandleFlags(t *testing.T) { th.SetupHTTP() defer th.TeardownHTTP() th.Mux.HandleFunc("/servers/detail", func(w http.ResponseWriter, r *http.Request) { w.Header().Add("Content-Type", "application/json") fmt.Fprintf(w, `{"servers":[{"ID":"server1","Name":"server1Name"}]}`) }) app := cli.NewApp() flagset := flag.NewFlagSet("flags", 1) flagset.String("id", "", "") flagset.String("image-id", "", "") flagset.String("admin-pass", "", "") flagset.String("ipv4", "", "") flagset.String("ipv6", "", "") flagset.String("metadata", "", "") flagset.String("rename", "", "") flagset.Set("id", "server1") flagset.Set("image-id", "123456789") flagset.Set("admin-pass", "secret") flagset.Set("ipv4", "123.45.67.89") flagset.Set("metadata", "img=bar,flavor=foo") flagset.Set("rename", "server1Rename") c := cli.NewContext(app, flagset, nil) cmd := &commandRebuild{ Ctx: &handler.Context{ CLIContext: c, }, } expected := &handler.Resource{ Params: ¶msRebuild{ opts: &servers.RebuildOpts{ ImageID: "123456789", AdminPass: "******", AccessIPv4: "123.45.67.89", Metadata: map[string]string{ "img": "bar", "flavor": "foo", }, Name: "server1Rename", }, serverID: "server1", }, } actual := &handler.Resource{} err := cmd.HandleFlags(actual) th.AssertNoErr(t, err) th.AssertDeepEquals(t, *expected.Params.(*paramsRebuild).opts, *actual.Params.(*paramsRebuild).opts) }
func TestDeleteHandleFlags(t *testing.T) { app := cli.NewApp() flagset := flag.NewFlagSet("flags", 1) c := cli.NewContext(app, flagset, nil) cmd := &commandDelete{ Ctx: &handler.Context{ CLIContext: c, }, } expected := &handler.Resource{ Params: ¶msDelete{}, } actual := &handler.Resource{} err := cmd.HandleFlags(actual) th.AssertNoErr(t, err) th.AssertDeepEquals(t, expected.Params.(*paramsDelete), actual.Params.(*paramsDelete)) }
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, nil) command := cli.Command{ Name: "test-cmd", Aliases: []string{"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") }
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") } }
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, nil) command := cli.Command{ Name: "test-cmd", Aliases: []string{"tc"}, Usage: "this is for testing", Description: "testing", Action: func(_ *cli.Context) {}, SkipFlagParsing: true, } err := command.Run(c) expect(t, err, nil) }
func TestRebootHandleFlags(t *testing.T) { app := cli.NewApp() flagset := flag.NewFlagSet("flags", 1) flagset.Bool("soft", false, "") flagset.Bool("hard", false, "") flagset.Set("soft", "true") c := cli.NewContext(app, flagset, nil) cmd := &commandReboot{ Ctx: &handler.Context{ CLIContext: c, }, } expected := &handler.Resource{ Params: ¶msReboot{ how: osServers.OSReboot, }, } actual := &handler.Resource{} err := cmd.HandleFlags(actual) th.AssertNoErr(t, err) th.AssertDeepEquals(t, *expected.Params.(*paramsReboot), *actual.Params.(*paramsReboot)) }
func TestDeleteHandleSingle(t *testing.T) { app := cli.NewApp() flagset := flag.NewFlagSet("flags", 1) flagset.String("name", "", "") flagset.Set("name", "keypair1Name") c := cli.NewContext(app, flagset, nil) cmd := &commandDelete{ Ctx: &handler.Context{ CLIContext: c, }, } expected := &handler.Resource{ Params: ¶msDelete{ keypair: "keypair1Name", }, } actual := &handler.Resource{ Params: ¶msDelete{}, } err := cmd.HandleSingle(actual) th.AssertNoErr(t, err) th.AssertEquals(t, expected.Params.(*paramsDelete).keypair, actual.Params.(*paramsDelete).keypair) }
func TestResizeHandleFlags(t *testing.T) { app := cli.NewApp() flagset := flag.NewFlagSet("flags", 1) flagset.String("flavor-id", "", "") flagset.Set("flavor-id", "2") c := cli.NewContext(app, flagset, nil) cmd := &commandResize{ Ctx: &handler.Context{ CLIContext: c, }, } expected := &handler.Resource{ Params: ¶msResize{ opts: &osServers.ResizeOpts{ FlavorRef: "2", }, }, } actual := &handler.Resource{} err := cmd.HandleFlags(actual) th.AssertNoErr(t, err) th.AssertDeepEquals(t, *expected.Params.(*paramsResize).opts, *actual.Params.(*paramsResize).opts) }
func TestUpdateHandleFlags(t *testing.T) { th.SetupHTTP() defer th.TeardownHTTP() th.Mux.HandleFunc("/servers/detail", func(w http.ResponseWriter, r *http.Request) { w.Header().Add("Content-Type", "application/json") fmt.Fprintf(w, `{"servers":[{"ID":"server1","Name":"server1Name"}]}`) }) app := cli.NewApp() flagset := flag.NewFlagSet("flags", 1) flagset.String("rename", "", "") flagset.String("ipv4", "", "") flagset.String("ipv6", "", "") flagset.String("id", "", "") flagset.Set("id", "server1") flagset.Set("rename", "server1NewName") flagset.Set("ipv4", "123.45.67.89") c := cli.NewContext(app, flagset, nil) cmd := &commandUpdate{ Ctx: &handler.Context{ CLIContext: c, }, } expected := &handler.Resource{ Params: ¶msUpdate{ opts: &osServers.UpdateOpts{ Name: "server1NewName", AccessIPv4: "123.45.67.89", }, serverID: "server1", }, } actual := &handler.Resource{} err := cmd.HandleFlags(actual) th.AssertNoErr(t, err) th.AssertDeepEquals(t, *expected.Params.(*paramsUpdate).opts, *actual.Params.(*paramsUpdate).opts) }
func newDlCmd(fs *flag.FlagSet) *commandDownload { return &commandDownload{Ctx: &handler.Context{ CLIContext: cli.NewContext(cli.NewApp(), fs, nil), }} }
func TestContext_Int(t *testing.T) { set := flag.NewFlagSet("test", 0) set.Int("myflag", 12, "doc") c := cli.NewContext(nil, set, nil) expect(t, c.Int("myflag"), 12) }
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, nil) expect(t, c.Duration("myflag"), time.Duration(12*time.Second)) }
func newUpDirCmd(fs *flag.FlagSet) *commandUploadDir { return &commandUploadDir{Ctx: &handler.Context{ CLIContext: cli.NewContext(cli.NewApp(), fs, nil), }} }
func TestContext_String(t *testing.T) { set := flag.NewFlagSet("test", 0) set.String("myflag", "hello world", "doc") c := cli.NewContext(nil, set, nil) expect(t, c.String("myflag"), "hello world") }
func TestContext_BoolT(t *testing.T) { set := flag.NewFlagSet("test", 0) set.Bool("myflag", true, "doc") c := cli.NewContext(nil, set, nil) expect(t, c.BoolT("myflag"), true) }
func newListCmd(fs *flag.FlagSet) *commandList { return &commandList{Ctx: &handler.Context{ CLIContext: cli.NewContext(cli.NewApp(), fs, nil), }} }