Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: dir no longer panics when HOME and XDG_CONFIG_HOME are not set #449

Merged
merged 16 commits into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -79,21 +79,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)
JasonTheDeveloper marked this conversation as resolved.
Show resolved Hide resolved
}
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
Loading