1
+ use bstr:: BStr ;
2
+ use gix_pathspec:: search:: MatchKind :: * ;
1
3
use std:: path:: Path ;
2
4
3
5
#[ test]
@@ -10,29 +12,67 @@ fn no_pathspecs_match_everything() -> crate::Result {
10
12
let mut search = gix_pathspec:: Search :: from_specs ( [ ] , None , Path :: new ( "" ) ) ?;
11
13
assert_eq ! ( search. patterns( ) . count( ) , 0 , "nothing artificial is added" ) ;
12
14
let m = search
13
- . pattern_matching_relative_path ( "hello" . into ( ) , None , & mut |_, _, _, _| {
14
- unreachable ! ( "must not be called" )
15
- } )
15
+ . pattern_matching_relative_path ( "hello" . into ( ) , None , & mut no_attrs)
16
16
. expect ( "matches" ) ;
17
17
assert_eq ! ( m. pattern. prefix_directory( ) , "" , "there is no prefix as none was given" ) ;
18
+ assert_eq ! ( m. kind, Always , "no pathspec always matches" ) ;
18
19
assert_eq ! (
19
20
m. sequence_number, 0 ,
20
21
"this is actually a fake pattern, as we have to match even though there isn't anything"
21
22
) ;
22
-
23
23
assert ! ( search. can_match_relative_path( "anything" . into( ) , None ) ) ;
24
+ Ok ( ( ) )
25
+ }
24
26
27
+ #[ test]
28
+ fn starts_with ( ) -> crate :: Result {
29
+ let mut search = gix_pathspec:: Search :: from_specs ( pathspecs ( & [ "a/*" ] ) , None , Path :: new ( "" ) ) ?;
30
+ assert ! (
31
+ search
32
+ . pattern_matching_relative_path( "a" . into( ) , Some ( false ) , & mut no_attrs)
33
+ . is_none( ) ,
34
+ "this can only match if it's a directory"
35
+ ) ;
36
+ assert ! (
37
+ search
38
+ . pattern_matching_relative_path( "a" . into( ) , Some ( true ) , & mut no_attrs)
39
+ . is_none( ) ,
40
+ "can't match as the '*' part is missing in value"
41
+ ) ;
42
+ assert ! (
43
+ search. can_match_relative_path( "a" . into( ) , Some ( true ) ) ,
44
+ "prefix-matches work though"
45
+ ) ;
46
+ assert ! (
47
+ search. can_match_relative_path( "a" . into( ) , Some ( false ) ) ,
48
+ "but not if it's a file"
49
+ ) ;
50
+ assert ! (
51
+ search. can_match_relative_path( "a" . into( ) , None ) ,
52
+ "if unspecified, we match for good measure"
53
+ ) ;
54
+ assert_eq ! (
55
+ search
56
+ . pattern_matching_relative_path( "a/file" . into( ) , None , & mut no_attrs)
57
+ . expect( "matches" )
58
+ . kind,
59
+ WildcardMatch ,
60
+ "a wildmatch is always performed here, even though it looks like a prefix"
61
+ ) ;
25
62
Ok ( ( ) )
26
63
}
27
64
28
65
#[ test]
29
66
fn simplified_search_respects_must_be_dir ( ) -> crate :: Result {
30
67
let mut search = gix_pathspec:: Search :: from_specs ( pathspecs ( & [ "a/be/" ] ) , None , Path :: new ( "" ) ) ?;
31
- search
32
- . pattern_matching_relative_path ( "a/be/file" . into ( ) , Some ( false ) , & mut |_, _, _, _| {
33
- unreachable ! ( "must not be called" )
34
- } )
35
- . expect ( "matches as this is a prefix match" ) ;
68
+ assert_eq ! (
69
+ search
70
+ . pattern_matching_relative_path( "a/be/file" . into( ) , Some ( false ) , & mut no_attrs)
71
+ . expect( "matches as this is a prefix match" )
72
+ . kind,
73
+ Prefix ,
74
+ "a verbatim part of the spec matches"
75
+ ) ;
36
76
assert ! (
37
77
!search. can_match_relative_path( "any" . into( ) , Some ( false ) ) ,
38
78
"not our directory: a, and must be dir"
@@ -79,7 +119,7 @@ fn simplified_search_respects_must_be_dir() -> crate::Result {
79
119
) ;
80
120
assert ! (
81
121
search
82
- . pattern_matching_relative_path( "a/b" . into( ) , None , & mut |_ , _ , _ , _| unreachable! ( "must not be called" ) )
122
+ . pattern_matching_relative_path( "a/b" . into( ) , None , & mut no_attrs )
83
123
. is_none( ) ,
84
124
"no match if it's not the whole pattern that matches"
85
125
) ;
@@ -183,21 +223,20 @@ fn no_pathspecs_respect_prefix() -> crate::Result {
183
223
) ;
184
224
assert ! (
185
225
search
186
- . pattern_matching_relative_path( "hello" . into( ) , None , & mut |_, _, _, _| unreachable!(
187
- "must not be called"
188
- ) )
226
+ . pattern_matching_relative_path( "hello" . into( ) , None , & mut no_attrs)
189
227
. is_none( ) ,
190
228
"not the right prefix"
191
229
) ;
192
230
assert ! ( !search. can_match_relative_path( "hello" . into( ) , None ) ) ;
193
231
let m = search
194
- . pattern_matching_relative_path ( "a/b" . into ( ) , None , & mut |_ , _ , _ , _| unreachable ! ( "must not be called" ) )
232
+ . pattern_matching_relative_path ( "a/b" . into ( ) , None , & mut no_attrs )
195
233
. expect ( "match" ) ;
196
234
assert_eq ! (
197
235
m. pattern. prefix_directory( ) ,
198
236
"a" ,
199
237
"the prefix directory matched verbatim"
200
238
) ;
239
+ assert_eq ! ( m. kind, Prefix , "the common path also works like a prefix" ) ;
201
240
assert ! ( search. can_match_relative_path( "a/" . into( ) , Some ( true ) ) ) ;
202
241
assert ! ( search. can_match_relative_path( "a" . into( ) , Some ( true ) ) ) ;
203
242
assert ! ( !search. can_match_relative_path( "a" . into( ) , Some ( false ) ) ) ;
@@ -249,7 +288,7 @@ fn prefixes_are_always_case_sensitive() -> crate::Result {
249
288
. iter ( )
250
289
. filter ( |relative_path| {
251
290
search
252
- . pattern_matching_relative_path ( relative_path. as_str ( ) . into ( ) , Some ( false ) , & mut |_ , _ , _ , _| false )
291
+ . pattern_matching_relative_path ( relative_path. as_str ( ) . into ( ) , Some ( false ) , & mut no_attrs )
253
292
. is_some ( )
254
293
} )
255
294
. collect ( ) ;
@@ -442,3 +481,7 @@ mod baseline {
442
481
Ok ( ( root, items, expected) )
443
482
}
444
483
}
484
+
485
+ fn no_attrs ( _: & BStr , _: gix_glob:: pattern:: Case , _: bool , _: & mut gix_attributes:: search:: Outcome ) -> bool {
486
+ unreachable ! ( "must not be called" )
487
+ }
0 commit comments