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
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
//! Fast CODEOWNERS pattern matching with shape-based specialization.
//!
//! Every CODEOWNERS pattern is classified at compile time into one of a
//! handful of shapes, and each shape gets its own matcher. Typical monorepo
//! CODEOWNERS files are ~80% `*.ext` extension patterns and directory
//! prefixes — those get raw `ends_with` / `starts_with` on bytes, no glob
//! engine involvement.
//!
//! For the minority of truly globby patterns (`src/**/*.rs`), we fall through
//! to [`fast_glob`](https://crates.io/crates/fast-glob).
/// A CODEOWNERS pattern, pre-processed for repeated fast matching.
///
/// Use [`CompiledPattern::new`] to compile, then [`CompiledPattern::matches`]
/// to match paths. Construction is O(pattern length); matching is close to
/// O(path length) for all variants except the glob ones.
pub enum CompiledPattern {
/// Matches everything (unanchored `*` or `**`).
MatchAll,
/// Anchored `/*` — matches only root-level files (no slashes in path).
RootFilesOnly,
/// Extension suffix like `.rs` (from `*.rs`) — simple `ends_with` check.
///
/// Stored as the literal suffix including the leading dot. Matcher also
/// verifies there is a `/` immediately before the suffix to avoid false
/// positives like `notrs` matching `.rs`.
ExtensionSuffix(String),
/// Single-segment glob like `*.rs` that fell out of the extension
/// fast-path — matched via `fast_glob` with a prepended `**/` so it
/// triggers in any directory.
SingleSegmentGlob(String),
/// Multi-segment glob like `src/**/*.rs` — matched directly via `fast_glob`.
MultiSegmentGlob(String),
/// Anchored directory pattern like `/src/` — matches a prefix at the root only.
AnchoredDirectory(String),
/// Unanchored directory pattern like `docs/` — matches anywhere in the tree.
UnanchoredDirectory(String),
/// An exact path, always anchored (no wildcards, no trailing slash).
Exact(String),
}
impl CompiledPattern {
/// Compile a CODEOWNERS pattern string into a specialized matcher.
///
/// The leading `/` (anchor) is inspected to distinguish anchored from
/// unanchored directory patterns; a trailing `/` selects directory
/// matching over exact matching.
pub fn new(pattern: &str) -> Self {
let anchored = pattern.starts_with('/');
let pattern = pattern.trim_start_matches('/');
// Catch-all patterns
if pattern == "*" || pattern == "**" {
if anchored && pattern == "*" {
return CompiledPattern::RootFilesOnly;
}
return CompiledPattern::MatchAll;
}
// Patterns with wildcards
if pattern.contains('*') {
// Unanchored single-segment like *.rs
if !anchored && !pattern.contains('/') {
// Fast path: *.ext patterns use suffix check (no glob needed)
if let Some(ext) = pattern.strip_prefix('*') {
if !ext.contains('*') && !ext.contains('?') {
return CompiledPattern::ExtensionSuffix(ext.to_string());
}
}
return CompiledPattern::SingleSegmentGlob(format!("**/{}", pattern));
}
return CompiledPattern::MultiSegmentGlob(pattern.to_string());
}
// Directory patterns (trailing /)
if pattern.ends_with('/') {
let dir = pattern.trim_end_matches('/').to_string();
if anchored {
return CompiledPattern::AnchoredDirectory(dir);
} else {
return CompiledPattern::UnanchoredDirectory(dir);
}
}
// Exact patterns (always anchored)
CompiledPattern::Exact(pattern.to_string())
}
/// Test whether `path` matches this pattern.
///
/// Empty paths never match. Pattern semantics follow GitHub's
/// CODEOWNERS rules (last match wins is handled by the caller —
/// see [`check_file_ownership_parsed`](crate::check_file_ownership_parsed)).
#[inline]
pub fn matches(&self, path: &str) -> bool {
if path.is_empty() {
return false;
}
match self {
CompiledPattern::MatchAll => true,
CompiledPattern::RootFilesOnly => !path.contains('/'),
CompiledPattern::ExtensionSuffix(ext) => {
path.ends_with(ext.as_str())
&& (path.len() == ext.len()
|| path.as_bytes()[path.len() - ext.len() - 1] != b'/')
}
CompiledPattern::SingleSegmentGlob(glob) => fast_glob::glob_match(glob, path),
CompiledPattern::MultiSegmentGlob(glob) => fast_glob::glob_match(glob, path),
CompiledPattern::AnchoredDirectory(dir) => {
path.starts_with(dir.as_str())
&& (path.len() == dir.len() || path.as_bytes().get(dir.len()) == Some(&b'/'))
}
CompiledPattern::UnanchoredDirectory(dir) => {
// Match at root
if path.starts_with(dir.as_str())
&& (path.len() == dir.len() || path.as_bytes().get(dir.len()) == Some(&b'/'))
{
return true;
}
// Match anywhere: check each path segment boundary
let dir_bytes = dir.as_bytes();
let path_bytes = path.as_bytes();
for (i, _) in path.match_indices('/') {
let rest = &path_bytes[i + 1..];
if rest.len() >= dir_bytes.len()
&& &rest[..dir_bytes.len()] == dir_bytes
&& (rest.len() == dir_bytes.len() || rest[dir_bytes.len()] == b'/')
{
return true;
}
}
false
}
CompiledPattern::Exact(exact) => {
path == exact
|| (path.starts_with(exact.as_str())
&& path.as_bytes().get(exact.len()) == Some(&b'/'))
}
}
}
}
/// Simple glob pattern matching for CODEOWNERS patterns
///
/// Key rules:
/// - Leading `/` anchors pattern to repository root
/// - No leading `/`:
/// - If ends with `/`: directory pattern, matches anywhere in tree
/// - If contains `/`: implicitly anchored to root
/// - If no `/` and has `*`: matches at any depth (e.g., `*.rs`)
/// - If no `/` and no `*`: anchored to root
/// - `*` matches any characters except `/`
/// - `**` matches zero or more directories
#[inline]
pub fn pattern_matches(pattern: &str, path: &str) -> bool {
// Empty path never matches (edge case)
if path.is_empty() {
return false;
}
// Check if pattern is anchored (has leading /)
let anchored = pattern.starts_with('/');
let pattern = pattern.trim_start_matches('/');
// Handle catch-all patterns (matches everything)
// Only unanchored * or ** are true catch-alls
if !anchored && (pattern == "*" || pattern == "**") {
return true;
}
// Handle patterns with wildcards
if pattern.contains('*') {
// Anchored single-star only matches root level
// /* should only match files directly in root, not nested
if anchored && pattern == "*" {
// Match only if path has no directory separator
return !path.contains('/');
}
// Single-segment patterns like *.rs (unanchored) match at any depth
if !anchored && !pattern.contains('/') {
// Fast path: *.ext is just a suffix check
if let Some(ext) = pattern.strip_prefix('*') {
return path.ends_with(ext)
&& (path.len() == ext.len()
|| path.as_bytes()[path.len() - ext.len() - 1] != b'/');
}
let glob_pattern = format!("**/{}", pattern);
return fast_glob::glob_match(&glob_pattern, path);
}
// Multi-segment patterns or anchored patterns: match directly
return fast_glob::glob_match(pattern, path);
}
// Handle directory patterns (trailing /)
if pattern.ends_with('/') {
let dir = pattern.trim_end_matches('/');
if anchored {
// /docs/ - only matches docs/ at root
return path.starts_with(dir)
&& (path.len() == dir.len() || path.as_bytes().get(dir.len()) == Some(&b'/'));
} else {
// docs/ - matches any directory named "docs" anywhere
if path.starts_with(dir)
&& (path.len() == dir.len() || path.as_bytes().get(dir.len()) == Some(&b'/'))
{
return true;
}
// Check each path segment boundary (no allocations)
let dir_bytes = dir.as_bytes();
let path_bytes = path.as_bytes();
for (i, _) in path.match_indices('/') {
let rest = &path_bytes[i + 1..];
if rest.len() >= dir_bytes.len()
&& &rest[..dir_bytes.len()] == dir_bytes
&& (rest.len() == dir_bytes.len() || rest[dir_bytes.len()] == b'/')
{
return true;
}
}
return false;
}
}
// Handle exact/prefix patterns (no wildcards, no trailing /)
// These are always anchored (either explicitly or implicitly)
path == pattern
|| (path.starts_with(pattern) && path.as_bytes().get(pattern.len()) == Some(&b'/'))
}
/// Check if pattern `a` is subsumed by pattern `b` (i.e., everything `a` matches, `b` also matches).
/// If true, and `b` comes after `a` in CODEOWNERS, then `a` is a dead rule.
///
/// Key anchoring rules:
/// - Unanchored patterns (no leading `/`) match MORE files than anchored patterns
/// - `/docs/` (anchored) IS subsumed by `docs/` (unanchored)
/// - `docs/` (unanchored) is NOT subsumed by `/docs/` (anchored)
#[inline]
pub fn pattern_subsumes(a: &str, b: &str) -> bool {
// Track anchoring before stripping
let a_anchored = a.starts_with('/');
let b_anchored = b.starts_with('/');
let a = a.trim_start_matches('/');
let b = b.trim_start_matches('/');
// Identical patterns (both same anchoring and content)
if a == b && a_anchored == b_anchored {
return true;
}
// For exact paths (no wildcards, no trailing /), patterns containing /
// are implicitly anchored, so /src/foo and src/foo are equivalent
let a_is_exact = !a.contains('*') && !a.ends_with('/');
let b_is_exact = !b.contains('*') && !b.ends_with('/');
if a_is_exact && b_is_exact && a == b && a.contains('/') {
// Both are exact paths with /, one has leading / and one doesn't
// They match the same files, so b subsumes a
return true;
}
// Universal patterns subsume everything. `/*` is not one of them — it
// covers root-level entries only, so it must not swallow nested rules.
if b == "**" || (b == "*" && !b_anchored) {
return true;
}
if b == "*" {
// Anchored `/*`: only rules that are themselves confined to the root.
return (a == "*" && a_anchored) || (!a.contains('/') && !a.contains('*'));
}
// Extension patterns: *.rs is subsumed by *
if let Some(a_ext) = a.strip_prefix('*') {
if b == "*" || b == "**" {
return true;
}
// *.rs.bak is subsumed by *.bak
if let Some(b_ext) = b.strip_prefix('*') {
return a_ext.ends_with(b_ext);
}
return false;
}
// Directory patterns: /src/lib/ is subsumed by /src/
let a_dir = a
.trim_end_matches('/')
.trim_end_matches("/**")
.trim_end_matches("/*");
let b_dir = b
.trim_end_matches('/')
.trim_end_matches("/**")
.trim_end_matches("/*");
let a_reach = dir_reach(a);
let b_reach = dir_reach(b);
// Only a bare trailing slash floats: `docs/` matches a docs directory at
// any depth, while `docs/*` and `docs/**` are globs pinned to the root.
let a_floats = !a_anchored && a.ends_with('/');
let b_floats = !b_anchored && b.ends_with('/');
// Handle anchored vs unanchored directory patterns
if let (Some(a_reach), Some(b_reach)) = (a_reach, b_reach) {
// The floating pattern matches MORE files, so it is never subsumed by
// a pinned one — `docs/` also covers nested `x/docs/`.
if a_floats && !b_floats {
return false;
}
if b_reach == DirReach::DirectChildren {
return a_reach == DirReach::DirectChildren && a_dir == b_dir;
}
return a_dir == b_dir || starts_with_dir(a_dir, b_dir);
}
// Exact file in directory: src/main.rs subsumed by src/ or src/**
// An exact path is always root-anchored, so only `b`'s reach matters here.
if let (None, Some(b_reach)) = (a_reach, b_reach) {
if b_reach == DirReach::DirectChildren {
return is_direct_child(a, b_dir);
}
return a == b_dir || starts_with_dir(a, b_dir);
}
false
}
/// How deep a directory-style pattern reaches.
///
/// `docs/` and `docs/**` cover the whole subtree, but `docs/*` stops at the
/// directory's immediate entries — GitHub's own docs spell this out: `docs/*`
/// matches `docs/getting-started.md` and not `docs/build-app/troubleshooting.md`.
/// Collapsing the two is what makes a rule look dead when it isn't.
#[derive(PartialEq, Clone, Copy)]
enum DirReach {
Recursive,
DirectChildren,
}
fn dir_reach(pattern: &str) -> Option<DirReach> {
if pattern.ends_with('/') || pattern.ends_with("/**") {
Some(DirReach::Recursive)
} else if pattern.ends_with("/*") {
Some(DirReach::DirectChildren)
} else {
None
}
}
/// Check if `path` starts with `dir` followed by `/`
#[inline]
fn starts_with_dir(path: &str, dir: &str) -> bool {
path.starts_with(dir) && path.as_bytes().get(dir.len()) == Some(&b'/')
}
/// Whether `path` sits directly inside `dir` with nothing nested in between.
#[inline]
fn is_direct_child(path: &str, dir: &str) -> bool {
starts_with_dir(path, dir) && !path[dir.len() + 1..].contains('/')
}
#[cfg(test)]
mod tests {
use super::*;
// =============================================================================
// GITHUB CODEOWNERS CONFORMANCE TEST SUITE
// =============================================================================
// Based on official GitHub documentation and observed behavior.
// Reference: reports/compass_artifact_wf-*.md
//
// Key rules:
// 1. Last matching pattern wins (not most specific)
// 2. Leading `/` anchors pattern to repository root
// 3. No leading `/` = pattern can match anywhere in tree (for certain patterns)
// 4. Trailing `/` = directory pattern, matches recursively
// 5. `*` matches any characters EXCEPT `/` (doesn't cross directories)
// 6. `**` matches zero or more directories (crosses boundaries)
// 7. Patterns are case-sensitive
// 8. `[abc]` character ranges NOT supported (unlike gitignore)
// 9. `!` negation patterns NOT supported (unlike gitignore)
// =============================================================================
// ---------------------------------------------------------------------------
// CATEGORY 1: CATCH-ALL PATTERNS
// ---------------------------------------------------------------------------
// `*` and `**` at the root level match every file in the repository.
#[test]
fn test_catchall_star() {
// * matches every file in the repository (not just root)
assert!(pattern_matches("*", "readme.md"));
assert!(pattern_matches("*", "src/main.rs"));
assert!(pattern_matches("*", "a/b/c/d/e/f.txt"));
assert!(pattern_matches("*", ".hidden"));
assert!(pattern_matches("*", "deep/nested/path/file.ext"));
}
#[test]
fn test_catchall_double_star() {
// ** also matches everything
assert!(pattern_matches("**", "readme.md"));
assert!(pattern_matches("**", "src/main.rs"));
assert!(pattern_matches("**", "a/b/c/d/e/f.txt"));
}
// ---------------------------------------------------------------------------
// CATEGORY 2: EXTENSION PATTERNS (no path separator)
// ---------------------------------------------------------------------------
// Patterns like `*.rs` without any `/` match files at ANY depth.
// This is CODEOWNERS-specific behavior.
#[test]
fn test_extension_matches_any_depth() {
// *.rs matches .rs files at any depth
assert!(pattern_matches("*.rs", "main.rs"));
assert!(pattern_matches("*.rs", "src/main.rs"));
assert!(pattern_matches("*.rs", "src/lib/mod.rs"));
assert!(pattern_matches("*.rs", "a/b/c/d/file.rs"));
}
#[test]
fn test_extension_no_match_wrong_ext() {
assert!(!pattern_matches("*.rs", "main.go"));
assert!(!pattern_matches("*.rs", "src/main.go"));
assert!(!pattern_matches("*.rs", "readme.md"));
assert!(!pattern_matches("*.rs", "file.rs.bak")); // .rs.bak != .rs
}
#[test]
fn test_extension_complex() {
// *.config.js should match config.js files
assert!(pattern_matches("*.config.js", "webpack.config.js"));
assert!(pattern_matches("*.config.js", "src/babel.config.js"));
assert!(!pattern_matches("*.config.js", "config.js")); // no prefix before .config.js
}
// ---------------------------------------------------------------------------
// CATEGORY 3: ANCHORED DIRECTORY PATTERNS (leading `/`, trailing `/`)
// ---------------------------------------------------------------------------
// `/docs/` = directory at root, matches ALL contents recursively
#[test]
fn test_anchored_dir_matches_recursively() {
// /docs/ matches docs/* and docs/**/* at root only
assert!(pattern_matches("/docs/", "docs/readme.md"));
assert!(pattern_matches("/docs/", "docs/api/index.md"));
assert!(pattern_matches("/docs/", "docs/a/b/c/deep.txt"));
}
#[test]
fn test_anchored_dir_no_match_elsewhere() {
// /docs/ does NOT match docs/ nested elsewhere
assert!(!pattern_matches("/docs/", "src/docs/file.txt"));
assert!(!pattern_matches("/docs/", "other/docs/readme.md"));
assert!(!pattern_matches("/docs/", "a/b/docs/c.txt"));
}
#[test]
fn test_anchored_dir_no_match_partial_name() {
// /docs/ should not match /documentation/
assert!(!pattern_matches("/docs/", "documentation/readme.md"));
assert!(!pattern_matches("/docs/", "docs-old/readme.md"));
}
// ---------------------------------------------------------------------------
// CATEGORY 4: UNANCHORED DIRECTORY PATTERNS (no leading `/`, trailing `/`)
// ---------------------------------------------------------------------------
// `docs/` (no leading slash) = matches ANY directory named `docs` ANYWHERE
#[test]
fn test_unanchored_dir_matches_at_root() {
assert!(pattern_matches("docs/", "docs/readme.md"));
assert!(pattern_matches("docs/", "docs/api/index.md"));
}
#[test]
fn test_unanchored_dir_matches_nested() {
// This is the critical difference from anchored patterns
assert!(pattern_matches("docs/", "src/docs/file.txt"));
assert!(pattern_matches("docs/", "a/b/docs/deep.txt"));
assert!(pattern_matches("docs/", "project/src/docs/api/v1/spec.md"));
}
#[test]
fn test_unanchored_dir_no_match_partial_name() {
// Must be exact directory name
assert!(!pattern_matches("docs/", "documentation/readme.md"));
assert!(!pattern_matches("docs/", "mydocs/readme.md"));
assert!(!pattern_matches("docs/", "src/mydocs/file.txt"));
}
// ---------------------------------------------------------------------------
// CATEGORY 5: ANCHORED PATH PATTERNS (leading `/`, no wildcards)
// ---------------------------------------------------------------------------
// `/src/main.rs` = exact file at root
// `/src` = file or directory named `src` at root
#[test]
fn test_anchored_exact_file() {
assert!(pattern_matches("/Makefile", "Makefile"));
assert!(pattern_matches("/src/main.rs", "src/main.rs"));
assert!(!pattern_matches("/Makefile", "build/Makefile"));
assert!(!pattern_matches("/src/main.rs", "other/src/main.rs"));
}
#[test]
fn test_anchored_name_as_directory() {
// /docs (no trailing slash) matches file or directory named docs at root
assert!(pattern_matches("/docs", "docs")); // exact file match
assert!(pattern_matches("/docs", "docs/anything")); // as directory prefix
assert!(pattern_matches("/docs", "docs/nested/deep.txt"));
assert!(!pattern_matches("/docs", "src/docs"));
assert!(!pattern_matches("/docs", "src/docs/file.txt"));
}
// ---------------------------------------------------------------------------
// CATEGORY 6: SINGLE-SEGMENT UNANCHORED PATTERNS (no `/` at all)
// ---------------------------------------------------------------------------
// `Makefile` (no slashes) - behavior depends on interpretation
// In gitignore: matches anywhere. In CODEOWNERS: typically anchored.
#[test]
fn test_single_segment_exact() {
// Single segment without slashes - anchored to root
assert!(pattern_matches("Makefile", "Makefile"));
assert!(pattern_matches("README.md", "README.md"));
}
#[test]
fn test_single_segment_as_directory() {
// Single segment can also act as directory prefix
assert!(pattern_matches("src", "src/main.rs"));
assert!(pattern_matches("src", "src/lib/mod.rs"));
}
#[test]
fn test_single_segment_no_match_nested() {
// Should NOT match same name nested elsewhere (anchored behavior)
assert!(!pattern_matches("Makefile", "build/Makefile"));
assert!(!pattern_matches("src", "project/src/file.rs"));
}
// ---------------------------------------------------------------------------
// CATEGORY 7: MULTI-SEGMENT UNANCHORED PATTERNS (has `/`, no leading `/`)
// ---------------------------------------------------------------------------
// `src/main.rs` (contains `/` but no leading `/`) - implicitly anchored
#[test]
fn test_multi_segment_implicitly_anchored() {
assert!(pattern_matches("src/main.rs", "src/main.rs"));
assert!(pattern_matches("src/lib/mod.rs", "src/lib/mod.rs"));
// Should NOT match nested (implicitly anchored)
assert!(!pattern_matches("src/main.rs", "project/src/main.rs"));
}
// ---------------------------------------------------------------------------
// CATEGORY 8: SINGLE STAR PATTERNS (`*` - doesn't cross directories)
// ---------------------------------------------------------------------------
// `*` matches any character EXCEPT `/`
#[test]
fn test_star_direct_children_only() {
// /docs/* matches only direct children of docs
assert!(pattern_matches("/docs/*", "docs/readme.md"));
assert!(pattern_matches("/docs/*", "docs/index.html"));
// Does NOT match nested paths
assert!(!pattern_matches("/docs/*", "docs/api/index.md"));
assert!(!pattern_matches("/docs/*", "docs/a/b/file.txt"));
}
#[test]
fn test_star_in_filename() {
// Pattern with * in filename portion
assert!(pattern_matches("docs/*.md", "docs/readme.md"));
assert!(pattern_matches("docs/*.md", "docs/CHANGELOG.md"));
assert!(!pattern_matches("docs/*.md", "docs/readme.txt"));
assert!(!pattern_matches("docs/*.md", "docs/api/readme.md")); // nested
}
#[test]
fn test_star_prefix_suffix() {
// *crowdin* matches files with crowdin in the name
assert!(pattern_matches(
".github/workflows/*crowdin*",
".github/workflows/crowdin-download.yaml"
));
assert!(pattern_matches(
".github/workflows/*crowdin*",
".github/workflows/upload-crowdin.yaml"
));
assert!(!pattern_matches(
".github/workflows/*crowdin*",
".github/workflows/deploy.yaml"
));
}
#[test]
fn test_star_middle_of_path() {
// deployment/*/config.yaml - single directory wildcard
assert!(pattern_matches(
"deployment/*/config.yaml",
"deployment/prod/config.yaml"
));
assert!(pattern_matches(
"deployment/*/config.yaml",
"deployment/staging/config.yaml"
));
// Does not match nested
assert!(!pattern_matches(
"deployment/*/config.yaml",
"deployment/env/prod/config.yaml"
));
}
// ---------------------------------------------------------------------------
// CATEGORY 9: DOUBLE STAR PATTERNS (`**` - crosses directories)
// ---------------------------------------------------------------------------
#[test]
fn test_double_star_leading() {
// **/file.txt matches file.txt at any depth
assert!(pattern_matches("**/config.json", "config.json"));
assert!(pattern_matches("**/config.json", "src/config.json"));
assert!(pattern_matches("**/config.json", "a/b/c/config.json"));
assert!(!pattern_matches("**/config.json", "src/other.json"));
}
#[test]
fn test_double_star_trailing() {
// docs/** matches everything inside docs recursively
assert!(pattern_matches("docs/**", "docs/readme.md"));
assert!(pattern_matches("docs/**", "docs/api/index.md"));
assert!(pattern_matches("docs/**", "docs/a/b/c/deep.txt"));
assert!(!pattern_matches("docs/**", "other/readme.md"));
}
#[test]
fn test_double_star_middle() {
// a/**/b matches a/b, a/x/b, a/x/y/b
assert!(pattern_matches("a/**/b", "a/b"));
assert!(pattern_matches("a/**/b", "a/x/b"));
assert!(pattern_matches("a/**/b", "a/x/y/b"));
assert!(pattern_matches("a/**/b", "a/x/y/z/b"));
assert!(!pattern_matches("a/**/b", "a/x/y/c"));
assert!(!pattern_matches("a/**/b", "x/a/b")); // anchored
}
#[test]
fn test_double_star_with_extension() {
// docs/**/*.md matches all .md files under docs
assert!(pattern_matches("docs/**/*.md", "docs/readme.md"));
assert!(pattern_matches("docs/**/*.md", "docs/api/spec.md"));
assert!(pattern_matches("docs/**/*.md", "docs/a/b/c/guide.md"));
assert!(!pattern_matches("docs/**/*.md", "docs/readme.txt"));
assert!(!pattern_matches("docs/**/*.md", "src/readme.md"));
}
// ---------------------------------------------------------------------------
// CATEGORY 10: CASE SENSITIVITY
// ---------------------------------------------------------------------------
#[test]
fn test_case_sensitive_exact() {
assert!(pattern_matches("/Docs/", "Docs/readme.md"));
assert!(!pattern_matches("/Docs/", "docs/readme.md"));
assert!(!pattern_matches("/docs/", "Docs/readme.md"));
}
#[test]
fn test_case_sensitive_extension() {
assert!(pattern_matches("*.RS", "main.RS"));
assert!(!pattern_matches("*.RS", "main.rs"));
assert!(!pattern_matches("*.rs", "main.RS"));
}
// ---------------------------------------------------------------------------
// CATEGORY 11: UNSUPPORTED FEATURES (should be treated literally or ignored)
// ---------------------------------------------------------------------------
// Unlike gitignore, CODEOWNERS does NOT support:
// - `[abc]` character ranges
// - `!pattern` negation
#[test]
fn test_character_range_not_supported() {
// [abc] should be treated literally, not as character class
// A file literally named "[abc].txt" would match
// But "a.txt", "b.txt", "c.txt" should NOT match
assert!(!pattern_matches("[abc].txt", "a.txt"));
assert!(!pattern_matches("[abc].txt", "b.txt"));
assert!(!pattern_matches("[abc].txt", "c.txt"));
// Note: matching "[abc].txt" literally is tricky due to glob libs
}
#[test]
fn test_negation_not_supported() {
// !pattern should NOT work as negation
// It should either fail to match or be treated literally
// The key is it shouldn't "un-match" files
// (Hard to test directly, but we verify it doesn't crash or behave unexpectedly)
assert!(!pattern_matches("!docs/", "docs/readme.md"));
}
// ---------------------------------------------------------------------------
// CATEGORY 12: COMPOSITE TABLE TEST (from GitHub docs)
// ---------------------------------------------------------------------------
#[test]
fn test_github_docs_table() {
// From the report:
// | Pattern | `/docs/readme.md` | `/docs/api/index.md` | `/src/docs/file.txt` |
// |--------------|-------------------|----------------------|----------------------|
// | `/docs/` | ✓ | ✓ | ✗ |
// | `/docs/*` | ✓ | ✗ | ✗ |
// | `/docs/**` | ✓ | ✓ | ✗ |
// | `docs/` | ✓ | ✓ | ✓ |
// | `**/docs/**` | ✓ | ✓ | ✓ |
// /docs/ - anchored, recursive
assert!(pattern_matches("/docs/", "docs/readme.md"));
assert!(pattern_matches("/docs/", "docs/api/index.md"));
assert!(!pattern_matches("/docs/", "src/docs/file.txt"));
// /docs/* - anchored, direct children only
assert!(pattern_matches("/docs/*", "docs/readme.md"));
assert!(!pattern_matches("/docs/*", "docs/api/index.md"));
assert!(!pattern_matches("/docs/*", "src/docs/file.txt"));
// /docs/** - anchored, recursive via **
assert!(pattern_matches("/docs/**", "docs/readme.md"));
assert!(pattern_matches("/docs/**", "docs/api/index.md"));
assert!(!pattern_matches("/docs/**", "src/docs/file.txt"));
// docs/ - unanchored, matches anywhere
assert!(pattern_matches("docs/", "docs/readme.md"));
assert!(pattern_matches("docs/", "docs/api/index.md"));
assert!(pattern_matches("docs/", "src/docs/file.txt"));
// **/docs/** - explicit anywhere match
assert!(pattern_matches("**/docs/**", "docs/readme.md"));
assert!(pattern_matches("**/docs/**", "docs/api/index.md"));
assert!(pattern_matches("**/docs/**", "src/docs/file.txt"));
}
// ---------------------------------------------------------------------------
// CATEGORY 13: EDGE CASES
// ---------------------------------------------------------------------------
#[test]
fn test_root_files_only() {
// /* should match only files at root, not nested
assert!(pattern_matches("/*", "readme.md"));
assert!(pattern_matches("/*", "Makefile"));
// Should NOT match nested files
assert!(!pattern_matches("/*", "src/main.rs"));
assert!(!pattern_matches("/*", "docs/readme.md"));
}
#[test]
fn test_hidden_files() {
// Patterns should match hidden files (starting with .)
assert!(pattern_matches("*", ".gitignore"));
assert!(pattern_matches(".*", ".gitignore"));
assert!(pattern_matches(".github/", ".github/workflows/ci.yml"));
}
#[test]
fn test_deeply_nested() {
let deep = "a/b/c/d/e/f/g/h/i/j/k/file.txt";
assert!(pattern_matches("*", deep));
assert!(pattern_matches("**", deep));
assert!(pattern_matches("a/", deep));
assert!(pattern_matches("a/**", deep));
assert!(pattern_matches("**/file.txt", deep));
assert!(!pattern_matches("a/*", deep)); // * doesn't cross dirs
}
#[test]
fn test_empty_path_segments() {
// Paths shouldn't have empty segments, but verify no panic
// This is more of a robustness test
assert!(!pattern_matches("/docs/", ""));
assert!(!pattern_matches("*", ""));
}
// ---------------------------------------------------------------------------
// SUBSUMPTION TESTS
// ---------------------------------------------------------------------------
// pattern_subsumes(a, b) = true if everything a matches, b also matches
// Used for detecting dead rules in CODEOWNERS
#[test]
fn test_subsumes_identical() {
assert!(pattern_subsumes("*.rs", "*.rs"));
assert!(pattern_subsumes("/src/", "/src/"));
assert!(pattern_subsumes("Makefile", "Makefile"));
}
#[test]
fn test_subsumes_catchall() {
// * and ** subsume everything - this is the "docs/ @baz" followed by "* @gav" case
// where docs/ becomes a dead rule because * matches everything and comes later
assert!(pattern_subsumes("*.rs", "*"));
assert!(pattern_subsumes("*.go", "*"));
assert!(pattern_subsumes("/src/", "*")); // anchored dir
assert!(pattern_subsumes("docs/", "*")); // unanchored dir - critical case!
assert!(pattern_subsumes("src/lib/", "*")); // unanchored nested dir
assert!(pattern_subsumes("Makefile", "*"));
assert!(pattern_subsumes("src/main.rs", "*"));
assert!(pattern_subsumes("*.rs", "**"));
assert!(pattern_subsumes("/src/lib/", "**"));
assert!(pattern_subsumes("docs/", "**")); // unanchored dir subsumed by **
}
#[test]
fn test_subsumes_dead_rule_scenarios() {
// Real-world CODEOWNERS footguns where earlier rules become dead
//
// Scenario 1: Catch-all at end shadows everything
// ```
// docs/ @docs-team
// * @default-team <- docs/ is now dead, @default-team owns docs/
// ```
assert!(pattern_subsumes("docs/", "*"));
// Scenario 2: Specific before general (WRONG order)
// ```
// /src/auth/ @security
// /src/ @backend <- /src/auth/ is dead, @backend owns it
// ```
assert!(pattern_subsumes("/src/auth/", "/src/"));
// Scenario 3: Extension pattern before catch-all
// ```
// *.rs @rust-team
// * @default <- *.rs is dead
// ```
assert!(pattern_subsumes("*.rs", "*"));
// Scenario 4: NOT dead - unanchored survives anchored
// ```
// docs/ @docs-team <- still matches src/docs/, lib/docs/, etc.
// /docs/ @root-docs <- only matches root docs/
// ```
assert!(!pattern_subsumes("docs/", "/docs/"));
}
#[test]
fn test_subsumes_extension() {
// *.rs.bak subsumed by *.bak
assert!(pattern_subsumes("*.rs.bak", "*.bak"));
assert!(!pattern_subsumes("*.bak", "*.rs.bak"));
assert!(!pattern_subsumes("*.rs", "*.go"));
}
#[test]
fn test_subsumes_directory() {
// /src/lib/ subsumed by /src/
assert!(pattern_subsumes("/src/lib/", "/src/"));
assert!(pattern_subsumes("src/lib/", "src/"));
assert!(pattern_subsumes("/src/lib/", "/src/**"));
assert!(!pattern_subsumes("/src/", "/src/lib/"));
}
#[test]
fn test_subsumes_file_in_dir() {
assert!(pattern_subsumes("src/main.rs", "src/"));
assert!(pattern_subsumes("src/main.rs", "src/**"));
assert!(!pattern_subsumes("src/main.rs", "lib/"));
}
#[test]
fn test_not_subsumed() {
assert!(!pattern_subsumes("*.rs", "*.go"));
assert!(!pattern_subsumes("/src/", "/lib/"));
assert!(!pattern_subsumes("*", "*.rs"));
}
// ---------------------------------------------------------------------------
// ANCHORED VS UNANCHORED SUBSUMPTION
// ---------------------------------------------------------------------------
// Unanchored patterns match MORE files than anchored patterns.
// - /docs/ (anchored) IS subsumed by docs/ (unanchored)
// - docs/ (unanchored) is NOT subsumed by /docs/ (anchored)
#[test]
fn test_subsumes_anchored_by_unanchored() {
// Anchored pattern IS subsumed by unanchored (unanchored matches more)
assert!(pattern_subsumes("/docs/", "docs/"));
assert!(pattern_subsumes("/src/lib/", "src/lib/"));
}
#[test]
fn test_subsumes_unanchored_not_by_anchored() {
// Unanchored pattern is NOT subsumed by anchored (unanchored matches nested paths)
assert!(!pattern_subsumes("docs/", "/docs/"));
assert!(!pattern_subsumes("src/", "/src/"));
}
#[test]
fn test_subsumes_both_anchored() {
// Both anchored - normal subsumption rules apply
assert!(pattern_subsumes("/src/lib/", "/src/"));
assert!(!pattern_subsumes("/src/", "/src/lib/"));
}
#[test]
fn test_subsumes_both_unanchored() {
// Both unanchored - normal subsumption rules apply
assert!(pattern_subsumes("src/lib/", "src/"));
assert!(!pattern_subsumes("src/", "src/lib/"));
}
// ---------------------------------------------------------------------------
// COMPILED PATTERN TESTS
// ---------------------------------------------------------------------------
// Tests specifically for the CompiledPattern struct
#[test]
fn test_compiled_pattern_match_all() {
let p = CompiledPattern::new("*");
assert!(p.matches("anything.txt"));
assert!(p.matches("a/b/c.txt"));
let p2 = CompiledPattern::new("**");
assert!(p2.matches("anything.txt"));
assert!(p2.matches("a/b/c.txt"));
}
#[test]
fn test_compiled_pattern_root_files_only() {
let p = CompiledPattern::new("/*");
assert!(p.matches("readme.md"));
assert!(p.matches("Makefile"));
assert!(!p.matches("src/main.rs"));
assert!(!p.matches("a/b/c.txt"));
}
#[test]
fn test_compiled_pattern_single_segment_glob() {
let p = CompiledPattern::new("*.rs");
assert!(p.matches("main.rs"));
assert!(p.matches("src/main.rs"));
assert!(p.matches("a/b/c/mod.rs"));
assert!(!p.matches("main.go"));
}
#[test]
fn test_compiled_pattern_multi_segment_glob() {
let p = CompiledPattern::new("src/**/*.rs");
assert!(p.matches("src/main.rs"));
assert!(p.matches("src/lib/mod.rs"));
assert!(!p.matches("lib/main.rs"));
let p2 = CompiledPattern::new("/docs/*.md");
assert!(p2.matches("docs/readme.md"));
assert!(!p2.matches("docs/api/readme.md"));
}
#[test]
fn test_compiled_pattern_anchored_directory() {
let p = CompiledPattern::new("/src/");
assert!(p.matches("src/main.rs"));
assert!(p.matches("src/lib/mod.rs"));
assert!(!p.matches("other/src/file.rs"));
}
#[test]
fn test_compiled_pattern_unanchored_directory() {
let p = CompiledPattern::new("docs/");
assert!(p.matches("docs/readme.md"));
assert!(p.matches("src/docs/file.txt"));
assert!(p.matches("a/b/docs/deep.txt"));
assert!(!p.matches("documentation/readme.md"));
}
#[test]
fn test_compiled_pattern_exact() {
let p = CompiledPattern::new("/Makefile");
assert!(p.matches("Makefile"));
assert!(!p.matches("build/Makefile"));
let p2 = CompiledPattern::new("/src");
assert!(p2.matches("src"));
assert!(p2.matches("src/main.rs"));
assert!(!p2.matches("other/src/file.rs"));
}
#[test]
fn test_compiled_pattern_empty_path() {
// Empty path should never match anything
let p = CompiledPattern::new("*");
assert!(!p.matches(""));
let p2 = CompiledPattern::new("/src/");
assert!(!p2.matches(""));
}
#[test]
fn test_compiled_pattern_unanchored_dir_exact_match() {
// Unanchored dir pattern matching exact dir name
let p = CompiledPattern::new("docs/");
// Test where path exactly equals the dir (edge case)
assert!(p.matches("docs/file.txt"));
}
#[test]
fn test_compiled_pattern_unanchored_dir_slash_search() {
// Test the /dir/ search path in UnanchoredDirectory
let p = CompiledPattern::new("lib/");
assert!(p.matches("src/lib/file.rs"));
assert!(p.matches("a/b/lib/c/d.rs"));
assert!(!p.matches("library/file.rs"));
}
// ---------------------------------------------------------------------------
// ADDITIONAL EDGE CASES
// ---------------------------------------------------------------------------
#[test]
fn test_subsumes_exact_paths_with_slash() {
// Exact paths containing / are implicitly anchored
// So /src/main.rs and src/main.rs are equivalent
assert!(pattern_subsumes("src/main.rs", "/src/main.rs"));
assert!(pattern_subsumes("/src/main.rs", "src/main.rs"));
}
#[test]
fn test_subsumes_unanchored_file_not_by_anchored_dir() {
// An unanchored file path shouldn't be subsumed by an anchored directory
// because the unanchored file might be somewhere else
// (This tests the !a_anchored && b_anchored case in subsumes)
// Note: "lib/foo.rs" contains / so is implicitly anchored
// For truly unanchored file, use single segment
assert!(!pattern_subsumes("foo.rs", "/src/")); // foo.rs is single segment, implicitly anchored
}
#[test]
fn test_compiled_anchored_star_vs_unanchored() {
// //* (anchored *) should be RootFilesOnly
// /* matches only root level files
let p = CompiledPattern::new("/*");
assert!(matches!(p, CompiledPattern::RootFilesOnly));
// Unanchored * is MatchAll
let p2 = CompiledPattern::new("*");
assert!(matches!(p2, CompiledPattern::MatchAll));
}
#[test]
fn test_anchored_dir_exact_length_match() {
// Test the exact length boundary in AnchoredDirectory
let p = CompiledPattern::new("/src/");
// Path exactly equal to dir (edge case) - matches because it's the dir itself
assert!(p.matches("src")); // Directory pattern matches the directory itself
// With content after
assert!(p.matches("src/file.rs"));
// But not partial prefix
assert!(!p.matches("srcfile.rs")); // "srcfile" != "src/"
}
#[test]
fn test_compiled_unanchored_dir_nested_match() {
// Test the loop in UnanchoredDirectory that checks /dir at various positions (lines 86-89)
let p = CompiledPattern::new("lib/");
// Should match lib at various depths
assert!(p.matches("lib/file.rs")); // root
assert!(p.matches("src/lib/file.rs")); // one level deep
assert!(p.matches("a/b/lib/c.rs")); // two levels deep
assert!(p.matches("x/y/z/lib/deep.txt")); // three levels deep
// Should not match partial names
assert!(!p.matches("libfoo/file.rs"));
assert!(!p.matches("src/libfoo/file.rs"));
}
#[test]
fn test_pattern_matches_unanchored_dir_loop() {
// Test the loop in pattern_matches for unanchored directories (lines 177-182)
// This covers the second check branch in the loop
assert!(pattern_matches("docs/", "project/docs/readme.md"));
assert!(pattern_matches("docs/", "a/b/docs/file.txt"));
assert!(pattern_matches("docs/", "x/docs/y/z.md"));
}
#[test]
fn test_subsumes_extension_not_by_non_extension() {
// Test line 234 - *.rs not subsumed by a non-* pattern
assert!(!pattern_subsumes("*.rs", "src/"));
assert!(!pattern_subsumes("*.rs", "/src/main.rs"));
assert!(!pattern_subsumes("*.js", "docs/"));
}
#[test]
fn test_compiled_exact_as_directory_prefix() {
// Test Exact pattern matching as directory prefix
let p = CompiledPattern::new("/build");
assert!(p.matches("build")); // exact
assert!(p.matches("build/output")); // as prefix
assert!(p.matches("build/a/b/c")); // deep prefix
assert!(!p.matches("builder")); // not prefix with /
assert!(!p.matches("src/build")); // nested - anchored
}
}