From ae6de6bf578bd059e88633d87cd45c09bbd27fdd Mon Sep 17 00:00:00 2001 From: Chen Asraf Date: Thu, 29 Jan 2026 14:06:47 +0200 Subject: [PATCH] fix: error & cancel outputs --- internal/cli/create_cmd_test.go | 10 +++++----- internal/cli/edit_cmd_test.go | 2 +- internal/cli/list_cmd_test.go | 4 ++-- internal/cli/main_cmd_test.go | 12 ++++++------ internal/cli/prj_cmd_test.go | 4 ++-- internal/cli/remove_cmd_test.go | 2 +- internal/cli/root.go | 8 +++----- internal/cli/show_cmd_test.go | 2 +- internal/config/loader_test.go | 4 ++-- internal/config/parser.go | 4 ++-- internal/config/parser_test.go | 2 +- internal/config/writer.go | 4 +++- internal/config/writer_test.go | 24 ++++++++++++------------ internal/exec/runner_test.go | 12 ++++++------ internal/tmux/session_test.go | 8 ++++---- 15 files changed, 51 insertions(+), 51 deletions(-) diff --git a/internal/cli/create_cmd_test.go b/internal/cli/create_cmd_test.go index 0137aa5..2c215a6 100644 --- a/internal/cli/create_cmd_test.go +++ b/internal/cli/create_cmd_test.go @@ -30,7 +30,7 @@ func TestCreateCmd_Aliases(t *testing.T) { func TestCreateCmd_Flags(t *testing.T) { rootDirFlag := createCmd.Flags().Lookup("root-dir") if rootDirFlag == nil { - t.Error("expected --root-dir flag") + t.Fatal("expected --root-dir flag") } if rootDirFlag.Shorthand != "r" { t.Errorf("expected -r shorthand, got %q", rootDirFlag.Shorthand) @@ -38,7 +38,7 @@ func TestCreateCmd_Flags(t *testing.T) { windowFlag := createCmd.Flags().Lookup("window") if windowFlag == nil { - t.Error("expected --window flag") + t.Fatal("expected --window flag") } if windowFlag.Shorthand != "w" { t.Errorf("expected -w shorthand, got %q", windowFlag.Shorthand) @@ -46,7 +46,7 @@ func TestCreateCmd_Flags(t *testing.T) { saveFlag := createCmd.Flags().Lookup("save") if saveFlag == nil { - t.Error("expected --save flag") + t.Fatal("expected --save flag") } if saveFlag.Shorthand != "s" { t.Errorf("expected -s shorthand, got %q", saveFlag.Shorthand) @@ -54,7 +54,7 @@ func TestCreateCmd_Flags(t *testing.T) { saveOnlyFlag := createCmd.Flags().Lookup("save-only") if saveOnlyFlag == nil { - t.Error("expected --save-only flag") + t.Fatal("expected --save-only flag") } if saveOnlyFlag.Shorthand != "S" { t.Errorf("expected -S shorthand, got %q", saveOnlyFlag.Shorthand) @@ -62,7 +62,7 @@ func TestCreateCmd_Flags(t *testing.T) { localFlag := createCmd.Flags().Lookup("local") if localFlag == nil { - t.Error("expected --local flag") + t.Fatal("expected --local flag") } if localFlag.Shorthand != "l" { t.Errorf("expected -l shorthand, got %q", localFlag.Shorthand) diff --git a/internal/cli/edit_cmd_test.go b/internal/cli/edit_cmd_test.go index ad7499a..7038674 100644 --- a/internal/cli/edit_cmd_test.go +++ b/internal/cli/edit_cmd_test.go @@ -30,7 +30,7 @@ func TestEditCmd_Aliases(t *testing.T) { func TestEditCmd_Flags(t *testing.T) { localFlag := editCmd.Flags().Lookup("local") if localFlag == nil { - t.Error("expected --local flag") + t.Fatal("expected --local flag") } if localFlag.Shorthand != "l" { t.Errorf("expected -l shorthand, got %q", localFlag.Shorthand) diff --git a/internal/cli/list_cmd_test.go b/internal/cli/list_cmd_test.go index 4664b2b..ee90713 100644 --- a/internal/cli/list_cmd_test.go +++ b/internal/cli/list_cmd_test.go @@ -34,7 +34,7 @@ func TestListCmd_Aliases(t *testing.T) { func TestListCmd_Flags(t *testing.T) { bareFlag := listCmd.Flags().Lookup("bare") if bareFlag == nil { - t.Error("expected --bare flag") + t.Fatal("expected --bare flag") } if bareFlag.Shorthand != "b" { t.Errorf("expected -b shorthand, got %q", bareFlag.Shorthand) @@ -42,7 +42,7 @@ func TestListCmd_Flags(t *testing.T) { sessionsFlag := listCmd.Flags().Lookup("sessions") if sessionsFlag == nil { - t.Error("expected --sessions flag") + t.Fatal("expected --sessions flag") } if sessionsFlag.Shorthand != "s" { t.Errorf("expected -s shorthand, got %q", sessionsFlag.Shorthand) diff --git a/internal/cli/main_cmd_test.go b/internal/cli/main_cmd_test.go index f4b1132..39776b9 100644 --- a/internal/cli/main_cmd_test.go +++ b/internal/cli/main_cmd_test.go @@ -10,8 +10,8 @@ func TestRunMain_NoConfig(t *testing.T) { // Create a temp directory with no config tmpDir := t.TempDir() oldWd, _ := os.Getwd() - defer os.Chdir(oldWd) - os.Chdir(tmpDir) + defer func() { _ = os.Chdir(oldWd) }() + _ = os.Chdir(tmpDir) // Set dry mode to prevent actual tmux operations dry = true @@ -41,8 +41,8 @@ testproject: } oldWd, _ := os.Getwd() - defer os.Chdir(oldWd) - os.Chdir(tmpDir) + defer func() { _ = os.Chdir(oldWd) }() + _ = os.Chdir(tmpDir) // Set dry mode dry = true @@ -70,8 +70,8 @@ existingproject: } oldWd, _ := os.Getwd() - defer os.Chdir(oldWd) - os.Chdir(tmpDir) + defer func() { _ = os.Chdir(oldWd) }() + _ = os.Chdir(tmpDir) dry = true defer func() { dry = false }() diff --git a/internal/cli/prj_cmd_test.go b/internal/cli/prj_cmd_test.go index 4b480c8..21acd9d 100644 --- a/internal/cli/prj_cmd_test.go +++ b/internal/cli/prj_cmd_test.go @@ -33,7 +33,7 @@ func TestPrjCmd_Aliases(t *testing.T) { func TestPrjCmd_Flags(t *testing.T) { saveFlag := prjCmd.Flags().Lookup("save") if saveFlag == nil { - t.Error("expected --save flag") + t.Fatal("expected --save flag") } if saveFlag.Shorthand != "s" { t.Errorf("expected -s shorthand, got %q", saveFlag.Shorthand) @@ -41,7 +41,7 @@ func TestPrjCmd_Flags(t *testing.T) { localFlag := prjCmd.Flags().Lookup("local") if localFlag == nil { - t.Error("expected --local flag") + t.Fatal("expected --local flag") } if localFlag.Shorthand != "l" { t.Errorf("expected -l shorthand, got %q", localFlag.Shorthand) diff --git a/internal/cli/remove_cmd_test.go b/internal/cli/remove_cmd_test.go index 146cc1c..e098469 100644 --- a/internal/cli/remove_cmd_test.go +++ b/internal/cli/remove_cmd_test.go @@ -30,7 +30,7 @@ func TestRemoveCmd_Aliases(t *testing.T) { func TestRemoveCmd_Flags(t *testing.T) { localFlag := removeCmd.Flags().Lookup("local") if localFlag == nil { - t.Error("expected --local flag") + t.Fatal("expected --local flag") } if localFlag.Shorthand != "l" { t.Errorf("expected -l shorthand, got %q", localFlag.Shorthand) diff --git a/internal/cli/root.go b/internal/cli/root.go index 81c5ba9..9c13b41 100644 --- a/internal/cli/root.go +++ b/internal/cli/root.go @@ -47,6 +47,8 @@ It supports complex pane layouts, fzf selection, and config merging.`, Args: cobra.MaximumNArgs(1), PersistentPreRunE: initConfig, RunE: runMain, + SilenceErrors: true, // We handle error printing in Execute() + SilenceUsage: true, // Don't print usage on runtime errors } // initConfig loads global configuration and applies settings @@ -65,11 +67,7 @@ func initConfig(cmd *cobra.Command, args []string) error { // Execute adds all child commands to the root command and sets flags appropriately func Execute() { if err := rootCmd.Execute(); err != nil { - if _, ok := err.(*UserError); ok { - fmt.Fprintln(os.Stderr, "Error:", err.Error()) - } else { - fmt.Fprintln(os.Stderr, err) - } + fmt.Fprintln(os.Stderr, "Error:", err.Error()) os.Exit(1) } } diff --git a/internal/cli/show_cmd_test.go b/internal/cli/show_cmd_test.go index e1f682c..dce59ff 100644 --- a/internal/cli/show_cmd_test.go +++ b/internal/cli/show_cmd_test.go @@ -33,7 +33,7 @@ func TestShowCmd_Aliases(t *testing.T) { func TestShowCmd_Flags(t *testing.T) { jsonFlag := showCmd.Flags().Lookup("json") if jsonFlag == nil { - t.Error("expected --json flag") + t.Fatal("expected --json flag") } if jsonFlag.Shorthand != "j" { t.Errorf("expected -j shorthand, got %q", jsonFlag.Shorthand) diff --git a/internal/config/loader_test.go b/internal/config/loader_test.go index e8261df..ee5e2df 100644 --- a/internal/config/loader_test.go +++ b/internal/config/loader_test.go @@ -297,8 +297,8 @@ testproject: // Change to temp directory oldWd, _ := os.Getwd() - defer os.Chdir(oldWd) - os.Chdir(tmpDir) + defer func() { _ = os.Chdir(oldWd) }() + _ = os.Chdir(tmpDir) result, err := findConfigFile("tmux") if err != nil { diff --git a/internal/config/parser.go b/internal/config/parser.go index ba6241a..52308ab 100644 --- a/internal/config/parser.go +++ b/internal/config/parser.go @@ -112,8 +112,8 @@ func parseWindow(w TmuxWindowInput, root string) ParsedTmuxWindow { func parseLayout(layoutInput *TmuxLayoutInput, root string) TmuxPaneLayout { if layoutInput == nil { return TmuxPaneLayout{ - Cwd: resolvePath(root, "."), - Zoom: DefaultEmptyLayout.Zoom, + Cwd: resolvePath(root, "."), + Zoom: DefaultEmptyLayout.Zoom, Split: copyTmuxSplitLayout(DefaultEmptyLayout.Split, root), } } diff --git a/internal/config/parser_test.go b/internal/config/parser_test.go index a6c8bcf..e86614f 100644 --- a/internal/config/parser_test.go +++ b/internal/config/parser_test.go @@ -15,7 +15,7 @@ func TestNameFix(t *testing.T) { {"foo", "foo"}, {"foo.bar", "foo"}, {"foo.bar.baz", "foo"}, - {".hidden", "hidden"}, // .hidden splits to ["", "hidden"], first non-empty is "hidden" + {".hidden", "hidden"}, // .hidden splits to ["", "hidden"], first non-empty is "hidden" {"", ""}, {"noextension", "noextension"}, } diff --git a/internal/config/writer.go b/internal/config/writer.go index 0ea74fa..c64cbd4 100644 --- a/internal/config/writer.go +++ b/internal/config/writer.go @@ -67,9 +67,11 @@ func AddSimpleConfigToFile(config ParsedTmuxConfigItem, local bool, dryRun bool) if err != nil { return err } - defer f.Close() _, err = f.WriteString(sb.String()) + if closeErr := f.Close(); err == nil { + err = closeErr + } return err } diff --git a/internal/config/writer_test.go b/internal/config/writer_test.go index e697816..1524e47 100644 --- a/internal/config/writer_test.go +++ b/internal/config/writer_test.go @@ -67,8 +67,8 @@ existing: // Change to temp directory so config is found oldWd, _ := os.Getwd() - defer os.Chdir(oldWd) - os.Chdir(tmpDir) + defer func() { _ = os.Chdir(oldWd) }() + _ = os.Chdir(tmpDir) config := ParsedTmuxConfigItem{ Name: "newproject", @@ -106,8 +106,8 @@ func TestAddSimpleConfigToFile(t *testing.T) { // Change to temp directory so config is found oldWd, _ := os.Getwd() - defer os.Chdir(oldWd) - os.Chdir(tmpDir) + defer func() { _ = os.Chdir(oldWd) }() + _ = os.Chdir(tmpDir) config := ParsedTmuxConfigItem{ Name: "newproject", @@ -161,8 +161,8 @@ third: // Change to temp directory so config is found oldWd, _ := os.Getwd() - defer os.Chdir(oldWd) - os.Chdir(tmpDir) + defer func() { _ = os.Chdir(oldWd) }() + _ = os.Chdir(tmpDir) err = RemoveConfigFromFile("second", false, false) if err != nil { @@ -199,8 +199,8 @@ func TestRemoveConfigFromFile_NotFound(t *testing.T) { // Change to temp directory so config is found oldWd, _ := os.Getwd() - defer os.Chdir(oldWd) - os.Chdir(tmpDir) + defer func() { _ = os.Chdir(oldWd) }() + _ = os.Chdir(tmpDir) err = RemoveConfigFromFile("nonexistent", false, false) if err == nil { @@ -223,8 +223,8 @@ func TestRemoveConfigFromFile_DryRun(t *testing.T) { // Change to temp directory so config is found oldWd, _ := os.Getwd() - defer os.Chdir(oldWd) - os.Chdir(tmpDir) + defer func() { _ = os.Chdir(oldWd) }() + _ = os.Chdir(tmpDir) err = RemoveConfigFromFile("toremove", false, true) if err != nil { @@ -259,8 +259,8 @@ last: // Change to temp directory so config is found oldWd, _ := os.Getwd() - defer os.Chdir(oldWd) - os.Chdir(tmpDir) + defer func() { _ = os.Chdir(oldWd) }() + _ = os.Chdir(tmpDir) err = RemoveConfigFromFile("last", false, false) if err != nil { diff --git a/internal/exec/runner_test.go b/internal/exec/runner_test.go index 5441510..2746b43 100644 --- a/internal/exec/runner_test.go +++ b/internal/exec/runner_test.go @@ -159,8 +159,8 @@ func TestGetShell_Default(t *testing.T) { // Unset env var oldEnv := os.Getenv("SHELL") - os.Unsetenv("SHELL") - defer os.Setenv("SHELL", oldEnv) + _ = os.Unsetenv("SHELL") + defer func() { _ = os.Setenv("SHELL", oldEnv) }() shell := getShell() // Should return one of the default shells or "sh" @@ -177,8 +177,8 @@ func TestGetShell_EnvVar(t *testing.T) { // Set env var oldEnv := os.Getenv("SHELL") - os.Setenv("SHELL", "/custom/shell") - defer os.Setenv("SHELL", oldEnv) + _ = os.Setenv("SHELL", "/custom/shell") + defer func() { _ = os.Setenv("SHELL", oldEnv) }() shell := getShell() if shell != "/custom/shell" { @@ -194,8 +194,8 @@ func TestGetShell_ConfigOverridesEnv(t *testing.T) { // Set env var too oldEnv := os.Getenv("SHELL") - os.Setenv("SHELL", "/env/shell") - defer os.Setenv("SHELL", oldEnv) + _ = os.Setenv("SHELL", "/env/shell") + defer func() { _ = os.Setenv("SHELL", oldEnv) }() shell := getShell() // Config should take priority over env diff --git a/internal/tmux/session_test.go b/internal/tmux/session_test.go index a316a13..d76fec1 100644 --- a/internal/tmux/session_test.go +++ b/internal/tmux/session_test.go @@ -19,10 +19,10 @@ func TestSessionExists_DryMode(t *testing.T) { func TestAttachToSession_InsideTmux(t *testing.T) { // Save original TMUX env origTmux := os.Getenv("TMUX") - defer os.Setenv("TMUX", origTmux) + defer func() { _ = os.Setenv("TMUX", origTmux) }() // Set TMUX to simulate being inside tmux - os.Setenv("TMUX", "/tmp/tmux-1000/default,12345,0") + _ = os.Setenv("TMUX", "/tmp/tmux-1000/default,12345,0") opts := exec.Opts{Verbose: false, Dry: true} err := AttachToSession(opts, "testsession") @@ -36,10 +36,10 @@ func TestAttachToSession_InsideTmux(t *testing.T) { func TestAttachToSession_OutsideTmux(t *testing.T) { // Save original TMUX env origTmux := os.Getenv("TMUX") - defer os.Setenv("TMUX", origTmux) + defer func() { _ = os.Setenv("TMUX", origTmux) }() // Unset TMUX to simulate being outside tmux - os.Unsetenv("TMUX") + _ = os.Unsetenv("TMUX") opts := exec.Opts{Verbose: false, Dry: true} err := AttachToSession(opts, "testsession")