Use filepath.clean instead of path.clean (#26404)
* internal/flags: use filepath.Clean instead of path.Clean * internal/flags: fix windows pipe issue * internal/flags: modify test for windows * internal/flags: use backticks, fix test
This commit is contained in:
parent
e04d63ebd3
commit
0e486a56c9
@ -23,7 +23,7 @@ import (
|
|||||||
"math/big"
|
"math/big"
|
||||||
"os"
|
"os"
|
||||||
"os/user"
|
"os/user"
|
||||||
"path"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/ethereum/go-ethereum/common/math"
|
"github.com/ethereum/go-ethereum/common/math"
|
||||||
@ -314,12 +314,16 @@ func GlobalBig(ctx *cli.Context, name string) *big.Int {
|
|||||||
// 3. cleans the path, e.g. /a/b/../c -> /a/c
|
// 3. cleans the path, e.g. /a/b/../c -> /a/c
|
||||||
// Note, it has limitations, e.g. ~someuser/tmp will not be expanded
|
// Note, it has limitations, e.g. ~someuser/tmp will not be expanded
|
||||||
func expandPath(p string) string {
|
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 strings.HasPrefix(p, "~/") || strings.HasPrefix(p, "~\\") {
|
||||||
if home := HomeDir(); home != "" {
|
if home := HomeDir(); home != "" {
|
||||||
p = home + p[1:]
|
p = home + p[1:]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return path.Clean(os.ExpandEnv(p))
|
return filepath.Clean(os.ExpandEnv(p))
|
||||||
}
|
}
|
||||||
|
|
||||||
func HomeDir() string {
|
func HomeDir() string {
|
||||||
|
@ -19,23 +19,43 @@ package flags
|
|||||||
import (
|
import (
|
||||||
"os"
|
"os"
|
||||||
"os/user"
|
"os/user"
|
||||||
|
"runtime"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestPathExpansion(t *testing.T) {
|
func TestPathExpansion(t *testing.T) {
|
||||||
user, _ := user.Current()
|
user, _ := user.Current()
|
||||||
tests := map[string]string{
|
var tests map[string]string
|
||||||
"/home/someuser/tmp": "/home/someuser/tmp",
|
|
||||||
"~/tmp": user.HomeDir + "/tmp",
|
if runtime.GOOS == "windows" {
|
||||||
"~thisOtherUser/b/": "~thisOtherUser/b",
|
tests = map[string]string{
|
||||||
"$DDDXXX/a/b": "/tmp/a/b",
|
`/home/someuser/tmp`: `\home\someuser\tmp`,
|
||||||
"/a/b/": "/a/b",
|
`~/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")
|
} 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`)
|
||||||
for test, expected := range tests {
|
for test, expected := range tests {
|
||||||
got := expandPath(test)
|
got := expandPath(test)
|
||||||
if got != expected {
|
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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user