From 8c0090458c69c7e9cb6a76b98e5b7c92b9354f3e Mon Sep 17 00:00:00 2001 From: andev0x Date: Mon, 22 Jun 2026 16:56:33 +0700 Subject: [PATCH] style(commands): normalize resource handling - remove unused imports - add error check for io operation - simplify recovery logic --- internal/cli/commands/hook_test.go | 74 +++++++++++++++--------------- 1 file changed, 37 insertions(+), 37 deletions(-) diff --git a/internal/cli/commands/hook_test.go b/internal/cli/commands/hook_test.go index c7d4601..6ff05c3 100644 --- a/internal/cli/commands/hook_test.go +++ b/internal/cli/commands/hook_test.go @@ -1,10 +1,8 @@ package commands import ( - "bytes" - "io" - "os" - "testing" + "os" + "testing" ) // Mockable os.Exit for testing @@ -12,37 +10,39 @@ var osExitOriginal = os.Exit var osExit = os.Exit func TestHookPreToolInvalidSymbol(t *testing.T) { - // Prepare JSON input with a non-existent symbol - jsonInput := []byte(`{"tool_name":"pizen-lea__impact","tool_input":{"symbol":"nonexistent_symbol"}}`) - // Replace stdin with pipe containing JSON input - oldStdin := os.Stdin - r, w, err := os.Pipe() - if err != nil { - t.Fatalf("pipe error: %v", err) - } - _, err = w.Write(jsonInput) - if err != nil { - t.Fatalf("write error: %v", err) - } - w.Close() - os.Stdin = r - // Capture exit code via mocking os.Exit - exitCode := 0 - osExit = func(code int) { - exitCode = code - panic("os.Exit called") - } - defer func() { - // Restore globals - os.Stdin = oldStdin - os.Exit = osExitOriginal - if r := recover(); r != nil { - // expected panic from os.Exit - } - if exitCode != 2 { - t.Fatalf("expected exit code 2, got %d", exitCode) - } - }() - // Run the pre-tool hook command - hookPreToolCmd.Run(nil, []string{}) + // Prepare JSON input with a non-existent symbol + jsonInput := []byte(`{"tool_name":"pizen-lea__impact","tool_input":{"symbol":"nonexistent_symbol"}}`) + // Replace stdin with pipe containing JSON input + oldStdin := os.Stdin + r, w, err := os.Pipe() + if err != nil { + t.Fatalf("pipe error: %v", err) + } + _, err = w.Write(jsonInput) + if err != nil { + t.Fatalf("write error: %v", err) + } + if err := w.Close(); err != nil { + t.Fatalf("close error: %v", err) + } + + os.Stdin = r + // Capture exit code via mocking os.Exit + exitCode := 0 + osExit = func(code int) { + exitCode = code + panic("os.Exit called") + } + defer func() { + // Restore stdin and osExit variable + os.Stdin = oldStdin + osExit = osExitOriginal + // Recover from mocked os.Exit panic + _ = recover() // ignore panic from os.Exit mock + if exitCode != 2 { + t.Fatalf("expected exit code 2, got %d", exitCode) + } + }() + // Run the pre-tool hook command + hookPreToolCmd.Run(nil, []string{}) }