Compare commits

..

2 Commits

Author SHA1 Message Date
9c74828b8d tests: fuzz urfave command parsing 2026-01-13 07:46:30 +01:00
9e78477eb5 tests: update cli help/json checks 2026-01-13 07:46:23 +01:00
2 changed files with 23 additions and 42 deletions

View File

@@ -1,9 +1,8 @@
package main
import (
"flag"
"context"
"fmt"
"os"
"strings"
"testing"
"unicode/utf8"
@@ -12,6 +11,7 @@ import (
"goyco/internal/config"
"goyco/internal/testutils"
"github.com/urfave/cli/v3"
"gorm.io/gorm"
)
@@ -36,32 +36,15 @@ func FuzzCLIArgs(f *testing.F) {
if len(args) == 0 {
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)
fs.SetOutput(os.Stderr)
fs.Usage = printRootUsage
showHelp := fs.Bool("help", false, "show this help message")
err := fs.Parse(args)
err := cmd.Run(context.Background(), append([]string{"goyco"}, args...))
if err != nil {
if !strings.Contains(err.Error(), "flag") && !strings.Contains(err.Error(), "help") {
t.Logf("Unexpected error format from flag 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")
if !strings.Contains(err.Error(), "flag") && !strings.Contains(err.Error(), "unknown command") {
t.Logf("Unexpected error format from command parsing: %v", err)
}
}
})

View File

@@ -3,14 +3,13 @@ package main
import (
"context"
"crypto/tls"
"errors"
"flag"
"net/http"
"net/http/httptest"
"os"
"testing"
"time"
"goyco/cmd/goyco/commands"
"goyco/internal/config"
"goyco/internal/database"
"goyco/internal/handlers"
@@ -267,25 +266,24 @@ func TestConfigLoadingInCLI(t *testing.T) {
}
func TestFlagParsingInCLI(t *testing.T) {
originalArgs := os.Args
defer func() {
os.Args = originalArgs
}()
t.Run("help flag", func(t *testing.T) {
os.Args = []string{"goyco", "--help"}
fs := flag.NewFlagSet("goyco", flag.ContinueOnError)
fs.SetOutput(os.Stderr)
showHelp := fs.Bool("help", false, "show help")
err := fs.Parse([]string{"--help"})
if err != nil && !errors.Is(err, flag.ErrHelp) {
cmd := buildRootCommand(testutils.NewTestConfig())
err := cmd.Run(context.Background(), []string{"goyco", "--help"})
if err != nil {
t.Errorf("Expected help flag parsing, got error: %v", err)
}
})
if !*showHelp {
t.Error("Expected help flag to be true")
t.Run("json flag", func(t *testing.T) {
cmd := buildRootCommand(testutils.NewTestConfig())
err := cmd.Run(context.Background(), []string{"goyco", "--json"})
if err != nil {
t.Errorf("Expected json flag parsing, got error: %v", err)
}
if !commands.IsJSONOutput() {
t.Error("Expected json output to be enabled")
}
commands.SetJSONOutput(false)
})
t.Run("command dispatch", func(t *testing.T) {