@@ -209,7 +209,7 @@ func TestSastRules(t *testing.T) {
209209 assert .Equal (t , filepath .Join (scannerTempDir , "results.sarif" ), sastScanManager .resultsFileName )
210210}
211211
212- // xscGitInfoWithChanged builds an XscGitInfoContext the way the client defines it (GitDiffContext with changed_files ).
212+ // xscGitInfoWithChanged builds an XscGitInfoContext the way the client defines it (GitDiffContext with changed files ).
213213// Must match the shape expected by SastChangedFilesForTarget in sastscanner.go.
214214func xscGitInfoWithChanged (t * testing.T , files ... string ) * xscservices.XscGitInfoContext {
215215 t .Helper ()
@@ -237,61 +237,62 @@ func TestSastChangedFilesForTarget(t *testing.T) {
237237 name string
238238 gitCtx * xscservices.XscGitInfoContext
239239 targetPath string
240- commonParent string
240+ rootDir string
241241 changedFilesMode bool
242242 // wantEmpty: expect no file roots (nil or empty slice) when mode is off or there is nothing to return.
243243 wantEmpty bool
244244 want []string
245245 }{
246- {name : "nil_context" , gitCtx : nil , targetPath : base , commonParent : base , changedFilesMode : true , wantEmpty : true },
247- {name : "changed_files_mode_off" , gitCtx : threeFiles , targetPath : modA , commonParent : base , changedFilesMode : false , wantEmpty : true },
248- {name : "empty_changed_files" , gitCtx : xscGitInfoWithChanged (t ), targetPath : modA , commonParent : base , changedFilesMode : true , wantEmpty : true },
249- {name : "empty_common_parent " , gitCtx : threeFiles , targetPath : modA , commonParent : "" , changedFilesMode : true , wantEmpty : true },
250- {name : "empty_target_path" , gitCtx : threeFiles , targetPath : "" , commonParent : base , changedFilesMode : true , wantEmpty : true },
246+ {name : "nil_context" , gitCtx : nil , targetPath : base , rootDir : base , changedFilesMode : true , wantEmpty : true },
247+ {name : "changed_files_mode_off" , gitCtx : threeFiles , targetPath : modA , rootDir : base , changedFilesMode : false , wantEmpty : true },
248+ {name : "empty_changed_files" , gitCtx : xscGitInfoWithChanged (t ), targetPath : modA , rootDir : base , changedFilesMode : true , wantEmpty : true },
249+ {name : "empty_root_dir " , gitCtx : threeFiles , targetPath : modA , rootDir : "" , changedFilesMode : true , wantEmpty : true },
250+ {name : "empty_target_path" , gitCtx : threeFiles , targetPath : "" , rootDir : base , changedFilesMode : true , wantEmpty : true },
251251 {
252- name : "target_is_common_parent_returns_all_as_abs " ,
252+ name : "target_is_repo_root_returns_all_as_abs " ,
253253 gitCtx : threeFiles ,
254254 targetPath : base ,
255- commonParent : base ,
255+ rootDir : base ,
256256 changedFilesMode : true ,
257257 want : []string {filepath .Join (base , "modA" , "a.go" ), filepath .Join (base , "modA" , "b.go" ), filepath .Join (base , "modB" , "x.go" )},
258258 },
259259 {
260260 name : "filters_to_modA_only" ,
261261 gitCtx : threeFiles ,
262262 targetPath : modA ,
263- commonParent : base ,
263+ rootDir : base ,
264264 changedFilesMode : true ,
265265 want : []string {filepath .Join (base , "modA" , "a.go" ), filepath .Join (base , "modA" , "b.go" )},
266266 },
267267 {
268268 name : "prefix_foo_does_not_match_foobar" ,
269269 gitCtx : & xscservices.XscGitInfoContext {GitDiffContext : xscservices.GitDiffContext {ChangedFiles : []string {"foo/x.go" , "foobar/y.go" }}},
270270 targetPath : filepath .Join (base , "foo" ),
271- commonParent : base ,
271+ rootDir : base ,
272272 changedFilesMode : true ,
273273 want : []string {filepath .Join (base , "foo" , "x.go" )},
274274 },
275275 {
276- name : "absolute_changed_file_under_repo" ,
277- gitCtx : xscGitInfoWithChanged (t , filepath .Join (base , "modA" , "abs.go" )),
276+ // belong-to-target matching uses repo-relative paths (as git reports); resolve to absolute under rootDir afterward.
277+ name : "repo_relative_changed_file_under_target" ,
278+ gitCtx : xscGitInfoWithChanged (t , "modA/abs.go" ),
278279 targetPath : modA ,
279- commonParent : base ,
280+ rootDir : base ,
280281 changedFilesMode : true ,
281282 want : []string {filepath .Join (base , "modA" , "abs.go" )},
282283 },
283284 {
284285 name : "deduplicates_same_paths" ,
285286 gitCtx : & xscservices.XscGitInfoContext {GitDiffContext : xscservices.GitDiffContext {ChangedFiles : []string {"modA/a.go" , "modA/a.go" , "./modA/a.go" }}},
286287 targetPath : modA ,
287- commonParent : base ,
288+ rootDir : base ,
288289 changedFilesMode : true ,
289290 want : []string {filepath .Join (base , "modA" , "a.go" )},
290291 },
291292 }
292293 for _ , tt := range tests {
293294 t .Run (tt .name , func (t * testing.T ) {
294- got := SastChangedFilesForTarget (tt .changedFilesMode , tt .gitCtx , tt .targetPath , tt .commonParent )
295+ got := SastChangedFilesForTarget (tt .changedFilesMode , tt .gitCtx , tt .targetPath , tt .rootDir )
295296 if tt .wantEmpty {
296297 assert .Empty (t , got , "SastChangedFilesForTarget should not return any paths in this case" )
297298 } else {
0 commit comments