Pulse/pkg/pulsecli/root_test.go
2026-03-18 16:06:30 +00:00

63 lines
1.7 KiB
Go

package pulsecli
import (
"bytes"
"context"
"io"
"strings"
"testing"
)
func TestNewRootCommandVersionSkipsRunE(t *testing.T) {
runCalled := false
cmd := NewRootCommand(CommandSpec{
Use: "pulse",
Short: "Pulse",
Long: "Pulse",
Version: "1.2.3",
VersionPrinter: func(w io.Writer) { _, _ = w.Write([]byte("Pulse 1.2.3\n")) },
}, RuntimeSpec{
Run: func(context.Context) error { runCalled = true; return nil },
}, CommandDeps{})
out := bytes.NewBuffer(nil)
cmd.SetOut(out)
cmd.SetErr(out)
cmd.SetArgs([]string{"version"})
if err := cmd.ExecuteContext(context.Background()); err != nil {
t.Fatalf("ExecuteContext(version): %v", err)
}
if runCalled {
t.Fatal("RunE should not be called for version")
}
if got := out.String(); !strings.Contains(got, "Pulse 1.2.3") {
t.Fatalf("version output = %q", got)
}
}
func TestNewRootCommandConfigInfoSkipsRunE(t *testing.T) {
runCalled := false
cmd := NewRootCommand(CommandSpec{
Use: "pulse",
Short: "Pulse",
Long: "Pulse",
}, RuntimeSpec{
Run: func(context.Context) error { runCalled = true; return nil },
}, CommandDeps{
Config: &ConfigDeps{},
})
out := bytes.NewBuffer(nil)
tempDir := t.TempDir()
t.Setenv("PULSE_DATA_DIR", tempDir)
cmd.SetOut(out)
cmd.SetErr(out)
cmd.SetArgs([]string{"config", "info"})
if err := cmd.ExecuteContext(context.Background()); err != nil {
t.Fatalf("ExecuteContext(config info): %v", err)
}
if runCalled {
t.Fatal("RunE should not be called for config info")
}
if got := out.String(); !strings.Contains(got, "Pulse Configuration Information") || !strings.Contains(got, tempDir+"/") {
t.Fatalf("config info output = %q", got)
}
}