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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
|
// SPDX-License-Identifier: GPL-3.0-or-later
package config
import (
"fmt"
"os"
"path/filepath"
"strings"
"testing"
"time"
)
// writeFiles creates each file under root, making directories as needed.
func writeFiles(t *testing.T, root string, files map[string]string) {
t.Helper()
for name, body := range files {
p := filepath.Join(root, name)
if err := os.MkdirAll(filepath.Dir(p), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(p, []byte(body), 0o644); err != nil {
t.Fatal(err)
}
}
}
func TestLoad(t *testing.T) {
root := t.TempDir()
writeFiles(t, root, map[string]string{
"krino.conf": `(include "a" "b") (defaults (min-age 5m))`,
"dirs/a.conf": `(path "/tmp/a") (min-age 1m) (rule "r" (stop))`,
"dirs/b.conf": `(path "/tmp/b") (rule "r" (stop))`,
})
main := filepath.Join(root, "krino.conf")
cfg, errs := Load(main)
if len(errs) > 0 {
t.Fatal(errs)
}
if len(cfg.Dirs) != 2 || cfg.Dirs[0].Name != "a" || cfg.Dirs[1].Path != "/tmp/b" {
t.Fatalf("dirs = %+v", cfg.Dirs)
}
if got := cfg.Resolved(cfg.Dirs[0]).MinAge; got != time.Minute {
t.Errorf("a min-age = %v, want the directory's 1m", got)
}
if got := cfg.Resolved(cfg.Dirs[1]).MinAge; got != 5*time.Minute {
t.Errorf("b min-age = %v, want the default 5m", got)
}
cfg, errs = Load(main, "b")
if len(errs) > 0 || len(cfg.Dirs) != 1 || cfg.Dirs[0].Name != "b" {
t.Fatalf("Load(b) = %+v, %v", cfg, errs)
}
}
func TestLoadErrors(t *testing.T) {
root := t.TempDir()
main := filepath.Join(root, "krino.conf")
cfg, errs := Load(main)
if cfg != nil || len(errs) != 1 || errs[0].Error() != main+": not found; create it with: krino init" {
t.Fatalf("missing main file: %v", errs)
}
writeFiles(t, root, map[string]string{"krino.conf": "(include \"gone\")\n"})
_, errs = Load(main)
want := fmt.Sprintf(`%s:1:10: included "gone", but %s does not exist; create it with: krino new gone PATH`,
main, filepath.Join(root, "dirs", "gone.conf"))
if len(errs) != 1 || errs[0].Error() != want {
t.Fatalf("missing dir file:\n got %v\n want %s", errs, want)
}
_, errs = Load(main, "other")
if len(errs) != 1 || !strings.HasSuffix(errs[0].Error(), `: "other" is not in include`) {
t.Fatalf("unknown name: %v", errs)
}
}
// TestLoadSyntaxErrorStopsAtOneDiag is item C: a krino.conf that fails to
// parse must report only the syntax error, not also "not in include" for
// names the caller asked for.
func TestLoadSyntaxErrorStopsAtOneDiag(t *testing.T) {
root := t.TempDir()
writeFiles(t, root, map[string]string{"krino.conf": `(include "dl"`})
main := filepath.Join(root, "krino.conf")
_, errs := Load(main, "dl")
if len(errs) != 1 {
t.Fatalf("errs = %v, want exactly one diag", errs)
}
want := fmt.Sprintf(`%s:1:1: "(" never closed: (include "dl")`, main)
if errs[0].Error() != want {
t.Fatalf("got %s\nwant %s", errs[0], want)
}
}
func TestDefaultFileAndLogFile(t *testing.T) {
t.Setenv("XDG_CONFIG_HOME", "/conf")
t.Setenv("XDG_STATE_HOME", "/state")
if got := DefaultFile(); got != "/conf/krino/krino.conf" {
t.Errorf("DefaultFile() = %q", got)
}
c := &Config{Main: &Main{}}
if got := c.LogFile(); got != "/state/krino/krino.log" {
t.Errorf("LogFile() = %q", got)
}
c.Main.Log = "/x.log"
if got := c.LogFile(); got != "/x.log" {
t.Errorf("LogFile() = %q", got)
}
}
func TestLockFile(t *testing.T) {
h := t.TempDir()
t.Setenv("HOME", h)
t.Setenv("XDG_STATE_HOME", "")
c := &Config{}
want := filepath.Join(h, ".local", "state", "krino", "dl.lock")
if got := c.LockFile("dl"); got != want {
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)
}
}
// TestLoadWithReportsAnUnusedOverride: an override whose path does not name
// a file the load reads - a different spelling of it, or a directory not in
// include - is reported, instead of the file on disk being read as though
// the unsaved text were fine (plan 13 review F4).
func TestLoadWithReportsAnUnusedOverride(t *testing.T) {
h := t.TempDir()
t.Setenv("HOME", h)
main := filepath.Join(h, "krino.conf")
os.MkdirAll(filepath.Join(h, "dirs"), 0o755)
os.WriteFile(main, []byte("(include \"dl\")\n"), 0o644)
os.WriteFile(filepath.Join(h, "dirs", "dl.conf"), []byte("(path \"/tmp\")\n"), 0o644)
broken := []byte("(path \"/tmp\")\n(rule \"BROKEN\")\n")
// The same file, spelled with a "." segment: the text must still be used.
uncleaned := filepath.Join(h, "dirs") + "/../dirs/dl.conf"
if _, errs := LoadWith(main, map[string][]byte{uncleaned: broken}); !strings.Contains(diagText(errs), "BROKEN") {
t.Errorf("an override keyed by an uncleaned path was ignored: %v", errs)
}
// A file this load never reads: say so rather than pass silently.
other := filepath.Join(h, "dirs", "other.conf")
errs := diagText(func() []*Diag { _, e := LoadWith(main, map[string][]byte{other: broken}); return e }())
if !strings.Contains(errs, "other.conf") {
t.Errorf("an override for a file that is not read went unreported: %s", errs)
}
}
func diagText(ds []*Diag) string {
var b strings.Builder
for _, d := range ds {
b.WriteString(d.Error() + "\n")
}
return b.String()
}
// TestLoadWithOverrideForAnotherIncludedDirectory: checking one directory
// while holding text for another included one is normal for an editor, and
// not an error; only text for a file no configuration file names is
// reported (plan 13 review F4 follow-up).
func TestLoadWithOverrideForAnotherIncludedDirectory(t *testing.T) {
h := t.TempDir()
t.Setenv("HOME", h)
main := filepath.Join(h, "krino.conf")
os.MkdirAll(filepath.Join(h, "dirs"), 0o755)
os.WriteFile(main, []byte("(include \"a\" \"b\")\n"), 0o644)
for _, n := range []string{"a", "b"} {
os.WriteFile(filepath.Join(h, "dirs", n+".conf"), []byte("(path \"/tmp\")\n"), 0o644)
}
bText := []byte("(path \"/tmp\")\n(rule \"r\" (move \"Out\"))\n")
if _, errs := LoadWith(main, map[string][]byte{filepath.Join(h, "dirs", "b.conf"): bText}, "a"); len(errs) > 0 {
t.Errorf("text for another included directory was reported: %v", errs)
}
strange := filepath.Join(h, "dirs", "nowhere.conf")
if _, errs := LoadWith(main, map[string][]byte{strange: bText}, "a"); len(errs) != 1 {
t.Errorf("text for a file no configuration names was not reported: %v", errs)
}
}
// TestLoadWithRefusesCollidingOverrides: two keys that name the same file
// would leave which text is read to map order, so they are refused (plan 13
// review F4 follow-up).
func TestLoadWithRefusesCollidingOverrides(t *testing.T) {
h := t.TempDir()
t.Setenv("HOME", h)
main := filepath.Join(h, "krino.conf")
os.MkdirAll(filepath.Join(h, "dirs"), 0o755)
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"),
filepath.Join(h, "dirs") + "/./dl.conf": []byte("(path \"/other\")\n"),
}
_, errs := LoadWith(main, over)
if len(errs) == 0 || !strings.Contains(diagText(errs), "twice") {
t.Errorf("colliding override keys were not refused: %v", errs)
}
}
|