From c3ab9763f04ec595ff8d127d0ce7e42ee4b908f7 Mon Sep 17 00:00:00 2001 From: Sophia Yang <171981480+yangyzs@users.noreply.github.com> Date: Thu, 9 Jul 2026 16:26:05 -0400 Subject: [PATCH 1/5] feat(internal/librarian/java): add ApplyMoveActionsToLibrary helper and unit tests Add ApplyMoveActionsToLibrary helper function to relocate generated files from temporary source paths to target library directories while applying file preservation logic. For #6516 --- internal/librarian/java/postprocess.go | 19 +++ internal/librarian/java/postprocess_test.go | 128 ++++++++++++++++++++ 2 files changed, 147 insertions(+) diff --git a/internal/librarian/java/postprocess.go b/internal/librarian/java/postprocess.go index de2cecdb18d..388cd6128d4 100644 --- a/internal/librarian/java/postprocess.go +++ b/internal/librarian/java/postprocess.go @@ -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) + } + } + } + return nil +} diff --git a/internal/librarian/java/postprocess_test.go b/internal/librarian/java/postprocess_test.go index 5728419efa5..23ce7595554 100644 --- a/internal/librarian/java/postprocess_test.go +++ b/internal/librarian/java/postprocess_test.go @@ -23,6 +23,7 @@ import ( "os" "path/filepath" "strings" + "syscall" "time" "testing" @@ -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) { + 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) + } +} + +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() { + 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 +} From a81a6a2e6b487d2584575a2b4a8aaea23435623e Mon Sep 17 00:00:00 2001 From: Sophia Yang <171981480+yangyzs@users.noreply.github.com> Date: Thu, 9 Jul 2026 21:45:37 -0400 Subject: [PATCH 2/5] fix(internal/librarian/java): propagate unexpected stat errors in ApplyMoveActionsToLibrary Update ApplyMoveActionsToLibrary to check for and propagate unexpected errors from os.Stat (like permission errors) instead of silently skipping them. Include a combined table-driven unit test validating this and other error scenarios. For #6516 --- internal/librarian/java/postprocess.go | 22 +++++---- internal/librarian/java/postprocess_test.go | 52 ++++++++++++++++----- 2 files changed, 54 insertions(+), 20 deletions(-) diff --git a/internal/librarian/java/postprocess.go b/internal/librarian/java/postprocess.go index 388cd6128d4..587d6e519ee 100644 --- a/internal/librarian/java/postprocess.go +++ b/internal/librarian/java/postprocess.go @@ -540,16 +540,20 @@ func createOrVerifyOwlbotPy(outDir string) (err error) { // 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) + if _, err := os.Stat(action.src); err != nil { + if errors.Is(err, fs.ErrNotExist) { + continue } + return fmt.Errorf("failed to check source directory %s: %w", action.src, err) + } + 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) } } return nil diff --git a/internal/librarian/java/postprocess_test.go b/internal/librarian/java/postprocess_test.go index 23ce7595554..bc4182dc2b4 100644 --- a/internal/librarian/java/postprocess_test.go +++ b/internal/librarian/java/postprocess_test.go @@ -1214,17 +1214,47 @@ func TestApplyMoveActionsToLibrary_NonExistentSource(t *testing.T) { 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) + for _, test := range []struct { + name string + files map[string]string + destFiles map[string]string + readOnlySrcParent bool + wantErr error + }{ + { + name: "directory to file type mismatch", + files: map[string]string{"mismatch/file.txt": "content"}, + destFiles: map[string]string{"mismatch": "content"}, + wantErr: syscall.ENOTDIR, + }, + { + name: "source parent directory inaccessible", + readOnlySrcParent: true, + wantErr: fs.ErrPermission, + }, + } { + t.Run(test.name, func(t *testing.T) { + t.Parallel() + dir := t.TempDir() + srcParent := filepath.Join(dir, "src_parent") + srcDir := filepath.Join(srcParent, "src") + destDir := filepath.Join(dir, "dest") + writeFiles(t, srcDir, test.files) + writeFiles(t, destDir, test.destFiles) + if test.readOnlySrcParent { + if err := os.Chmod(srcParent, 0000); err != nil { + t.Fatal(err) + } + defer os.Chmod(srcParent, 0755) + } + actions := []moveAction{ + {src: srcDir, dest: destDir, description: "test files"}, + } + err := ApplyMoveActionsToLibrary(actions, destDir, nil) + if !errors.Is(err, test.wantErr) { + t.Errorf("ApplyMoveActionsToLibrary() error = %v, wantErr %v", err, test.wantErr) + } + }) } } From 8d9678d15177a0f56b6c5c2bab98d747d223e05a Mon Sep 17 00:00:00 2001 From: Sophia Yang <171981480+yangyzs@users.noreply.github.com> Date: Thu, 9 Jul 2026 22:43:36 -0400 Subject: [PATCH 3/5] fix(internal/librarian/java): fail on missing source dirs in ApplyMoveActionsToLibrary Update ApplyMoveActionsToLibrary to return errors for missing source directories (fs.ErrNotExist) and consolidate error assertions under table-driven tests. --- internal/librarian/java/postprocess.go | 3 -- internal/librarian/java/postprocess_test.go | 35 ++++++++------------- 2 files changed, 13 insertions(+), 25 deletions(-) diff --git a/internal/librarian/java/postprocess.go b/internal/librarian/java/postprocess.go index 587d6e519ee..0a6e503c39e 100644 --- a/internal/librarian/java/postprocess.go +++ b/internal/librarian/java/postprocess.go @@ -541,9 +541,6 @@ func createOrVerifyOwlbotPy(outDir string) (err error) { func ApplyMoveActionsToLibrary(actions []moveAction, destRoot string, keepSet map[string]bool) error { for _, action := range actions { if _, err := os.Stat(action.src); err != nil { - if errors.Is(err, fs.ErrNotExist) { - continue - } return fmt.Errorf("failed to check source directory %s: %w", action.src, err) } if err := os.MkdirAll(action.dest, 0755); err != nil { diff --git a/internal/librarian/java/postprocess_test.go b/internal/librarian/java/postprocess_test.go index bc4182dc2b4..d85d6a78659 100644 --- a/internal/librarian/java/postprocess_test.go +++ b/internal/librarian/java/postprocess_test.go @@ -1192,26 +1192,6 @@ func TestApplyMoveActionsToLibrary(t *testing.T) { } } -func TestApplyMoveActionsToLibrary_NonExistentSource(t *testing.T) { - 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() for _, test := range []struct { @@ -1219,6 +1199,7 @@ func TestApplyMoveActionsToLibrary_Error(t *testing.T) { files map[string]string destFiles map[string]string readOnlySrcParent bool + missingSrc bool wantErr error }{ { @@ -1232,6 +1213,11 @@ func TestApplyMoveActionsToLibrary_Error(t *testing.T) { readOnlySrcParent: true, wantErr: fs.ErrPermission, }, + { + name: "source directory does not exist", + missingSrc: true, + wantErr: fs.ErrNotExist, + }, } { t.Run(test.name, func(t *testing.T) { t.Parallel() @@ -1239,7 +1225,9 @@ func TestApplyMoveActionsToLibrary_Error(t *testing.T) { srcParent := filepath.Join(dir, "src_parent") srcDir := filepath.Join(srcParent, "src") destDir := filepath.Join(dir, "dest") - writeFiles(t, srcDir, test.files) + if !test.missingSrc { + writeFiles(t, srcDir, test.files) + } writeFiles(t, destDir, test.destFiles) if test.readOnlySrcParent { if err := os.Chmod(srcParent, 0000); err != nil { @@ -1278,9 +1266,12 @@ 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() { + if err != nil { return err } + if d.IsDir() { + return nil + } rel, err := filepath.Rel(dir, path) if err != nil { t.Fatal(err) From 996411ae77bef6ac535cace338875cac97879d96 Mon Sep 17 00:00:00 2001 From: Sophia Yang <171981480+yangyzs@users.noreply.github.com> Date: Fri, 10 Jul 2026 14:47:44 -0400 Subject: [PATCH 4/5] fix(internal/librarian/java): revert strict check and silently skip missing source directories for now Return to the legacy behavior of silently skipping non-existent source directories in ApplyMoveActionsToLibrary. This allows unit tests (like TestGenerateAPI_NoTools) to run without mocking directory structures on disk until owlbot.py is removed. Refs: #6752 --- internal/librarian/java/postprocess.go | 4 ++++ internal/librarian/java/postprocess_test.go | 25 ++++++++++++++++----- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/internal/librarian/java/postprocess.go b/internal/librarian/java/postprocess.go index 4cd6a26ffb8..20c656a23c9 100644 --- a/internal/librarian/java/postprocess.go +++ b/internal/librarian/java/postprocess.go @@ -541,6 +541,10 @@ func createOrVerifyOwlbotPy(outDir string) (err error) { func ApplyMoveActionsToLibrary(actions []moveAction, destRoot string, keepSet map[string]bool) error { for _, action := range actions { if _, err := os.Stat(action.src); err != nil { + if errors.Is(err, fs.ErrNotExist) { + // TODO(https://github.com/googleapis/librarian/issues/6752): Return an error here once owlbot.py is removed. + continue + } return fmt.Errorf("failed to check source directory %s: %w", action.src, err) } if err := os.MkdirAll(action.dest, 0755); err != nil { diff --git a/internal/librarian/java/postprocess_test.go b/internal/librarian/java/postprocess_test.go index d0f5379cc86..53b75d42be9 100644 --- a/internal/librarian/java/postprocess_test.go +++ b/internal/librarian/java/postprocess_test.go @@ -1213,11 +1213,6 @@ func TestApplyMoveActionsToLibrary_Error(t *testing.T) { readOnlySrcParent: true, wantErr: fs.ErrPermission, }, - { - name: "source directory does not exist", - missingSrc: true, - wantErr: fs.ErrNotExist, - }, } { t.Run(test.name, func(t *testing.T) { t.Parallel() @@ -1246,6 +1241,26 @@ func TestApplyMoveActionsToLibrary_Error(t *testing.T) { } } + +func TestApplyMoveActionsToLibrary_NonExistentSource(t *testing.T) { + t.Parallel() + dir := t.TempDir() + srcDir := filepath.Join(dir, "src") + destDir := filepath.Join(dir, "dest") + 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 writeFiles(t *testing.T, dir string, files map[string]string) { t.Helper() if err := os.MkdirAll(dir, 0755); err != nil { From ae0a8be11c67c4bc875b5e7cc42cb249df9459f2 Mon Sep 17 00:00:00 2001 From: Sophia Yang <171981480+yangyzs@users.noreply.github.com> Date: Fri, 10 Jul 2026 15:00:21 -0400 Subject: [PATCH 5/5] fix(internal/librarian/java): address goimports formatting linter failure --- internal/librarian/java/postprocess_test.go | 1 - 1 file changed, 1 deletion(-) diff --git a/internal/librarian/java/postprocess_test.go b/internal/librarian/java/postprocess_test.go index 53b75d42be9..0d24208e34b 100644 --- a/internal/librarian/java/postprocess_test.go +++ b/internal/librarian/java/postprocess_test.go @@ -1241,7 +1241,6 @@ func TestApplyMoveActionsToLibrary_Error(t *testing.T) { } } - func TestApplyMoveActionsToLibrary_NonExistentSource(t *testing.T) { t.Parallel() dir := t.TempDir()