// 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) } } }