From 952e6dc25cd0d0b1ac654e31acccac1c870401f1 Mon Sep 17 00:00:00 2001 From: Kimmo Lehto Date: Fri, 23 Feb 2024 15:32:41 +0200 Subject: [PATCH] Make homedir_test windows compatible Signed-off-by: Kimmo Lehto --- homedir/expand.go | 35 +++++++++++++++++++++++++++++++++ homedir/expand_windows.go | 22 +++++++++++++++++++++ homedir/homedir.go | 31 ++--------------------------- homedir/homedir_test.go | 10 ++++++---- homedir/homedir_windows_test.go | 22 +++++++++++++++++++++ 5 files changed, 87 insertions(+), 33 deletions(-) create mode 100644 homedir/expand.go create mode 100644 homedir/expand_windows.go create mode 100644 homedir/homedir_windows_test.go diff --git a/homedir/expand.go b/homedir/expand.go new file mode 100644 index 00000000..08e1e2cb --- /dev/null +++ b/homedir/expand.go @@ -0,0 +1,35 @@ +//go:build !windows + +// Package homedir provides functions for getting the user's home directory in Go. +package homedir + +import ( + "errors" + "fmt" + "os" + "path/filepath" + "strings" +) + +var errNotImplemented = errors.New("not implemented") + +// Expand does ~/ style path expansion for files under current user home. ~user/ style paths are not supported. +func Expand(path string) (string, error) { + if !strings.HasPrefix(path, "~") { + return path, nil + } + + parts := strings.Split(path, string(os.PathSeparator)) + if parts[0] != "~" { + return "", fmt.Errorf("%w: ~user/ style paths not supported", errNotImplemented) + } + + home, err := os.UserHomeDir() + if err != nil { + return "", fmt.Errorf("homedir expand: %w", err) + } + + parts[0] = home + + return filepath.Join(parts...), nil +} diff --git a/homedir/expand_windows.go b/homedir/expand_windows.go new file mode 100644 index 00000000..7d338dfe --- /dev/null +++ b/homedir/expand_windows.go @@ -0,0 +1,22 @@ +package homedir + +import ( + "fmt" + "os" + "path/filepath" + "strings" +) + +// Expand does ~/ style path expansion for files under current user home. On windows, this supports paths like %USERPROFILE%\path. +func Expand(path string) (string, error) { + parts := strings.Split(path, string(os.PathSeparator)) + if parts[0] != "~" && parts[0] != "%USERPROFILE%" && parts[0] != "%userprofile%" && parts[0] != "%HOME%" && parts[0] != "%home%" { + return path, nil + } + home, err := os.UserHomeDir() + if err != nil { + return "", fmt.Errorf("homedir expand: get user home: %w", err) + } + parts[0] = home + return filepath.Join(parts...), nil +} diff --git a/homedir/homedir.go b/homedir/homedir.go index 34c56329..7a6f0651 100644 --- a/homedir/homedir.go +++ b/homedir/homedir.go @@ -5,37 +5,10 @@ import ( "errors" "fmt" "os" - gopath "path" - "path/filepath" - "strings" ) -var ( - errNotImplemented = errors.New("not implemented") - // ErrInvalidPath is returned when the given path is invalid. - ErrInvalidPath = errors.New("invalid path") -) - -// Expand does ~/ style path expansion for files under current user home. ~user/ style paths are not supported. -func Expand(path string) (string, error) { - if !strings.HasPrefix(path, "~") { - return path, nil - } - - parts := strings.Split(filepath.FromSlash(path), "/") - if parts[0] != "~" { - return "", fmt.Errorf("%w: ~user/ style paths not supported", errNotImplemented) - } - - home, err := os.UserHomeDir() - if err != nil { - return "", fmt.Errorf("homedir expand: %w", err) - } - - parts[0] = home - - return gopath.Join(parts...), nil -} +// ErrInvalidPath is returned when the given path is invalid. +var ErrInvalidPath = errors.New("invalid path") func expandStat(path string) (os.FileInfo, error) { if len(path) == 0 { diff --git a/homedir/homedir_test.go b/homedir/homedir_test.go index 2499816b..d94ad97c 100644 --- a/homedir/homedir_test.go +++ b/homedir/homedir_test.go @@ -1,3 +1,5 @@ +//go:build !windows + package homedir_test import ( @@ -10,11 +12,11 @@ import ( func TestExpand(t *testing.T) { t.Setenv("HOME", "/home/test") - home, err := homedir.Expand("~/tmp") + homeTmp, err := homedir.Expand("~/tmp") assert.NoError(t, err) - assert.Equal(t, home, "/home/test/tmp") + assert.Equal(t, "/home/test/tmp", homeTmp) - home, err = homedir.Expand("/tmp") + tmp, err := homedir.Expand("/tmp/foo") assert.NoError(t, err) - assert.Equal(t, home, "/tmp") + assert.Equal(t, "/tmp/foo", tmp) } diff --git a/homedir/homedir_windows_test.go b/homedir/homedir_windows_test.go new file mode 100644 index 00000000..b7b7c8bc --- /dev/null +++ b/homedir/homedir_windows_test.go @@ -0,0 +1,22 @@ +//go:build windows + +package homedir_test + +import ( + "testing" + + "github.com/k0sproject/rig/homedir" + "github.com/stretchr/testify/assert" +) + +func TestExpand(t *testing.T) { + t.Setenv("USERPROFILE", "C:\\Users\\test") + + homeTmp, err := homedir.Expand("%USERPROFILE%\\tmp") + assert.NoError(t, err) + assert.Equal(t, "C:\\Users\\test\\tmp", homeTmp) + + tmp, err := homedir.Expand("C:\\tmp\\foo") + assert.NoError(t, err) + assert.Equal(t, "C:\\tmp\\foo", tmp) +}