diff options
| author | Lukasz Kasprzak <lukas@labunix.xyz> | 2026-09-16 00:53:52 +0200 |
|---|---|---|
| committer | Lukasz Kasprzak <lukas@labunix.xyz> | 2026-09-16 00:53:52 +0200 |
| commit | 9a77de31dc9759708ce9c2d14ea7c0876c214e71 (patch) | |
| tree | 9bd8bab97206468bdd1eed3d77481e37d3fe75e7 /internal/engine | |
| parent | 1b39b2683eb820560ba882936d4818af0dcfcec7 (diff) | |
| download | krino-9a77de31dc9759708ce9c2d14ea7c0876c214e71.tar.gz krino-9a77de31dc9759708ce9c2d14ea7c0876c214e71.zip | |
config and engine can load with unsaved text
Diffstat (limited to 'internal/engine')
| -rw-r--r-- | internal/engine/engine.go | 9 | ||||
| -rw-r--r-- | internal/engine/engine_test.go | 18 |
2 files changed, 26 insertions, 1 deletions
diff --git a/internal/engine/engine.go b/internal/engine/engine.go index 958e4f9..cc776e6 100644 --- a/internal/engine/engine.go +++ b/internal/engine/engine.go @@ -80,7 +80,14 @@ type Rule struct { // Engine and every diagnostic: krino never acts on a configuration it only // partly understood. Duplicate names are ignored after their first use. func Load(mainFile string, names ...string) (*Engine, []*config.Diag) { - cfg, errs := config.Load(mainFile, dedupeNames(names)...) + return LoadWith(mainFile, nil, names...) +} + +// LoadWith is Load with some configuration files' text supplied by the +// caller (config.LoadWith): the GUI checks unsaved editor text with it, +// compiled exactly as a run would compile it (GUI design §1.3). +func LoadWith(mainFile string, overrides map[string][]byte, names ...string) (*Engine, []*config.Diag) { + cfg, errs := config.LoadWith(mainFile, overrides, dedupeNames(names)...) if cfg == nil { return nil, errs } diff --git a/internal/engine/engine_test.go b/internal/engine/engine_test.go index 7de2f76..b6b50fc 100644 --- a/internal/engine/engine_test.go +++ b/internal/engine/engine_test.go @@ -11,6 +11,7 @@ import ( "time" "krino/internal/cond" + "krino/internal/config" ) // sandbox gives a test its own HOME with no XDG overrides and returns it. @@ -296,3 +297,20 @@ func TestLoadRefusesBadPlaceholders(t *testing.T) { t.Errorf("valid placeholders refused: %v", errs) } } + +// TestEngineLoadWith: the engine compiles overridden text too, so unsaved +// rules are checked exactly as a run would read them (GUI design §1.3). +func TestEngineLoadWith(t *testing.T) { + h := sandbox(t) + os.MkdirAll(filepath.Join(h, "dl"), 0o755) + main := writeConfig(t, h, `(include "dl")`, map[string]string{"dl": "(path \"~/dl\")\n"}) + over := map[string][]byte{config.DirFile(main, "dl"): []byte("(path \"~/dl\")\n(rule \"r\" (when (bogus)) (move \"Out\"))\n")} + if _, errs := LoadWith(main, over); len(errs) == 0 { + t.Error("a mistake in the overridden directory file was not reported") + } + good := map[string][]byte{config.DirFile(main, "dl"): []byte("(path \"~/dl\")\n(rule \"r\" (move \"Out\"))\n")} + e, errs := LoadWith(main, good) + if len(errs) > 0 || len(e.Dirs) != 1 || len(e.Dirs[0].Rules) != 1 { + t.Errorf("overridden rules not compiled: %v", errs) + } +} |
