Skip to content

Commit e6c7fef

Browse files
JeyJeyGaoJasonTheDevelopershizhMSFTTwo-Hearts
authored
fix: dir no longer panics when HOME and XDG_CONFIG_HOME are not set (#449) (#450)
This PR addresses the issue #446 for `release-1.1` branch This commit was cherry picked from commit 4d76f9a 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: JasonTheDeveloper <[email protected]> Co-authored-by: Shiwei Zhang <[email protected]> Co-authored-by: Patrick Zheng <[email protected]>
1 parent 0989bf5 commit e6c7fef

File tree

4 files changed

+58
-26
lines changed

4 files changed

+58
-26
lines changed

dir/fs.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,10 @@ func NewSysFS(root string) SysFS {
5151

5252
// ConfigFS is the config SysFS
5353
func ConfigFS() SysFS {
54-
return NewSysFS(UserConfigDir)
54+
return NewSysFS(userConfigDirPath())
5555
}
5656

5757
// PluginFS is the plugin SysFS
5858
func PluginFS() SysFS {
59-
return NewSysFS(filepath.Join(UserLibexecDir, PathPlugins))
59+
return NewSysFS(filepath.Join(userLibexecDirPath(), PathPlugins))
6060
}

dir/fs_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func TestPluginFS(t *testing.T) {
6767
if err != nil {
6868
t.Fatalf("SysPath() failed. err = %v", err)
6969
}
70-
if path != filepath.Join(UserLibexecDir, PathPlugins, "plugin") {
71-
t.Fatalf(`SysPath() failed. got: %q, want: %q`, path, filepath.Join(UserLibexecDir, PathPlugins, "plugin"))
70+
if path != filepath.Join(userLibexecDirPath(), PathPlugins, "plugin") {
71+
t.Fatalf(`SysPath() failed. got: %q, want: %q`, path, filepath.Join(userLibexecDirPath(), PathPlugins, "plugin"))
7272
}
7373
}

dir/path.go

+19-12
Original file line numberDiff line numberDiff line change
@@ -73,21 +73,28 @@ const (
7373

7474
var userConfigDir = os.UserConfigDir // for unit test
7575

76-
func init() {
77-
loadUserPath()
76+
// userConfigDirPath returns the user level {NOTATION_CONFIG} path.
77+
func userConfigDirPath() string {
78+
if UserConfigDir == "" {
79+
userDir, err := userConfigDir()
80+
if err != nil {
81+
// fallback to current directory
82+
UserConfigDir = "." + notation
83+
return UserConfigDir
84+
}
85+
// set user config
86+
UserConfigDir = filepath.Join(userDir, notation)
87+
}
88+
return UserConfigDir
7889
}
7990

80-
// loadUserPath function defines UserConfigDir and UserLibexecDir.
81-
func loadUserPath() {
82-
// set user config
83-
userDir, err := userConfigDir()
84-
if err != nil {
85-
panic(err)
91+
// userLibexecDirPath returns the user level {NOTATION_LIBEXEC} path.
92+
func userLibexecDirPath() string {
93+
if UserLibexecDir == "" {
94+
// set user libexec
95+
UserLibexecDir = userConfigDirPath()
8696
}
87-
UserConfigDir = filepath.Join(userDir, notation)
88-
89-
// set user libexec
90-
UserLibexecDir = UserConfigDir
97+
return UserLibexecDir
9198
}
9299

93100
// LocalKeyPath returns the local key and local cert relative paths.

dir/path_test.go

+35-10
Original file line numberDiff line numberDiff line change
@@ -14,30 +14,53 @@
1414
package dir
1515

1616
import (
17-
"path/filepath"
17+
"os"
1818
"testing"
1919
)
2020

2121
func mockGetUserConfig() (string, error) {
2222
return "/path/", nil
2323
}
2424

25-
func Test_loadPath(t *testing.T) {
26-
wantDir := filepath.FromSlash("/path/notation")
25+
func setup() {
26+
UserConfigDir = ""
27+
UserLibexecDir = ""
28+
}
29+
30+
func Test_UserConfigDirPath(t *testing.T) {
2731
userConfigDir = mockGetUserConfig
28-
loadUserPath()
29-
if UserConfigDir != wantDir {
30-
t.Fatalf(`loadPath() UserConfigDir is incorrect. got: %q, want: %q`, UserConfigDir, wantDir)
32+
setup()
33+
got := userConfigDirPath()
34+
if got != "/path/notation" {
35+
t.Fatalf(`UserConfigDirPath() = %q, want "/path/notation"`, got)
3136
}
37+
}
3238

33-
if UserLibexecDir != UserConfigDir {
34-
t.Fatalf(`loadPath() UserLibexecDir is incorrect. got: %q, want: %q`, UserLibexecDir, wantDir)
39+
func Test_NoHomeVariable(t *testing.T) {
40+
t.Setenv("HOME", "")
41+
t.Setenv("XDG_CONFIG_HOME", "")
42+
setup()
43+
userConfigDir = os.UserConfigDir
44+
got := userConfigDirPath()
45+
if got != ".notation" {
46+
t.Fatalf(`UserConfigDirPath() = %q, want ".notation"`, UserConfigDir)
47+
}
48+
}
49+
50+
func Test_UserLibexecDirPath(t *testing.T) {
51+
userConfigDir = mockGetUserConfig
52+
setup()
53+
got := userLibexecDirPath()
54+
if got != "/path/notation" {
55+
t.Fatalf(`UserConfigDirPath() = %q, want "/path/notation"`, got)
3556
}
3657
}
3758

3859
func TestLocalKeyPath(t *testing.T) {
3960
userConfigDir = mockGetUserConfig
40-
loadUserPath()
61+
setup()
62+
_ = userConfigDirPath()
63+
_ = userLibexecDirPath()
4164
gotKeyPath, gotCertPath := LocalKeyPath("web")
4265
if gotKeyPath != "localkeys/web.key" {
4366
t.Fatalf(`LocalKeyPath() gotKeyPath = %q, want "localkeys/web.key"`, gotKeyPath)
@@ -49,7 +72,9 @@ func TestLocalKeyPath(t *testing.T) {
4972

5073
func TestX509TrustStoreDir(t *testing.T) {
5174
userConfigDir = mockGetUserConfig
52-
loadUserPath()
75+
setup()
76+
_ = userConfigDirPath()
77+
_ = userLibexecDirPath()
5378
if got := X509TrustStoreDir("ca", "web"); got != "truststore/x509/ca/web" {
5479
t.Fatalf(`X509TrustStoreDir() = %q, want "truststore/x509/ca/web"`, got)
5580
}

0 commit comments

Comments
 (0)