Skip to content

Commit

Permalink
fix: dir no longer panics when HOME and XDG_CONFIG_HOME are not set (#…
Browse files Browse the repository at this point in the history
…449)

This PR addresses the issue #446

In this PR I:

- I removed the `init()` function from `dir/path`
- When `userConfigDir()` returns an error, instead of `panic(err)` I
default to the current directory instead
- Split `loadUserPath()` into two new functions used to setup and return
the values for `UserConfigDir` and `UserLibexecDir`
- Added additional unit tests for the two new functions and to test the
default directory is used when `HOME` is set to `""`

---------

Signed-off-by: Jason <[email protected]>
Signed-off-by: JasonTheDeveloper <[email protected]>
Signed-off-by: Patrick Zheng <[email protected]>
Co-authored-by: Shiwei Zhang <[email protected]>
Co-authored-by: Patrick Zheng <[email protected]>
  • Loading branch information
3 people authored and JeyJeyGao committed Sep 4, 2024
1 parent 0989bf5 commit eb08659
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 26 deletions.
4 changes: 2 additions & 2 deletions dir/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ func NewSysFS(root string) SysFS {

// ConfigFS is the config SysFS
func ConfigFS() SysFS {
return NewSysFS(UserConfigDir)
return NewSysFS(userConfigDirPath())
}

// PluginFS is the plugin SysFS
func PluginFS() SysFS {
return NewSysFS(filepath.Join(UserLibexecDir, PathPlugins))
return NewSysFS(filepath.Join(userLibexecDirPath(), PathPlugins))
}
4 changes: 2 additions & 2 deletions dir/fs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func TestPluginFS(t *testing.T) {
if err != nil {
t.Fatalf("SysPath() failed. err = %v", err)
}
if path != filepath.Join(UserLibexecDir, PathPlugins, "plugin") {
t.Fatalf(`SysPath() failed. got: %q, want: %q`, path, filepath.Join(UserLibexecDir, PathPlugins, "plugin"))
if path != filepath.Join(userLibexecDirPath(), PathPlugins, "plugin") {
t.Fatalf(`SysPath() failed. got: %q, want: %q`, path, filepath.Join(userLibexecDirPath(), PathPlugins, "plugin"))
}
}
31 changes: 19 additions & 12 deletions dir/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,21 +73,28 @@ const (

var userConfigDir = os.UserConfigDir // for unit test

func init() {
loadUserPath()
// userConfigDirPath returns the user level {NOTATION_CONFIG} path.
func userConfigDirPath() string {
if UserConfigDir == "" {
userDir, err := userConfigDir()
if err != nil {
// fallback to current directory
UserConfigDir = "." + notation
return UserConfigDir
}
// set user config
UserConfigDir = filepath.Join(userDir, notation)
}
return UserConfigDir
}

// loadUserPath function defines UserConfigDir and UserLibexecDir.
func loadUserPath() {
// set user config
userDir, err := userConfigDir()
if err != nil {
panic(err)
// userLibexecDirPath returns the user level {NOTATION_LIBEXEC} path.
func userLibexecDirPath() string {
if UserLibexecDir == "" {
// set user libexec
UserLibexecDir = userConfigDirPath()
}
UserConfigDir = filepath.Join(userDir, notation)

// set user libexec
UserLibexecDir = UserConfigDir
return UserLibexecDir
}

// LocalKeyPath returns the local key and local cert relative paths.
Expand Down
45 changes: 35 additions & 10 deletions dir/path_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,53 @@
package dir

import (
"path/filepath"
"os"
"testing"
)

func mockGetUserConfig() (string, error) {
return "/path/", nil
}

func Test_loadPath(t *testing.T) {
wantDir := filepath.FromSlash("/path/notation")
func setup() {
UserConfigDir = ""
UserLibexecDir = ""
}

func Test_UserConfigDirPath(t *testing.T) {
userConfigDir = mockGetUserConfig
loadUserPath()
if UserConfigDir != wantDir {
t.Fatalf(`loadPath() UserConfigDir is incorrect. got: %q, want: %q`, UserConfigDir, wantDir)
setup()
got := userConfigDirPath()
if got != "/path/notation" {
t.Fatalf(`UserConfigDirPath() = %q, want "/path/notation"`, got)
}
}

if UserLibexecDir != UserConfigDir {
t.Fatalf(`loadPath() UserLibexecDir is incorrect. got: %q, want: %q`, UserLibexecDir, wantDir)
func Test_NoHomeVariable(t *testing.T) {
t.Setenv("HOME", "")
t.Setenv("XDG_CONFIG_HOME", "")
setup()
userConfigDir = os.UserConfigDir
got := userConfigDirPath()
if got != ".notation" {
t.Fatalf(`UserConfigDirPath() = %q, want ".notation"`, UserConfigDir)
}
}

func Test_UserLibexecDirPath(t *testing.T) {
userConfigDir = mockGetUserConfig
setup()
got := userLibexecDirPath()
if got != "/path/notation" {
t.Fatalf(`UserConfigDirPath() = %q, want "/path/notation"`, got)
}
}

func TestLocalKeyPath(t *testing.T) {
userConfigDir = mockGetUserConfig
loadUserPath()
setup()
_ = userConfigDirPath()
_ = userLibexecDirPath()
gotKeyPath, gotCertPath := LocalKeyPath("web")
if gotKeyPath != "localkeys/web.key" {
t.Fatalf(`LocalKeyPath() gotKeyPath = %q, want "localkeys/web.key"`, gotKeyPath)
Expand All @@ -49,7 +72,9 @@ func TestLocalKeyPath(t *testing.T) {

func TestX509TrustStoreDir(t *testing.T) {
userConfigDir = mockGetUserConfig
loadUserPath()
setup()
_ = userConfigDirPath()
_ = userLibexecDirPath()
if got := X509TrustStoreDir("ca", "web"); got != "truststore/x509/ca/web" {
t.Fatalf(`X509TrustStoreDir() = %q, want "truststore/x509/ca/web"`, got)
}
Expand Down

0 comments on commit eb08659

Please sign in to comment.