96 lines
2.7 KiB
Go
96 lines
2.7 KiB
Go
package installer
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"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 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 }
|