Skip to content

Commit 10e2c5d

Browse files
committed
chore: address feedback
1 parent dd12481 commit 10e2c5d

File tree

9 files changed

+49
-47
lines changed

9 files changed

+49
-47
lines changed

internal/librariangen/bazel/parser.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ func (c *Config) Validate() error {
6565
}
6666

6767
var javaGapicLibraryRE = regexp.MustCompile(`java_gapic_library\((?s:.)*?\)`)
68+
6869
// Parse reads a BUILD.bazel file from the given directory and extracts the
6970
// relevant configuration from the java_gapic_library rule.
7071
func Parse(dir string) (*Config, error) {

internal/librariangen/bazel/parser_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,4 +256,4 @@ func TestParse_noBuildFile(t *testing.T) {
256256
if err == nil {
257257
t.Error("Parse() succeeded; want error")
258258
}
259-
}
259+
}

internal/librariangen/execv/execv.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,4 @@ func Run(ctx context.Context, args []string, workingDir string) error {
4848
return fmt.Errorf("librariangen: command failed: %w", err)
4949
}
5050
return nil
51-
}
51+
}

internal/librariangen/execv/execv_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,4 @@ func TestRun(t *testing.T) {
7676
}
7777
})
7878
}
79-
}
79+
}

internal/librariangen/generate/generator.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,11 @@ func Generate(ctx context.Context, cfg *Config) error {
8383
GRPCDir: filepath.Join(cfg.OutputDir, "grpc"),
8484
ProtoDir: filepath.Join(cfg.OutputDir, "proto"),
8585
}
86-
defer cleanupIntermediateFiles(outputConfig)
86+
defer func() {
87+
if err := cleanupIntermediateFiles(outputConfig); err != nil {
88+
slog.Error("librariangen: failed to clean up intermediate files", "error", err)
89+
}
90+
}()
8791

8892
generateReq, err := readGenerateReq(cfg.LibrarianDir)
8993
if err != nil {
@@ -133,7 +137,7 @@ func invokeProtoc(ctx context.Context, cfg *Config, generateReq *message.Library
133137
}
134138

135139
if err := execvRun(ctx, args, cfg.OutputDir); err != nil {
136-
return fmt.Errorf("librariangen: protoc failed for api %q in library %q: %w", api.Path, generateReq.ID, err)
140+
return fmt.Errorf("librariangen: protoc failed for api %q in library %q: %w, execvRun error: %v", api.Path, generateReq.ID, err, err)
137141
}
138142
}
139143
return nil
@@ -165,7 +169,7 @@ func moveFiles(sourceDir, targetDir string) error {
165169
newPath := filepath.Join(targetDir, f.Name())
166170
slog.Debug("librariangen: moving file", "from", oldPath, "to", newPath)
167171
if err := os.Rename(oldPath, newPath); err != nil {
168-
return fmt.Errorf("librariangen: failed to move %s to %s: %w", oldPath, newPath, err)
172+
return fmt.Errorf("librariangen: failed to move %s to %s: %w, os.Rename error: %v", oldPath, newPath, err, err)
169173
}
170174
}
171175
return nil
@@ -211,7 +215,7 @@ func restructureOutput(outputDir, libraryID string) error {
211215
gapicSrcDir: gapicDestDir,
212216
gapicTestDir: gapicTestDestDir,
213217
protoSrcDir: protoDestDir,
214-
grpcSrcDir: grpcDestDir,
218+
grpcSrcDir: grpcDestDir,
215219
samplesDir: samplesDestDir,
216220
}
217221
for src, dest := range moves {
@@ -250,20 +254,21 @@ func copyAndMerge(src, dest string) error {
250254
}
251255
} else {
252256
if err := os.Rename(srcPath, destPath); err != nil {
253-
return fmt.Errorf("librariangen: failed to move %s to %s: %w", srcPath, destPath, err)
257+
return fmt.Errorf("librariangen: failed to move %s to %s: %w, os.Rename error: %v", srcPath, destPath, err, err)
254258
}
255259
}
256260
}
257261
return nil
258262
}
259263

