diff options
Diffstat (limited to 'internal/config')
| -rw-r--r-- | internal/config/load.go | 22 | ||||
| -rw-r--r-- | internal/config/load_test.go | 27 |
2 files changed, 47 insertions, 2 deletions
diff --git a/internal/config/load.go b/internal/config/load.go index cd4d97c..3811802 100644 --- a/internal/config/load.go +++ b/internal/config/load.go @@ -20,6 +20,15 @@ type Config struct { Dirs []*Dir } +// readSource is the text of file: the caller's override when it has one, +// else the file's own bytes. +func readSource(overrides map[string][]byte, file string) ([]byte, error) { + if src, ok := overrides[file]; ok { + return src, nil + } + return os.ReadFile(file) +} + // DefaultFile is krino.conf in the XDG config directory. func DefaultFile() string { return filepath.Join(xdg.ConfigHome(), "krino", "krino.conf") @@ -34,7 +43,16 @@ func DirFile(mainFile, name string) string { // With names, only those directories are read, and each must be included. // The Config is nil only when the main file itself cannot be read. func Load(mainFile string, names ...string) (*Config, []*Diag) { - src, err := os.ReadFile(mainFile) + return LoadWith(mainFile, nil, names...) +} + +// LoadWith is Load with some files' text supplied by the caller: overrides +// maps a file path - mainFile, or DirFile(mainFile, name) - to the text to +// read instead of that file's own, so an editor can have unsaved text +// checked exactly as a run would read it (GUI design §1.3). A nil map is +// Load. +func LoadWith(mainFile string, overrides map[string][]byte, names ...string) (*Config, []*Diag) { + src, err := readSource(overrides, mainFile) if errors.Is(err, fs.ErrNotExist) { return nil, []*Diag{{File: mainFile, Msg: "not found; create it with: krino init"}} } @@ -62,7 +80,7 @@ func Load(mainFile string, names ...string) (*Config, []*Diag) { } for _, name := range want { file := DirFile(mainFile, name) - src, err := os.ReadFile(file) + src, err := readSource(overrides, file) if err != nil { msg := err.Error() if errors.Is(err, fs.ErrNotExist) { diff --git a/internal/config/load_test.go b/internal/config/load_test.go index 1cc3ac9..13ef9d8 100644 --- a/internal/config/load_test.go +++ b/internal/config/load_test.go @@ -115,3 +115,30 @@ func TestLockFile(t *testing.T) { t.Errorf("LockFile = %q, want %q", got, want) } } + +// TestLoadWithOverriddenText: LoadWith reads the text the caller supplies +// instead of a file's own, so an editor can check what it has not saved yet +// (GUI design §1.3); the files on disk are neither read differently nor +// changed. +func TestLoadWithOverriddenText(t *testing.T) { + h := t.TempDir() + t.Setenv("HOME", h) + main := filepath.Join(h, "krino.conf") + if err := os.MkdirAll(filepath.Join(h, "dirs"), 0o755); err != nil { + t.Fatal(err) + } + os.WriteFile(main, []byte("(include \"dl\")\n"), 0o644) + os.WriteFile(filepath.Join(h, "dirs", "dl.conf"), []byte("(path \"/tmp\")\n"), 0o644) + + over := map[string][]byte{filepath.Join(h, "dirs", "dl.conf"): []byte("(path \"/tmp\")\n(rule \"r\" (move \"Out\"))\n")} + cfg, errs := LoadWith(main, over) + if len(errs) > 0 || len(cfg.Dirs) != 1 || len(cfg.Dirs[0].Rules) != 1 { + t.Fatalf("overridden text not used: %v %+v", errs, cfg.Dirs) + } + if _, errs := LoadWith(main, map[string][]byte{main: []byte("(include \"dl\"")}); len(errs) == 0 { + t.Error("a mistake in the overridden main file was not reported") + } + if cfg, errs := Load(main); len(errs) > 0 || len(cfg.Dirs[0].Rules) != 0 { + t.Errorf("the files on disk were read differently: %v %+v", errs, cfg.Dirs) + } +} |
