| 1 |
package pathutil |
| 2 |
|
| 3 |
import ( |
| 4 |
"testing" |
| 5 |
|
| 6 |
"github.com/stretchr/testify/assert" |
| 7 |
) |
| 8 |
|
| 9 |
func TestWindowsValidPath(t *testing.T) { |
| 10 |
t.Parallel() |
| 11 |
tests := []struct { |
| 12 |
path string |
| 13 |
want bool |
| 14 |
}{ |
| 15 |
{".git", true}, |
| 16 |
{".git . . .", false}, |
| 17 |
{".git ", false}, |
| 18 |
{".git ", false}, |
| 19 |
{".git . .", false}, |
| 20 |
{".git . .", false}, |
| 21 |
{".git::$INDEX_ALLOCATION", false}, |
| 22 |
{".git:", false}, |
| 23 |
{"git~1 ", false}, |
| 24 |
{"git~1.", false}, |
| 25 |
{"GIT~1 ", false}, |
| 26 |
{"git~1::$DATA", false}, |
| 27 |
{"CON", false}, |
| 28 |
{"con", false}, |
| 29 |
{"CON.txt", false}, |
| 30 |
{"CON:ads", false}, |
| 31 |
{"CON ", false}, |
| 32 |
{"PRN", false}, |
| 33 |
{"AUX", false}, |
| 34 |
{"NUL", false}, |
| 35 |
{"COM1", false}, |
| 36 |
{"COM9", false}, |
| 37 |
{"LPT1", false}, |
| 38 |
{"LPT9", false}, |
| 39 |
{"CONIN$", false}, |
| 40 |
{"CONOUT$", false}, |
| 41 |
{"a", true}, |
| 42 |
{"a\\b", true}, |
| 43 |
{"a/b", true}, |
| 44 |
{".gitm", true}, |
| 45 |
{"CONNECT", true}, |
| 46 |
{"comic", true}, |
| 47 |
{"COM", true}, |
| 48 |
{"COM0", true}, |
| 49 |
{"LPT0", true}, |
| 50 |
// Bare ".git" / "git~1" stay valid here; the caller decides |
| 51 |
// whether they are permissible at the current path position. |
| 52 |
{".git", true}, |
| 53 |
{"git~1", true}, |
| 54 |
} |
| 55 |
|
| 56 |
for _, tc := range tests { |
| 57 |
t.Run(tc.path, func(t *testing.T) { |
| 58 |
t.Parallel() |
| 59 |
got := WindowsValidPath(tc.path) |
| 60 |
assert.Equal(t, tc.want, got) |
| 61 |
}) |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
func TestIsNTFSDotGit(t *testing.T) { |
| 66 |
t.Parallel() |
| 67 |
|
| 68 |
tests := []struct { |
| 69 |
part string |
| 70 |
want bool |
| 71 |
}{ |
| 72 |
// Bare canonical names match (parity with upstream). |
| 73 |
{".git", true}, |
| 74 |
{".GIT", true}, |
| 75 |
{"git~1", true}, |
| 76 |
{"GIT~1", true}, |
| 77 |
// Trailing-space / period / ADS variants on .git. |
| 78 |
{".git ", true}, |
| 79 |
{".git.", true}, |
| 80 |
{".git . . .", true}, |
| 81 |
{".git::$INDEX_ALLOCATION", true}, |
| 82 |
{".git:foo", true}, |
| 83 |
// Same shapes on git~1. |
| 84 |
{"git~1 ", true}, |
| 85 |
{"git~1.", true}, |
| 86 |
{"git~1 . ", true}, |
| 87 |
{"git~1::$DATA", true}, |
| 88 |
{"GIT~1.", true}, |
| 89 |
// Negatives. |
| 90 |
{".gitignore", false}, |
| 91 |
{".gitmodules", false}, |
| 92 |
{"gitfoo", false}, |
| 93 |
{"git~2", false}, |
| 94 |
{"git~10", false}, |
| 95 |
{"git", false}, |
| 96 |
{"", false}, |
| 97 |
{".", false}, |
| 98 |
{"readme.md", false}, |
| 99 |
} |
| 100 |
|
| 101 |
for _, tc := range tests { |
| 102 |
t.Run(tc.part, func(t *testing.T) { |
| 103 |
t.Parallel() |
| 104 |
got := IsNTFSDotGit(tc.part) |
| 105 |
assert.Equal(t, tc.want, got, "IsNTFSDotGit(%q)", tc.part) |
| 106 |
}) |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
func TestIsNTFSDot(t *testing.T) { |
| 111 |
t.Parallel() |
| 112 |
|
| 113 |
tests := []struct { |
| 114 |
name string |
| 115 |
part string |
| 116 |
dotgit string |
| 117 |
shortnamePrefix string |
| 118 |
want bool |
| 119 |
}{ |
| 120 |
// .gitmodules direct match. |
| 121 |
{"plain .gitmodules", ".gitmodules", "gitmodules", "gi7eba", true}, |
| 122 |
{"uppercase .GITMODULES", ".GITMODULES", "gitmodules", "gi7eba", true}, |
| 123 |
{"mixed case .GitModules", ".GitModules", "gitmodules", "gi7eba", true}, |
| 124 |
// NTFS trailing-space / period / ADS variants on .gitmodules. |
| 125 |
{"trailing space", ".gitmodules ", "gitmodules", "gi7eba", true}, |
| 126 |
{"trailing dot", ".gitmodules.", "gitmodules", "gi7eba", true}, |
| 127 |
{"trailing space and dot", ".gitmodules .", "gitmodules", "gi7eba", true}, |
| 128 |
{"trailing many spaces", ".gitmodules ", "gitmodules", "gi7eba", true}, |
| 129 |
{"alternate data stream", ".gitmodules:foo", "gitmodules", "gi7eba", true}, |
| 130 |
// 8.3 short-name standard form: gitmod~[1-4]. |
| 131 |
{"short ~1", "gitmod~1", "gitmodules", "gi7eba", true}, |
| 132 |
{"short ~4", "gitmod~4", "gitmodules", "gi7eba", true}, |
| 133 |
{"short uppercase", "GITMOD~1", "gitmodules", "gi7eba", true}, |
| 134 |
{"short ~5 not valid", "gitmod~5", "gitmodules", "gi7eba", false}, |
| 135 |
{"short with ads", "gitmod~1:foo", "gitmodules", "gi7eba", true}, |
| 136 |
// Fall-back short-name keyed on shortnamePrefix. |
| 137 |
{"fallback gi7eba~1", "gi7eba~1", "gitmodules", "gi7eba", true}, |
| 138 |
{"fallback gi7eba~12345", "gi7eba~1", "gitmodules", "gi7eba", true}, |
| 139 |
{"fallback gi7ebaX missing tilde", "gi7ebaXY", "gitmodules", "gi7eba", false}, |
| 140 |
{"fallback gi7eba1 missing tilde", "gi7eba1X", "gitmodules", "gi7eba", false}, |
| 141 |
// Negatives. |
| 142 |
{"plain .gitmodulesfoo", ".gitmodulesfoo", "gitmodules", "gi7eba", false}, |
| 143 |
{"plain .gitignore is not gitmodules", ".gitignore", "gitmodules", "gi7eba", false}, |
| 144 |
{"plain readme.md", "readme.md", "gitmodules", "gi7eba", false}, |
| 145 |
{"empty string", "", "gitmodules", "gi7eba", false}, |
| 146 |
{"only dot", ".", "gitmodules", "gi7eba", false}, |
| 147 |
} |
| 148 |
|
| 149 |
for _, tc := range tests { |
| 150 |
t.Run(tc.name, func(t *testing.T) { |
| 151 |
t.Parallel() |
| 152 |
got := IsNTFSDot(tc.part, tc.dotgit, tc.shortnamePrefix) |
| 153 |
assert.Equal(t, tc.want, got, "IsNTFSDot(%q, %q, %q)", tc.part, tc.dotgit, tc.shortnamePrefix) |
| 154 |
}) |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
func TestIsNTFSDotGitmodules(t *testing.T) { |
| 159 |
t.Parallel() |
| 160 |
|
| 161 |
tests := []struct { |
| 162 |
part string |
| 163 |
want bool |
| 164 |
}{ |
| 165 |
{".gitmodules", true}, |
| 166 |
{".GITMODULES", true}, |
| 167 |
{".gitmodules ", true}, |
| 168 |
{".gitmodules.", true}, |
| 169 |
{".gitmodules .", true}, |
| 170 |
{".gitmodules:foo", true}, |
| 171 |
{"gitmod~1", true}, |
| 172 |
{"GITMOD~4", true}, |
| 173 |
{"gi7eba~1", true}, |
| 174 |
{".gitmodulesfoo", false}, |
| 175 |
{".gitignore", false}, |
| 176 |
{"readme.md", false}, |
| 177 |
{"", false}, |
| 178 |
} |
| 179 |
|
| 180 |
for _, tc := range tests { |
| 181 |
t.Run(tc.part, func(t *testing.T) { |
| 182 |
t.Parallel() |
| 183 |
got := IsNTFSDotGitmodules(tc.part) |
| 184 |
assert.Equal(t, tc.want, got, "IsNTFSDotGitmodules(%q)", tc.part) |
| 185 |
}) |
| 186 |
} |
| 187 |
} |