Skip to content

Commit f9da4c3

Browse files
committed
fix(internal/librarian): default to source_roots when remove_regex is not provided
1 parent 8f409cc commit f9da4c3

File tree

2 files changed

+86
-12
lines changed

2 files changed

+86
-12
lines changed

internal/librarian/command.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"os"
2424
"path"
2525
"path/filepath"
26+
"regexp"
2627
"strings"
2728
"time"
2829

@@ -203,7 +204,18 @@ func cleanAndCopyLibrary(state *config.LibrarianState, repoDir, libraryID, outpu
203204
return fmt.Errorf("library %q not found during clean and copy, despite being found in earlier steps", libraryID)
204205
}
205206

206-
if err := clean(repoDir, library.RemoveRegex, library.PreserveRegex); err != nil {
207+
removePatterns := library.RemoveRegex
208+
if len(removePatterns) == 0 {
209+
slog.Info("remove_regex not provided, defaulting to source_roots")
210+
removePatterns = make([]string, len(library.SourceRoots))
211+
// For each SourceRoot, create a regex pattern to match the source root
212+
// directory itself, and any file or subdirectory within it.
213+
for i, root := range library.SourceRoots {
214+
removePatterns[i] = fmt.Sprintf("^%s(/.*)?$", regexp.QuoteMeta(root))
215+
}
216+
}
217+
218+
if err := clean(repoDir, removePatterns, library.PreserveRegex); err != nil {
207219
return fmt.Errorf("failed to clean library, %s: %w", library.ID, err)
208220
}
209221

internal/librarian/command_test.go

Lines changed: 73 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -278,14 +278,16 @@ func TestCloneOrOpenLanguageRepo(t *testing.T) {
278278
func TestCleanAndCopyLibrary(t *testing.T) {
279279
t.Parallel()
280280
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
289291
}{
290292
{
291293
name: "library not found",
@@ -330,7 +332,7 @@ func TestCleanAndCopyLibrary(t *testing.T) {
330332
},
331333
},
332334
repo: newTestGitRepo(t),
333-
setup: func(t *testing.T, outputDir string) {
335+
setup: func(t *testing.T, repoDir, outputDir string) {
334336
// Create a symlink in the output directory to trigger an error.
335337
if err := os.Symlink("target", filepath.Join(outputDir, "symlink")); err != nil {
336338
t.Fatalf("os.Symlink() = %v", err)
@@ -339,13 +341,59 @@ func TestCleanAndCopyLibrary(t *testing.T) {
339341
wantErr: true,
340342
errContains: "failed to copy",
341343
},
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+
},
342389
} {
343390
t.Run(test.name, func(t *testing.T) {
391+
repoDir := test.repo.GetDir()
344392
outputDir := t.TempDir()
345393
if test.setup != nil {
346-
test.setup(t, outputDir)
394+
test.setup(t, repoDir, outputDir)
347395
}
348-
err := cleanAndCopyLibrary(test.state, test.repo.GetDir(), test.libraryID, outputDir)
396+
err := cleanAndCopyLibrary(test.state, repoDir, test.libraryID, outputDir)
349397
if test.wantErr {
350398
if err == nil {
351399
t.Errorf("%s should return error", test.name)
@@ -359,6 +407,20 @@ func TestCleanAndCopyLibrary(t *testing.T) {
359407
if err != nil {
360408
t.Fatal(err)
361409
}
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+
}
362424
})
363425
}
364426
}

0 commit comments

Comments
 (0)