@@ -16,13 +16,10 @@ package generate
1616
1717import (
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+
126142func 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
564565func 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}
0 commit comments