| 1 |
package gitconfig |
| 2 |
|
| 3 |
import ( |
| 4 |
"strings" |
| 5 |
"testing" |
| 6 |
|
| 7 |
"github.com/stretchr/testify/assert" |
| 8 |
"github.com/stretchr/testify/require" |
| 9 |
) |
| 10 |
|
| 11 |
func TestTrim(t *testing.T) { |
| 12 |
t.Parallel() |
| 13 |
|
| 14 |
for _, tc := range [][]string{ |
| 15 |
{" a ", "b ", "\tc\n"}, |
| 16 |
} { |
| 17 |
trim(tc) |
| 18 |
for _, e := range tc { |
| 19 |
assert.Equal(t, strings.TrimSpace(e), e) |
| 20 |
} |
| 21 |
} |
| 22 |
} |
| 23 |
|
| 24 |
func TestGlobMatch(t *testing.T) { |
| 25 |
t.Parallel() |
| 26 |
|
| 27 |
testCases := []struct { |
| 28 |
name string |
| 29 |
pattern string |
| 30 |
input string |
| 31 |
want bool |
| 32 |
wantErr bool |
| 33 |
}{ |
| 34 |
{ |
| 35 |
name: "single asterisk matches within component", |
| 36 |
pattern: "feat/*", |
| 37 |
input: "feat/test", |
| 38 |
want: true, |
| 39 |
}, |
| 40 |
{ |
| 41 |
name: "double asterisk matches across components", |
| 42 |
pattern: "feat/**", |
| 43 |
input: "feat/foo/bar/baz", |
| 44 |
want: true, |
| 45 |
}, |
| 46 |
{ |
| 47 |
name: "single asterisk no match", |
| 48 |
pattern: "feat/*", |
| 49 |
input: "feat/foo/bar", |
| 50 |
want: false, |
| 51 |
}, |
| 52 |
{ |
| 53 |
name: "question mark matches single character", |
| 54 |
pattern: "?.js", |
| 55 |
input: "a.js", |
| 56 |
want: true, |
| 57 |
}, |
| 58 |
{ |
| 59 |
name: "question mark multiple", |
| 60 |
pattern: "test_?_?.go", |
| 61 |
input: "test_a_b.go", |
| 62 |
want: true, |
| 63 |
}, |
| 64 |
{ |
| 65 |
name: "character class matching", |
| 66 |
pattern: "[ab].txt", |
| 67 |
input: "a.txt", |
| 68 |
want: true, |
| 69 |
}, |
| 70 |
{ |
| 71 |
name: "character range", |
| 72 |
pattern: "[a-z]*.txt", |
| 73 |
input: "names.txt", |
| 74 |
want: true, |
| 75 |
}, |
| 76 |
{ |
| 77 |
name: "no match", |
| 78 |
pattern: "*.md", |
| 79 |
input: "file.go", |
| 80 |
want: false, |
| 81 |
}, |
| 82 |
{ |
| 83 |
name: "exact match", |
| 84 |
pattern: "exact.txt", |
| 85 |
input: "exact.txt", |
| 86 |
want: true, |
| 87 |
}, |
| 88 |
{ |
| 89 |
name: "invalid pattern - bad range", |
| 90 |
pattern: "[z-a].txt", |
| 91 |
input: "a.txt", |
| 92 |
wantErr: true, |
| 93 |
}, |
| 94 |
{ |
| 95 |
name: "invalid pattern - bad bracket", |
| 96 |
pattern: "[.txt", |
| 97 |
input: "a.txt", |
| 98 |
wantErr: true, |
| 99 |
}, |
| 100 |
} |
| 101 |
|
| 102 |
for _, tc := range testCases { |
| 103 |
t.Run(tc.name, func(t *testing.T) { |
| 104 |
got, err := globMatch(tc.pattern, tc.input) |
| 105 |
if tc.wantErr { |
| 106 |
assert.Error(t, err) |
| 107 |
} else { |
| 108 |
require.NoError(t, err) |
| 109 |
assert.Equal(t, tc.want, got) |
| 110 |
} |
| 111 |
}) |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
func TestSplitKey(t *testing.T) { |
| 116 |
t.Parallel() |
| 117 |
|
| 118 |
for _, tc := range []struct { |
| 119 |
in string |
| 120 |
section string |
| 121 |
subsection string |
| 122 |
key string |
| 123 |
}{ |
| 124 |
{ |
| 125 |
in: "[email protected]:.pushinsteadof", |
| 126 |
section: "url", |
| 127 |
subsection: "[email protected]:", |
| 128 |
key: "pushinsteadof", |
| 129 |
}, |
| 130 |
{ |
| 131 |
in: "gc.auto", |
| 132 |
section: "gc", |
| 133 |
key: "auto", |
| 134 |
}, |
| 135 |
} { |
| 136 |
sec, sub, key := splitKey(tc.in) |
| 137 |
assert.Equal(t, tc.section, sec, sec) |
| 138 |
assert.Equal(t, tc.subsection, sub, sub) |
| 139 |
assert.Equal(t, tc.key, key, key) |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
// TestParseLineForComment tests the parseLineForComment function with various inputs. |
| 144 |
func TestParseLineForComment(t *testing.T) { |
| 145 |
testCases := []struct { |
| 146 |
name string |
| 147 |
input string |
| 148 |
wantContent string |
| 149 |
wantComment string |
| 150 |
}{ |
| 151 |
{ |
| 152 |
name: "Double quotes with hash comment", |
| 153 |
input: `"foo#bar#baz" # comment1`, |
| 154 |
wantContent: `foo#bar#baz`, |
| 155 |
wantComment: `comment1`, |
| 156 |
}, |
| 157 |
{ |
| 158 |
name: "Single quotes with semicolon comment - single quotes are not valid", |
| 159 |
input: `'foo;bar' ; comment2`, |
| 160 |
wantContent: `'foo`, |
| 161 |
wantComment: `bar' ; comment2`, |
| 162 |
}, |
| 163 |
{ |
| 164 |
name: "No quotes with hash comment", |
| 165 |
input: `no quotes here # comment3`, |
| 166 |
wantContent: `no quotes here`, |
| 167 |
wantComment: `comment3`, |
| 168 |
}, |
| 169 |
{ |
| 170 |
name: "Nested single quotes with hash comment", |
| 171 |
input: `"nested 'quotes' # works" # comment4`, |
| 172 |
wantContent: `nested 'quotes' # works`, |
| 173 |
wantComment: `comment4`, |
| 174 |
}, |
| 175 |
{ |
| 176 |
name: "No comment present", |
| 177 |
input: `no comment here`, |
| 178 |
wantContent: `no comment here`, |
| 179 |
wantComment: ``, |
| 180 |
}, |
| 181 |
{ |
| 182 |
name: "Leading space content with semicolon comment", |
| 183 |
input: ` "leading space content" ; comment6`, |
| 184 |
wantContent: `leading space content`, |
| 185 |
wantComment: `comment6`, |
| 186 |
}, |
| 187 |
{ |
| 188 |
name: "Trailing space content and comment with hash", |
| 189 |
input: `trailing space content # comment7 `, |
| 190 |
wantContent: `trailing space content`, |
| 191 |
wantComment: `comment7`, |
| 192 |
}, |
| 193 |
{ |
| 194 |
name: "Hash comment line", |
| 195 |
input: `# comment line`, |
| 196 |
wantContent: ``, |
| 197 |
wantComment: `comment line`, |
| 198 |
}, |
| 199 |
{ |
| 200 |
name: "Semicolon comment line", |
| 201 |
input: `; another comment line`, |
| 202 |
wantContent: ``, |
| 203 |
wantComment: `another comment line`, |
| 204 |
}, |
| 205 |
{ |
| 206 |
name: "Quoted content spanning potential comment char", |
| 207 |
input: ` "quotes spanning ; comment char" `, |
| 208 |
wantContent: `quotes spanning ; comment char`, |
| 209 |
wantComment: ``, |
| 210 |
}, |
| 211 |
{ |
| 212 |
name: "Unterminated quote before hash comment", |
| 213 |
input: ` "unterminated ' quote # comment"`, |
| 214 |
wantContent: `unterminated ' quote # comment`, |
| 215 |
wantComment: ``, |
| 216 |
}, |
| 217 |
{ |
| 218 |
name: "Hash inside quotes with comment outside", |
| 219 |
input: ` "hash # inside" # comment outside `, |
| 220 |
wantContent: `hash # inside`, |
| 221 |
wantComment: `comment outside`, |
| 222 |
}, |
| 223 |
{ |
| 224 |
name: "Hash inside quotes part of string", |
| 225 |
input: ` string with #"# hash inside quotes`, |
| 226 |
wantContent: `string with`, |
| 227 |
wantComment: `"# hash inside quotes`, |
| 228 |
}, |
| 229 |
{ |
| 230 |
name: "Empty input string", |
| 231 |
input: ``, |
| 232 |
wantContent: ``, |
| 233 |
wantComment: ``, |
| 234 |
}, |
| 235 |
{ |
| 236 |
name: "Whitespace only input string", |
| 237 |
input: ` `, |
| 238 |
wantContent: ``, |
| 239 |
wantComment: ``, |
| 240 |
}, |
| 241 |
{ |
| 242 |
name: "Key value pair like structure", |
| 243 |
input: `key = value # comment`, |
| 244 |
wantContent: `key = value`, |
| 245 |
wantComment: `comment`, |
| 246 |
}, |
| 247 |
{ |
| 248 |
name: "Only double quoted content", |
| 249 |
input: `"only quotes"`, |
| 250 |
wantContent: `only quotes`, |
| 251 |
wantComment: ``, |
| 252 |
}, |
| 253 |
{ |
| 254 |
name: "Only single quoted content", |
| 255 |
input: `'single quotes'`, |
| 256 |
wantContent: `'single quotes'`, |
| 257 |
wantComment: ``, |
| 258 |
}, |
| 259 |
{ |
| 260 |
name: "Mismatched surrounding quotes 1", |
| 261 |
input: ` " mismatched quote'`, |
| 262 |
wantContent: ` mismatched quote'`, |
| 263 |
wantComment: ``, |
| 264 |
}, |
| 265 |
{ |
| 266 |
name: "Mismatched surrounding quotes 2", |
| 267 |
input: ` 'mismatched quote"`, |
| 268 |
wantContent: `'mismatched quote"`, |
| 269 |
wantComment: ``, |
| 270 |
}, |
| 271 |
{ |
| 272 |
name: "Single quote only content", |
| 273 |
input: ` '`, |
| 274 |
wantContent: `'`, |
| 275 |
wantComment: ``, |
| 276 |
}, |
| 277 |
{ |
| 278 |
name: "Double quote only content", |
| 279 |
input: `"`, |
| 280 |
wantContent: ``, |
| 281 |
wantComment: ``, |
| 282 |
}, |
| 283 |
{ |
| 284 |
name: "Empty double quotes", |
| 285 |
input: `""`, |
| 286 |
wantContent: ``, |
| 287 |
wantComment: ``, |
| 288 |
}, |
| 289 |
{ |
| 290 |
name: "Empty single quotes", |
| 291 |
input: `''`, |
| 292 |
wantContent: `''`, |
| 293 |
wantComment: ``, |
| 294 |
}, |
| 295 |
{ |
| 296 |
name: "Content followed immediately by hash", |
| 297 |
input: `content#`, |
| 298 |
wantContent: `content`, |
| 299 |
wantComment: ``, |
| 300 |
}, |
| 301 |
{ |
| 302 |
name: "Content followed immediately by semicolon", |
| 303 |
input: `content;`, |
| 304 |
wantContent: `content`, |
| 305 |
wantComment: ``, |
| 306 |
}, |
| 307 |
{ |
| 308 |
name: "Content followed by delimiter and spaces", |
| 309 |
input: `content # `, |
| 310 |
wantContent: `content`, |
| 311 |
wantComment: ``, |
| 312 |
}, |
| 313 |
} |
| 314 |
|
| 315 |
// Iterate over the test cases |
| 316 |
for _, tc := range testCases { |
| 317 |
t.Run(tc.name, func(t *testing.T) { |
| 318 |
gotContent, gotComment := parseLineForComment(tc.input) |
| 319 |
|
| 320 |
if gotContent != tc.wantContent { |
| 321 |
t.Errorf("parseLineForComment(%q) got content %q, want %q", tc.input, gotContent, tc.wantContent) |
| 322 |
} |
| 323 |
|
| 324 |
if gotComment != tc.wantComment { |
| 325 |
t.Errorf("parseLineForComment(%q) got comment %q, want %q", tc.input, gotComment, tc.wantComment) |
| 326 |
} |
| 327 |
}) |
| 328 |
} |
| 329 |
} |
| 330 |
|
| 331 |
func TestCanonicalizeKey(t *testing.T) { |
| 332 |
t.Parallel() |
| 333 |
|
| 334 |
testCases := []struct { |
| 335 |
name string |
| 336 |
input string |
| 337 |
expected string |
| 338 |
}{ |
| 339 |
{ |
| 340 |
name: "Simple key", |
| 341 |
input: "core.autocrlf", |
| 342 |
expected: "core.autocrlf", |
| 343 |
}, |
| 344 |
{ |
| 345 |
name: "Key with subsection", |
| 346 |
input: "remote.origin.url", |
| 347 |
expected: "remote.origin.url", |
| 348 |
}, |
| 349 |
{ |
| 350 |
name: "Key with mixed case section and key", |
| 351 |
input: "Core.AutoCRLF", |
| 352 |
expected: "core.autocrlf", |
| 353 |
}, |
| 354 |
{ |
| 355 |
name: "Key with mixed case section, subsection, and key", |
| 356 |
input: "Remote.Origin.URL", |
| 357 |
expected: "remote.Origin.url", |
| 358 |
}, |
| 359 |
{ |
| 360 |
name: "Key with subsection containing dots", |
| 361 |
input: "[email protected]:.pushinsteadof", |
| 362 |
expected: "[email protected]:.pushinsteadof", |
| 363 |
}, |
| 364 |
{ |
| 365 |
name: "Key with mixed case and subsection containing dots", |
| 366 |
input: "[email protected]:.PushInsteadOf", |
| 367 |
expected: "[email protected]:.pushinsteadof", |
| 368 |
}, |
| 369 |
{ |
| 370 |
name: "Empty input - invalid", |
| 371 |
input: "", |
| 372 |
expected: "", |
| 373 |
}, |
| 374 |
{ |
| 375 |
name: "Single part input - invalid", |
| 376 |
input: "section", |
| 377 |
expected: "", |
| 378 |
}, |
| 379 |
{ |
| 380 |
name: "Key starting with dot - invalid", |
| 381 |
input: ".key", |
| 382 |
expected: "", |
| 383 |
}, |
| 384 |
{ |
| 385 |
name: "Key ending with dot - invalid", |
| 386 |
input: "section.", |
| 387 |
expected: "", |
| 388 |
}, |
| 389 |
{ |
| 390 |
name: "Key with multiple dots in subsection", |
| 391 |
input: "section.sub.section.key", |
| 392 |
expected: "section.sub.section.key", |
| 393 |
}, |
| 394 |
{ |
| 395 |
name: "Key with uppercase subsection", |
| 396 |
input: "section.SUBSECTION.key", |
| 397 |
expected: "section.SUBSECTION.key", |
| 398 |
}, |
| 399 |
} |
| 400 |
|
| 401 |
for _, tc := range testCases { |
| 402 |
t.Run(tc.name, func(t *testing.T) { |
| 403 |
t.Parallel() |
| 404 |
actual := canonicalizeKey(tc.input) |
| 405 |
assert.Equal(t, tc.expected, actual) |
| 406 |
}) |
| 407 |
} |
| 408 |
} |