summaryrefslogtreecommitdiff
path: root/gui/internal/model/newdir_test.go
blob: 19957d968aef750ce953471c770f881858e18f9b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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)
		}
	}
}