Skip to content

Commit 5fcdbb0

Browse files
authored
Merge pull request #285 from kcl-lang/feat-xdg-directory
feat: xdg directory
2 parents 109b193 + 205650e commit 5fcdbb0

2 files changed

Lines changed: 156 additions & 0 deletions

File tree

go/path/home_test.go

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,128 @@ func TestHomeDirWithError(t *testing.T) {
2828
}
2929
})
3030
}
31+
32+
func TestXDGDataHome(t *testing.T) {
33+
if runtime.GOOS == "windows" || runtime.GOOS == "darwin" {
34+
t.Skip("Skipping test on Windows and Darwin")
35+
}
36+
37+
originalHome := os.Getenv("HOME")
38+
originalXDGDataHome := os.Getenv("XDG_DATA_HOME")
39+
defer func() {
40+
os.Setenv("HOME", originalHome)
41+
os.Setenv("XDG_DATA_HOME", originalXDGDataHome)
42+
}()
43+
44+
t.Run("DefaultWithoutEnvVar", func(t *testing.T) {
45+
os.Setenv("HOME", "/home/testuser")
46+
os.Unsetenv("XDG_DATA_HOME")
47+
48+
expected := "/home/testuser/.local/share"
49+
result := DataHome()
50+
if result != expected {
51+
t.Errorf("Expected DataHome to be %v, got %v", expected, result)
52+
}
53+
})
54+
55+
t.Run("RespectsEnvVar", func(t *testing.T) {
56+
os.Setenv("XDG_DATA_HOME", "/custom/data")
57+
58+
expected := "/custom/data"
59+
result := DataHome()
60+
if result != expected {
61+
t.Errorf("Expected DataHome to be %v, got %v", expected, result)
62+
}
63+
})
64+
65+
t.Run("IgnoresRelativePathEnvVar", func(t *testing.T) {
66+
os.Setenv("HOME", "/home/testuser")
67+
os.Setenv("XDG_DATA_HOME", "relative/path")
68+
69+
// Should fall back to default when XDG_DATA_HOME is relative
70+
expected := "/home/testuser/.local/share"
71+
result := DataHome()
72+
if result != expected {
73+
t.Errorf("Expected DataHome to fall back to %v, got %v", expected, result)
74+
}
75+
})
76+
77+
t.Run("IgnoresEmptyEnvVar", func(t *testing.T) {
78+
os.Setenv("HOME", "/home/testuser")
79+
os.Setenv("XDG_DATA_HOME", "")
80+
81+
expected := "/home/testuser/.local/share"
82+
result := DataHome()
83+
if result != expected {
84+
t.Errorf("Expected DataHome to be %v, got %v", expected, result)
85+
}
86+
})
87+
}
88+
89+
func TestXDGConfigHome(t *testing.T) {
90+
if runtime.GOOS == "windows" || runtime.GOOS == "darwin" {
91+
t.Skip("Skipping test on Windows and Darwin")
92+
}
93+
94+
originalHome := os.Getenv("HOME")
95+
originalXDGConfigHome := os.Getenv("XDG_CONFIG_HOME")
96+
defer func() {
97+
os.Setenv("HOME", originalHome)
98+
os.Setenv("XDG_CONFIG_HOME", originalXDGConfigHome)
99+
}()
100+
101+
t.Run("DefaultWithoutEnvVar", func(t *testing.T) {
102+
os.Setenv("HOME", "/home/testuser")
103+
os.Unsetenv("XDG_CONFIG_HOME")
104+
105+
expected := "/home/testuser/.config"
106+
result := ConfigHome()
107+
if result != expected {
108+
t.Errorf("Expected ConfigHome to be %v, got %v", expected, result)
109+
}
110+
})
111+
112+
t.Run("RespectsEnvVar", func(t *testing.T) {
113+
os.Setenv("XDG_CONFIG_HOME", "/custom/config")
114+
115+
expected := "/custom/config"
116+
result := ConfigHome()
117+
if result != expected {
118+
t.Errorf("Expected ConfigHome to be %v, got %v", expected, result)
119+
}
120+
})
121+
}
122+
123+
func TestXDGCacheHome(t *testing.T) {
124+
if runtime.GOOS == "windows" || runtime.GOOS == "darwin" {
125+
t.Skip("Skipping test on Windows and Darwin")
126+
}
127+
128+
originalHome := os.Getenv("HOME")
129+
originalXDGCacheHome := os.Getenv("XDG_CACHE_HOME")
130+
defer func() {
131+
os.Setenv("HOME", originalHome)
132+
os.Setenv("XDG_CACHE_HOME", originalXDGCacheHome)
133+
}()
134+
135+
t.Run("DefaultWithoutEnvVar", func(t *testing.T) {
136+
os.Setenv("HOME", "/home/testuser")
137+
os.Unsetenv("XDG_CACHE_HOME")
138+
139+
expected := "/home/testuser/.cache"
140+
result := CacheHome()
141+
if result != expected {
142+
t.Errorf("Expected CacheHome to be %v, got %v", expected, result)
143+
}
144+
})
145+
146+
t.Run("RespectsEnvVar", func(t *testing.T) {
147+
os.Setenv("XDG_CACHE_HOME", "/custom/cache")
148+
149+
expected := "/custom/cache"
150+
result := CacheHome()
151+
if result != expected {
152+
t.Errorf("Expected CacheHome to be %v, got %v", expected, result)
153+
}
154+
})
155+
}

