From 9e78477eb570656d7f8f331bac307349f2da25a9 Mon Sep 17 00:00:00 2001 From: Kharec Date: Tue, 13 Jan 2026 07:46:23 +0100 Subject: [PATCH] tests: update cli help/json checks --- cmd/goyco/server_test.go | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/cmd/goyco/server_test.go b/cmd/goyco/server_test.go index db41a16..c7d1627 100644 --- a/cmd/goyco/server_test.go +++ b/cmd/goyco/server_test.go @@ -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) {