Skip to content

Commit

Permalink
chore: add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
almas1992 committed Dec 16, 2024
1 parent 42f1311 commit 9a4ceb5
Show file tree
Hide file tree
Showing 3 changed files with 165 additions and 3 deletions.
5 changes: 2 additions & 3 deletions console/cli_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ var commandTemplate = `{{ $cv := offsetCommands .VisibleCommands 5}}{{range .Vis
{{$s := join .Names ", "}}{{green $s}}{{ $sp := subtract $cv (offset $s 3) }}{{ indent $sp ""}}{{wrap (colorize .Usage) $cv}}{{end}}{{end}}`

var flagTemplate = `{{ $cv := offsetFlags .VisibleFlags 5}}{{range .VisibleFlags}}
{{$s := getFlagName .}}{{green $s}}{{ $sp := subtract $cv (offset $s 1) }}{{ indent $sp ""}}{{wrap (capitalize .Usage) $cv}}{{$df := getFlagDefaultText . }}{{if $df}} {{yellow $df}}{{end}}{{end}}`
{{$s := getFlagName .}}{{green $s}}{{ $sp := subtract $cv (offset $s 1) }}{{ indent $sp ""}}{{$us := (capitalize .Usage)}}{{wrap (colorize $us) $cv}}{{$df := getFlagDefaultText . }}{{if $df}} {{yellow $df}}{{end}}{{end}}`

var appHelpTemplate = `{{$v := offset .Usage 6}}{{wrap (colorize .Usage) 3}}{{if .Version}} {{green (wrap .Version $v)}}{{end}}
Expand Down Expand Up @@ -239,7 +239,7 @@ func printHelpCustom(out io.Writer, templ string, data interface{}, _ map[string

func commandNotFound(ctx *cli.Context, command string) {
var (
msgTxt = fmt.Sprintf("Command '%s'' is not defined.", command)
msgTxt = fmt.Sprintf("Command '%s' is not defined.", command)
suggestion string
)
if alternatives := findAlternatives(command, func() (collection []string) {
Expand All @@ -257,7 +257,6 @@ func commandNotFound(ctx *cli.Context, command string) {
}
color.Errorln(msgTxt)
color.Gray().Println(suggestion)
os.Exit(0)
}

func onUsageError(_ *cli.Context, err error, _ bool) error {
Expand Down
158 changes: 158 additions & 0 deletions console/cli_helper_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
package console

import (
"bytes"
"github.com/goravel/framework/contracts/console"
"github.com/goravel/framework/contracts/console/command"
"github.com/goravel/framework/support/color"
"github.com/stretchr/testify/assert"
"io"
"testing"
)

func TestShowCommandHelp_HelpPrinterCustom(t *testing.T) {
cliApp := NewApplication("test", "test", "test", "test", true)
cliApp.Register([]console.Command{
&TestFooCommand{},
&TestBarCommand{},
})
tests := []struct {
name string
call string
containsOutput []string
}{
{
name: "print app help",
containsOutput: []string{
color.Yellow().Sprint("Usage:"),
color.Yellow().Sprint("Options:"),
color.Yellow().Sprint("Available commands:"),
color.Yellow().Sprint("test"),
color.Green().Sprint("test:foo"),
color.Green().Sprint("test:bar"),
},
},
{
name: "print command help",
call: "help test:foo",
containsOutput: []string{
color.Yellow().Sprint("Description:"),
color.Yellow().Sprint("Usage:"),
color.Yellow().Sprint("Options:"),
color.Green().Sprint("-b, --bool"),
color.Green().Sprint("-i, --int"),
color.Blue().Sprint("int"),
},
},
{
name: "print version",
call: "--version",
containsOutput: []string{
"test " + color.Green().Sprint("test"),
},
},
{
name: "command not found",
call: "not-found",
containsOutput: []string{
color.New(color.FgLightRed).Sprint("Command 'not-found' is not defined."),
},
},
{
name: "command not found(suggest)",
call: "test",
containsOutput: []string{
color.New(color.FgLightRed).Sprint("Command 'test' is not defined. Did you mean one of these?"),
color.Gray().Sprint(" test:bar"),
color.Gray().Sprint(" test:foo"),
},
},
{
name: "command not found(suggest)",
call: "fo",
containsOutput: []string{
color.New(color.FgLightRed).Sprint("Command 'fo' is not defined. Did you mean this?"),
color.Gray().Sprint(" test:foo"),
},
},
{
name: "option not found",
call: "test:foo --not-found",
containsOutput: []string{
color.Red().Sprint("The 'not-found' option does not exist."),
},
},
{
name: "option needs a value",
call: "test:foo --int",
containsOutput: []string{
color.Red().Sprint("The 'int' option requires a value."),
},
},
{
name: "option value is not valid",
call: "test:foo --int not-a-number",
containsOutput: []string{
color.Red().Sprint("Invalid value 'not-a-number' for option 'int'."),
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
output := &bytes.Buffer{}
cliApp.(*Application).instance.Writer = output
got := color.CaptureOutput(func(io.Writer) {
cliApp.Call(tt.call)
})
if len(got) == 0 {
got = output.String()
}
//fmt.Println("got=========>", got)
for _, contain := range tt.containsOutput {
assert.Contains(t, got, contain)
}
})
}
}

type TestFooCommand struct {
}

type TestBarCommand struct {
TestFooCommand
}

func (receiver *TestFooCommand) Signature() string {
return "test:foo"
}

func (receiver *TestFooCommand) Description() string {
return "Test command"
}

func (receiver *TestFooCommand) Extend() command.Extend {
return command.Extend{
Category: "test",
Flags: []command.Flag{
&command.BoolFlag{
Name: "bool",
Aliases: []string{"b"},
Usage: "bool flag",
},
&command.IntFlag{
Name: "int",
Aliases: []string{"i"},
Usage: "{{ blue \"int\" }} flag",
},
},
}
}

func (receiver *TestFooCommand) Handle(_ console.Context) error {

return nil
}

func (receiver *TestBarCommand) Signature() string {
return "test:bar"
}
5 changes: 5 additions & 0 deletions support/color/color.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,11 @@ func Warnln(a ...any) { warn.Println(a...) }
// CaptureOutput simulates capturing of os.stdout with a buffer and returns what was written to the screen
func CaptureOutput(f func(w io.Writer)) string {
var outBuf bytes.Buffer
info.Writer = &outBuf
warn.Writer = &outBuf
err.Writer = &outBuf
debug.Writer = &outBuf
success.Writer = &outBuf
pterm.SetDefaultOutput(&outBuf)
f(&outBuf)

Expand Down

0 comments on commit 9a4ceb5

Please sign in to comment.