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
|
// SPDX-License-Identifier: GPL-3.0-or-later
package model
import (
"os"
"path/filepath"
"testing"
)
// sandboxHome points HOME and every XDG_* at a temporary directory, as the
// other tests here do.
func sandboxHome(t *testing.T) string {
t.Helper()
h := t.TempDir()
t.Setenv("HOME", h)
for _, v := range []string{"XDG_CONFIG_HOME", "XDG_STATE_HOME", "XDG_DATA_HOME", "XDG_CACHE_HOME"} {
t.Setenv(v, "")
}
return h
}
// TestPrefsRoundTrip: what is saved comes back, and the file lands beside
// krino.conf.
func TestPrefsRoundTrip(t *testing.T) {
h := sandboxHome(t)
p := DefaultPrefs()
p.PreviewHeight = 420
p.Colours = false
p.SelectAll = false
if err := p.Save(); err != nil {
t.Fatal(err)
}
if got := LoadPrefs(); got != p {
t.Errorf("loaded %+v, want %+v", got, p)
}
want := filepath.Join(h, ".config", "krino", "gui.json")
if _, err := os.Stat(want); err != nil {
t.Errorf("not written to %s: %v", want, err)
}
}
// TestPrefsWithoutAFile: the first run has no file, and damaged text is not
// worth refusing to open a window over.
func TestPrefsWithoutAFile(t *testing.T) {
sandboxHome(t)
if got := LoadPrefs(); got != DefaultPrefs() {
t.Errorf("with no file: %+v, want the defaults", got)
}
if err := os.MkdirAll(filepath.Dir(PrefsFile()), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(PrefsFile(), []byte("{not json"), 0o644); err != nil {
t.Fatal(err)
}
if got := LoadPrefs(); got != DefaultPrefs() {
t.Errorf("with damaged text: %+v, want the defaults", got)
}
}
// TestWithDisplayKeepsTheDividers: the Settings window used to compose a
// whole Prefs from its own controls, which wrote the remembered pane
// positions back as zeros - so dragging the dividers and then ticking any
// checkbox in Settings threw the positions away and the next window opened
// at the defaults.
func TestWithDisplayKeepsTheDividers(t *testing.T) {
p := Prefs{
PreviewWidth: 640, ListWidth: 800, ListHeight: 300, PreviewHeight: 281,
Sort: SortName, ShowSize: true, ShowAge: true, ShowRule: true,
Colours: true, Preview: true, SelectAll: true, Layout: LayoutSide,
}
d := p.DisplayOf()
d.ShowRule = false
d.Sort = SortSize
got := p.WithDisplay(d)
if got.PreviewWidth != 640 || got.ListWidth != 800 || got.ListHeight != 300 {
t.Errorf("the dividers were disturbed: %+v", got)
}
if got.ShowRule || got.Sort != SortSize {
t.Errorf("the change did not take: %+v", got)
}
if !got.ShowSize || !got.ShowAge || !got.Colours || !got.Preview || !got.SelectAll {
t.Errorf("something else changed: %+v", got)
}
}
|