Desertbit/grumble: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
(Created page with "=External= * https://github.com/desertbit/grumble =Internal= * Go Open Source Packages =Overview= =Unit Testing Commands= ==Option 1== <syntaxhighlight lang='go'> command := somepackage.SomeCommand(...) app := grumble.New(&grumble.Config{Name: "test"}) app.AddCommand(command) args := []string{ "somecommand", "--arg1", "test1", "--arg2", "test2", } err := app.RunCommand(args) </syntaxhighlight> ==Option 2== <syntaxhighl...")
 
 
(2 intermediate revisions by the same user not shown)
Line 5: Line 5:
=Overview=
=Overview=
=Unit Testing Commands=
=Unit Testing Commands=
These options do not capture the command output, just side effects:


==Option 1==
==Option 1==
Line 21: Line 23:


==Option 2==
==Option 2==
In this case, a flag '''must''' be registered, even if it has a zero value, otherwise the attempt to get the flag will panic the runtime.


<syntaxhighlight lang='go'>
<syntaxhighlight lang='go'>
flags := map[string]*grumble.FlagMapItem{
flags := map[string]*grumble.FlagMapItem{
"arg1":    flagValue(arg1),
"arg1":    return &grumble.FlagMapItem{Value: arg1},
"arg2":    flagValue(arg2),
"arg2":    return &grumble.FlagMapItem{Value: arg2},
}
}
ctx := &grumble.Context{
ctx := &grumble.Context{
Line 34: Line 38:
err := command.Run(ctx)
err := command.Run(ctx)
</syntaxhighlight>
</syntaxhighlight>
==Capture and Test Output==

Latest revision as of 19:23, 6 November 2024

External

Internal

Overview

Unit Testing Commands

These options do not capture the command output, just side effects:

Option 1

command := somepackage.SomeCommand(...)
app := grumble.New(&grumble.Config{Name: "test"})
app.AddCommand(command)
args := []string{
    "somecommand",
    "--arg1", "test1",
    "--arg2", "test2",
}
err := app.RunCommand(args)

Option 2

In this case, a flag must be registered, even if it has a zero value, otherwise the attempt to get the flag will panic the runtime.

flags := map[string]*grumble.FlagMapItem{
	"arg1":    return &grumble.FlagMapItem{Value: arg1},
	"arg2":    return &grumble.FlagMapItem{Value: arg2},
}
ctx := &grumble.Context{
	Flags:   flags,
	Context: context.Background(),
}
command := somepackage.SomeCommand(...)
err := command.Run(ctx)

Capture and Test Output