2026-03-05 17:44:19 +08:00
|
|
|
package shell
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"runtime"
|
|
|
|
|
"strings"
|
|
|
|
|
"testing"
|
|
|
|
|
"time"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestCallRejectsEmptyCommand(t *testing.T) {
|
|
|
|
|
tool := New([]string{"echo"}, ".", time.Second, 4000, nil)
|
|
|
|
|
_, err := tool.Call(context.Background(), " ")
|
|
|
|
|
if err == nil {
|
|
|
|
|
t.Fatal("expected error for empty command")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestCallAllowsNonAllowlistedCommand(t *testing.T) {
|
|
|
|
|
tool := New([]string{"echo"}, ".", time.Second, 4000, nil)
|
|
|
|
|
out, err := tool.Call(context.Background(), "go version")
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("expected command to run without allowlist restriction, got err=%v", err)
|
|
|
|
|
}
|
|
|
|
|
if out == "" {
|
|
|
|
|
t.Fatal("expected non-empty output")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestCallWindowsDateIsNonInteractive(t *testing.T) {
|
|
|
|
|
if runtime.GOOS != "windows" {
|
|
|
|
|
t.Skip("windows-only test")
|
|
|
|
|
}
|
|
|
|
|
tool := New(nil, ".", 3*time.Second, 4000, nil)
|
|
|
|
|
out, err := tool.Call(context.Background(), "date")
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("expected bare date command to succeed on windows, got err=%v output=%q", err, out)
|
|
|
|
|
}
|
|
|
|
|
if strings.TrimSpace(out) == "" {
|
|
|
|
|
t.Fatal("expected non-empty output for date command")
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-03-08 22:38:29 +08:00
|
|
|
|
|
|
|
|
func TestRequiresShellParsing(t *testing.T) {
|
|
|
|
|
if !requiresShellParsing("echo hi | cat") {
|
|
|
|
|
t.Fatal("expected pipe command to require shell parsing")
|
|
|
|
|
}
|
|
|
|
|
if requiresShellParsing("echo hello") {
|
|
|
|
|
t.Fatal("expected simple command to not require shell parsing")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestCallSupportsPipeOnUnix(t *testing.T) {
|
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
|
t.Skip("unix-only behavior test")
|
|
|
|
|
}
|
|
|
|
|
tool := New([]string{"echo"}, ".", 3*time.Second, 4000, nil)
|
|
|
|
|
out, err := tool.Call(context.Background(), "printf hello | wc -c")
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("expected pipeline command success, got err=%v output=%q", err, out)
|
|
|
|
|
}
|
|
|
|
|
trimmed := strings.TrimSpace(out)
|
|
|
|
|
if trimmed != "5" {
|
|
|
|
|
t.Fatalf("expected output 5, got %q", trimmed)
|
|
|
|
|
}
|
|
|
|
|
}
|