hugin/cli/internal/tools/opencode_test.go
2026-10-27 08:33:21 +01:00

38 lines
1.2 KiB
Go

package tools
import (
"os"
"path/filepath"
"testing"
)
// fakeOpenCode legt ein Programm „opencode“ in einen eigenen PATH.
func fakeOpenCode(t *testing.T, script string) {
t.Helper()
bin := t.TempDir()
if err := os.WriteFile(filepath.Join(bin, "opencode"), []byte("#!/bin/sh\n"+script), 0o755); err != nil {
t.Fatal(err)
}
t.Setenv("PATH", bin)
}
func TestCheckOpenCode(t *testing.T) {
repo := t.TempDir()
fakeOpenCode(t, "exit 0\n")
if msg := CheckOpenCode(repo); msg != "" {
t.Errorf("gültige Konfiguration: %q", msg)
}
fakeOpenCode(t, `printf '\033[91m\033[1mError: \033[0mConfiguration is invalid at %s/opencode.json\n↳ Missing key mcp.x.enabled\n' "$PWD" >&2; exit 1`+"\n")
msg := CheckOpenCode(repo)
want := "Configuration is invalid at " + repo + "/opencode.json\n↳ Missing key mcp.x.enabled"
if resolved, _ := filepath.EvalSymlinks(repo); msg != want && msg != "Configuration is invalid at "+resolved+"/opencode.json\n↳ Missing key mcp.x.enabled" {
t.Errorf("Meldung ohne Farbcodes erwartet:\n%q\nbekommen:\n%q", want, msg)
}
t.Setenv("PATH", t.TempDir())
if msg := CheckOpenCode(repo); msg != "" {
t.Errorf("ohne OpenCode wird nichts geprüft: %q", msg)
}
}