feat(modules): port shellconfig module to Go
This commit is contained in:
58
internal/modules/shellconfig/shellconfig_test.go
Normal file
58
internal/modules/shellconfig/shellconfig_test.go
Normal 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{}
|
||||
Reference in New Issue
Block a user