@@ -24,6 +24,8 @@ import (
2424 "path/filepath"
2525 "strings"
2626 "testing"
27+
28+ "cloud.google.com/java/internal/librariangen/protoc"
2729)
2830
2931// testEnv encapsulates a temporary test environment.
@@ -110,40 +112,15 @@ func createFakeZip(t *testing.T, path string) {
110112 zipWriter := zip .NewWriter (newZipFile )
111113 defer zipWriter .Close ()
112114
113- // Create a temporary empty zip file to be included in the main zip file.
114- tmpfile , err := os .CreateTemp ("" , "temp-zip-*.zip" )
115- if err != nil {
116- t .Fatalf ("failed to create temp file: %v" , err )
117- }
118- defer os .Remove (tmpfile .Name ())
119-
120- tempZipWriter := zip .NewWriter (tmpfile )
121- // Add the src/main/java directory to the inner zip file.
122- _ , err = tempZipWriter .Create ("src/main/java/" )
115+ // Add the src/main/java directory to the zip file.
116+ _ , err = zipWriter .Create ("src/main/java/" )
123117 if err != nil {
124118 t .Fatalf ("failed to create directory in zip: %v" , err )
125119 }
126- _ , err = tempZipWriter .Create ("src/test/java/" )
120+ _ , err = zipWriter .Create ("src/test/java/" )
127121 if err != nil {
128122 t .Fatalf ("failed to create directory in zip: %v" , err )
129123 }
130- tempZipWriter .Close ()
131-
132- // Read the content of the temporary zip file.
133- zipBytes , err := os .ReadFile (tmpfile .Name ())
134- if err != nil {
135- t .Fatalf ("failed to read temp zip file: %v" , err )
136- }
137-
138- // Add the temporary zip file to the main zip file as temp-codegen.srcjar.
139- w , err := zipWriter .Create ("temp-codegen.srcjar" )
140- if err != nil {
141- t .Fatalf ("failed to create empty file in zip: %v" , err )
142- }
143- _ , err = w .Write (zipBytes )
144- if err != nil {
145- t .Fatalf ("failed to write content to zip: %v" , err )
146- }
147124}
148125
149126func TestGenerate (t * testing.T ) {
@@ -255,12 +232,19 @@ java_gapic_library(
255232 }
256233 if tt .protocErr == nil && tt .name != "unzip fails" {
257234 // Simulate protoc creating the zip file.
258- createFakeZip (t , filepath .Join (e .outputDir , "java_gapic.zip" ))
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 )
259240 // Create the directory that is expected by restructureOutput.
260- if err := os .MkdirAll (filepath .Join (e .outputDir , "com" ), 0755 ); err != nil {
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 {
261245 t .Fatalf ("failed to create directory: %v" , err )
262246 }
263- if err := os .MkdirAll (filepath .Join (e .outputDir , "java_gapic_srcjar " , "samples" , "snippets" ), 0755 ); err != nil {
247+ if err := os .MkdirAll (filepath .Join (e .outputDir , "gapic " , "samples" , "snippets" ), 0755 ); err != nil {
264248 t .Fatalf ("failed to create directory: %v" , err )
265249 }
266250 }
@@ -348,48 +332,45 @@ func TestConfig_Validate(t *testing.T) {
348332func TestRestructureOutput (t * testing.T ) {
349333 e := newTestEnv (t )
350334 defer e .cleanup (t )
351- // Create dummy files and directories to be restructured.
352- if err := os .MkdirAll (filepath .Join (e .outputDir , "java_gapic_srcjar" , "src" , "main" , "java" , "com" ), 0755 ); err != nil {
353- t .Fatal (err )
354- }
355- if err := os .WriteFile (filepath .Join (e .outputDir , "java_gapic_srcjar" , "src" , "main" , "java" , "com" , "foo.java" ), nil , 0644 ); err != nil {
356- t .Fatal (err )
357- }
358- if err := os .MkdirAll (filepath .Join (e .outputDir , "java_gapic_srcjar" , "src" , "test" , "java" , "com" ), 0755 ); err != nil {
359- t .Fatal (err )
360- }
361- if err := os .WriteFile (filepath .Join (e .outputDir , "java_gapic_srcjar" , "src" , "test" , "java" , "com" , "foo_test.java" ), nil , 0644 ); err != nil {
362- t .Fatal (err )
363- }
364- if err := os .MkdirAll (filepath .Join (e .outputDir , "com" ), 0755 ); err != nil {
365- t .Fatal (err )
366- }
367- if err := os .WriteFile (filepath .Join (e .outputDir , "com" , "bar.proto" ), nil , 0644 ); err != nil {
368- t .Fatal (err )
369- }
370- if err := os .MkdirAll (filepath .Join (e .outputDir , "java_gapic_srcjar" , "samples" , "snippets" , "com" ), 0755 ); err != nil {
371- t .Fatal (err )
372- }
373- if err := os .WriteFile (filepath .Join (e .outputDir , "java_gapic_srcjar" , "samples" , "snippets" , "com" , "baz.java" ), nil , 0644 ); err != nil {
374- t .Fatal (err )
335+
336+ // 1. Setup: Create all the source directories and dummy files.
337+ sourceFiles := map [string ]string {
338+ "gapic/src/main/java/com/google/foo.java" : "" ,
339+ "gapic/src/test/java/com/google/foo_test.java" : "" ,
340+ "proto/com/google/bar.proto" : "" ,
341+ "grpc/com/google/bar_grpc.java" : "" ,
342+ "gapic/samples/snippets/com/google/baz.java" : "" ,
343+ "gapic/proto/src/main/java/com/google/resname.java" : "" ,
344+ }
345+ for path , content := range sourceFiles {
346+ fullPath := filepath .Join (e .outputDir , path )
347+ if err := os .MkdirAll (filepath .Dir (fullPath ), 0755 ); err != nil {
348+ t .Fatalf ("failed to create source directory for %s: %v" , path , err )
349+ }
350+ if err := os .WriteFile (fullPath , []byte (content ), 0644 ); err != nil {
351+ t .Fatalf ("failed to write source file for %s: %v" , path , err )
352+ }
375353 }
376354
355+ // 2. Execute: Call the function under test.
377356 if err := restructureOutput (e .outputDir , "my-library" ); err != nil {
378357 t .Fatalf ("restructureOutput() failed: %v" , err )
379358 }
380359
381- // Check that the files were moved to the correct locations.
382- if _ , err := os .Stat (filepath .Join (e .outputDir , "google-cloud-my-library" , "src" , "main" , "java" , "com" , "foo.java" )); err != nil {
383- t .Errorf ("file not moved to main: %v" , err )
384- }
385- if _ , err := os .Stat (filepath .Join (e .outputDir , "google-cloud-my-library" , "src" , "test" , "java" , "com" , "foo_test.java" )); err != nil {
386- t .Errorf ("file not moved to test: %v" , err )
360+ // 3. Verify: Check that all files were moved to their expected destinations.
361+ expectedFiles := []string {
362+ "google-cloud-my-library/src/main/java/com/google/foo.java" ,
363+ "google-cloud-my-library/src/test/java/com/google/foo_test.java" ,
364+ "proto-google-cloud-my-library-v1/src/main/java/com/google/bar.proto" ,
365+ "grpc-google-cloud-my-library-v1/src/main/java/com/google/bar_grpc.java" ,
366+ "samples/snippets/com/google/baz.java" ,
367+ "proto-google-cloud-my-library-v1/src/main/java/com/google/resname.java" ,
387368 }
388- if _ , err := os . Stat ( filepath . Join ( e . outputDir , "proto-google-cloud-my-library-v1" , "src" , "main" , "java" , "bar.proto" )); err != nil {
389- t . Errorf ( "file not moved to proto: %v" , err )
390- }
391- if _ , err := os . Stat ( filepath . Join ( e . outputDir , "samples" , "snippets" , "com" , "baz.java" )); err != nil {
392- t . Errorf ( "file not moved to samples: %v" , err )
369+ for _ , path := range expectedFiles {
370+ fullPath := filepath . Join ( e . outputDir , path )
371+ if _ , err := os . Stat ( fullPath ); err != nil {
372+ t . Errorf ( "expected file not found at %s: %v" , fullPath , err )
373+ }
393374 }
394375}
395376
@@ -502,7 +483,7 @@ func TestCleanupIntermediateFiles(t *testing.T) {
502483 defer e .cleanup (t )
503484
504485 // Create a file that cannot be deleted.
505- protectedDir := filepath .Join (e .outputDir , "com " )
486+ protectedDir := filepath .Join (e .outputDir , "proto " )
506487 if err := os .Mkdir (protectedDir , 0755 ); err != nil {
507488 t .Fatalf ("failed to create protected dir: %v" , err )
508489 }
@@ -515,7 +496,12 @@ func TestCleanupIntermediateFiles(t *testing.T) {
515496 }
516497 defer os .Chmod (protectedDir , 0755 ) // Restore permissions for cleanup.
517498
518- cleanupIntermediateFiles (e .outputDir )
499+ outputConfig := & protoc.OutputConfig {
500+ GAPICDir : filepath .Join (e .outputDir , "gapic" ),
501+ GRPCDir : filepath .Join (e .outputDir , "grpc" ),
502+ ProtoDir : protectedDir ,
503+ }
504+ cleanupIntermediateFiles (outputConfig )
519505
520506 if ! strings .Contains (buf .String (), "failed to clean up intermediate file" ) {
521507 t .Errorf ("cleanupIntermediateFiles() should log an error on failure, but did not. Log: %s" , buf .String ())
0 commit comments