@@ -20,6 +20,7 @@ import (
2020 "net/url"
2121 "os"
2222 "path/filepath"
23+ "runtime"
2324 "sync"
2425 "testing"
2526
@@ -554,3 +555,52 @@ 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+ path , err := getCredentialPath ()
582+ require .NoError (t , err )
583+ assert .Contains (t , path , "dbc" )
584+ assert .Contains (t , path , "credentials" )
585+ assert .Contains (t , path , "credentials.toml" )
586+
587+ case "darwin" :
588+ path , err := getCredentialPath ()
589+ require .NoError (t , err )
590+ assert .Contains (t , path , "Library" )
591+ assert .Contains (t , path , "dbc" )
592+ assert .Contains (t , path , "credentials" )
593+ assert .Contains (t , path , "credentials.toml" )
594+
595+ default :
596+ path , err := getCredentialPath ()
597+ require .NoError (t , err )
598+ assert .Contains (t , path , ".local" )
599+ assert .Contains (t , path , "share" )
600+ assert .Contains (t , path , "dbc" )
601+ assert .Contains (t , path , "credentials" )
602+ assert .Contains (t , path , "credentials.toml" )
603+
604+ }
605+ })
606+ }
0 commit comments