Skip to content

Commit

Permalink
add some unit test. fix: repeat display error message
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Apr 26, 2019
1 parent 02da580 commit 1ffe50d
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 18 deletions.
2 changes: 1 addition & 1 deletion _examples/alone/alone.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,6 @@ func main() {
}

// Alone Running
cmd.Run(nil)
cmd.MustRun(nil)
// cmd.Run(os.Args[1:])
}
16 changes: 11 additions & 5 deletions _examples/cmd/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ func (ns *Names) Set(value string) error {

// options for the command
var exampleOpts = struct {
id int
c string
dir string
opt string
names Names
id int
c string
dir string
opt string
showErr bool
names Names
}{}

// ExampleCommand command definition
Expand All @@ -42,6 +43,7 @@ func ExampleCommand() *gcli.Command {

// bind options
cmd.IntOpt(&exampleOpts.id, "id", "", 2, "the id option")
cmd.BoolOpt(&exampleOpts.showErr, "err", "e", false, "display error example")
cmd.StrOpt(&exampleOpts.c, "config", "c", "value", "the config option")
// notice `DIRECTORY` will replace to option value type
cmd.StrOpt(&exampleOpts.dir, "dir", "d", "", "the `DIRECTORY` option")
Expand All @@ -65,6 +67,10 @@ func ExampleCommand() *gcli.Command {
func exampleExecute(c *gcli.Command, args []string) error {
fmt.Print("hello, in example command\n")

if exampleOpts.showErr {
return c.Errorf("OO, An error has occurred!!")
}

magentaln := color.Magenta.Println

magentaln("All options:")
Expand Down
2 changes: 1 addition & 1 deletion app.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ type App struct {
// Strict use strict mode.
// If True:
// - short opt must be begin '-', long opt must be begin '--'
// - will check invalid arguments, like to many arguments
// - will check invalid arguments, like to many arguments
Strict bool
// vars you can add some vars map for render help info
// vars map[string]string
Expand Down
9 changes: 7 additions & 2 deletions cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,13 @@ func (c *Command) initialize() *Command {

c.Fire(EvtInit, nil)

// add default error handler.
c.SimpleHooks.Add(EvtError, defaultErrHandler)
// if not set application instance
if c.app == nil {
// mark is alone
c.alone = true
// add default error handler.
c.SimpleHooks.Add(EvtError, defaultErrHandler)
}

// init for Flags
c.Flags.Init(c.Name, flag.ContinueOnError)
Expand Down
23 changes: 15 additions & 8 deletions cmd_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,15 @@ func (c *Command) Execute(args []string) (err error) {
}

if err != nil {
c.app.AddError(err)
// if run in application, report error to app.
if !c.alone {
c.app.AddError(err)
}

c.Fire(EvtError, err)
} else {
c.Fire(EvtAfter, nil)
}

return
}

Expand All @@ -49,7 +52,7 @@ func (c *Command) collectNamedArgs(inArgs []string) error {
inNum := len(inArgs)

for i, arg := range c.args {
num = i + 1 // num is equal index + 1
num = i + 1 // num is equal index + 1
if num > inNum { // no enough arg
if arg.Required {
return fmt.Errorf("must set value for the argument: %s (position %d)", arg.ShowName, arg.index)
Expand Down Expand Up @@ -100,15 +103,19 @@ func (c *Command) Copy() *Command {
* alone running
*************************************************************/

// MustRun the current command
func (c *Command) MustRun(inArgs []string) {
if err := c.Run(inArgs); err != nil {
panic(err)
}
}

// Run the current command
func (c *Command) Run(inArgs []string) {
func (c *Command) Run(inArgs []string) error {
if c.app == nil {
// don't display date on print log
log.SetFlags(0)

// mark is alone
c.alone = true

// init the command
c.initialize()

Expand All @@ -125,7 +132,7 @@ func (c *Command) Run(inArgs []string) {
inArgs = c.Flags.Args()
}

_ = c.Execute(inArgs)
return c.Execute(inArgs)
}

/*************************************************************
Expand Down
66 changes: 66 additions & 0 deletions cmd_test.go
Original file line number Diff line number Diff line change
@@ -1 +1,67 @@
package gcli_test

import (
"testing"

"github.com/gookit/gcli"
"github.com/stretchr/testify/assert"
)

var simpleArgs = []string{"hi"}

func TestNewCommand(t *testing.T) {
ris := assert.New(t)

c := gcli.NewCommand("test", "desc test", func(c *gcli.Command) {
c.Aliases = []string{"alias1", "alias2"}
})

ris.NotEmpty(c)
ris.Nil(c.App())

err := c.Run(simpleArgs)
ris.NoError(err)
ris.True(c.IsAlone())
ris.False(c.NotAlone())

// ris.Equal("", c.ArgLine())
ris.Equal("alias1,alias2", c.AliasesString())
ris.Equal("alias1alias2", c.AliasesString(""))
}

func TestCommand_Errorf(t *testing.T) {
ris := assert.New(t)

c := gcli.NewCommand("test", "desc test", nil)
c.SetFunc(func(c *gcli.Command, args []string) error {
return c.Errorf("error message")
})

ris.NotEmpty(c)

err := c.Run(simpleArgs)
ris.Error(err)
ris.Equal("error message", err.Error())

ris.Panics(func() {
c.MustRun(simpleArgs)
})
}

func TestCommand_Run(t *testing.T) {
ris := assert.New(t)

c := gcli.NewCommand("test", "desc test", func(c *gcli.Command) {
ris.Equal("test", c.Name)
c.Aliases = []string{"alias1"}
})
c.SetFunc(func(c *gcli.Command, args []string) error {
return nil
})

ris.NotEmpty(c)
err := c.Run(simpleArgs)
ris.NoError(err)

ris.Equal("alias1", c.AliasesString(""))
}
2 changes: 1 addition & 1 deletion helper/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func printDownloadPercent(done chan int64, path string, total int64) {

for {
select {
case <-done: // end
case <-done: // end
fmt.Println() // output newline
return
default:
Expand Down

0 comments on commit 1ffe50d

Please sign in to comment.