-
Notifications
You must be signed in to change notification settings - Fork 301
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
84a3a97
commit a84465e
Showing
4 changed files
with
65 additions
and
2 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
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,12 @@ | ||
package osutil | ||
|
||
import "os" | ||
|
||
// UserHomeDir is similar to os.UserHomeDir, but prefers $HOME when available | ||
// over other options (such as USERPROFILE on Windows). | ||
func UserHomeDir() (string, error) { | ||
if h := os.Getenv("HOME"); h != "" { | ||
return h, nil | ||
} | ||
return os.UserHomeDir() | ||
} |
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,50 @@ | ||
package osutil | ||
|
||
import ( | ||
"os" | ||
"runtime" | ||
"testing" | ||
) | ||
|
||
func TestUserHomeDir(t *testing.T) { | ||
// not parallel because it messes with env vars | ||
origHome := os.Getenv("HOME") | ||
origUserProfile := os.Getenv("USERPROFILE") | ||
t.Cleanup(func() { | ||
os.Setenv("HOME", origHome) | ||
os.Setenv("USERPROFILE", origUserProfile) | ||
}) | ||
|
||
type testCase struct { | ||
home, userProfile, want string | ||
} | ||
|
||
tests := []testCase{ | ||
{ | ||
// Prefer $HOME on all platforms | ||
home: "home", | ||
userProfile: "userProfile", | ||
want: "home", | ||
}, | ||
} | ||
if runtime.GOOS == "windows" { | ||
// Windows can use %USERPROFILE% as a treat when $HOME is unavailable | ||
tests = append(tests, testCase{ | ||
home: "", | ||
userProfile: "userProfile", | ||
want: "userProfile", | ||
}) | ||
} | ||
|
||
for _, test := range tests { | ||
os.Setenv("HOME", test.home) | ||
os.Setenv("USERPROFILE", test.userProfile) | ||
got, err := UserHomeDir() | ||
if err != nil { | ||
t.Errorf("HOME=%q USERPROFILE=%q UserHomeDir() error = %v", test.home, test.userProfile, err) | ||
} | ||
if got != test.want { | ||
t.Errorf("HOME=%q USERPROFILE=%q UserHomeDir() = %q, want %q", test.home, test.userProfile, got, test.want) | ||
} | ||
} | ||
} |