go/path/lazypath_unix.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,49 @@
44
package path
55

66
import (
7+
"os"
78
"path/filepath"
89
)
910

1011
// DataHome defines the base directory relative to which user specific data files should be stored.
12+
// It respects the XDG_DATA_HOME environment variable and defaults to ~/.local/share.
1113
func DataHome() string {
14+
dataHome := os.Getenv("XDG_DATA_HOME")
15+
if dataHome != "" && filepath.IsAbs(dataHome) {
16+
return dataHome
17+
}
1218
return filepath.Join(HomeDir(), ".local", "share")
1319
}
1420

1521
// ConfigHome defines the base directory relative to which user specific configuration files should
1622
// be stored.
23+
// It respects the XDG_CONFIG_HOME environment variable and defaults to ~/.config.
1724
func ConfigHome() string {
25+
configHome := os.Getenv("XDG_CONFIG_HOME")
26+
if configHome != "" && filepath.IsAbs(configHome) {
27+
return configHome
28+
}
1829
return filepath.Join(HomeDir(), ".config")
1930
}
2031

2132
// CacheHome defines the base directory relative to which user specific non-essential data files
2233
// should be stored.
34+
// It respects the XDG_CACHE_HOME environment variable and defaults to ~/.cache.
2335
func CacheHome() string {
36+
cacheHome := os.Getenv("XDG_CACHE_HOME")
37+
if cacheHome != "" && filepath.IsAbs(cacheHome) {
38+
return cacheHome
39+
}
2440
return filepath.Join(HomeDir(), ".cache")
2541
}
2642

2743
// DataHomeWithError defines the base directory relative to which user specific data files should be stored.
44+
// It respects the XDG_DATA_HOME environment variable and defaults to ~/.local/share.
2845
func DataHomeWithError() (string, error) {
46+
dataHome := os.Getenv("XDG_DATA_HOME")
47+
if dataHome != "" && filepath.IsAbs(dataHome) {
48+
return dataHome, nil
49+
}
2950
homeDir, err := HomeDirWithError()
3051
if err != nil {
3152
return "", err
@@ -35,7 +56,12 @@ func DataHomeWithError() (string, error) {
3556

3657
// ConfigHomeWithError defines the base directory relative to which user specific configuration files should
3758
// be stored.
59+
// It respects the XDG_CONFIG_HOME environment variable and defaults to ~/.config.
3860
func ConfigHomeWithError() (string, error) {
61+
configHome := os.Getenv("XDG_CONFIG_HOME")
62+
if configHome != "" && filepath.IsAbs(configHome) {
63+
return configHome, nil
64+
}
3965
homeDir, err := HomeDirWithError()
4066
if err != nil {
4167
return "", err
@@ -45,7 +71,12 @@ func ConfigHomeWithError() (string, error) {
4571

4672
// CacheHomeWithError defines the base directory relative to which user specific non-essential data files
4773
// should be stored.
74+
// It respects the XDG_CACHE_HOME environment variable and defaults to ~/.cache.
4875
func CacheHomeWithError() (string, error) {
76+
cacheHome := os.Getenv("XDG_CACHE_HOME")
77+
if cacheHome != "" && filepath.IsAbs(cacheHome) {
78+
return cacheHome, nil
79+
}
4980
homeDir, err := HomeDirWithError()
5081
if err != nil {
5182
return "", err

0 commit comments

Comments
 (0)