-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make homedir_test windows compatible
Signed-off-by: Kimmo Lehto <[email protected]>
- Loading branch information
Showing
5 changed files
with
87 additions
and
33 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
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 | ||
} |
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,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 | ||
} |
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,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") | ||
} |