|
| 1 | +// SPDX-FileCopyrightText: Copyright 2015-2025 go-swagger maintainers |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package vfs |
| 5 | + |
| 6 | +import ( |
| 7 | + "testing" |
| 8 | + "testing/fstest" |
| 9 | + |
| 10 | + "github.com/go-openapi/testify/v2/assert" |
| 11 | +) |
| 12 | + |
| 13 | +// A path handed to a virtual filesystem is normalised by its own shape, not by the host's. |
| 14 | +// |
| 15 | +// The two forms have to land in the same place: io/fs holds one rooted namespace, and a caller who writes an absolute |
| 16 | +// path means the top of it either way. A Windows drive is the case that does not follow from the POSIX rule — it is |
| 17 | +// what makes the path absolute and it is not a separator — so "D:\a\b" left as "D:/a/b" addressed nothing, and every |
| 18 | +// read under a scan pointed at such a path failed. |
| 19 | +func TestCleanNormalisesEveryRootedForm(t *testing.T) { |
| 20 | + t.Parallel() |
| 21 | + |
| 22 | + v := New(fstest.MapFS{}) |
| 23 | + |
| 24 | + for _, tc := range []struct { |
| 25 | + name string |
| 26 | + in string |
| 27 | + want string |
| 28 | + }{ |
| 29 | + {"posix absolute", "/a/b", "a/b"}, |
| 30 | + {"posix relative", "./a/b", "a/b"}, |
| 31 | + {"windows drive", `D:\a\b`, "a/b"}, |
| 32 | + {"windows drive, slashes", "D:/a/b", "a/b"}, |
| 33 | + {"windows drive, lowercase", `c:\a\b`, "a/b"}, |
| 34 | + {"windows relative", `a\b`, "a/b"}, |
| 35 | + {"drive root", `D:\`, "."}, |
| 36 | + {"empty", "", "."}, |
| 37 | + {"root", "/", "."}, |
| 38 | + {"colon that is not a drive", "ab:c/d", "ab:c/d"}, |
| 39 | + } { |
| 40 | + t.Run(tc.name, func(t *testing.T) { |
| 41 | + t.Parallel() |
| 42 | + |
| 43 | + assert.Equal(t, tc.want, v.Clean(tc.in)) |
| 44 | + }) |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +// Naming the last element of a directory is how the walk recognises a vendor directory, so it has to work whichever |
| 49 | +// separator the path carries: path.Base looks for a forward slash and hands a Windows path back whole, which reads as |
| 50 | +// "this directory is not called vendor" and lets the wildcard walk straight into it. |
| 51 | +func TestBaseNamesTheLastElement(t *testing.T) { |
| 52 | + t.Parallel() |
| 53 | + |
| 54 | + v := New(fstest.MapFS{}) |
| 55 | + |
| 56 | + assert.Equal(t, "vendor", v.Base("/src/m/vendor")) |
| 57 | + assert.Equal(t, "vendor", v.Base(`D:\src\m\vendor`)) |
| 58 | + assert.Equal(t, "m", v.Base("src/m")) |
| 59 | +} |
| 60 | + |
| 61 | +// A walk hands paths back in the form the caller wrote them, because they become the file names in every position the |
| 62 | +// scan reports. The walk itself happens in io/fs's slash-separated namespace, so the caller's separator has to be put |
| 63 | +// back on the way out. |
| 64 | +func TestRebaseKeepsTheCallersForm(t *testing.T) { |
| 65 | + t.Parallel() |
| 66 | + |
| 67 | + for _, tc := range []struct { |
| 68 | + name string |
| 69 | + root, inner, out string |
| 70 | + want string |
| 71 | + }{ |
| 72 | + {"posix absolute", "/src/m", "src/m", "src/m/sub", "/src/m/sub"}, |
| 73 | + {"posix root", "/", ".", "sub/pkg", "/sub/pkg"}, |
| 74 | + {"relative", ".", ".", "sub", "./sub"}, |
| 75 | + {"the root itself", "/src/m", "src/m", "src/m", "/src/m"}, |
| 76 | + {"windows drive", `D:\src\m`, "src/m", "src/m/sub/pkg", `D:\src\m\sub\pkg`}, |
| 77 | + {"windows drive root", `D:\`, ".", "sub", `D:\sub`}, |
| 78 | + } { |
| 79 | + t.Run(tc.name, func(t *testing.T) { |
| 80 | + t.Parallel() |
| 81 | + |
| 82 | + assert.Equal(t, tc.want, rebase(tc.root, tc.inner, tc.out)) |
| 83 | + }) |
| 84 | + } |
| 85 | +} |
0 commit comments