112 lines
3.2 KiB
Go
112 lines
3.2 KiB
Go
package themes
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"gitlab.hive-brain.de/MDietermann/Archduino/internal/config"
|
|
"gitlab.hive-brain.de/MDietermann/Archduino/internal/module"
|
|
)
|
|
|
|
type nop struct{}
|
|
|
|
func (nop) Progress(string, int, int, string) {}
|
|
func (nop) Log(string, string) {}
|
|
|
|
func TestThemesCopiesFiles(t *testing.T) {
|
|
src := t.TempDir()
|
|
if err := os.WriteFile(filepath.Join(src, "one.toml"), []byte("A"), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
os.MkdirAll(filepath.Join(src, "sub"), 0o755)
|
|
os.WriteFile(filepath.Join(src, "sub", "two.toml"), []byte("B"), 0o644)
|
|
|
|
home := t.TempDir()
|
|
t.Setenv("HOME", home)
|
|
m := New(config.ThemesConfig{Source: src})
|
|
st, _ := m.Detect(context.Background())
|
|
p, _ := m.Plan(context.Background(), st)
|
|
if err := m.Apply(context.Background(), p, nop{}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
got, _ := os.ReadFile(filepath.Join(home, ".config", "archduino", "themes", "one.toml"))
|
|
if string(got) != "A" {
|
|
t.Fatalf("one.toml: %q", got)
|
|
}
|
|
got, _ = os.ReadFile(filepath.Join(home, ".config", "archduino", "themes", "sub", "two.toml"))
|
|
if string(got) != "B" {
|
|
t.Fatalf("two.toml: %q", got)
|
|
}
|
|
}
|
|
|
|
func TestThemesIdempotent(t *testing.T) {
|
|
src := t.TempDir()
|
|
os.WriteFile(filepath.Join(src, "x.toml"), []byte("X"), 0o644)
|
|
home := t.TempDir()
|
|
t.Setenv("HOME", home)
|
|
m := New(config.ThemesConfig{Source: src})
|
|
_ = applyOnce(t, m)
|
|
st, _ := m.Detect(context.Background())
|
|
p, _ := m.Plan(context.Background(), st)
|
|
if !p.IsEmpty() {
|
|
t.Fatalf("second plan not empty: %+v", p.Actions)
|
|
}
|
|
}
|
|
|
|
func applyOnce(t *testing.T, m Module) error {
|
|
t.Helper()
|
|
st, _ := m.Detect(context.Background())
|
|
p, _ := m.Plan(context.Background(), st)
|
|
return m.Apply(context.Background(), p, nop{})
|
|
}
|
|
|
|
func TestPlanForceRewritesAllThemes(t *testing.T) {
|
|
src := t.TempDir()
|
|
os.WriteFile(filepath.Join(src, "one.toml"), []byte("FRESH-A"), 0o644)
|
|
os.MkdirAll(filepath.Join(src, "sub"), 0o755)
|
|
os.WriteFile(filepath.Join(src, "sub", "two.toml"), []byte("FRESH-B"), 0o644)
|
|
|
|
home := t.TempDir()
|
|
t.Setenv("HOME", home)
|
|
m := New(config.ThemesConfig{Source: src})
|
|
dst := filepath.Join(home, ".config", "archduino", "themes")
|
|
os.MkdirAll(filepath.Join(dst, "sub"), 0o755)
|
|
os.WriteFile(filepath.Join(dst, "one.toml"), []byte("FRESH-A"), 0o644)
|
|
os.WriteFile(filepath.Join(dst, "sub", "two.toml"), []byte("FRESH-B"), 0o644)
|
|
|
|
st, _ := m.Detect(context.Background())
|
|
p, _ := m.Plan(context.Background(), st)
|
|
if !p.IsEmpty() {
|
|
t.Fatalf("precondition: plan should be empty, got %+v", p.Actions)
|
|
}
|
|
|
|
os.WriteFile(filepath.Join(dst, "one.toml"), []byte("STALE"), 0o644)
|
|
os.WriteFile(filepath.Join(dst, "sub", "two.toml"), []byte("STALE"), 0o644)
|
|
|
|
plan, err := m.PlanForce(context.Background())
|
|
if err != nil {
|
|
t.Fatalf("PlanForce: %v", err)
|
|
}
|
|
if plan.IsEmpty() {
|
|
t.Fatal("force plan must not be empty")
|
|
}
|
|
for _, a := range plan.Actions {
|
|
if err := a.Exec(context.Background()); err != nil {
|
|
t.Fatalf("Exec: %v", err)
|
|
}
|
|
}
|
|
got, _ := os.ReadFile(filepath.Join(dst, "one.toml"))
|
|
if string(got) != "FRESH-A" {
|
|
t.Fatalf("one.toml not rewritten: %q", got)
|
|
}
|
|
got, _ = os.ReadFile(filepath.Join(dst, "sub", "two.toml"))
|
|
if string(got) != "FRESH-B" {
|
|
t.Fatalf("two.toml not rewritten: %q", got)
|
|
}
|
|
}
|
|
|
|
var _ module.Module = Module{}
|