-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgitclone_test.zig
More file actions
776 lines (623 loc) · 25.5 KB
/
Copy pathgitclone_test.zig
File metadata and controls
776 lines (623 loc) · 25.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
const std = @import("std");
const testing = std.testing;
const mem = std.mem;
const gitclone = @import("gitclone.zig");
// Alias for easier access
const parseGitUrl = gitclone.parseGitUrl;
const parsePathComponent = gitclone.parsePathComponent;
const parseGitProgress = gitclone.parseGitProgress;
const parseGitPercentage = gitclone.parseGitPercentage;
const destinationExists = gitclone.destinationExists;
const GitUrl = gitclone.GitUrl;
const ProgressBar = gitclone.ProgressBar;
const GitProgressInfo = gitclone.GitProgressInfo;
// ============================================================================
// URL PARSING TESTS - Valid Cases
// ============================================================================
test "parse SSH URL with .git suffix" {
var arena = std.heap.ArenaAllocator.init(testing.allocator);
defer arena.deinit();
const allocator = arena.allocator();
const url = "git@github.com:torvalds/linux.git";
const result = try parseGitUrl(allocator, url);
try testing.expectEqualStrings("torvalds", result.org);
try testing.expectEqualStrings("linux", result.repo);
}
test "parse SSH URL without .git suffix" {
var arena = std.heap.ArenaAllocator.init(testing.allocator);
defer arena.deinit();
const allocator = arena.allocator();
const url = "git@github.com:drewr/zigutils";
const result = try parseGitUrl(allocator, url);
try testing.expectEqualStrings("drewr", result.org);
try testing.expectEqualStrings("zigutils", result.repo);
}
test "parse HTTPS URL with .git suffix" {
var arena = std.heap.ArenaAllocator.init(testing.allocator);
defer arena.deinit();
const allocator = arena.allocator();
const url = "https://github.com:443/microsoft/vscode.git";
const result = try parseGitUrl(allocator, url);
try testing.expectEqualStrings("microsoft", result.org);
try testing.expectEqualStrings("vscode", result.repo);
}
test "parse HTTPS URL without .git suffix" {
var arena = std.heap.ArenaAllocator.init(testing.allocator);
defer arena.deinit();
const allocator = arena.allocator();
const url = "https://github.com/golang/go";
const result = try parseGitUrl(allocator, url);
try testing.expectEqualStrings("golang", result.org);
try testing.expectEqualStrings("go", result.repo);
}
test "parse HTTP URL with .git suffix" {
var arena = std.heap.ArenaAllocator.init(testing.allocator);
defer arena.deinit();
const allocator = arena.allocator();
const url = "http://github.com/rust-lang/rust.git";
const result = try parseGitUrl(allocator, url);
try testing.expectEqualStrings("rust-lang", result.org);
try testing.expectEqualStrings("rust", result.repo);
}
test "parse SSH URL with complex org/repo names" {
var arena = std.heap.ArenaAllocator.init(testing.allocator);
defer arena.deinit();
const allocator = arena.allocator();
const url = "git@github.com:org-name/repo-with-dashes.git";
const result = try parseGitUrl(allocator, url);
try testing.expectEqualStrings("org-name", result.org);
try testing.expectEqualStrings("repo-with-dashes", result.repo);
}
test "parse HTTPS URL with different domain" {
var arena = std.heap.ArenaAllocator.init(testing.allocator);
defer arena.deinit();
const allocator = arena.allocator();
const url = "https://gitlab.com/group/project.git";
const result = try parseGitUrl(allocator, url);
try testing.expectEqualStrings("group", result.org);
try testing.expectEqualStrings("project", result.repo);
}
test "parse SSH protocol URL (ssh://) with Codeberg" {
var arena = std.heap.ArenaAllocator.init(testing.allocator);
defer arena.deinit();
const allocator = arena.allocator();
const url = "ssh://git@codeberg.org/ziglang/zig.git";
const result = try parseGitUrl(allocator, url);
try testing.expectEqualStrings("ziglang", result.org);
try testing.expectEqualStrings("zig", result.repo);
}
test "parse HTTPS URL with Codeberg" {
var arena = std.heap.ArenaAllocator.init(testing.allocator);
defer arena.deinit();
const allocator = arena.allocator();
const url = "https://codeberg.org/ziglang/zig.git";
const result = try parseGitUrl(allocator, url);
try testing.expectEqualStrings("ziglang", result.org);
try testing.expectEqualStrings("zig", result.repo);
}
test "parse SSH protocol URL (ssh://) without .git suffix" {
var arena = std.heap.ArenaAllocator.init(testing.allocator);
defer arena.deinit();
const allocator = arena.allocator();
const url = "ssh://git@github.com/microsoft/vscode";
const result = try parseGitUrl(allocator, url);
try testing.expectEqualStrings("microsoft", result.org);
try testing.expectEqualStrings("vscode", result.repo);
}
// ============================================================================
// URL PARSING TESTS - Invalid Cases (Generative)
// ============================================================================
test "reject URL without @ or ://" {
var arena = std.heap.ArenaAllocator.init(testing.allocator);
defer arena.deinit();
const allocator = arena.allocator();
const invalid_urls = [_][]const u8{
"github.com:org/repo",
"local/path/to/repo",
"just-a-name",
"C:\\Windows\\Path",
"/absolute/path",
};
for (invalid_urls) |url| {
const result = parseGitUrl(allocator, url);
try testing.expectError(error.InvalidUrl, result);
}
}
test "reject SSH URLs missing colon separator" {
var arena = std.heap.ArenaAllocator.init(testing.allocator);
defer arena.deinit();
const allocator = arena.allocator();
const invalid_urls = [_][]const u8{
"git@github.com/org/repo",
"git@github.comorg/repo",
"git@/org/repo",
"git@",
};
for (invalid_urls) |url| {
const result = parseGitUrl(allocator, url);
try testing.expectError(error.InvalidUrl, result);
}
}
test "reject SSH URLs missing org/repo separator" {
var arena = std.heap.ArenaAllocator.init(testing.allocator);
defer arena.deinit();
const allocator = arena.allocator();
const invalid_urls = [_][]const u8{
"git@github.com:onlyrepo",
"git@github.com:repo.git",
"git@github.com:",
};
for (invalid_urls) |url| {
const result = parseGitUrl(allocator, url);
try testing.expectError(error.InvalidUrl, result);
}
}
test "reject HTTPS URLs without path" {
var arena = std.heap.ArenaAllocator.init(testing.allocator);
defer arena.deinit();
const allocator = arena.allocator();
const invalid_urls = [_][]const u8{
"https://github.com",
"https://github.com/",
"https://",
"https://github.com/org",
};
for (invalid_urls) |url| {
const result = parseGitUrl(allocator, url);
try testing.expectError(error.InvalidUrl, result);
}
}
test "reject HTTP URLs missing org/repo separator" {
var arena = std.heap.ArenaAllocator.init(testing.allocator);
defer arena.deinit();
const allocator = arena.allocator();
const invalid_urls = [_][]const u8{
"http://github.com/onlyrepo",
"http://github.com/repo.git",
"http://github.com/",
};
for (invalid_urls) |url| {
const result = parseGitUrl(allocator, url);
try testing.expectError(error.InvalidUrl, result);
}
}
test "reject malformed protocol URLs" {
var arena = std.heap.ArenaAllocator.init(testing.allocator);
defer arena.deinit();
const allocator = arena.allocator();
const invalid_urls = [_][]const u8{
"htp://github.com/org/repo",
"ftp://github.com/org/repo",
"git://github.com/org/repo",
"github.com://org/repo",
"http/github.com/org/repo",
"ssh://git@github.com:org/repo.git", // ssh:// with SCP-style colon path
};
for (invalid_urls) |url| {
const result = parseGitUrl(allocator, url);
try testing.expectError(error.InvalidUrl, result);
}
}
test "reject empty or special character URLs" {
var arena = std.heap.ArenaAllocator.init(testing.allocator);
defer arena.deinit();
const allocator = arena.allocator();
const invalid_urls = [_][]const u8{
"",
" ",
"\n",
"\t",
};
for (invalid_urls) |url| {
const result = parseGitUrl(allocator, url);
try testing.expectError(error.InvalidUrl, result);
}
}
test "reject URLs with multiple slashes in path" {
var arena = std.heap.ArenaAllocator.init(testing.allocator);
defer arena.deinit();
const allocator = arena.allocator();
const invalid_urls = [_][]const u8{
"https://github.com/org/repo/extra",
"git@github.com:org/repo/extra.git",
"https://github.com/a/b/c/d",
};
for (invalid_urls) |url| {
const result = parseGitUrl(allocator, url);
try testing.expectError(error.InvalidUrl, result);
}
}
// ============================================================================
// PATH COMPONENT PARSING TESTS
// ============================================================================
test "parsePathComponent handles repo with .git" {
var arena = std.heap.ArenaAllocator.init(testing.allocator);
defer arena.deinit();
const allocator = arena.allocator();
const path = "org/repo.git";
const result = try parsePathComponent(allocator, path);
try testing.expectEqualStrings("org", result.org);
try testing.expectEqualStrings("repo", result.repo);
}
test "parsePathComponent handles repo without .git" {
var arena = std.heap.ArenaAllocator.init(testing.allocator);
defer arena.deinit();
const allocator = arena.allocator();
const path = "org/repo";
const result = try parsePathComponent(allocator, path);
try testing.expectEqualStrings("org", result.org);
try testing.expectEqualStrings("repo", result.repo);
}
test "parsePathComponent with hyphenated names" {
var arena = std.heap.ArenaAllocator.init(testing.allocator);
defer arena.deinit();
const allocator = arena.allocator();
const path = "org-name/repo-name.git";
const result = try parsePathComponent(allocator, path);
try testing.expectEqualStrings("org-name", result.org);
try testing.expectEqualStrings("repo-name", result.repo);
}
test "parsePathComponent with numbers" {
var arena = std.heap.ArenaAllocator.init(testing.allocator);
defer arena.deinit();
const allocator = arena.allocator();
const path = "org123/repo456.git";
const result = try parsePathComponent(allocator, path);
try testing.expectEqualStrings("org123", result.org);
try testing.expectEqualStrings("repo456", result.repo);
}
test "parsePathComponent rejects path without slash" {
var arena = std.heap.ArenaAllocator.init(testing.allocator);
defer arena.deinit();
const allocator = arena.allocator();
const invalid_paths = [_][]const u8{
"onlyrepo",
"repo.git",
"",
};
for (invalid_paths) |path| {
const result = parsePathComponent(allocator, path);
try testing.expectError(error.InvalidUrl, result);
}
}
test "parsePathComponent removes multiple .git occurrences correctly" {
var arena = std.heap.ArenaAllocator.init(testing.allocator);
defer arena.deinit();
const allocator = arena.allocator();
const path = "org/repo.git.git";
const result = try parsePathComponent(allocator, path);
try testing.expectEqualStrings("org", result.org);
try testing.expectEqualStrings("repo.git", result.repo);
}
// ============================================================================
// PROGRESS PARSING TESTS
// ============================================================================
test "parseGitPercentage parses standard format" {
const line = "Counting objects: 100% (123/456)";
const result = parseGitPercentage(line);
try testing.expect(result != null);
try testing.expectEqual(@as(usize, 123), result.?.current);
try testing.expectEqual(@as(usize, 456), result.?.total);
}
test "parseGitPercentage parses format with extra bytes info" {
const line = "Receiving objects: 75% (750/1000, 5.2 MiB)";
const result = parseGitPercentage(line);
try testing.expect(result != null);
try testing.expectEqual(@as(usize, 750), result.?.current);
try testing.expectEqual(@as(usize, 1000), result.?.total);
}
test "parseGitPercentage parses format with spaces" {
const line = "Resolving deltas: 100% ( 456 / 456 )";
const result = parseGitPercentage(line);
try testing.expect(result != null);
try testing.expectEqual(@as(usize, 456), result.?.current);
try testing.expectEqual(@as(usize, 456), result.?.total);
}
test "parseGitPercentage handles various object counts" {
const test_cases = [_]struct {
line: []const u8,
expected_current: usize,
expected_total: usize,
}{
.{ .line = "Counting: (1/1)", .expected_current = 1, .expected_total = 1 },
.{ .line = "Compressing: (0/100)", .expected_current = 0, .expected_total = 100 },
.{ .line = "Receiving: (9999/10000)", .expected_current = 9999, .expected_total = 10000 },
.{ .line = "Resolving: (1000000/1000000)", .expected_current = 1000000, .expected_total = 1000000 },
};
for (test_cases) |tc| {
const result = parseGitPercentage(tc.line);
try testing.expect(result != null);
try testing.expectEqual(tc.expected_current, result.?.current);
try testing.expectEqual(tc.expected_total, result.?.total);
}
}
test "parseGitPercentage rejects malformed input" {
const invalid_lines = [_][]const u8{
"No parentheses here",
"Only (one number)",
"()",
"(abc/def)",
"(100/)",
"(/100)",
"Missing close (100/200",
"Missing open 100/200)",
"",
};
for (invalid_lines) |line| {
const result = parseGitPercentage(line);
try testing.expect(result == null);
}
}
test "parseGitProgress updates phase correctly" {
var progress = ProgressBar{};
parseGitProgress("Counting objects: 100% (123/456)", &progress);
try testing.expectEqualStrings("Counting ", progress.phase);
try testing.expectEqual(@as(usize, 123), progress.current);
parseGitProgress("Compressing objects: 50% (50/100)", &progress);
try testing.expectEqualStrings("Compressing", progress.phase);
try testing.expectEqual(@as(usize, 50), progress.current);
parseGitProgress("Receiving objects: 75% (750/1000)", &progress);
try testing.expectEqualStrings("Receiving ", progress.phase);
try testing.expectEqual(@as(usize, 750), progress.current);
parseGitProgress("Resolving deltas: 100% (456/456)", &progress);
try testing.expectEqualStrings("Resolving ", progress.phase);
try testing.expectEqual(@as(usize, 456), progress.current);
}
test "parseGitProgress ignores unrecognized lines" {
var progress = ProgressBar{};
const initial_phase = progress.phase;
const initial_current = progress.current;
parseGitProgress("Unknown line that doesn't match patterns", &progress);
try testing.expectEqualStrings(initial_phase, progress.phase);
try testing.expectEqual(initial_current, progress.current);
}
test "parseGitProgress handles incremental updates" {
var progress = ProgressBar{};
// Simulate a cloning session with multiple updates
parseGitProgress("Counting objects: 10% (10/100)", &progress);
try testing.expectEqual(@as(usize, 10), progress.current);
try testing.expectEqual(@as(usize, 100), progress.total);
parseGitProgress("Counting objects: 50% (50/100)", &progress);
try testing.expectEqual(@as(usize, 50), progress.current);
parseGitProgress("Counting objects: 100% (100/100)", &progress);
try testing.expectEqual(@as(usize, 100), progress.current);
parseGitProgress("Compressing objects: 30% (30/100)", &progress);
try testing.expectEqualStrings("Compressing", progress.phase);
try testing.expectEqual(@as(usize, 30), progress.current);
}
// ============================================================================
// PROGRESS BAR STRUCT TESTS
// ============================================================================
test "ProgressBar calculates percentage correctly" {
const progress = ProgressBar{ .current = 50, .total = 100 };
const percent = if (progress.total > 0)
@min(100, (progress.current * 100) / progress.total)
else
0;
try testing.expectEqual(@as(usize, 50), percent);
}
test "ProgressBar handles zero total" {
const progress = ProgressBar{ .current = 10, .total = 0 };
const percent = if (progress.total > 0)
@min(100, (progress.current * 100) / progress.total)
else
0;
try testing.expectEqual(@as(usize, 0), percent);
}
test "ProgressBar calculates filled blocks correctly" {
const progress = ProgressBar{
.current = 50,
.total = 100,
.width = 40,
};
const percent = if (progress.total > 0)
@min(100, (progress.current * 100) / progress.total)
else
0;
const filled = (progress.width * percent) / 100;
try testing.expectEqual(@as(usize, 20), filled);
}
test "ProgressBar handles 100 percent" {
const progress = ProgressBar{
.current = 100,
.total = 100,
.width = 40,
};
const percent = if (progress.total > 0)
@min(100, (progress.current * 100) / progress.total)
else
0;
const filled = (progress.width * percent) / 100;
try testing.expectEqual(@as(usize, 40), filled);
}
test "ProgressBar handles edge case where current exceeds total" {
const progress = ProgressBar{
.current = 150,
.total = 100,
.width = 40,
};
const percent = if (progress.total > 0)
@min(100, (progress.current * 100) / progress.total)
else
0;
try testing.expectEqual(@as(usize, 100), percent);
}
// ============================================================================
// INTEGRATION TESTS - Simulating real scenarios
// ============================================================================
test "full SSH URL parsing pipeline" {
var arena = std.heap.ArenaAllocator.init(testing.allocator);
defer arena.deinit();
const allocator = arena.allocator();
const url = "git@gitlab.com:kubernetes/kubernetes.git";
const result = try parseGitUrl(allocator, url);
try testing.expectEqualStrings("kubernetes", result.org);
try testing.expectEqualStrings("kubernetes", result.repo);
}
test "full HTTPS URL parsing pipeline" {
var arena = std.heap.ArenaAllocator.init(testing.allocator);
defer arena.deinit();
const allocator = arena.allocator();
const url = "https://github.com/pytorch/pytorch.git";
const result = try parseGitUrl(allocator, url);
try testing.expectEqualStrings("pytorch", result.org);
try testing.expectEqualStrings("pytorch", result.repo);
}
test "progress parsing with realistic git output" {
var progress = ProgressBar{};
// Simulate realistic git clone output
const git_lines = [_][]const u8{
"Cloning into 'repo'...",
"Counting objects: 10% (100/1000)\r",
"Counting objects: 50% (500/1000)\r",
"Counting objects: 100% (1000/1000)\r",
"Compressing objects: 20% (200/1000)\r",
"Compressing objects: 100% (1000/1000)\r",
"Receiving objects: 30% (300/1000, 2.5 MiB)\r",
"Receiving objects: 70% (700/1000, 5.2 MiB)\r",
"Receiving objects: 100% (1000/1000, 8.3 MiB)\r",
"Resolving deltas: 50% (500/1000)\r",
"Resolving deltas: 100% (1000/1000)\r",
};
for (git_lines) |line| {
parseGitProgress(line, &progress);
}
try testing.expectEqualStrings("Resolving ", progress.phase);
try testing.expectEqual(@as(usize, 1000), progress.current);
try testing.expectEqual(@as(usize, 1000), progress.total);
}
test "destinationExists detects existing path" {
var tmp = testing.tmpDir(.{});
defer tmp.cleanup();
const tmp_abs = try std.Io.Dir.realPathFileAlloc(tmp.dir, testing.io, ".", testing.allocator);
defer testing.allocator.free(tmp_abs);
const existing = try std.fs.path.join(testing.allocator, &[_][]const u8{ tmp_abs, "already-there" });
defer testing.allocator.free(existing);
try std.Io.Dir.createDirPath(tmp.dir, testing.io, "already-there");
try testing.expectEqual(true, try destinationExists(testing.io, existing));
}
test "destinationExists detects missing path" {
var tmp = testing.tmpDir(.{});
defer tmp.cleanup();
const tmp_abs = try std.Io.Dir.realPathFileAlloc(tmp.dir, testing.io, ".", testing.allocator);
defer testing.allocator.free(tmp_abs);
const missing = try std.fs.path.join(testing.allocator, &[_][]const u8{ tmp_abs, "does-not-exist" });
defer testing.allocator.free(missing);
try testing.expectEqual(false, try destinationExists(testing.io, missing));
}
// ============================================================================
// PROPERTY-BASED TESTS - Generative invalid input testing
// ============================================================================
test "reject all variations of SSH URLs with missing components" {
var arena = std.heap.ArenaAllocator.init(testing.allocator);
defer arena.deinit();
const allocator = arena.allocator();
// Test various malformations of SSH URLs
const bad_ssh_urls = [_][]const u8{
"git@github.com", // Missing colon and path
"git@github.com:", // Missing path
"git@github.com:org", // Missing repo separator
"@github.com:org/repo.git", // Missing user
"git@", // Incomplete
};
for (bad_ssh_urls) |url| {
const result = parseGitUrl(allocator, url);
try testing.expectError(error.InvalidUrl, result);
}
}
test "reject all variations of HTTPS URLs with missing components" {
var arena = std.heap.ArenaAllocator.init(testing.allocator);
defer arena.deinit();
const allocator = arena.allocator();
const bad_https_urls = [_][]const u8{
"https://", // Only protocol
"https://github.com", // Missing path
"https://github.com/", // Missing org/repo
"https://github.com/org", // Missing repo
"https:///org/repo.git", // Missing host
};
for (bad_https_urls) |url| {
const result = parseGitUrl(allocator, url);
try testing.expectError(error.InvalidUrl, result);
}
}
test "reject HTTP URLs with protocol variations" {
var arena = std.heap.ArenaAllocator.init(testing.allocator);
defer arena.deinit();
const allocator = arena.allocator();
const bad_http_urls = [_][]const u8{
"http://", // Only protocol
"http://github.com", // Missing path
"http://github.com/", // Missing org/repo
"http:///org/repo.git", // Missing host
};
for (bad_http_urls) |url| {
const result = parseGitUrl(allocator, url);
try testing.expectError(error.InvalidUrl, result);
}
}
test "reject SSH protocol URLs (ssh://) with missing components" {
var arena = std.heap.ArenaAllocator.init(testing.allocator);
defer arena.deinit();
const allocator = arena.allocator();
const bad_ssh_proto_urls = [_][]const u8{
"ssh://", // Only protocol
"ssh://github.com", // Missing path
"ssh://github.com/", // Missing org/repo
"ssh:///org/repo.git", // Missing host
"ssh://github.com/onlyrepo", // Missing org/repo separator
};
for (bad_ssh_proto_urls) |url| {
const result = parseGitUrl(allocator, url);
try testing.expectError(error.InvalidUrl, result);
}
}
test "reject URLs with special characters in org/repo names" {
var arena = std.heap.ArenaAllocator.init(testing.allocator);
defer arena.deinit();
const allocator = arena.allocator();
// These should parse successfully but we test that special chars are preserved
const special_char_urls = [_][]const u8{
"git@github.com:org_name/repo_name.git",
"git@github.com:org.name/repo.name.git",
"https://github.com/org-name/repo-name.git",
};
for (special_char_urls) |url| {
const result = parseGitUrl(allocator, url);
try testing.expect(result != error.InvalidUrl);
}
}
test "reject parseGitPercentage with invalid number formats" {
const invalid_percentage_lines = [_][]const u8{
"Objects: (abc/def)",
"Objects: (99/xyz)",
"Objects: (--5/100)",
"Objects: (1.5/100)",
"Objects: (1e10/100)",
"Objects: (+100/-50)",
};
for (invalid_percentage_lines) |line| {
const result = parseGitPercentage(line);
try testing.expect(result == null);
}
}
test "stress test parseGitPercentage with boundary numbers" {
const boundary_cases = [_]struct {
line: []const u8,
expect_success: bool,
}{
.{ .line = "Objects: (0/1)", .expect_success = true },
.{ .line = "Objects: (1/1)", .expect_success = true },
.{ .line = "Objects: (4294967295/4294967295)", .expect_success = true }, // Near u32 max
.{ .line = "Objects: (-1/100)", .expect_success = false },
.{ .line = "Objects: (100/-1)", .expect_success = false },
};
for (boundary_cases) |tc| {
const result = parseGitPercentage(tc.line);
if (tc.expect_success) {
try testing.expect(result != null);
} else {
try testing.expect(result == null);
}
}
}