Skip to content

Commit 9db8013

Browse files
authored
chore: add tests for getCredentialPath (#247)
Adds basic regression tests for the getCredentialPath helper in credentials.go.
1 parent 31f2e15 commit 9db8013

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

auth/credentials_test.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"net/url"
2121
"os"
2222
"path/filepath"
23+
"runtime"
2324
"sync"
2425
"testing"
2526

@@ -554,3 +555,56 @@ func TestUpdateCreds(t *testing.T) {
554555
assert.Equal(t, "test-key", creds[0].ApiKey)
555556
})
556557
}
558+
559+
func TestGetCredentialPath(t *testing.T) {
560+
t.Run("honors XDG_DATA_HOME when set to an absolute path", func(t *testing.T) {
561+
tmpDir := t.TempDir()
562+
t.Setenv("XDG_DATA_HOME", tmpDir)
563+
564+
path, err := getCredentialPath()
565+
require.NoError(t, err)
566+
expected := filepath.Join(tmpDir, "dbc", "credentials", "credentials.toml")
567+
assert.Equal(t, expected, path)
568+
})
569+
570+
t.Run("errors if XDG_DATA_HOME is set to a relative path", func(t *testing.T) {
571+
t.Setenv("XDG_DATA_HOME", "any/relative/path")
572+
573+
_, err := getCredentialPath()
574+
assert.Error(t, err)
575+
assert.Contains(t, err.Error(), "path in $XDG_DATA_HOME is relative")
576+
})
577+
578+
t.Run("default behavior for each platform", func(t *testing.T) {
579+
switch runtime.GOOS {
580+
case "windows":
581+
appData := os.Getenv("LocalAppData")
582+
if appData == "" {
583+
t.Errorf("failed to get LocalAppData")
584+
}
585+
path, err := getCredentialPath()
586+
require.NoError(t, err)
587+
assert.Equal(t, filepath.Join(appData, "dbc", "credentials", "credentials.toml"), path)
588+
589+
case "darwin":
590+
userHome, err := os.UserHomeDir()
591+
if err != nil {
592+
t.Errorf("failed to get user home directory")
593+
}
594+
595+
path, err := getCredentialPath()
596+
require.NoError(t, err)
597+
assert.Equal(t, filepath.Join(userHome, "Library", "dbc", "credentials", "credentials.toml"), path)
598+
599+
default:
600+
userHome, err := os.UserHomeDir()
601+
if err != nil {
602+
t.Errorf("failed to get user home directory")
603+
}
604+
605+
path, err := getCredentialPath()
606+
require.NoError(t, err)
607+
assert.Equal(t, filepath.Join(userHome, ".local", "share", "dbc", "credentials", "credentials.toml"), path)
608+
}
609+
})
610+
}

0 commit comments

Comments
 (0)