Skip to content

Commit

Permalink
CHG: ResolveHomeDir => ExpandHomeDir; ResolveBase => ResolveUrlPath
Browse files Browse the repository at this point in the history
  • Loading branch information
whoisnian committed Dec 27, 2023
1 parent dfa0162 commit 4f31a72
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 22 deletions.
30 changes: 15 additions & 15 deletions util/fsutil/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,25 @@ import (
"os"
"path"
"path/filepath"
"strings"
)

// ResolveHomeDir resolve prefix '~' in path with environment variable.
func ResolveHomeDir(rawPath string) (string, error) {
if strings.HasPrefix(rawPath, "~/") {
homeDir, err := os.UserHomeDir()
if err != nil {
return "", err
}
return filepath.Join(homeDir, rawPath[2:]), nil
// ExpandHomeDir expands prefix '~' in rawFilePath with HOME environment variable.
func ExpandHomeDir(rawFilePath string) (string, error) {
if len(rawFilePath) == 0 || rawFilePath[0] != '~' || (len(rawFilePath) > 1 && rawFilePath[1] != '/' && rawFilePath[1] != '\\') {
return filepath.Clean(rawFilePath), nil
}
return filepath.Clean(rawPath), nil

homeDir, err := os.UserHomeDir()
if err != nil || len(rawFilePath) == 1 {
return homeDir, err
}
return filepath.Join(homeDir, rawFilePath[1:]), nil
}

// ResolveBase resolve rawPath within the basePath.
func ResolveBase(basePath string, rawPath string) string {
if rawPath == "" || rawPath[0] != '/' {
rawPath = "/" + rawPath
// ResolveUrlPath resolves rawUrlPath within the baseFilePath.
func ResolveUrlPath(baseFilePath string, rawUrlPath string) string {
if rawUrlPath == "" || rawUrlPath[0] != '/' {
rawUrlPath = "/" + rawUrlPath
}
return filepath.Join(basePath, filepath.FromSlash(path.Clean(rawPath)))
return filepath.Join(baseFilePath, filepath.FromSlash(path.Clean(rawUrlPath)))
}
16 changes: 9 additions & 7 deletions util/fsutil/path_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/whoisnian/glb/util/fsutil"
)

func TestResolveHomeDir(t *testing.T) {
func TestExpandHomeDir(t *testing.T) {
homeKey, homeVal := "HOME", "/home/nian"
if runtime.GOOS == "windows" {
homeKey = "USERPROFILE"
Expand All @@ -26,6 +26,8 @@ func TestResolveHomeDir(t *testing.T) {
{"", "."}, // clean only
{"./doc", "doc"}, // clean only
{"/tmp", "/tmp"}, // clean only
{"~tmp", "~tmp"}, // clean only
{"~", homeVal},
{"~/", homeVal},
{"~/.", homeVal},
{"~/..", filepath.Dir(homeVal)},
Expand All @@ -36,15 +38,15 @@ func TestResolveHomeDir(t *testing.T) {
if runtime.GOOS == "windows" {
want = filepath.FromSlash(test.want)
}
if got, err := fsutil.ResolveHomeDir(test.input); err != nil {
t.Errorf("ResolveHomeDir(%q) error: %v", test.input, err)
if got, err := fsutil.ExpandHomeDir(test.input); err != nil {
t.Errorf("ExpandHomeDir(%q) error: %v", test.input, err)
} else if got != want {
t.Errorf("ResolveHomeDir(%q) = %q, want %q", test.input, got, want)
t.Errorf("ExpandHomeDir(%q) = %q, want %q", test.input, got, want)
}
}
}

func TestResolveBase(t *testing.T) {
func TestResolveUrlPath(t *testing.T) {
var tests = []struct {
inputBase string
inputRaw string
Expand All @@ -63,8 +65,8 @@ func TestResolveBase(t *testing.T) {
if runtime.GOOS == "windows" {
want = filepath.FromSlash(test.want)
}
if got := fsutil.ResolveBase(test.inputBase, test.inputRaw); got != want {
t.Errorf("ResolveBase(%q, %q) = %q, want %q", test.inputBase, test.inputRaw, got, want)
if got := fsutil.ResolveUrlPath(test.inputBase, test.inputRaw); got != want {
t.Errorf("ResolveUrlPath(%q, %q) = %q, want %q", test.inputBase, test.inputRaw, got, want)
}
}
}

0 comments on commit 4f31a72

Please sign in to comment.