@@ -78,13 +78,17 @@ pub enum OwnerMatcher {
7878impl OwnerMatcher {
7979 pub fn new_glob_with_candidate_subtracted_globs (
8080 glob : String ,
81- candidate_subtracted_globs : Vec < String > ,
81+ candidate_subtracted_globs : & [ String ] ,
8282 team_name : TeamName ,
8383 source : Source ,
8484 ) -> Self {
85- let subtracted_globs = candidate_subtracted_globs. iter ( ) . filter ( |candidate_subtracted_glob| {
86- glob_match ( candidate_subtracted_glob, & glob) || glob_match ( & glob, candidate_subtracted_glob)
87- } ) . cloned ( ) . collect ( ) ;
85+ let subtracted_globs = candidate_subtracted_globs
86+ . iter ( )
87+ . filter ( |candidate_subtracted_glob| {
88+ glob_match ( candidate_subtracted_glob, & glob) || glob_match ( & glob, candidate_subtracted_glob)
89+ } )
90+ . cloned ( )
91+ . collect ( ) ;
8892 OwnerMatcher :: Glob {
8993 glob,
9094 subtracted_globs,
@@ -109,13 +113,10 @@ impl OwnerMatcher {
109113 subtracted_globs,
110114 team_name,
111115 source,
112- } => {
113- if glob_match ( glob, relative_path. to_str ( ) . unwrap ( ) ) {
114- ( Some ( team_name) , source)
115- } else {
116- ( None , source)
117- }
118- }
116+ } => relative_path
117+ . to_str ( )
118+ . filter ( |path| glob_match ( glob, path) && !subtracted_globs. iter ( ) . any ( |subtracted| glob_match ( subtracted, path) ) )
119+ . map_or ( ( None , source) , |_| ( Some ( team_name) , source) ) ,
119120 OwnerMatcher :: ExactMatches ( path_to_team, source) => ( path_to_team. get ( relative_path) , source) ,
120121 }
121122 }
@@ -125,15 +126,15 @@ impl OwnerMatcher {
125126mod tests {
126127 use super :: * ;
127128
128- fn assert_owner_for ( glob : & str , relative_path : & str , expect_match : bool ) {
129+ fn assert_owner_for ( glob : & str , subtracted_globs : & [ & str ] , relative_path : & str , expect_match : bool ) {
129130 let source = Source :: Directory ( "packs/bam" . to_string ( ) ) ;
130131 let team_name = "team1" . to_string ( ) ;
131- let owner_matcher = OwnerMatcher :: Glob {
132- glob : glob . to_string ( ) ,
133- subtracted_globs : vec ! [ ] ,
134- team_name : team_name . clone ( ) ,
135- source : source . clone ( ) ,
136- } ;
132+ let owner_matcher = OwnerMatcher :: new_glob_with_candidate_subtracted_globs (
133+ glob. to_string ( ) ,
134+ & subtracted_globs. iter ( ) . map ( |s| s . to_string ( ) ) . collect :: < Vec < String > > ( ) ,
135+ team_name. clone ( ) ,
136+ source. clone ( ) ,
137+ ) ;
137138 let response = owner_matcher. owner_for ( Path :: new ( relative_path) ) ;
138139 if expect_match {
139140 assert_eq ! ( response, ( Some ( & team_name) , & source) ) ;
@@ -144,26 +145,50 @@ mod tests {
144145
145146 #[ test]
146147 fn owner_for_without_brackets_in_glob ( ) {
147- assert_owner_for ( "packs/bam/**/**" , "packs/bam/app/components/sidebar.jsx" , true ) ;
148- assert_owner_for ( "packs/bam/**/**" , "packs/baz/app/components/sidebar.jsx" , false ) ;
149- assert_owner_for ( "packs/bam/**/**" , "packs/bam/app/[components]/gadgets/sidebar.jsx" , true ) ;
150- assert_owner_for ( "packs/bam/**/**" , "packs/bam/app/sidebar_[component].jsx" , true ) ;
148+ assert_owner_for ( "packs/bam/**/**" , & [ ] , "packs/bam/app/components/sidebar.jsx" , true ) ;
149+ assert_owner_for ( "packs/bam/**/**" , & [ ] , "packs/baz/app/components/sidebar.jsx" , false ) ;
150+ assert_owner_for ( "packs/bam/**/**" , & [ ] , "packs/bam/app/[components]/gadgets/sidebar.jsx" , true ) ;
151+ assert_owner_for ( "packs/bam/**/**" , & [ ] , "packs/bam/app/sidebar_[component].jsx" , true ) ;
152+ assert_owner_for (
153+ "packs/bam/**/**" ,
154+ & [ "packs/bam/app/excluded/**" ] ,
155+ "packs/bam/app/excluded/sidebar_[component].jsx" ,
156+ false ,
157+ ) ;
158+ }
159+
160+ #[ test]
161+ fn subtracted_globs ( ) {
162+ assert_owner_for (
163+ "packs/bam/**/**" ,
164+ & [ "packs/bam/app/excluded/**" ] ,
165+ "packs/bam/app/excluded/some_file.rb" ,
166+ false ,
167+ ) ;
168+ assert_owner_for (
169+ "packs/bam/**/**" ,
170+ & [ "packs/bam/app/excluded/**" ] ,
171+ "packs/bam/app/not_excluded/some_file.rb" ,
172+ true ,
173+ ) ;
151174 }
152175
153176 #[ test]
154177 fn owner_for_with_brackets_in_glob ( ) {
155178 assert_owner_for (
156179 "packs/bam/app/\\ [components\\ ]/**/**" ,
180+ & [ ] ,
157181 "packs/bam/app/[components]/gadgets/sidebar.jsx" ,
158182 true ,
159183 ) ;
160- assert_owner_for ( "packs/\\ [bam\\ ]/**/**" , "packs/[bam]/app/components/sidebar.jsx" , true ) ;
184+ assert_owner_for ( "packs/\\ [bam\\ ]/**/**" , & [ ] , "packs/[bam]/app/components/sidebar.jsx" , true ) ;
161185 }
162186
163187 #[ test]
164188 fn owner_for_with_multiple_brackets_in_glob ( ) {
165189 assert_owner_for (
166190 "packs/\\ [bam\\ ]/bar/\\ [foo\\ ]/**/**" ,
191+ & [ ] ,
167192 "packs/[bam]/bar/[foo]/app/components/sidebar.jsx" ,
168193 true ,
169194 ) ;
@@ -192,17 +217,25 @@ mod tests {
192217 fn test_new_glob_with_candidate_subtracted_globs ( ) {
193218 assert_new_glob_with_candidate_subtracted_globs ( "packs/bam/**/**" , & [ ] , & [ ] ) ;
194219 assert_new_glob_with_candidate_subtracted_globs ( "packs/bam/**/**" , & [ "packs/bam/app/**/**" ] , & [ "packs/bam/app/**/**" ] ) ;
195- assert_new_glob_with_candidate_subtracted_globs ( "packs/bam/**/**" , & [ "packs/bam/app/an/exceptional/path/it.rb" ] , & [ "packs/bam/app/an/exceptional/path/it.rb" ] ) ;
220+ assert_new_glob_with_candidate_subtracted_globs (
221+ "packs/bam/**/**" ,
222+ & [ "packs/bam/app/an/exceptional/path/it.rb" ] ,
223+ & [ "packs/bam/app/an/exceptional/path/it.rb" ] ,
224+ ) ;
196225 assert_new_glob_with_candidate_subtracted_globs ( "packs/bam/**/**" , & [ "packs/bam.rb" ] , & [ ] ) ;
197226 assert_new_glob_with_candidate_subtracted_globs ( "packs/bam/**/**" , & [ "packs/nope/app/**/**" ] , & [ ] ) ;
198227 assert_new_glob_with_candidate_subtracted_globs ( "packs/**" , & [ "packs/yep/app/**/**" ] , & [ "packs/yep/app/**/**" ] ) ;
199228 assert_new_glob_with_candidate_subtracted_globs ( "packs/foo.yml" , & [ "packs/foo/**/**" ] , & [ ] ) ;
200229 }
201230
202- fn assert_new_glob_with_candidate_subtracted_globs ( glob : & str , candidate_subtracted_globs : & [ & str ] , expected_subtracted_globs : & [ & str ] ) {
231+ fn assert_new_glob_with_candidate_subtracted_globs (
232+ glob : & str ,
233+ candidate_subtracted_globs : & [ & str ] ,
234+ expected_subtracted_globs : & [ & str ] ,
235+ ) {
203236 let owner_matcher = OwnerMatcher :: new_glob_with_candidate_subtracted_globs (
204237 glob. to_string ( ) ,
205- candidate_subtracted_globs. iter ( ) . map ( |s| s. to_string ( ) ) . collect ( ) ,
238+ & candidate_subtracted_globs. iter ( ) . map ( |s| s. to_string ( ) ) . collect :: < Vec < String > > ( ) ,
206239 "team1" . to_string ( ) ,
207240 Source :: TeamGlob ( glob. to_string ( ) ) ,
208241 ) ;
0 commit comments