Skip to content
19 changes: 19 additions & 0 deletions internal/librarian/java/postprocess.go
Original file line number Diff line number Diff line change
Expand Up @@ -535,3 +535,22 @@ func createOrVerifyOwlbotPy(outDir string) (err error) {
}
return nil
}

// ApplyMoveActionsToLibrary moves generated code to the repository directory
// structure, merging directories and preserving files matching the keepSet.
func ApplyMoveActionsToLibrary(actions []moveAction, destRoot string, keepSet map[string]bool) error {
for _, action := range actions {
if _, err := os.Stat(action.src); err == nil {
if err := os.MkdirAll(action.dest, 0755); err != nil {
return fmt.Errorf("failed to create directory %s: %w", action.dest, err)
}
err := filesystem.MoveAndMergeWithKeep(action.src, action.dest, destRoot, func(rel string) bool {
return shouldPreserve(rel, keepSet)
})
if err != nil {
return fmt.Errorf("failed to move %s: %w", action.description, err)
}
}
Comment thread
yangyzs marked this conversation as resolved.
Resolved
}
return nil
}
128 changes: 128 additions & 0 deletions internal/librarian/java/postprocess_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"os"
"path/filepath"
"strings"
"syscall"
"time"
Comment thread
yangyzs marked this conversation as resolved.
Resolved

"testing"
Expand Down Expand Up @@ -1139,3 +1140,130 @@ func TestCreateOrVerifyOwlbotPy_Error(t *testing.T) {
t.Errorf("error = %v, wantErr %v", err, fs.ErrPermission)
}
}

func TestApplyMoveActionsToLibrary(t *testing.T) {
t.Parallel()
for _, test := range []struct {
name string
srcFiles map[string]string
destFiles map[string]string
keepSet map[string]bool
wantFiles map[string]string
}{
{
name: "new file",
srcFiles: map[string]string{"file.txt": "content"},
keepSet: nil,
wantFiles: map[string]string{"file.txt": "content"},
},
{
name: "overwrite",
srcFiles: map[string]string{"file.txt": "new content"},
destFiles: map[string]string{"file.txt": "old content"},
keepSet: nil,
wantFiles: map[string]string{"file.txt": "new content"},
},
{
name: "keepset preserve",
srcFiles: map[string]string{"file.txt": "new content"},
destFiles: map[string]string{"file.txt": "old content"},
keepSet: map[string]bool{"file.txt": true},
wantFiles: map[string]string{"file.txt": "old content"},
},
} {
t.Run(test.name, func(t *testing.T) {
t.Parallel()
dir := t.TempDir()
srcDir := filepath.Join(dir, "src")
destDir := filepath.Join(dir, "dest")
writeFiles(t, srcDir, test.srcFiles)
writeFiles(t, destDir, test.destFiles)
actions := []moveAction{
{src: srcDir, dest: destDir, description: "test files"},
}
if err := ApplyMoveActionsToLibrary(actions, destDir, test.keepSet); err != nil {
t.Fatal(err)
}
got := readDirFiles(t, destDir)
if diff := cmp.Diff(test.wantFiles, got); diff != "" {
t.Errorf("mismatch (-want +got):\n%s", diff)
}
})
}
}

func TestApplyMoveActionsToLibrary_NonExistentSource(t *testing.T) {
Comment thread
yangyzs marked this conversation as resolved.
Resolved
t.Parallel()
dir := t.TempDir()
srcDir := filepath.Join(dir, "src")
destDir := filepath.Join(dir, "dest")
// srcDir is not created on disk to test that non-existent source directories are skipped.
writeFiles(t, destDir, nil)
actions := []moveAction{
{src: srcDir, dest: destDir, description: "non-existent src"},
}
if err := ApplyMoveActionsToLibrary(actions, destDir, nil); err != nil {
t.Fatal(err)
}
got := readDirFiles(t, destDir)
want := map[string]string{}
if diff := cmp.Diff(want, got); diff != "" {
t.Errorf("mismatch (-want +got):\n%s", diff)
}
}

func TestApplyMoveActionsToLibrary_Error(t *testing.T) {
t.Parallel()
dir := t.TempDir()
srcDir := filepath.Join(dir, "src")
destDir := filepath.Join(dir, "dest")
writeFiles(t, srcDir, map[string]string{"mismatch/file.txt": "content"})
writeFiles(t, destDir, map[string]string{"mismatch": "content"})
actions := []moveAction{
{src: srcDir, dest: destDir, description: "test files"},
}
err := ApplyMoveActionsToLibrary(actions, destDir, nil)
if !errors.Is(err, syscall.ENOTDIR) {
t.Errorf("ApplyMoveActionsToLibrary() error = %v, wantErr %v", err, syscall.ENOTDIR)
}
Comment thread
yangyzs marked this conversation as resolved.
Resolved
}

func writeFiles(t *testing.T, dir string, files map[string]string) {
t.Helper()
if err := os.MkdirAll(dir, 0755); err != nil {
t.Fatal(err)
}
for rel, content := range files {
p := filepath.Join(dir, rel)
if err := os.MkdirAll(filepath.Dir(p), 0755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(p, []byte(content), 0644); err != nil {
t.Fatal(err)
}
}
}

func readDirFiles(t *testing.T, dir string) map[string]string {
t.Helper()
got := make(map[string]string)
err := filepath.WalkDir(dir, func(path string, d os.DirEntry, err error) error {
if err != nil || d.IsDir() {
Comment thread
yangyzs marked this conversation as resolved.
Resolved
return err
}
rel, err := filepath.Rel(dir, path)
if err != nil {
t.Fatal(err)
}
b, err := os.ReadFile(path)
if err != nil {
t.Fatal(err)
}
got[filepath.ToSlash(rel)] = string(b)
return nil
})
if err != nil {
t.Fatal(err)
}
return got
}
Loading