260-
func cleanupIntermediateFiles(outputConfig *protoc.OutputConfig) {
264+
func cleanupIntermediateFiles(outputConfig *protoc.OutputConfig) error {
261265
slog.Debug("librariangen: cleaning up intermediate files")
262266
for _, path := range []string{outputConfig.GAPICDir, outputConfig.GRPCDir, outputConfig.ProtoDir} {
263267
if err := os.RemoveAll(path); err != nil {
264-
slog.Error("librariangen: failed to clean up intermediate file", "path", path, "error", err)
268+
return fmt.Errorf("failed to clean up intermediate file at %s: %w", path, err)
265269
}
266270
}
271+
return nil
267272
}
268273

269274
func unzip(src, dest string) error {

internal/librariangen/generate/generator_test.go

Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,10 @@ package generate
1616

1717
import (
1818
"archive/zip"
19-
"bytes"
2019
"context"
2120
"errors"
22-
"log/slog"
2321
"os"
2422
"path/filepath"
25-
"strings"
2623
"testing"
2724

2825
"cloud.google.com/java/internal/librariangen/protoc"
@@ -123,6 +120,25 @@ func createFakeZip(t *testing.T, path string) {
123120
}
124121
}
125122

123+
func setupFakeProtocOutput(t *testing.T, e *testEnv) {
124+
// Simulate protoc creating the zip file.
125+
zipPath := filepath.Join(e.outputDir, "gapic", "temp-codegen.srcjar")
126+
if err := os.MkdirAll(filepath.Dir(zipPath), 0755); err != nil {
127+
t.Fatalf("failed to create directory: %v", err)
128+
}
129+
createFakeZip(t, zipPath)
130+
// Create the directory that is expected by restructureOutput.
131+
if err := os.MkdirAll(filepath.Join(e.outputDir, "gapic", "src", "main", "java"), 0755); err != nil {
132+
t.Fatalf("failed to create directory: %v", err)
133+
}
134+
if err := os.MkdirAll(filepath.Join(e.outputDir, "gapic", "src", "test", "java"), 0755); err != nil {
135+
t.Fatalf("failed to create directory: %v", err)
136+
}
137+
if err := os.MkdirAll(filepath.Join(e.outputDir, "gapic", "samples", "snippets"), 0755); err != nil {
138+
t.Fatalf("failed to create directory: %v", err)
139+
}
140+
}
141+
126142
func TestGenerate(t *testing.T) {
127143
singleAPIRequest := `{"id": "foo", "apis": [{"path": "api/v1"}]}`
128144
validBazel := `
@@ -231,22 +247,7 @@ java_gapic_library(
231247
t.Errorf("protocRun called with %s; want %s", args[0], want)
232248
}
233249
if tt.protocErr == nil && tt.name != "unzip fails" {
234-
// Simulate protoc creating the zip file.
235-
zipPath := filepath.Join(e.outputDir, "gapic", "temp-codegen.srcjar")
236-
if err := os.MkdirAll(filepath.Dir(zipPath), 0755); err != nil {
237-
t.Fatalf("failed to create directory: %v", err)
238-
}
239-
createFakeZip(t, zipPath)
240-
// Create the directory that is expected by restructureOutput.
241-
if err := os.MkdirAll(filepath.Join(e.outputDir, "gapic", "src", "main", "java"), 0755); err != nil {
242-
t.Fatalf("failed to create directory: %v", err)
243-
}
244-
if err := os.MkdirAll(filepath.Join(e.outputDir, "gapic", "src", "test", "java"), 0755); err != nil {
245-
t.Fatalf("failed to create directory: %v", err)
246-
}
247-
if err := os.MkdirAll(filepath.Join(e.outputDir, "gapic", "samples", "snippets"), 0755); err != nil {
248-
t.Fatalf("failed to create directory: %v", err)
249-
}
250+
setupFakeProtocOutput(t, e)
250251
}
251252
protocRunCount++
252253
return tt.protocErr
@@ -382,12 +383,12 @@ func TestCopyAndMerge(t *testing.T) {
382383
srcDir := filepath.Join(e.tmpDir, "src")
383384
destDir := filepath.Join(e.tmpDir, "dest")
384385
sourceFiles := map[string]string{
385-
"com/google/foo.java": "",
386-
"com/google/bar/baz.java": "",
386+
"com/google/foo.java": "",
387+
"com/google/bar/baz.java": "",
387388
"com/google/bar/qux/quux.java": "",
388389
}
389390
destFiles := map[string]string{
390-
"com/google/existing.java": "",
391+
"com/google/existing.java": "",
391392
"com/google/bar/another.java": "",
392393
}
393394
for path, content := range sourceFiles {
@@ -562,9 +563,6 @@ func TestMoveFiles(t *testing.T) {
562563
}
563564

564565
func TestCleanupIntermediateFiles(t *testing.T) {
565-
var buf bytes.Buffer
566-
slog.SetDefault(slog.New(slog.NewTextHandler(&buf, nil)))
567-
568566
e := newTestEnv(t)
569567
defer e.cleanup(t)
570568

@@ -587,9 +585,7 @@ func TestCleanupIntermediateFiles(t *testing.T) {
587585
GRPCDir: filepath.Join(e.outputDir, "grpc"),
588586
ProtoDir: protectedDir,
589587
}
590-
cleanupIntermediateFiles(outputConfig)
591-
592-
if !strings.Contains(buf.String(), "failed to clean up intermediate file") {
593-
t.Errorf("cleanupIntermediateFiles() should log an error on failure, but did not. Log: %s", buf.String())
588+
if err := cleanupIntermediateFiles(outputConfig); err == nil {
589+
t.Error("cleanupIntermediateFiles() should return an error on failure, but did not")
594590
}
595591
}

internal/librariangen/main_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,4 @@ func TestParseLogLevel(t *testing.T) {
6363
}
6464
})
6565
}
66-
}
66+
}

internal/librariangen/protoc/protoc.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,4 +106,4 @@ func Build(apiServiceDir string, config ConfigProvider, sourceDir string, output
106106
args = append(args, protoFiles...)
107107

108108
return args, nil
109-
}
109+
}

internal/librariangen/protoc/protoc_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,10 @@ func TestBuild(t *testing.T) {
4747
t.Fatalf("failed to get absolute path for sourceDir: %v", err)
4848
}
4949
tests := []struct {
50-
name string
51-
apiPath string
52-
config mockConfigProvider
53-
want []string
50+
name string
51+
apiPath string
52+
config mockConfigProvider
53+
want []string
5454
}{
5555
{
5656
name: "java_grpc_library rule",
@@ -112,7 +112,7 @@ func TestBuild(t *testing.T) {
112112
name: "proto-only",
113113
apiPath: "google/cloud/secretmanager/v1beta2",
114114
config: mockConfigProvider{
115-
hasGAPIC: false,
115+
hasGAPIC: false,
116116
},
117117
want: []string{
118118
"protoc",
@@ -141,4 +141,4 @@ func TestBuild(t *testing.T) {
141141
}
142142
})
143143
}
144-
}
144+
}

0 commit comments

Comments
 (0)