feat(modules): port shellconfig module to Go

This commit is contained in:
MDietermann
2026-04-14 09:10:16 +02:00
parent 99db34d70f
commit 7de9d6c72c
2 changed files with 148 additions and 0 deletions

View File

@@ -0,0 +1,58 @@
package shellconfig
import (
"context"
"os"
"path/filepath"
"strings"
"testing"
"github.com/MDietermann/ArduinoArchSetup/internal/config"
"github.com/MDietermann/ArduinoArchSetup/internal/module"
)
type nop struct{}
func (nop) Progress(string, int, int, string) {}
func (nop) Log(string, string) {}
func TestShellConfigWritesBlocks(t *testing.T) {
home := t.TempDir()
t.Setenv("HOME", home)
m := New(config.ShellConfigSection{
Blocks: []config.ShellConfigBlock{
{Name: "path", File: ".zshrc", Content: `export PATH="$HOME/.local/bin:$PATH"`},
},
})
st, _ := m.Detect(context.Background())
p, _ := m.Plan(context.Background(), st)
if err := m.Apply(context.Background(), p, nop{}); err != nil {
t.Fatal(err)
}
data, _ := os.ReadFile(filepath.Join(home, ".zshrc"))
if !strings.Contains(string(data), ">>> path >>>") {
t.Fatalf("block not present: %q", data)
}
if !strings.Contains(string(data), `export PATH="$HOME/.local/bin:$PATH"`) {
t.Fatalf("content missing: %q", data)
}
}
func TestShellConfigIdempotent(t *testing.T) {
home := t.TempDir()
t.Setenv("HOME", home)
cfg := config.ShellConfigSection{Blocks: []config.ShellConfigBlock{
{Name: "x", File: ".zshrc", Content: "echo x"},
}}
m := New(cfg)
st, _ := m.Detect(context.Background())
p, _ := m.Plan(context.Background(), st)
_ = m.Apply(context.Background(), p, nop{})
st2, _ := m.Detect(context.Background())
p2, _ := m.Plan(context.Background(), st2)
if !p2.IsEmpty() {
t.Fatalf("second plan not empty: %+v", p2.Actions)
}
}
var _ module.Module = Module{}