aboutsummaryrefslogtreecommitdiff
path: root/lib/locations/locations_test.go
blob: 2864769160fc4c20bc629af6c20779d299ef1477 (plain)
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
// Copyright (C) 2023 The Syncthing Authors.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at https://mozilla.org/MPL/2.0/.

//go:build !windows

package locations

import (
	"path/filepath"
	"testing"
	"time"

	"golang.org/x/exp/slices"
)

func TestUnixConfigDir(t *testing.T) {
	t.Parallel()

	cases := []struct {
		userHome      string
		xdgConfigHome string
		xdgStateHome  string
		filesExist    []string
		expected      string
	}{
		// First some "new installations", no files exist previously.

		// No variables set, use our current default
		{"/home/user", "", "", nil, "/home/user/.local/state/syncthing"},
		// Config home set, doesn't matter
		{"/home/user", "/somewhere/else", "", nil, "/home/user/.local/state/syncthing"},
		// State home set, use that
		{"/home/user", "", "/var/state", nil, "/var/state/syncthing"},
		// State home set, again config home doesn't matter
		{"/home/user", "/somewhere/else", "/var/state", nil, "/var/state/syncthing"},

		// Now some "upgrades", where we have files in the old locations.

		// Config home set, a file exists in the default location
		{"/home/user", "/somewhere/else", "", []string{"/home/user/.config/syncthing/config.xml"}, "/home/user/.config/syncthing"},
		// State home set, a file exists in the default location
		{"/home/user", "", "/var/state", []string{"/home/user/.config/syncthing/config.xml"}, "/home/user/.config/syncthing"},
		// Both config home and state home set, a file exists in the default location
		{"/home/user", "/somewhere/else", "/var/state", []string{"/home/user/.config/syncthing/config.xml"}, "/home/user/.config/syncthing"},

		// Config home set, and a file exists at that place
		{"/home/user", "/somewhere/else", "", []string{"/somewhere/else/syncthing/config.xml"}, "/somewhere/else/syncthing"},
		// Config home and state home set, and a file exists in config home
		{"/home/user", "/somewhere/else", "/var/state", []string{"/somewhere/else/syncthing/config.xml"}, "/somewhere/else/syncthing"},
	}

	for _, c := range cases {
		fileExists := func(path string) bool { return slices.Contains(c.filesExist, path) }
		actual := unixConfigDir(c.userHome, c.xdgConfigHome, c.xdgStateHome, fileExists)
		if actual != c.expected {
			t.Errorf("unixConfigDir(%q, %q, %q) == %q, expected %q", c.userHome, c.xdgConfigHome, c.xdgStateHome, actual, c.expected)
		}
	}
}

func TestUnixDataDir(t *testing.T) {
	t.Parallel()

	cases := []struct {
		userHome     string
		configDir    string
		xdgDataHome  string
		xdgStateHome string
		filesExist   []string
		expected     string
	}{
		// First some "new installations", no files exist previously.

		// No variables set, use our current default
		{"/home/user", "", "", "", nil, "/home/user/.local/state/syncthing"},
		// Data home set, doesn't matter
		{"/home/user", "", "/somewhere/else", "", nil, "/home/user/.local/state/syncthing"},
		// State home set, use that
		{"/home/user", "", "", "/var/state", nil, "/var/state/syncthing"},

		// Now some "upgrades", where we have files in the old locations.

		// A database exists in the old default location, use that
		{"/home/user", "", "", "", []string{"/home/user/.config/syncthing/index-v0.14.0.db"}, "/home/user/.config/syncthing"},
		{"/home/user", "/config/dir", "/xdg/data/home", "/xdg/state/home", []string{"/home/user/.config/syncthing/index-v0.14.0.db"}, "/home/user/.config/syncthing"},

		// A database exists in the config dir, use that
		{"/home/user", "/config/dir", "/xdg/data/home", "/xdg/state/home", []string{"/config/dir/index-v0.14.0.db"}, "/config/dir"},

		// A database exists in the old xdg data home, use that
		{"/home/user", "/config/dir", "/xdg/data/home", "/xdg/state/home", []string{"/xdg/data/home/syncthing/index-v0.14.0.db"}, "/xdg/data/home/syncthing"},
	}

	for _, c := range cases {
		fileExists := func(path string) bool { return slices.Contains(c.filesExist, path) }
		actual := unixDataDir(c.userHome, c.configDir, c.xdgDataHome, c.xdgStateHome, fileExists)
		if actual != c.expected {
			t.Errorf("unixDataDir(%q, %q, %q, %q) == %q, expected %q", c.userHome, c.configDir, c.xdgDataHome, c.xdgStateHome, actual, c.expected)
		}
	}
}

func TestGetTimestamped(t *testing.T) {
	s := getTimestampedAt(PanicLog, time.Date(2023, 10, 25, 21, 47, 0, 0, time.UTC))
	exp := "panic-20231025-214700.log"
	if file := filepath.Base(s); file != exp {
		t.Errorf("got %q, expected %q", file, exp)
	}
}