@@ -484,3 +484,68 @@ func Test_autoMigrateStateDir_migration(t *testing.T) {
484484 assert .Equal (t , 1 , len (files ))
485485 assert .Equal (t , "state.yml" , files [0 ].Name ())
486486}
487+
488+ func Test_DataDir (t * testing.T ) {
489+ tempDir := t .TempDir ()
490+
491+ tests := []struct {
492+ name string
493+ onlyWindows bool
494+ env map [string ]string
495+ output string
496+ }{
497+ {
498+ name : "HOME/USERPROFILE specified" ,
499+ env : map [string ]string {
500+ "XDG_DATA_HOME" : "" ,
501+ "GH_CONFIG_DIR" : "" ,
502+ "XDG_CONFIG_HOME" : "" ,
503+ "LocalAppData" : "" ,
504+ "USERPROFILE" : tempDir ,
505+ "HOME" : tempDir ,
506+ },
507+ output : filepath .Join (tempDir , ".local" , "share" , "gh" ),
508+ },
509+ {
510+ name : "XDG_DATA_HOME specified" ,
511+ env : map [string ]string {
512+ "XDG_DATA_HOME" : tempDir ,
513+ },
514+ output : filepath .Join (tempDir , "gh" ),
515+ },
516+ {
517+ name : "LocalAppData specified" ,
518+ onlyWindows : true ,
519+ env : map [string ]string {
520+ "LocalAppData" : tempDir ,
521+ },
522+ output : filepath .Join (tempDir , "GitHub CLI" ),
523+ },
524+ {
525+ name : "XDG_DATA_HOME and LocalAppData specified" ,
526+ onlyWindows : true ,
527+ env : map [string ]string {
528+ "XDG_DATA_HOME" : tempDir ,
529+ "LocalAppData" : tempDir ,
530+ },
531+ output : filepath .Join (tempDir , "gh" ),
532+ },
533+ }
534+
535+ for _ , tt := range tests {
536+ if tt .onlyWindows && runtime .GOOS != "windows" {
537+ continue
538+ }
539+ t .Run (tt .name , func (t * testing.T ) {
540+ if tt .env != nil {
541+ for k , v := range tt .env {
542+ old := os .Getenv (k )
543+ os .Setenv (k , v )
544+ defer os .Setenv (k , old )
545+ }
546+ }
547+
548+ assert .Equal (t , tt .output , DataDir ())
549+ })
550+ }
551+ }
0 commit comments