59 lines
1.6 KiB
Go
59 lines
1.6 KiB
Go
package shellconfig
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"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 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{}
|