summaryrefslogtreecommitdiff
path: root/gui/internal/model/prefs_test.go
blob: 328ac315c58a4431cf5f1b20b48ecac69e4178a1 (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
// 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)
	}
}