Files
Archduino/internal/modules/themes/themes_test.go

67 lines
1.8 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{})
}
var _ module.Module = Module{}