Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion internal/librarian/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"os"
"path"
"path/filepath"
"regexp"
"strings"
"time"

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

if err := clean(repoDir, library.RemoveRegex, library.PreserveRegex); err != nil {
removePatterns := library.RemoveRegex
if len(removePatterns) == 0 {
slog.Info("remove_regex not provided, defaulting to source_roots")
removePatterns = make([]string, len(library.SourceRoots))
// For each SourceRoot, create a regex pattern to match the source root
// directory itself, and any file or subdirectory within it.
for i, root := range library.SourceRoots {
removePatterns[i] = fmt.Sprintf("^%s(/.*)?$", regexp.QuoteMeta(root))
}
}

if err := clean(repoDir, removePatterns, library.PreserveRegex); err != nil {
return fmt.Errorf("failed to clean library, %s: %w", library.ID, err)
}

Expand Down
84 changes: 73 additions & 11 deletions internal/librarian/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,14 +278,16 @@ func TestCloneOrOpenLanguageRepo(t *testing.T) {
func TestCleanAndCopyLibrary(t *testing.T) {
t.Parallel()
for _, test := range []struct {
name string
libraryID string
state *config.LibrarianState
repo gitrepo.Repository
outputDir string
setup func(t *testing.T, outputDir string)
wantErr bool
errContains string
name string
libraryID string
state *config.LibrarianState
repo gitrepo.Repository
outputDir string
setup func(t *testing.T, repoDir, outputDir string)
wantErr bool
errContains string
shouldCopy []string
shouldDelete []string
}{
{
name: "library not found",
Expand Down Expand Up @@ -330,7 +332,7 @@ func TestCleanAndCopyLibrary(t *testing.T) {
},
},
repo: newTestGitRepo(t),
setup: func(t *testing.T, outputDir string) {
setup: func(t *testing.T, repoDir, outputDir string) {
// Create a symlink in the output directory to trigger an error.
if err := os.Symlink("target", filepath.Join(outputDir, "symlink")); err != nil {
t.Fatalf("os.Symlink() = %v", err)
Expand All @@ -339,13 +341,59 @@ func TestCleanAndCopyLibrary(t *testing.T) {
wantErr: true,
errContains: "failed to copy",
},
{
name: "empty RemoveRegex defaults to source root",
libraryID: "some-library",
state: &config.LibrarianState{
Libraries: []*config.LibraryState{
{
ID: "some-library",
SourceRoots: []string{"a/path"},
},
},
},
repo: newTestGitRepo(t),
setup: func(t *testing.T, repoDir, outputDir string) {
// Create a stale file in the repo directory to test cleaning.
staleFile := filepath.Join(repoDir, "a/path/stale.txt")
if err := os.MkdirAll(filepath.Dir(staleFile), 0755); err != nil {
t.Fatal(err)
}
if _, err := os.Create(staleFile); err != nil {
t.Fatal(err)
}

// Create generated files in the output directory.
filesToCreate := []string{
"a/path/new_generated_file_to_copy.txt",
"skipped/path/example.txt",
}
for _, relPath := range filesToCreate {
fullPath := filepath.Join(outputDir, relPath)
if err := os.MkdirAll(filepath.Dir(fullPath), 0755); err != nil {
t.Fatal(err)
}
if _, err := os.Create(fullPath); err != nil {
t.Fatal(err)
}
}
},
shouldCopy: []string{
"a/path/new_generated_file_to_copy.txt",
},
shouldDelete: []string{
"skipped/path/example.txt",
"a/path/stale.txt",
},
},
} {
t.Run(test.name, func(t *testing.T) {
repoDir := test.repo.GetDir()
outputDir := t.TempDir()
if test.setup != nil {
test.setup(t, outputDir)
test.setup(t, repoDir, outputDir)
}
err := cleanAndCopyLibrary(test.state, test.repo.GetDir(), test.libraryID, outputDir)
err := cleanAndCopyLibrary(test.state, repoDir, test.libraryID, outputDir)
if test.wantErr {
if err == nil {
t.Errorf("%s should return error", test.name)
Expand All @@ -359,6 +407,20 @@ func TestCleanAndCopyLibrary(t *testing.T) {
if err != nil {
t.Fatal(err)
}

for _, file := range test.shouldCopy {
fullPath := filepath.Join(repoDir, file)
if _, err := os.Stat(fullPath); err != nil {
t.Errorf("file %s is not copied to %s", file, repoDir)
}
}

for _, file := range test.shouldDelete {
fullPath := filepath.Join(repoDir, file)
if _, err := os.Stat(fullPath); !os.IsNotExist(err) {
t.Errorf("file %s should not be copied to %s", file, repoDir)
}
}
})
}
}
Expand Down