66 "strings"
77 "testing"
88
9+ "github.com/AndroidGoLab/jni/spec"
910 "github.com/AndroidGoLab/jni/tools/pkg/javagen"
1011 "github.com/AndroidGoLab/jni-proxy/tools/pkg/protogen"
1112 "github.com/AndroidGoLab/jni-proxy/tools/pkg/protoscan"
@@ -20,19 +21,9 @@ func emptyGoNames() protoscan.GoNames {
2021}
2122
2223func TestBuildClientData_Location (t * testing.T ) {
23- root := findJNIRepoRoot (t )
24- specPath := filepath .Join (root , "spec" , "java" , "location.yaml" )
25- overlayPath := filepath .Join (root , "spec" , "overlays" , "java" , "location.yaml" )
26-
27- spec , err := javagen .LoadSpec (specPath )
28- if err != nil {
29- t .Fatalf ("load spec: %v" , err )
30- }
31- overlay , err := javagen .LoadOverlay (overlayPath )
32- if err != nil {
33- t .Fatalf ("load overlay: %v" , err )
34- }
35- merged , err := javagen .Merge (spec , overlay )
24+ sp := loadSpec (t , "location.yaml" )
25+ overlay := loadOverlay (t , "location.yaml" )
26+ merged , err := javagen .Merge (sp , overlay )
3627 if err != nil {
3728 t .Fatalf ("merge: %v" , err )
3829 }
@@ -69,19 +60,9 @@ func TestBuildClientData_Location(t *testing.T) {
6960}
7061
7162func TestBuildClientData_LocationMethod_Object (t * testing.T ) {
72- root := findJNIRepoRoot (t )
73- specPath := filepath .Join (root , "spec" , "java" , "location.yaml" )
74- overlayPath := filepath .Join (root , "spec" , "overlays" , "java" , "location.yaml" )
75-
76- spec , err := javagen .LoadSpec (specPath )
77- if err != nil {
78- t .Fatalf ("load spec: %v" , err )
79- }
80- overlay , err := javagen .LoadOverlay (overlayPath )
81- if err != nil {
82- t .Fatalf ("load overlay: %v" , err )
83- }
84- merged , err := javagen .Merge (spec , overlay )
63+ sp := loadSpec (t , "location.yaml" )
64+ overlay := loadOverlay (t , "location.yaml" )
65+ merged , err := javagen .Merge (sp , overlay )
8566 if err != nil {
8667 t .Fatalf ("merge: %v" , err )
8768 }
@@ -121,19 +102,9 @@ func TestBuildClientData_LocationMethod_Object(t *testing.T) {
121102}
122103
123104func TestRenderClientToString_Location (t * testing.T ) {
124- root := findJNIRepoRoot (t )
125- specPath := filepath .Join (root , "spec" , "java" , "location.yaml" )
126- overlayPath := filepath .Join (root , "spec" , "overlays" , "java" , "location.yaml" )
127-
128- spec , err := javagen .LoadSpec (specPath )
129- if err != nil {
130- t .Fatalf ("load spec: %v" , err )
131- }
132- overlay , err := javagen .LoadOverlay (overlayPath )
133- if err != nil {
134- t .Fatalf ("load overlay: %v" , err )
135- }
136- merged , err := javagen .Merge (spec , overlay )
105+ sp := loadSpec (t , "location.yaml" )
106+ overlay := loadOverlay (t , "location.yaml" )
107+ merged , err := javagen .Merge (sp , overlay )
137108 if err != nil {
138109 t .Fatalf ("merge: %v" , err )
139110 }
@@ -170,40 +141,66 @@ func TestRenderClientToString_Location(t *testing.T) {
170141}
171142
172143func TestGenerateClient_AllRealSpecs (t * testing.T ) {
173- root := findJNIRepoRoot (t )
174- specsDir := filepath .Join (root , "spec" , "java" )
175- overlaysDir := filepath .Join (root , "spec" , "overlays" , "java" )
176- outputDir := t .TempDir ()
177- goModule := "github.com/AndroidGoLab/jni"
178-
179- specFiles , err := filepath .Glob (filepath .Join (specsDir , "*.yaml" ))
144+ entries , err := spec .Java .ReadDir ("java" )
180145 if err != nil {
181- t .Fatalf ("glob specs : %v" , err )
146+ t .Fatalf ("reading embedded spec dir : %v" , err )
182147 }
183- if len (specFiles ) < 40 {
184- t .Fatalf ("expected at least 40 spec files, found %d" , len (specFiles ))
148+ if len (entries ) < 40 {
149+ t .Fatalf ("expected at least 40 spec files, found %d" , len (entries ))
185150 }
186151
152+ tmpDir := t .TempDir ()
153+ outputDir := t .TempDir ()
154+ goModule := "github.com/AndroidGoLab/jni"
155+
187156 var generated , skipped int
188157 var failed []string
189- for _ , specPath := range specFiles {
190- baseName := strings .TrimSuffix (filepath .Base (specPath ), ".yaml" )
191- overlayPath := filepath .Join (overlaysDir , baseName + ".yaml" )
158+ for _ , entry := range entries {
159+ if entry .IsDir () || ! strings .HasSuffix (entry .Name (), ".yaml" ) {
160+ continue
161+ }
162+ baseName := strings .TrimSuffix (entry .Name (), ".yaml" )
163+
164+ // Write embedded spec data to temp file.
165+ specData , err := spec .Java .ReadFile ("java/" + entry .Name ())
166+ if err != nil {
167+ t .Errorf ("%s: read embedded spec: %v" , baseName , err )
168+ failed = append (failed , baseName )
169+ continue
170+ }
171+ specFile := filepath .Join (tmpDir , entry .Name ())
172+ if err := os .WriteFile (specFile , specData , 0644 ); err != nil {
173+ t .Fatalf ("write temp spec %s: %v" , baseName , err )
174+ }
175+
176+ // Write embedded overlay data to temp file (may not exist).
177+ overlayFile := filepath .Join (tmpDir , "overlay_" + entry .Name ())
178+ overlayData , overlayErr := spec .Overlays .ReadFile ("overlays/java/" + entry .Name ())
179+ if overlayErr != nil {
180+ if err := os .WriteFile (overlayFile , []byte ("{}" ), 0644 ); err != nil {
181+ t .Fatalf ("write temp overlay %s: %v" , baseName , err )
182+ }
183+ } else {
184+ if err := os .WriteFile (overlayFile , overlayData , 0644 ); err != nil {
185+ t .Fatalf ("write temp overlay %s: %v" , baseName , err )
186+ }
187+ }
192188
193- if err := GenerateClient (specPath , overlayPath , outputDir , goModule , "" ); err != nil {
189+ if err := GenerateClient (specFile , overlayFile , outputDir , goModule , "" ); err != nil {
194190 t .Errorf ("GenerateClient %s: %v" , baseName , err )
195191 failed = append (failed , baseName )
196192 continue
197193 }
198194
199- spec , loadErr := javagen .LoadSpec (specPath )
195+ // Parse spec to determine package name.
196+ sp , loadErr := javagen .ParseSpec (specData )
200197 if loadErr != nil {
201- t .Errorf ("%s: load spec: %v" , baseName , loadErr )
198+ t .Errorf ("%s: parse spec: %v" , baseName , loadErr )
202199 failed = append (failed , baseName )
203200 continue
204201 }
205202
206- clientPath := filepath .Join (outputDir , "grpc" , "client" , spec .Package , "client.go" )
203+ clientPath := filepath .Join (outputDir , "grpc" , "client" , sp .Package , "client.go" )
207204 if _ , err := os .Stat (clientPath ); err != nil {
208205 skipped ++
209206 continue
@@ -229,7 +226,7 @@ func TestGenerateClient_AllRealSpecs(t *testing.T) {
229226 }
230227
231228 t .Logf ("processed %d specs: %d generated, %d skipped (no service), %d failures" ,
232- len (specFiles ), generated , skipped , len (failed ))
229+ len (entries ), generated , skipped , len (failed ))
233230 if len (failed ) > 0 {
234231 t .Errorf ("failed specs: %s" , strings .Join (failed , ", " ))
235232 }
0 commit comments