Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use filepath.clean instead of path.clean #26404

Merged
merged 4 commits into from
Jan 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions internal/flags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
"math/big"
"os"
"os/user"
"path"
"path/filepath"
"strings"

"github.com/ethereum/go-ethereum/common/math"
Expand Down Expand Up @@ -314,12 +314,16 @@ func GlobalBig(ctx *cli.Context, name string) *big.Int {
// 3. cleans the path, e.g. /a/b/../c -> /a/c
// Note, it has limitations, e.g. ~someuser/tmp will not be expanded
func expandPath(p string) string {
// Named pipes are not file paths on windows, ignore
if strings.HasPrefix(p, `\\.\pipe`) {
return p
}
if strings.HasPrefix(p, "~/") || strings.HasPrefix(p, "~\\") {
if home := HomeDir(); home != "" {
p = home + p[1:]
}
}
return path.Clean(os.ExpandEnv(p))
return filepath.Clean(os.ExpandEnv(p))
}

func HomeDir() string {
Expand Down
36 changes: 28 additions & 8 deletions internal/flags/flags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,43 @@ package flags
import (
"os"
"os/user"
"runtime"
"testing"
)

func TestPathExpansion(t *testing.T) {
user, _ := user.Current()
tests := map[string]string{
"/home/someuser/tmp": "/home/someuser/tmp",
"~/tmp": user.HomeDir + "/tmp",
"~thisOtherUser/b/": "~thisOtherUser/b",
"$DDDXXX/a/b": "/tmp/a/b",
"/a/b/": "/a/b",
var tests map[string]string

if runtime.GOOS == "windows" {
tests = map[string]string{
`/home/someuser/tmp`: `\home\someuser\tmp`,
`~/tmp`: user.HomeDir + `\tmp`,
`~thisOtherUser/b/`: `~thisOtherUser\b`,
`$DDDXXX/a/b`: `\tmp\a\b`,
`/a/b/`: `\a\b`,
`C:\Documents\Newsletters\`: `C:\Documents\Newsletters`,
`C:\`: `C:\`,
`\\.\pipe\\pipe\geth621383`: `\\.\pipe\\pipe\geth621383`,
}
} else {
tests = map[string]string{
`/home/someuser/tmp`: `/home/someuser/tmp`,
`~/tmp`: user.HomeDir + `/tmp`,
`~thisOtherUser/b/`: `~thisOtherUser/b`,
`$DDDXXX/a/b`: `/tmp/a/b`,
`/a/b/`: `/a/b`,
`C:\Documents\Newsletters\`: `C:\Documents\Newsletters\`,
`C:\`: `C:\`,
`\\.\pipe\\pipe\geth621383`: `\\.\pipe\\pipe\geth621383`,
}
}
os.Setenv("DDDXXX", "/tmp")

os.Setenv(`DDDXXX`, `/tmp`)
for test, expected := range tests {
got := expandPath(test)
if got != expected {
t.Errorf("test %s, got %s, expected %s\n", test, got, expected)
t.Errorf(`test %s, got %s, expected %s\n`, test, got, expected)
}
}
}