Files
sherlock/internal/installer/installer_test.go
T
amacocian 59eaf9e47d
Release / release (push) Failing after 6s
SearXNG support
2026-06-14 21:33:22 +02:00

159 lines
4.3 KiB
Go

package installer
import (
"os"
"path/filepath"
"strings"
"testing"
)
func TestVersionFile(t *testing.T) {
dir := t.TempDir()
if v := versionFile(dir); v != "" {
t.Fatalf("missing VERSION should yield empty, got %q", v)
}
if err := os.WriteFile(filepath.Join(dir, "VERSION"), []byte("0.3.0\n"), 0o644); err != nil {
t.Fatal(err)
}
if v := versionFile(dir); v != "0.3.0" {
t.Fatalf("versionFile = %q, want 0.3.0", v)
}
}
func TestPickEditor_PrefersVisualThenEditor(t *testing.T) {
t.Setenv("VISUAL", "myvisual")
t.Setenv("EDITOR", "myeditor")
if got := pickEditor(); got != "myvisual" {
t.Fatalf("pickEditor = %q, want myvisual", got)
}
t.Setenv("VISUAL", "")
if got := pickEditor(); got != "myeditor" {
t.Fatalf("pickEditor = %q, want myeditor", got)
}
}
func TestContainsPlaceholders(t *testing.T) {
dir := t.TempDir()
p := filepath.Join(dir, "config.toml")
_ = os.WriteFile(p, []byte("client_id = \"REPLACE_ME\"\n"), 0o600)
if !containsPlaceholders(p) {
t.Fatal("expected placeholders detected")
}
_ = os.WriteFile(p, []byte("client_id = \"abc123\"\n"), 0o600)
if containsPlaceholders(p) {
t.Fatal("did not expect placeholders")
}
}
func TestSeedConfig_DoesNotClobber(t *testing.T) {
dir := t.TempDir()
example := filepath.Join(dir, "config.example.toml")
_ = os.WriteFile(example, []byte("example\n"), 0o644)
cfg := filepath.Join(dir, "out", "config.toml")
opts := &Options{Out: io_discard{}, Err: io_discard{}}
// First seed: creates the file from the example.
if err := seedConfig(opts, example, cfg); err != nil {
t.Fatalf("seedConfig: %v", err)
}
if b, _ := os.ReadFile(cfg); string(b) != "example\n" {
t.Fatalf("seeded content = %q", b)
}
// Operator fills it in.
_ = os.WriteFile(cfg, []byte("filled\n"), 0o600)
// Re-seed must NOT overwrite.
if err := seedConfig(opts, example, cfg); err != nil {
t.Fatalf("seedConfig re-run: %v", err)
}
if b, _ := os.ReadFile(cfg); string(b) != "filled\n" {
t.Fatalf("re-seed clobbered config: %q", b)
}
}
func TestAppendMissingConfigSections(t *testing.T) {
dir := t.TempDir()
example := filepath.Join(dir, "config.example.toml")
cfg := filepath.Join(dir, "config.toml")
if err := os.WriteFile(example, []byte(`
[providers.sherlock-cli]
issuer = "https://id.example/"
client_id = "REPLACE"
[services.gitea]
issuer = "https://gitea.example"
client_id = "gitea"
base_url = "https://gitea.example"
[services.searxng]
provider = "sherlock-cli"
base_url = "https://search.example"
`), 0o644); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(cfg, []byte(`
[providers.sherlock-cli]
issuer = "https://id.real/"
client_id = "real"
[services.gitea]
issuer = "https://gitea.real"
client_id = "gitea-real"
base_url = "https://gitea.real"
`), 0o600); err != nil {
t.Fatal(err)
}
opts := &Options{Out: io_discard{}, Err: io_discard{}}
added, err := appendMissingConfigSections(opts, example, cfg)
if err != nil {
t.Fatalf("appendMissingConfigSections: %v", err)
}
if len(added) != 1 || added[0] != "services.searxng" {
t.Fatalf("added = %v, want [services.searxng]", added)
}
b, err := os.ReadFile(cfg)
if err != nil {
t.Fatal(err)
}
body := string(b)
if !strings.Contains(body, "Added by sherlock") {
t.Fatalf("missing marker comment in %q", body)
}
if strings.Count(body, "[services.searxng]") != 1 {
t.Fatalf("searxng section count wrong in %q", body)
}
added, err = appendMissingConfigSections(opts, example, cfg)
if err != nil {
t.Fatalf("second appendMissingConfigSections: %v", err)
}
if len(added) != 0 {
t.Fatalf("second added = %v, want none", added)
}
}
func TestFishCompletionsDir(t *testing.T) {
tmp := t.TempDir()
t.Setenv("__fish_config_dir", "")
t.Setenv("XDG_CONFIG_HOME", tmp)
// No fish dir yet → empty.
if d := fishCompletionsDir(); d != "" {
t.Fatalf("no fish dir should yield empty, got %q", d)
}
// Create the fish dir → returns its completions subdir.
if err := os.MkdirAll(filepath.Join(tmp, "fish"), 0o755); err != nil {
t.Fatal(err)
}
want := filepath.Join(tmp, "fish", "completions")
if d := fishCompletionsDir(); d != want {
t.Fatalf("fishCompletionsDir = %q, want %q", d, want)
}
}
// io_discard is a tiny io.Writer that drops everything (avoids importing
// io just for io.Discard in this test file).
type io_discard struct{}
func (io_discard) Write(p []byte) (int, error) { return len(p), nil }