Skip to content

Commit

Permalink
Make homedir_test windows compatible
Browse files Browse the repository at this point in the history
Signed-off-by: Kimmo Lehto <[email protected]>
  • Loading branch information
kke committed Feb 23, 2024
1 parent b348fed commit d4d105f
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 33 deletions.
35 changes: 35 additions & 0 deletions homedir/expand.go
Original file line number Diff line number Diff line change
@@ -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
}
22 changes: 22 additions & 0 deletions homedir/expand_windows.go
Original file line number Diff line number Diff line change
@@ -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
}
31 changes: 2 additions & 29 deletions homedir/homedir.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
10 changes: 6 additions & 4 deletions homedir/homedir_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build !windows

package homedir_test

import (
Expand All @@ -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, homeTmp, "/home/test/tmp")

home, err = homedir.Expand("/tmp")
tmp, err := homedir.Expand("/tmp/foo")
assert.NoError(t, err)
assert.Equal(t, home, "/tmp")
assert.Equal(t, tmp, "/tmp/foo")
}
22 changes: 22 additions & 0 deletions homedir/homedir_windows_test.go
Original file line number Diff line number Diff line change
@@ -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, homeTmp, "C:\\Users\\test\\tmp")

tmp, err := homedir.Expand("C:\\tmp\\foo")
assert.NoError(t, err)
assert.Equal(t, tmp, "C:\\tmp\\foo")
}

0 comments on commit d4d105f

Please sign in to comment.