Skip to content

Commit 0e486a5

Browse files
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
1 parent e04d63e commit 0e486a5

File tree

2 files changed

+34
-10
lines changed

2 files changed

+34
-10
lines changed

internal/flags/flags.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import (
2323
"math/big"
2424
"os"
2525
"os/user"
26-
"path"
26+
"path/filepath"
2727
"strings"
2828

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

325329
func HomeDir() string {

internal/flags/flags_test.go

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,43 @@ package flags
1919
import (
2020
"os"
2121
"os/user"
22+
"runtime"
2223
"testing"
2324
)
2425

2526
func TestPathExpansion(t *testing.T) {
2627
user, _ := user.Current()
27-
tests := map[string]string{
28-
"/home/someuser/tmp": "/home/someuser/tmp",
29-
"~/tmp": user.HomeDir + "/tmp",
30-
"~thisOtherUser/b/": "~thisOtherUser/b",
31-
"$DDDXXX/a/b": "/tmp/a/b",
32-
"/a/b/": "/a/b",
28+
var tests map[string]string
29+
30+
if runtime.GOOS == "windows" {
31+
tests = map[string]string{
32+
`/home/someuser/tmp`: `\home\someuser\tmp`,
33+
`~/tmp`: user.HomeDir + `\tmp`,
34+
`~thisOtherUser/b/`: `~thisOtherUser\b`,
35+
`$DDDXXX/a/b`: `\tmp\a\b`,
36+
`/a/b/`: `\a\b`,
37+
`C:\Documents\Newsletters\`: `C:\Documents\Newsletters`,
38+
`C:\`: `C:\`,
39+
`\\.\pipe\\pipe\geth621383`: `\\.\pipe\\pipe\geth621383`,
40+
}
41+
} else {
42+
tests = map[string]string{
43+
`/home/someuser/tmp`: `/home/someuser/tmp`,
44+
`~/tmp`: user.HomeDir + `/tmp`,
45+
`~thisOtherUser/b/`: `~thisOtherUser/b`,
46+
`$DDDXXX/a/b`: `/tmp/a/b`,
47+
`/a/b/`: `/a/b`,
48+
`C:\Documents\Newsletters\`: `C:\Documents\Newsletters\`,
49+
`C:\`: `C:\`,
50+
`\\.\pipe\\pipe\geth621383`: `\\.\pipe\\pipe\geth621383`,
51+
}
3352
}
34-
os.Setenv("DDDXXX", "/tmp")
53+
54+
os.Setenv(`DDDXXX`, `/tmp`)
3555
for test, expected := range tests {
3656
got := expandPath(test)
3757
if got != expected {
38-
t.Errorf("test %s, got %s, expected %s\n", test, got, expected)
58+
t.Errorf(`test %s, got %s, expected %s\n`, test, got, expected)
3959
}
4060
}
4161
}

0 commit comments

Comments
 (0)