@@ -278,14 +278,16 @@ func TestCloneOrOpenLanguageRepo(t *testing.T) {
278
278
func TestCleanAndCopyLibrary (t * testing.T ) {
279
279
t .Parallel ()
280
280
for _ , test := range []struct {
281
- name string
282
- libraryID string
283
- state * config.LibrarianState
284
- repo gitrepo.Repository
285
- outputDir string
286
- setup func (t * testing.T , outputDir string )
287
- wantErr bool
288
- errContains string
281
+ name string
282
+ libraryID string
283
+ state * config.LibrarianState
284
+ repo gitrepo.Repository
285
+ outputDir string
286
+ setup func (t * testing.T , repoDir , outputDir string )
287
+ wantErr bool
288
+ errContains string
289
+ shouldCopy []string
290
+ shouldDelete []string
289
291
}{
290
292
{
291
293
name : "library not found" ,
@@ -330,7 +332,7 @@ func TestCleanAndCopyLibrary(t *testing.T) {
330
332
},
331
333
},
332
334
repo : newTestGitRepo (t ),
333
- setup : func (t * testing.T , outputDir string ) {
335
+ setup : func (t * testing.T , repoDir , outputDir string ) {
334
336
// Create a symlink in the output directory to trigger an error.
335
337
if err := os .Symlink ("target" , filepath .Join (outputDir , "symlink" )); err != nil {
336
338
t .Fatalf ("os.Symlink() = %v" , err )
@@ -339,13 +341,59 @@ func TestCleanAndCopyLibrary(t *testing.T) {
339
341
wantErr : true ,
340
342
errContains : "failed to copy" ,
341
343
},
344
+ {
345
+ name : "empty RemoveRegex defaults to source root" ,
346
+ libraryID : "some-library" ,
347
+ state : & config.LibrarianState {
348
+ Libraries : []* config.LibraryState {
349
+ {
350
+ ID : "some-library" ,
351
+ SourceRoots : []string {"a/path" },
352
+ },
353
+ },
354
+ },
355
+ repo : newTestGitRepo (t ),
356
+ setup : func (t * testing.T , repoDir , outputDir string ) {
357
+ // Create a stale file in the repo directory to test cleaning.
358
+ staleFile := filepath .Join (repoDir , "a/path/stale.txt" )
359
+ if err := os .MkdirAll (filepath .Dir (staleFile ), 0755 ); err != nil {
360
+ t .Fatal (err )
361
+ }
362
+ if _ , err := os .Create (staleFile ); err != nil {
363
+ t .Fatal (err )
364
+ }
365
+
366
+ // Create generated files in the output directory.
367
+ filesToCreate := []string {
368
+ "a/path/new_generated_file_to_copy.txt" ,
369
+ "skipped/path/example.txt" ,
370
+ }
371
+ for _ , relPath := range filesToCreate {
372
+ fullPath := filepath .Join (outputDir , relPath )
373
+ if err := os .MkdirAll (filepath .Dir (fullPath ), 0755 ); err != nil {
374
+ t .Fatal (err )
375
+ }
376
+ if _ , err := os .Create (fullPath ); err != nil {
377
+ t .Fatal (err )
378
+ }
379
+ }
380
+ },
381
+ shouldCopy : []string {
382
+ "a/path/new_generated_file_to_copy.txt" ,
383
+ },
384
+ shouldDelete : []string {
385
+ "skipped/path/example.txt" ,
386
+ "a/path/stale.txt" ,
387
+ },
388
+ },
342
389
} {
343
390
t .Run (test .name , func (t * testing.T ) {
391
+ repoDir := test .repo .GetDir ()
344
392
outputDir := t .TempDir ()
345
393
if test .setup != nil {
346
- test .setup (t , outputDir )
394
+ test .setup (t , repoDir , outputDir )
347
395
}
348
- err := cleanAndCopyLibrary (test .state , test . repo . GetDir () , test .libraryID , outputDir )
396
+ err := cleanAndCopyLibrary (test .state , repoDir , test .libraryID , outputDir )
349
397
if test .wantErr {
350
398
if err == nil {
351
399
t .Errorf ("%s should return error" , test .name )
@@ -359,6 +407,20 @@ func TestCleanAndCopyLibrary(t *testing.T) {
359
407
if err != nil {
360
408
t .Fatal (err )
361
409
}
410
+
411
+ for _ , file := range test .shouldCopy {
412
+ fullPath := filepath .Join (repoDir , file )
413
+ if _ , err := os .Stat (fullPath ); err != nil {
414
+ t .Errorf ("file %s is not copied to %s" , file , repoDir )
415
+ }
416
+ }
417
+
418
+ for _ , file := range test .shouldDelete {
419
+ fullPath := filepath .Join (repoDir , file )
420
+ if _ , err := os .Stat (fullPath ); ! os .IsNotExist (err ) {
421
+ t .Errorf ("file %s should not be copied to %s" , file , repoDir )
422
+ }
423
+ }
362
424
})
363
425
}
364
426
}
0 commit comments