Skip to content

Commit 0d3b7eb

Browse files
authored
fix: do not delete any files in .librarian/generator-input directory (#1934)
In case the remove-regex contains a directory that exists in .librarian/generator-input, the librarian CLI should not delete it when calling clean. fixes: #1925
1 parent de1fc01 commit 0d3b7eb

File tree

2 files changed

+113
-1
lines changed

2 files changed

+113
-1
lines changed

internal/librarian/command.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ const (
4141
release = "release"
4242
)
4343

44+
var globalPreservePatterns = []string{
45+
fmt.Sprintf(`^%s(/.*)?$`, regexp.QuoteMeta(config.GeneratorInputDir)), // Preserve the generator-input directory and its contents.
46+
}
47+
4448
// GitHubClientFactory type for creating a GitHubClient.
4549
type GitHubClientFactory func(token string, repo *github.Repository) (GitHubClient, error)
4650

@@ -254,7 +258,10 @@ func cleanAndCopyLibrary(state *config.LibrarianState, repoDir, libraryID, outpu
254258
}
255259
}
256260

257-
if err := clean(repoDir, removePatterns, library.PreserveRegex); err != nil {
261+
preservePatterns := append(library.PreserveRegex,
262+
globalPreservePatterns...)
263+
264+
if err := clean(repoDir, removePatterns, preservePatterns); err != nil {
258265
return fmt.Errorf("failed to clean library, %s: %w", library.ID, err)
259266
}
260267

internal/librarian/command_test.go

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,111 @@ func TestCleanAndCopyLibrary(t *testing.T) {
388388
"a/path/stale.txt",
389389
},
390390
},
391+
{
392+
name: "clean never deletes generator-input directory",
393+
libraryID: "some-library",
394+
state: &config.LibrarianState{
395+
Libraries: []*config.LibraryState{
396+
{
397+
ID: "some-library",
398+
SourceRoots: []string{"a/path"},
399+
RemoveRegex: []string{"a/path"},
400+
},
401+
},
402+
},
403+
repo: newTestGitRepo(t),
404+
setup: func(t *testing.T, repoDir, outputDir string) {
405+
// Create a file in the repo directory to test cleaning.
406+
fileToBeDeleted := filepath.Join(repoDir, "a/path/delete.txt")
407+
fileToNotBeDeleted := filepath.Join(repoDir, ".librarian/generator-input/a/path/do-not-delete.txt")
408+
if err := os.MkdirAll(filepath.Dir(fileToBeDeleted), 0755); err != nil {
409+
t.Fatal(err)
410+
}
411+
if _, err := os.Create(fileToBeDeleted); err != nil {
412+
t.Fatal(err)
413+
}
414+
if err := os.MkdirAll(filepath.Dir(fileToNotBeDeleted), 0755); err != nil {
415+
t.Fatal(err)
416+
}
417+
if _, err := os.Create(fileToNotBeDeleted); err != nil {
418+
t.Fatal(err)
419+
}
420+
421+
// Create generated files in the output directory.
422+
filesToCreate := []string{
423+
"a/path/new_generated_file_to_copy.txt",
424+
}
425+
for _, relPath := range filesToCreate {
426+
fullPath := filepath.Join(outputDir, relPath)
427+
if err := os.MkdirAll(filepath.Dir(fullPath), 0755); err != nil {
428+
t.Fatal(err)
429+
}
430+
if _, err := os.Create(fullPath); err != nil {
431+
t.Fatal(err)
432+
}
433+
}
434+
},
435+
shouldCopy: []string{
436+
".librarian/generator-input/a/path/do-not-delete.txt",
437+
"a/path/new_generated_file_to_copy.txt",
438+
},
439+
shouldDelete: []string{
440+
"a/path/delete.txt",
441+
},
442+
},
443+
{
444+
name: "if same value is present in preserve regex and global preserver regex list there is no conflict",
445+
libraryID: "some-library",
446+
state: &config.LibrarianState{
447+
Libraries: []*config.LibraryState{
448+
{
449+
ID: "some-library",
450+
SourceRoots: []string{"a/path"},
451+
RemoveRegex: []string{"a/path"},
452+
PreserveRegex: globalPreservePatterns,
453+
},
454+
},
455+
},
456+
repo: newTestGitRepo(t),
457+
setup: func(t *testing.T, repoDir, outputDir string) {
458+
// Create a file in the repo directory to test cleaning.
459+
fileToBeDeleted := filepath.Join(repoDir, "a/path/delete.txt")
460+
fileToNotBeDeleted := filepath.Join(repoDir, ".librarian/generator-input/a/path/do-not-delete.txt")
461+
if err := os.MkdirAll(filepath.Dir(fileToBeDeleted), 0755); err != nil {
462+
t.Fatal(err)
463+
}
464+
if _, err := os.Create(fileToBeDeleted); err != nil {
465+
t.Fatal(err)
466+
}
467+
if err := os.MkdirAll(filepath.Dir(fileToNotBeDeleted), 0755); err != nil {
468+
t.Fatal(err)
469+
}
470+
if _, err := os.Create(fileToNotBeDeleted); err != nil {
471+
t.Fatal(err)
472+
}
473+
474+
// Create generated files in the output directory.
475+
filesToCreate := []string{
476+
"a/path/new_generated_file_to_copy.txt",
477+
}
478+
for _, relPath := range filesToCreate {
479+
fullPath := filepath.Join(outputDir, relPath)
480+
if err := os.MkdirAll(filepath.Dir(fullPath), 0755); err != nil {
481+
t.Fatal(err)
482+
}
483+
if _, err := os.Create(fullPath); err != nil {
484+
t.Fatal(err)
485+
}
486+
}
487+
},
488+
shouldCopy: []string{
489+
".librarian/generator-input/a/path/do-not-delete.txt",
490+
"a/path/new_generated_file_to_copy.txt",
491+
},
492+
shouldDelete: []string{
493+
"a/path/delete.txt",
494+
},
495+
},
391496
} {
392497
t.Run(test.name, func(t *testing.T) {
393498
repoDir := test.repo.GetDir()

0 commit comments

Comments
 (0)