Skip to content

Commit

Permalink
update some tests case
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Feb 27, 2021
1 parent 74ed9ed commit 71ab90a
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 82 deletions.
31 changes: 13 additions & 18 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,12 @@ func NewApp(fn ...func(app *App)) *App {
// config
ExitOnEnd: true,
// group
moduleCommands: make(map[string]map[string]*Command),
// moduleCommands: make(map[string]map[string]*Command),
commandBase: newCommandBase(),
}

// internal core
Logf(VerbCrazy, "create new core on init application")
app.core = core{
cmdLine: CLI,
gFlags: NewFlags("app.GlobalOpts").WithOption(FlagsOption{
Expand All @@ -102,10 +104,8 @@ func NewApp(fn ...func(app *App)) *App {

// init commandBase
Logf(VerbCrazy, "create new commandBase on init application")
app.commandBase = newCommandBase()
// set a default version
app.Version = "1.0.0"
app.SetLogo("", "info")

if len(fn) > 0 {
fn[0](app)
Expand Down Expand Up @@ -173,11 +173,6 @@ func (app *App) initialize() {
* register commands
*************************************************************/

// NewCommand create a new command
// func (app *App) NewCommand(name, useFor string, config func(c *Command)) *Command {
// return NewCommand(name, useFor, config)
// }

// Add add one or multi command(s)
func (app *App) Add(c *Command, more ...*Command) {
app.AddCommand(c)
Expand All @@ -200,8 +195,6 @@ func (app *App) AddCommand(c *Command) {
// inherit global flags from application
c.core.gFlags = app.gFlags

// Logf(VerbCrazy, "add command '%s' to the application. aliases: %v", c.Name, c.Aliases)

// do add command
app.commandBase.addCommand(app.Name, c)
}
Expand All @@ -222,10 +215,10 @@ func (app *App) AddAliases(command string, aliases ...string) {
}

// addAliases add alias names for a command
func (app *App) addAliases(command string, aliases []string, sync bool) {
c, has := app.commands[command]
func (app *App) addAliases(name string, aliases []string, sync bool) {
c, has := app.Command(name)
if !has {
panicf("The command '%s' is not exists", command)
panicf("The command '%s' is not exists", name)
}

// add alias
Expand All @@ -234,7 +227,7 @@ func (app *App) addAliases(command string, aliases []string, sync bool) {
panicf("The name '%s' has been used as an command name", alias)
}

app.cmdAliases.AddAlias(command, alias)
app.cmdAliases.AddAlias(name, alias)

// sync to Command
if sync {
Expand Down Expand Up @@ -567,7 +560,7 @@ var AppHelpTemplate = `{{.Desc}} (Version: <info>{{.Version}}</>)
<info>{{$c.Name | paddingName }}</> {{$c.Desc}}{{if $c.Aliases}} (alias: <green>{{ join $c.Aliases ","}}</>){{end}}{{end}}
<info>{{ paddingName "help" }}</> Display help information
Use "<cyan>{$binName} {COMMAND} -h</>" for more information about a command
Use "<cyan>{$binName} COMMAND -h</>" for more information about a command
`

// display app help and list all commands. showCommandList()
Expand Down Expand Up @@ -596,7 +589,8 @@ func (app *App) showApplicationHelp() {
// showCommandHelp display help for an command
func (app *App) showCommandHelp(list []string) (code int) {
binName := app.binName
if len(list) == 0 {
// if len(list) == 0 { TODO support multi level sub command?
if len(list) > 1 {
color.Error.Tips("Too many arguments given.\n\nUsage: %s help COMMAND", binName)
return ERR
}
Expand All @@ -610,12 +604,13 @@ func (app *App) showCommandHelp(list []string) (code int) {
color.Printf(`<yellow>Usage:</>
<cyan>%s COMMAND --help</>
<cyan>%s COMMAND SUB_COMMAND --help</>
<cyan>%s COMMAND SUB_COMMAND ... --help</>
<cyan>%s help COMMAND</>
`, binName, binName, binName)
`, binName, binName, binName, binName)
return
}

cmd, exist := app.commands[name]
cmd, exist := app.Command(name)
if !exist {
color.Error.Prompt("Unknown command name '%s'. Run '%s -h' see all commands", name, binName)
return ERR
Expand Down
68 changes: 34 additions & 34 deletions app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func TestStdApp(t *testing.T) {

app := gcli.StdApp()
app.Config(func(a *gcli.App) {
a.Logo = gcli.Logo{
a.Logo = &gcli.Logo{
Text: "logo1",
Style: "warn",
}
Expand Down Expand Up @@ -97,27 +97,27 @@ func TestApp_Add(t *testing.T) {
is.Equal("c2", c.Name)
}))
app.AddCommand(&gcli.Command{
Name: "m1:c3",
Name: "c3",
Desc: "{$cmd} desc",
Aliases: []string{"alias1"},
Config: func(c *gcli.Command) {
is.Equal("m1:c3", c.Name)
is.Equal("c3", c.Name)
},
})

is.True(app.IsCommand("c1"))
is.True(app.IsCommand("c2"))
is.True(app.HasCommand("m1:c3"))
is.True(app.HasCommand("c3"))
is.Len(app.CmdNames(), 3)
is.Len(app.CmdNameMap(), 3)
is.NotEmpty(app.CommandNames())

c := gcli.NewCommand("mdl:test", "desc test2")
c := gcli.NewCommand("mdl-test", "desc test2")
app.AddCommand(c)

is.Equal("mdl", c.ParentName())
is.Equal("test", c.Name)
is.Equal("m1:c3", app.ResolveAlias("alias1"))
is.Equal("", c.ParentName())
is.Equal("mdl-test", c.Name)
is.Equal("c3", app.ResolveAlias("alias1"))
is.True(app.IsAlias("alias1"))
}

Expand Down Expand Up @@ -170,6 +170,12 @@ func TestApp_AddAliases(t *testing.T) {
func TestApp_Run(t *testing.T) {
is := assert.New(t)

app := gcli.NewApp(func(a *gcli.App) {
a.ExitOnEnd = false
a.Version = "1.3.9"
})
app.Run([]string{})

// disable color code, re-set output for test
buf := new(bytes.Buffer)
color.Disable()
Expand All @@ -181,20 +187,15 @@ func TestApp_Run(t *testing.T) {
gcli.SetVerbose(gcli.VerbError)
}()

app := gcli.NewApp(func(a *gcli.App) {
a.ExitOnEnd = false
a.Version = "1.3.9"
})

// run
code := app.Run([]string{"./myapp"})
code := app.Run([]string{})
str := buf.String()
buf.Reset()

is.Equal(0, code)
is.Contains(str, "1.3.9")
is.Contains(str, "Version: 1.3.9")
is.Contains(str, "This is my CLI application")
is.Contains(str, "This is my console application")
is.Contains(str, "Display help information")

var argStr, cmdRet string
Expand All @@ -213,7 +214,7 @@ func TestApp_Run(t *testing.T) {
})

// run an command
code = app.Run([]string{"./myapp", "test"})
code = app.Run([]string{"test"})
is.Equal(0, code)
is.Equal("", argStr)
is.Equal("test", cmdRet)
Expand All @@ -236,42 +237,44 @@ func TestApp_Run(t *testing.T) {
func TestApp_showCommandHelp(t *testing.T) {
is := assert.New(t)

// disable color code, re-set output for test
buf := new(bytes.Buffer)
color.Disable()
color.SetOutput(buf)
defer color.ResetOptions()

app := gcli.NewApp(func(a *gcli.App) {
a.ExitOnEnd = false
})

app.AddCommand(gcli.NewCommand("test", "desc for test command"))

app.Run([]string{"help", "test"})

// disable color code, re-set output for test
buf := new(bytes.Buffer)
color.Disable()
color.SetOutput(buf)
defer color.ResetOptions()

// show command help
code := app.Run([]string{"./myapp", "help", "test"})
code := app.Run([]string{"help", "test"})
str := buf.String()
buf.Reset()
is.Equal(0, code)
is.Contains(str, "Name: test")
is.Contains(str, "Desc for test command")

// show command help: arg error
code = app.Run([]string{"./myapp", "help", "test", "more"})
code = app.Run([]string{"help", "test", "more"})
str = buf.String()
buf.Reset()
is.Equal(gcli.ERR, code)
is.Contains(str, "ERROR: Too many arguments given.")

// show command help for 'help'
code = app.Run([]string{"./myapp", "help", "help"})
code = app.Run([]string{"help", "help"})
str = buf.String()
buf.Reset()
is.Equal(gcli.OK, code)
is.Contains(str, "Display help message for application or command.")

// show command help: unknown command
code = app.Run([]string{"./myapp", "help", "not-exist"})
code = app.Run([]string{"help", "not-exist"})
str = buf.String()
buf.Reset()
is.Equal(gcli.ERR, code)
Expand All @@ -283,21 +286,18 @@ func TestApp_showVersion(t *testing.T) {
a.ExitOnEnd = false
a.Version = "1.3.9"
a.Desc = "application desc"
a.Logo = gcli.Logo{
Text: "MY-LOGO",
Style: "info",
}
a.Logo.Text = "MY-LOGO"
})

app.Run([]string{"./myapp", "--version"})
app.Run([]string{"--version"})

// disable color code, re-set output for test
buf := new(bytes.Buffer)
color.Disable()
color.SetOutput(buf)
defer color.ResetOptions()

app.Run([]string{"./myapp", "--version"})
app.Run([]string{"--version"})
str := buf.String()
buf.Reset()
assert.Contains(t, str, "Version: 1.3.9")
Expand All @@ -309,15 +309,15 @@ func TestApp_showCommandTips(t *testing.T) {
app := gcli.NewApp()

app.AddCommand(emptyCmd)
app.Run([]string{"./myapp", "emp"})
app.Run([]string{"emp"})

// disable color code, re-set output for test
buf := new(bytes.Buffer)
color.Disable()
color.SetOutput(buf)
defer color.ResetOptions()

app.Run([]string{"./myapp", "emp"})
app.Run([]string{"emp"})
str := buf.String()
buf.Reset()
assert.Contains(t, str, "ERROR: unknown input command \"emp\"")
Expand Down
18 changes: 5 additions & 13 deletions base.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ func (hv *HelpVars) ReplaceVars(input string) string {
// will inject to every Command
type commandBase struct {
// Logo ASCII logo setting
Logo Logo
Logo *Logo
// Version app version. like "1.0.1"
Version string

Expand Down Expand Up @@ -347,6 +347,8 @@ type commandBase struct {

func newCommandBase() commandBase {
return commandBase{
Logo: &Logo{Style: "info"},
// init mapping
cmdNames: make(map[string]int),
// name2idx: make(map[string]int),
commands: make(map[string]*Command),
Expand Down Expand Up @@ -410,7 +412,7 @@ func (b commandBase) addCommand(pName string, c *Command) {
}

if c.IsDisabled() {
Logf(VerbDebug, "command '%s' has been disabled, skip add", cName)
Debugf("command '%s' has been disabled, skip add", cName)
return
}

Expand Down Expand Up @@ -461,17 +463,7 @@ func (b commandBase) Match(names []string) *Command {

// Match command by path. eg. "top:sub" or "top sub"
func (b commandBase) MatchByPath(path string) *Command {
var names []string
path = strings.TrimSpace(path)
if path != "" {
if strings.ContainsRune(path, ' ') {
names = strings.Split(path, " ")
} else {
names = strings.Split(path, CommandSep)
}
}

return b.Match(names)
return b.Match(splitPath2names(path))
}

// SetLogo text and color style
Expand Down
Loading

0 comments on commit 71ab90a

Please sign in to comment.