aboutsummaryrefslogtreecommitdiff
path: root/gui/internal/model/newdir_test.go
diff options
context:
space:
mode:
authorLukasz Kasprzak <lukas@labunix.xyz>2026-09-17 10:56:19 +0200
committerLukasz Kasprzak <lukas@labunix.xyz>2026-09-17 10:56:19 +0200
commit48d1781fed3f9ea5cfd2b270bd92fd2826605636 (patch)
tree8abddf0ae7aaab97426cad1f65038560c8b85b9f /gui/internal/model/newdir_test.go
parenta2cb20851499e9c00bd3bf642680a9ce3148cae8 (diff)
downloadkrino-48d1781fed3f9ea5cfd2b270bd92fd2826605636.tar.gz
krino-48d1781fed3f9ea5cfd2b270bd92fd2826605636.zip
gui: conditions as a nested tree, and a way to add a directory
Diffstat (limited to 'gui/internal/model/newdir_test.go')
-rw-r--r--gui/internal/model/newdir_test.go72
1 files changed, 72 insertions, 0 deletions
diff --git a/gui/internal/model/newdir_test.go b/gui/internal/model/newdir_test.go
new file mode 100644
index 0000000..19957d9
--- /dev/null
+++ b/gui/internal/model/newdir_test.go
@@ -0,0 +1,72 @@
+// SPDX-License-Identifier: GPL-3.0-or-later
+
+package model
+
+import (
+ "os"
+ "path/filepath"
+ "strings"
+ "testing"
+
+ "krino/internal/engine"
+)
+
+// TestAddDirectory: a directory added in the window is one krino loads, its
+// file written from the template and its name in the include.
+func TestAddDirectory(t *testing.T) {
+ conf := "(path \"~/dl\")\n(rule \"all\" (move \"Out\"))\n"
+ e, h := sandboxDir(t, conf, map[string]string{"a.pdf": "one"})
+ target := filepath.Join(h, "papers")
+ if err := os.MkdirAll(target, 0o755); err != nil {
+ t.Fatal(err)
+ }
+
+ file, err := AddDirectory(e, "papers", target)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if _, err := os.Stat(file); err != nil {
+ t.Fatalf("the file was not written: %v", err)
+ }
+ main, err := os.ReadFile(e.MainFile)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if !strings.Contains(string(main), "papers") {
+ t.Errorf("the include does not name it:\n%s", main)
+ }
+
+ fresh, diags := engine.Load(e.MainFile)
+ if len(diags) > 0 {
+ t.Fatalf("the configuration no longer loads: %v", diags)
+ }
+ found := false
+ for _, d := range fresh.Dirs {
+ if d.Name == "papers" && d.Root == target {
+ found = true
+ }
+ }
+ if !found {
+ t.Errorf("the new directory is not in the configuration: %+v", fresh.Dirs)
+ }
+ // And the window can open its rules straight away.
+ if _, err := OpenRules(fresh, "papers"); err != nil {
+ t.Errorf("its rules do not open: %v", err)
+ }
+}
+
+// TestAddDirectoryRefusals: the checks krino new makes are the checks the
+// window makes, because it is the same code.
+func TestAddDirectoryRefusals(t *testing.T) {
+ conf := "(path \"~/dl\")\n(rule \"all\" (move \"Out\"))\n"
+ e, h := sandboxDir(t, conf, map[string]string{"a.pdf": "one"})
+ for _, c := range []struct{ name, path, why string }{
+ {"has space", h, "a name with a space"},
+ {"check", h, "a krino command"},
+ {"dl", h, "a name already taken"},
+ } {
+ if _, err := AddDirectory(e, c.name, c.path); err == nil {
+ t.Errorf("%s was accepted", c.why)
+ }
+ }
+}