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 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")
} }
} }
}) })

View File

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