Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 37 additions & 37 deletions internal/cli/commands/hook_test.go
Original file line number Diff line number Diff line change
@@ -1,48 +1,48 @@
package commands

import (
"bytes"
"io"
"os"
"testing"
"os"
"testing"
)

// Mockable os.Exit for testing
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{})
}
Loading