1package platform
2
3import (
4 "os"
5 "path/filepath"
6 "strings"
7 "testing"
8)
9
10func TestStateDir(t *testing.T) {
11 tests := []struct {
12 name string
13 xdgState string
14 home string
15 want func(*testing.T, string, string) string
16 }{
17 {
18 name: "xdg state home",
19 xdgState: filepath.Join(t.TempDir(), "xdg-state"),
20 home: filepath.Join(t.TempDir(), "home"),
21 want: func(_ *testing.T, xdgState, _ string) string {
22 return filepath.Join(xdgState, "vev")
23 },
24 },
25 {
26 name: "home fallback",
27 home: filepath.Join(t.TempDir(), "home"),
28 want: func(_ *testing.T, _, home string) string {
29 return filepath.Join(home, ".local", "state", "vev")
30 },
31 },
32 {
33 name: "temp fallback without home",
34 want: func(t *testing.T, _, _ string) string {
35 t.Helper()
36 return filepath.Join(os.TempDir(), "vev-state-")
37 },
38 },
39 }
40
41 for _, tt := range tests {
42 t.Run(tt.name, func(t *testing.T) {
43 t.Setenv("XDG_STATE_HOME", tt.xdgState)
44 t.Setenv("HOME", tt.home)
45
46 want := tt.want(t, tt.xdgState, tt.home)
47 got := StateDir()
48 if strings.HasSuffix(want, "vev-state-") {
49 if !strings.HasPrefix(got, want) {
50 t.Fatalf("StateDir() = %q, want prefix %q", got, want)
51 }
52 return
53 }
54 if got != want {
55 t.Fatalf("StateDir() = %q, want %q", got, want)
56 }
57 })
58 }
59}
60
61func TestConfigPath(t *testing.T) {
62 tests := []struct {
63 name string
64 xdgConfig string
65 home string
66 want func(*testing.T, string, string) string
67 }{
68 {
69 name: "xdg config home",
70 xdgConfig: filepath.Join(t.TempDir(), "xdg-config"),
71 home: filepath.Join(t.TempDir(), "home"),
72 want: func(_ *testing.T, xdgConfig, _ string) string {
73 return filepath.Join(xdgConfig, "vev", "config")
74 },
75 },
76 {
77 name: "home fallback",
78 home: filepath.Join(t.TempDir(), "home"),
79 want: func(_ *testing.T, _, home string) string {
80 return filepath.Join(home, ".config", "vev", "config")
81 },
82 },
83 {
84 name: "temp fallback without home",
85 want: func(t *testing.T, _, _ string) string {
86 t.Helper()
87 return filepath.Join(os.TempDir(), "vev-config-")
88 },
89 },
90 }
91
92 for _, tt := range tests {
93 t.Run(tt.name, func(t *testing.T) {
94 t.Setenv("XDG_CONFIG_HOME", tt.xdgConfig)
95 t.Setenv("HOME", tt.home)
96
97 want := tt.want(t, tt.xdgConfig, tt.home)
98 got := ConfigPath()
99 if strings.HasSuffix(want, "vev-config-") {
100 if !strings.HasPrefix(got, want) || filepath.Base(got) != "config" {
101 t.Fatalf("ConfigPath() = %q, want prefix %q and base config", got, want)
102 }
103 return
104 }
105 if got != want {
106 t.Fatalf("ConfigPath() = %q, want %q", got, want)
107 }
108 })
109 }
110}
111
112func TestDirOrHome(t *testing.T) {
113 existing := t.TempDir()
114 home := filepath.Join(t.TempDir(), "home")
115 nonexistent := filepath.Join(t.TempDir(), "missing")
116
117 tests := []struct {
118 name string
119 dir string
120 home string
121 want string
122 }{
123 {name: "existing dir", dir: existing, home: home, want: existing},
124 {name: "non-existent dir falls back to home", dir: nonexistent, home: home, want: home},
125 {name: "non-existent dir without home falls back to root", dir: nonexistent, want: "/"},
126 }
127
128 for _, tt := range tests {
129 t.Run(tt.name, func(t *testing.T) {
130 t.Setenv("HOME", tt.home)
131
132 if got := DirOrHome(tt.dir); got != tt.want {
133 t.Fatalf("DirOrHome(%q) = %q, want %q", tt.dir, got, tt.want)
134 }
135 })
136 }
137}