feat(themes): implement PlanForce
This commit is contained in:
@@ -4,7 +4,6 @@ package themes
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/fs"
|
"io/fs"
|
||||||
"os"
|
"os"
|
||||||
@@ -116,5 +115,45 @@ func (m Module) Verify(ctx context.Context) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (m Module) PlanForce(ctx context.Context) (module.Plan, error) {
|
func (m Module) PlanForce(ctx context.Context) (module.Plan, error) {
|
||||||
return module.Plan{}, errors.New("PlanForce not implemented")
|
dst, err := destRoot()
|
||||||
|
if err != nil {
|
||||||
|
return module.Plan{}, err
|
||||||
|
}
|
||||||
|
var pairs []filePair
|
||||||
|
err = filepath.WalkDir(m.cfg.Source, func(p string, d fs.DirEntry, werr error) error {
|
||||||
|
if werr != nil {
|
||||||
|
return werr
|
||||||
|
}
|
||||||
|
if d.IsDir() {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
rel, _ := filepath.Rel(m.cfg.Source, p)
|
||||||
|
pairs = append(pairs, filePair{src: p, dst: filepath.Join(dst, rel)})
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
if err != nil && !os.IsNotExist(err) {
|
||||||
|
return module.Plan{}, err
|
||||||
|
}
|
||||||
|
if len(pairs) == 0 {
|
||||||
|
return module.Plan{}, nil
|
||||||
|
}
|
||||||
|
return module.Plan{Actions: []module.Action{{
|
||||||
|
Description: fmt.Sprintf("rewrite %d theme files (force)", len(pairs)),
|
||||||
|
Kind: module.ActionFileWrite,
|
||||||
|
Exec: func(ctx context.Context) error {
|
||||||
|
for _, fp := range pairs {
|
||||||
|
if err := os.MkdirAll(filepath.Dir(fp.dst), 0o755); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
data, err := os.ReadFile(fp.src)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := os.WriteFile(fp.dst, data, 0o644); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
}}}, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -63,4 +63,49 @@ func applyOnce(t *testing.T, m Module) error {
|
|||||||
return m.Apply(context.Background(), p, nop{})
|
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{}
|
var _ module.Module = Module{}
|
||||||
|
|||||||
Reference in New Issue
Block a user