Skip to content

Commit 22d93c1

Browse files
authored
fix: config directory detection when XDG_CONFIG_HOME is unset (#62)
1 parent 3dfc687 commit 22d93c1

File tree

2 files changed

+45
-25
lines changed

2 files changed

+45
-25
lines changed

pkg/config/config.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"fmt"
77
"log/slog"
88
"os"
9-
"os/user"
109
"path/filepath"
1110

1211
"github.com/invopop/jsonschema"
@@ -283,10 +282,17 @@ func GetPath() string {
283282
return filepath.Join(xdgHome, "kat", "config.yaml")
284283
}
285284

286-
usr, err := user.Current()
287-
if err != nil && usr != nil {
288-
return filepath.Join(usr.HomeDir, ".config", "kat", "config.yaml")
285+
usrHome, err := os.UserHomeDir()
286+
if err == nil && usrHome != "" {
287+
return filepath.Join(usrHome, ".config", "kat", "config.yaml")
289288
}
290289

291-
return filepath.Join(os.TempDir(), "kat", "config.yaml")
290+
tmpConfig := filepath.Join(os.TempDir(), "kat", "config.yaml")
291+
292+
slog.Warn("could not determine user config directory, using temp path for config",
293+
slog.String("path", tmpConfig),
294+
slog.Any("error", fmt.Errorf("$XDG_CONFIG_HOME is unset, fall back to home directory: %w", err)),
295+
)
296+
297+
return tmpConfig
292298
}

pkg/config/config_test.go

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package config_test
33
import (
44
"os"
55
"path/filepath"
6-
"strings"
76
"testing"
87

98
"github.com/stretchr/testify/assert"
@@ -347,48 +346,63 @@ func TestConfig_MarshalYAML(t *testing.T) {
347346

348347
//nolint:paralleltest // We need to set environment variables, so run tests sequentially.
349348
func TestGetPath(t *testing.T) {
350-
tests := map[string]struct {
351-
setupEnv func(t *testing.T)
352-
cleanupEnv func(t *testing.T)
353-
validate func(t *testing.T, path string)
349+
tcs := map[string]struct {
350+
setupEnv func(t *testing.T)
351+
want string
354352
}{
355-
"with XDG_CONFIG_HOME": {
353+
"XDG_CONFIG_HOME is set and not empty": {
356354
setupEnv: func(t *testing.T) {
357355
t.Helper()
358356
t.Setenv("XDG_CONFIG_HOME", "/custom/config")
359357
},
360-
validate: func(t *testing.T, path string) {
358+
want: "/custom/config/kat/config.yaml",
359+
},
360+
"XDG_CONFIG_HOME is empty and HOME is set": {
361+
setupEnv: func(t *testing.T) {
362+
t.Helper()
363+
t.Setenv("XDG_CONFIG_HOME", "")
364+
t.Setenv("HOME", "/test/home")
365+
},
366+
want: "/test/home/.config/kat/config.yaml",
367+
},
368+
"XDG_CONFIG_HOME is not set and HOME is set": {
369+
setupEnv: func(t *testing.T) {
361370
t.Helper()
362-
assert.Equal(t, "/custom/config/kat/config.yaml", path)
371+
err := os.Unsetenv("XDG_CONFIG_HOME")
372+
require.NoError(t, err)
373+
t.Setenv("HOME", "/test/home")
363374
},
375+
want: "/test/home/.config/kat/config.yaml",
364376
},
365-
"without XDG_CONFIG_HOME": {
377+
"XDG_CONFIG_HOME is empty and HOME is empty": {
366378
setupEnv: func(t *testing.T) {
367379
t.Helper()
368380
t.Setenv("XDG_CONFIG_HOME", "")
381+
t.Setenv("HOME", "")
369382
},
370-
validate: func(t *testing.T, path string) {
383+
want: filepath.Join(os.TempDir(), "kat", "config.yaml"), //nolint:usetesting // Needs to equal host.
384+
},
385+
"XDG_CONFIG_HOME is not set and HOME is empty": {
386+
setupEnv: func(t *testing.T) {
371387
t.Helper()
372-
// Should fall back to ~/.config/kat/config.yaml or temp dir.
373-
assert.Contains(t, path, "kat/config.yaml")
388+
err := os.Unsetenv("XDG_CONFIG_HOME")
389+
require.NoError(t, err)
390+
t.Setenv("HOME", "")
374391
},
392+
want: filepath.Join(os.TempDir(), "kat", "config.yaml"), //nolint:usetesting // Needs to equal host.
375393
},
376394
}
377395

378-
//nolint:paralleltest // We need to set environment variables, so run tests sequentially.
379-
for name, tc := range tests {
396+
for name, tc := range tcs {
380397
t.Run(name, func(t *testing.T) {
381398
if tc.setupEnv != nil {
382399
tc.setupEnv(t)
383400
}
384401

385-
path := config.GetPath()
386-
assert.NotEmpty(t, path)
387-
assert.True(t, strings.HasSuffix(path, "config.yaml"))
402+
got := config.GetPath()
388403

389-
if tc.validate != nil {
390-
tc.validate(t, path)
391-
}
404+
assert.NotEmpty(t, got)
405+
assert.Equal(t, tc.want, got)
392406
})
393407
}
394408
}

0 commit comments

Comments
 (0)