@@ -3,7 +3,6 @@ package config_test
33import (
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.
349348func 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