-
Notifications
You must be signed in to change notification settings - Fork 302
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3000 from buildkite/fix-homedir
Prefer $HOME on all platforms
- Loading branch information
Showing
16 changed files
with
98 additions
and
37 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
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
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,2 @@ | ||
// Package osutil provides some OS-level helper functions. | ||
package osutil |
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package utils | ||
package osutil | ||
|
||
import ( | ||
"fmt" | ||
|
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) | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package utils | ||
package osutil | ||
|
||
import ( | ||
"errors" | ||
|
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package utils | ||
package osutil | ||
|
||
import ( | ||
"os" | ||
|
2 changes: 1 addition & 1 deletion
2
internal/utils/path_windows_test.go → internal/osutil/path_windows_test.go
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 |
---|---|---|
@@ -1,7 +1,7 @@ | ||
//go:build windows | ||
// +build windows | ||
|
||
package utils | ||
package osutil | ||
|
||
import ( | ||
"os" | ||
|
This file was deleted.
Oops, something went wrong.