From 0e486a56c99bd4646b0a26f195e1cd218cee4e63 Mon Sep 17 00:00:00 2001 From: Marius van der Wijden Date: Fri, 13 Jan 2023 15:48:42 +0100 Subject: [PATCH] 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 --- internal/flags/flags.go | 8 ++++++-- internal/flags/flags_test.go | 36 ++++++++++++++++++++++++++++-------- 2 files changed, 34 insertions(+), 10 deletions(-) diff --git a/internal/flags/flags.go b/internal/flags/flags.go index 0ae2c6a512..b0756b4e0a 100644 --- a/internal/flags/flags.go +++ b/internal/flags/flags.go @@ -23,7 +23,7 @@ import ( "math/big" "os" "os/user" - "path" + "path/filepath" "strings" "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 // 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 { diff --git a/internal/flags/flags_test.go b/internal/flags/flags_test.go index a0d4af7ca3..681586b46c 100644 --- a/internal/flags/flags_test.go +++ b/internal/flags/flags_test.go @@ -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) } } }