From 1b510b3aab28a9311f60ca6a1a33a645fb4c377d Mon Sep 17 00:00:00 2001 From: Inhere Date: Tue, 4 Oct 2022 13:45:40 +0800 Subject: [PATCH] test: fix cmd init event fire error and some test error --- app.go | 1 + base_test.go | 13 ++++++------- builtin/any.go | 7 +++++++ cmd.go | 2 +- cmd_test.go | 6 +++--- events/events.go | 3 +++ gflag/any_test.go | 7 +++++++ 7 files changed, 28 insertions(+), 11 deletions(-) create mode 100644 builtin/any.go create mode 100644 gflag/any_test.go diff --git a/app.go b/app.go index 5bb27ca..5cfc58d 100644 --- a/app.go +++ b/app.go @@ -210,6 +210,7 @@ func (app *App) Add(c *Command, more ...*Command) { func (app *App) AddCommand(c *Command) { // initialize application before add command app.initialize() + app.fireWithCmd(events.OnAppCmdAdd, c, nil) // init command c.app = app diff --git a/base_test.go b/base_test.go index b708f33..b8135d6 100644 --- a/base_test.go +++ b/base_test.go @@ -43,23 +43,22 @@ func TestApp_Hooks_EvtAppInit(t *testing.T) { assert.Eq(t, "trigger "+events.OnGlobalOptsParsed+", args:[simple]", buf.String()) } -func TestApp_Hooks_EvtCmdInit(t *testing.T) { +func TestApp_Hooks_OnAppCmdAdd(t *testing.T) { buf.Reset() cli := newNotExitApp() - cli.On(events.OnCmdInitAfter, func(ctx *gcli.HookCtx) (stop bool) { - buf.WriteString(events.OnCmdInitAfter) - buf.WriteString(":") - + cli.On(events.OnAppCmdAdd, func(ctx *gcli.HookCtx) (stop bool) { + buf.WriteString(ctx.Name()) + buf.WriteString(" - ") buf.WriteString(ctx.Cmd.Name + ";") return }) cli.Add(emptyCmd) - assert.Eq(t, "cmd.init:empty;", buf.String()) + assert.Eq(t, "app.cmd.add.before - empty;", buf.String()) cli.Add(simpleCmd) - assert.Eq(t, "cmd.init:empty;cmd.init:simple;", buf.String()) + assert.Eq(t, "app.cmd.add.before - empty;app.cmd.add.before - simple;", buf.String()) } func TestCommand_Hooks_EvtCmdOptParsed(t *testing.T) { diff --git a/builtin/any.go b/builtin/any.go new file mode 100644 index 0000000..7a0e2ab --- /dev/null +++ b/builtin/any.go @@ -0,0 +1,7 @@ +//go:build !go1.18 +// +build !go1.18 + +package builtin + +// alias of interface{}, use for go < 1.18 +type any = interface{} diff --git a/cmd.go b/cmd.go index bd035bf..c306b5c 100644 --- a/cmd.go +++ b/cmd.go @@ -247,7 +247,6 @@ func (c *Command) initialize() { // check command name cName := c.goodName() - c.Fire(events.OnCmdInitBefore, nil) Debugf("initialize the command '%s': init flags, run config func", cName) c.initialized = true @@ -255,6 +254,7 @@ func (c *Command) initialize() { // init base c.initCommandBase(cName) + c.Fire(events.OnCmdInitBefore, nil) // load common subs if len(c.Subs) > 0 { diff --git a/cmd_test.go b/cmd_test.go index ec17ea1..66364d2 100644 --- a/cmd_test.go +++ b/cmd_test.go @@ -308,7 +308,7 @@ func TestCommand_Run_emptyArgs(t *testing.T) { is.Eq("arg0", c0.Arg("arg0").Name) } -func TestCommand_Run_XshowHelp1(t *testing.T) { +func TestCommand_Run_showHelp1(t *testing.T) { is := assert.New(t) bf.Reset() @@ -342,7 +342,7 @@ func TestCommand_Run_showHelp2(t *testing.T) { is.StrContains(str, "Arg1 desc") } -func TestCommand_Run_XparseOptions(t *testing.T) { +func TestCommand_Run_parseOptions(t *testing.T) { bf.Reset() is := assert.New(t) @@ -355,7 +355,7 @@ func TestCommand_Run_XparseOptions(t *testing.T) { dump.P(gcli.GOpts()) err := c0.Run([]string{"--int", "10", "--str=abc", "txt"}) - dump.P(gcli.GOpts(), c0.Context) + // dump.P(gcli.GOpts(), c0.Context) is.NoErr(err) is.Eq("test", c0.Get("name")) is.Eq([]string{"txt"}, c0.Get("args")) diff --git a/events/events.go b/events/events.go index 4c390e3..8921d5e 100644 --- a/events/events.go +++ b/events/events.go @@ -15,6 +15,9 @@ const ( OnAppBindOptsBefore = "app.bind.opts.before" OnAppBindOptsAfter = "app.bind.opts.after" + // OnAppCmdAdd on app cmd add + OnAppCmdAdd = "app.cmd.add.before" + // OnAppCmdAdded on app cmd added OnAppCmdAdded = "app.cmd.added" diff --git a/gflag/any_test.go b/gflag/any_test.go new file mode 100644 index 0000000..8909f1b --- /dev/null +++ b/gflag/any_test.go @@ -0,0 +1,7 @@ +//go:build !go1.18 +// +build !go1.18 + +package gflag_test + +// alias of interface{}, use for go < 1.18 +type any = interface{}