tests: fuzz urfave command parsing
This commit is contained in:
@@ -1,9 +1,8 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"flag"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
"unicode/utf8"
|
"unicode/utf8"
|
||||||
@@ -12,6 +11,7 @@ import (
|
|||||||
"goyco/internal/config"
|
"goyco/internal/config"
|
||||||
"goyco/internal/testutils"
|
"goyco/internal/testutils"
|
||||||
|
|
||||||
|
"github.com/urfave/cli/v3"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -36,32 +36,15 @@ func FuzzCLIArgs(f *testing.F) {
|
|||||||
if len(args) == 0 {
|
if len(args) == 0 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
cmd := buildRootCommand(testutils.NewTestConfig())
|
||||||
|
for _, sub := range cmd.Commands {
|
||||||
|
sub.Action = func(context.Context, *cli.Command) error { return nil }
|
||||||
|
}
|
||||||
|
|
||||||
fs := flag.NewFlagSet("goyco", flag.ContinueOnError)
|
err := cmd.Run(context.Background(), append([]string{"goyco"}, args...))
|
||||||
fs.SetOutput(os.Stderr)
|
|
||||||
fs.Usage = printRootUsage
|
|
||||||
showHelp := fs.Bool("help", false, "show this help message")
|
|
||||||
|
|
||||||
err := fs.Parse(args)
|
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if !strings.Contains(err.Error(), "flag") && !strings.Contains(err.Error(), "help") {
|
if !strings.Contains(err.Error(), "flag") && !strings.Contains(err.Error(), "unknown command") {
|
||||||
t.Logf("Unexpected error format from flag parsing: %v", err)
|
t.Logf("Unexpected error format from command parsing: %v", err)
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if *showHelp && err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
remaining := fs.Args()
|
|
||||||
if len(remaining) > 0 {
|
|
||||||
cmdName := remaining[0]
|
|
||||||
if len(cmdName) == 0 {
|
|
||||||
t.Fatal("Command name cannot be empty")
|
|
||||||
}
|
|
||||||
if !isValidUTF8(cmdName) {
|
|
||||||
t.Fatal("Command name must be valid UTF-8")
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user