44 "context"
55 "fmt"
66 "log/slog"
7+ "os"
78 "runtime"
89 "strings"
910 "testing"
@@ -205,14 +206,26 @@ func TestGetCurrentUserInfo(t *testing.T) {
205206}
206207
207208func TestGetConfigDir (t * testing.T ) {
209+ // Save original XDG_CONFIG_HOME and restore after test
210+ originalXDG := os .Getenv ("XDG_CONFIG_HOME" )
211+ defer func () {
212+ if originalXDG != "" {
213+ os .Setenv ("XDG_CONFIG_HOME" , originalXDG )
214+ } else {
215+ os .Unsetenv ("XDG_CONFIG_HOME" )
216+ }
217+ }()
218+
208219 tests := []struct {
209- name string
210- homeDir string
211- expected func (string ) bool // validation function
220+ name string
221+ homeDir string
222+ xdgConfig string // XDG_CONFIG_HOME value to set
223+ expected func (string ) bool // validation function
212224 }{
213225 {
214226 name : "normal home directory" ,
215227 homeDir : "/home/testuser" ,
228+ xdgConfig : "" , // unset XDG_CONFIG_HOME
216229 expected : func (configDir string ) bool {
217230 return strings .HasPrefix (configDir , "/home/testuser" ) &&
218231 (strings .Contains (configDir , ".config" ) || strings .Contains (configDir , "jail" ))
@@ -221,21 +234,38 @@ func TestGetConfigDir(t *testing.T) {
221234 {
222235 name : "root home directory" ,
223236 homeDir : "/root" ,
237+ xdgConfig : "" , // unset XDG_CONFIG_HOME
224238 expected : func (configDir string ) bool {
225239 return strings .HasPrefix (configDir , "/root" )
226240 },
227241 },
228242 {
229243 name : "empty home directory" ,
230244 homeDir : "" ,
245+ xdgConfig : "" , // unset XDG_CONFIG_HOME
231246 expected : func (configDir string ) bool {
232247 return configDir != "" // should have some fallback
233248 },
234249 },
250+ {
251+ name : "XDG_CONFIG_HOME set" ,
252+ homeDir : "/home/testuser" ,
253+ xdgConfig : "/custom/config" ,
254+ expected : func (configDir string ) bool {
255+ return configDir == "/custom/config/coder_jail"
256+ },
257+ },
235258 }
236259
237260 for _ , tt := range tests {
238261 t .Run (tt .name , func (t * testing.T ) {
262+ // Set up environment for this test
263+ if tt .xdgConfig != "" {
264+ os .Setenv ("XDG_CONFIG_HOME" , tt .xdgConfig )
265+ } else {
266+ os .Unsetenv ("XDG_CONFIG_HOME" )
267+ }
268+
239269 configDir := getConfigDir (tt .homeDir )
240270
241271 if configDir == "" {
@@ -244,7 +274,7 @@ func TestGetConfigDir(t *testing.T) {
244274 }
245275
246276 if ! tt .expected (configDir ) {
247- t .Errorf ("config directory %s does not match expected pattern for home %s" , configDir , tt .homeDir )
277+ t .Errorf ("config directory %s does not match expected pattern for home %s (XDG_CONFIG_HOME=%s) " , configDir , tt .homeDir , tt . xdgConfig )
248278 }
249279 })
250280 }
0 commit comments