diff options
| author | Damien Neil <dneil@google.com> | 2023-09-30 10:38:01 -0700 |
|---|---|---|
| committer | Damien Neil <dneil@google.com> | 2024-02-26 18:08:14 +0000 |
| commit | e596e8831885ad057a7f1db1391fddfd53f06431 (patch) | |
| tree | 0e5893682f270732c95e823e3623c49ae87d98ec /src/path/filepath/path_test.go | |
| parent | 7b583fd1a1aeda98daa5a9d485b35786c031e941 (diff) | |
| download | go-e596e8831885ad057a7f1db1391fddfd53f06431.tar.xz | |
path/filepath: add Localize
Add the Localize function, which takes an io/fs slash-separated path
and returns an operating system path.
Localize returns an error if the path cannot be represented on
the current platform.
Replace internal/safefile.FromFS with Localize,
which serves the same purpose as this function.
The internal/safefile package remains separate from path/filepath
to avoid a dependency cycle with the os package.
Fixes #57151
Change-Id: I75c88047ddea17808276761da07bf79172c4f6fc
Reviewed-on: https://go-review.googlesource.com/c/go/+/531677
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Diffstat (limited to 'src/path/filepath/path_test.go')
| -rw-r--r-- | src/path/filepath/path_test.go | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/src/path/filepath/path_test.go b/src/path/filepath/path_test.go index c96a758c69..1b2a66bc6d 100644 --- a/src/path/filepath/path_test.go +++ b/src/path/filepath/path_test.go @@ -237,6 +237,73 @@ func TestIsLocal(t *testing.T) { } } +type LocalizeTest struct { + path string + want string +} + +var localizetests = []LocalizeTest{ + {"", ""}, + {".", "."}, + {"..", ""}, + {"a/..", ""}, + {"/", ""}, + {"/a", ""}, + {"a\xffb", ""}, + {"a/", ""}, + {"a/./b", ""}, + {"\x00", ""}, + {"a", "a"}, + {"a/b/c", "a/b/c"}, +} + +var plan9localizetests = []LocalizeTest{ + {"#a", ""}, + {`a\b:c`, `a\b:c`}, +} + +var unixlocalizetests = []LocalizeTest{ + {"#a", "#a"}, + {`a\b:c`, `a\b:c`}, +} + +var winlocalizetests = []LocalizeTest{ + {"#a", "#a"}, + {"c:", ""}, + {`a\b`, ""}, + {`a:b`, ""}, + {`a/b:c`, ""}, + {`NUL`, ""}, + {`a/NUL`, ""}, + {`./com1`, ""}, + {`a/nul/b`, ""}, +} + +func TestLocalize(t *testing.T) { + tests := localizetests + switch runtime.GOOS { + case "plan9": + tests = append(tests, plan9localizetests...) + case "windows": + tests = append(tests, winlocalizetests...) + for i := range tests { + tests[i].want = filepath.FromSlash(tests[i].want) + } + default: + tests = append(tests, unixlocalizetests...) + } + for _, test := range tests { + got, err := filepath.Localize(test.path) + wantErr := "<nil>" + if test.want == "" { + wantErr = "error" + } + if got != test.want || ((err == nil) != (test.want != "")) { + t.Errorf("IsLocal(%q) = %q, %v want %q, %v", test.path, got, err, test.want, wantErr) + } + } +} + const sep = filepath.Separator var slashtests = []PathTest{ |
