-
Notifications
You must be signed in to change notification settings - Fork 285
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Load config from different locations prefer XDG_CONFIG_HOME before HO…
…ME (#912)
- Loading branch information
1 parent
d631844
commit cd2737a
Showing
5 changed files
with
545 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package cli | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
"github.com/spf13/afero" | ||
) | ||
|
||
func init() { | ||
os.Unsetenv("HOME") | ||
os.Unsetenv("XDG_CONFIG_HOME") | ||
AppFs = afero.NewMemMapFs() | ||
} | ||
|
||
func TestXDGConfigDirIsPrefferedFirst(t *testing.T) { | ||
|
||
xdgDirPath := "/tmp-iofs/xdg/config" | ||
homeDirPath := "/tmp-iofs/home/tester" | ||
AppFs.MkdirAll(xdgDirPath, 0755) | ||
AppFs.MkdirAll(homeDirPath, 0755) | ||
|
||
afero.WriteFile(AppFs, xdgDirPath+"/revive.toml", []byte("\n"), 0644) | ||
os.Setenv("XDG_CONFIG_HOME", xdgDirPath) | ||
|
||
afero.WriteFile(AppFs, homeDirPath+"/revive.toml", []byte("\n"), 0644) | ||
os.Setenv("HOME", homeDirPath) | ||
|
||
got := buildDefaultConfigPath() | ||
want := xdgDirPath + "/revive.toml" | ||
|
||
if got != want { | ||
t.Errorf("got %q, wanted %q", got, want) | ||
} | ||
|
||
//reset fs after test | ||
AppFs = afero.NewMemMapFs() | ||
} | ||
|
||
func TestHomeConfigDir(t *testing.T) { | ||
|
||
homeDirPath := "/tmp-iofs/home/tester" | ||
AppFs.MkdirAll(homeDirPath, 0755) | ||
|
||
afero.WriteFile(AppFs, homeDirPath+"/revive.toml", []byte("\n"), 0644) | ||
os.Setenv("HOME", homeDirPath) | ||
|
||
got := buildDefaultConfigPath() | ||
want := homeDirPath + "/revive.toml" | ||
|
||
if got != want { | ||
t.Errorf("got %q, wanted %q", got, want) | ||
} | ||
} | ||
|
||
func TestXDGConfigDir(t *testing.T) { | ||
xdgDirPath := "/tmp-iofs/xdg/config" | ||
AppFs.MkdirAll(xdgDirPath, 0755) | ||
|
||
afero.WriteFile(AppFs, xdgDirPath+"/revive.toml", []byte("\n"), 0644) | ||
os.Setenv("XDG_CONFIG_HOME", xdgDirPath) | ||
|
||
got := buildDefaultConfigPath() | ||
want := xdgDirPath + "/revive.toml" | ||
|
||
if got != want { | ||
t.Errorf("got %q, wanted %q", got, want) | ||
} | ||
} | ||
|
||
func TestXDGConfigDirNoFile(t *testing.T) { | ||
xdgDirPath := "/tmp-iofs/xdg/config" | ||
os.Setenv("XDG_CONFIG_HOME", xdgDirPath) | ||
|
||
got := buildDefaultConfigPath() | ||
want := xdgDirPath + "/revive.toml" | ||
|
||
if got != want { | ||
t.Errorf("got %q, wanted %q", got, want) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.