Skip to content

Commit

Permalink
Add osutil.UserHomeDir
Browse files Browse the repository at this point in the history
  • Loading branch information
DrJosh9000 committed Sep 17, 2024
1 parent 84a3a97 commit a84465e
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 2 deletions.
2 changes: 1 addition & 1 deletion clicommand/agent_start.go
Original file line number Diff line number Diff line change
Expand Up @@ -1383,7 +1383,7 @@ func agentLifecycleHook(hookName string, log logger.Logger, cfg AgentStartConfig
}

func defaultSocketsPath() string {
home, err := os.UserHomeDir()
home, err := osutil.UserHomeDir()
if err != nil {
return filepath.Join(os.TempDir(), "buildkite-sockets")
}
Expand Down
3 changes: 2 additions & 1 deletion internal/job/knownhosts.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"time"

"github.com/buildkite/agent/v3/internal/job/shell"
"github.com/buildkite/agent/v3/internal/osutil"
"golang.org/x/crypto/ssh/knownhosts"
)

Expand All @@ -19,7 +20,7 @@ type knownHosts struct {
}

func findKnownHosts(sh *shell.Shell) (*knownHosts, error) {
userHomePath, err := os.UserHomeDir()
userHomePath, err := osutil.UserHomeDir()
if err != nil {
return nil, fmt.Errorf("Could not find the current users home directory (%s)", err)
}
Expand Down
12 changes: 12 additions & 0 deletions internal/osutil/homedir.go
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()
}
50 changes: 50 additions & 0 deletions internal/osutil/homedir_test.go
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)
}
}
}

0 comments on commit a84465e

Please sign in to comment.