Debian Codegraph @

internal/librariangen/config/repoconfig_test.go

raw
1 // Copyright 2025 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package config
16
17 import (
18 "os"
19 "path/filepath"
20 "testing"
21
22 "github.com/google/go-cmp/cmp"
23 )
24
25 func TestLoadRepoConfig_success(t *testing.T) {
26 tempDir := t.TempDir()
27
28 const configFileContent = `
29 modules:
30 - name: maps
31 apis:
32 - path: google/maps/fleetengine/v1
33 proto_package: maps.fleetengine.v1
34 - path: google/maps/fleetengine/delivery/v1
35 proto_package: maps.fleetengine.delivery.v1
36 - name: monitoring
37 apis:
38 - path: google/monitoring/v3
39 client_directory: apiv3/v2
40 - name: recaptchaenterprise
41 module_path_version: v2
42 `
43
44 generatorInputDir := filepath.Join(tempDir, GeneratorInputDir)
45 if err := os.Mkdir(generatorInputDir, 0755); err != nil {
46 t.Fatal(err)
47 }
48 configPath := filepath.Join(generatorInputDir, RepoConfigFile)
49 if err := os.WriteFile(configPath, []byte(configFileContent), 0644); err != nil {
50 t.Fatal(err)
51 }
52
53 want := &RepoConfig{
54 Modules: []*ModuleConfig{
55 {
56 Name: "maps",
57 APIs: []*APIConfig{
58 {
59 Path: "google/maps/fleetengine/v1",
60 ProtoPackage: "maps.fleetengine.v1",
61 },
62 {
63 Path: "google/maps/fleetengine/delivery/v1",
64 ProtoPackage: "maps.fleetengine.delivery.v1",
65 },
66 },
67 },
68 {
69 Name: "monitoring",
70 APIs: []*APIConfig{
71 {
72 Path: "google/monitoring/v3",
73 ClientDirectory: "apiv3/v2",
74 },
75 },
76 },
77 {
78 Name: "recaptchaenterprise",
79 ModulePathVersion: "v2",
80 },
81 },
82 }
83
84 got, err := LoadRepoConfig(tempDir)
85 if err != nil {
86 t.Fatalf("LoadRepoConfig() failed: %v", err)
87 }
88 if diff := cmp.Diff(want, got); diff != "" {
89 t.Errorf("Transform() mismatch (-want +got):\n%s", diff)
90 }
91 }
92
93 func TestLoadRepoConfig_NotFound(t *testing.T) {
94 tempDir := t.TempDir()
95 _, err := LoadRepoConfig(tempDir)
96 if err == nil {
97 t.Error("LoadRepoConfig() succeeded, want error")
98 }
99 }
100
101 func TestGetModuleConfig(t *testing.T) {
102 config := &RepoConfig{
103 Modules: []*ModuleConfig{
104 {
105 Name: "spanner",
106 ModulePathVersion: "v2",
107 },
108 {Name: "functions"},
109 },
110 }
111
112 for _, test := range []struct {
113 name string
114 moduleName string
115 want *ModuleConfig
116 }{
117 {
118 name: "present in config",
119 moduleName: "spanner",
120 want: &ModuleConfig{
121 Name: "spanner",
122 ModulePathVersion: "v2",
123 },
124 },
125 {
126 name: "absent in config",
127 moduleName: "other",
128 want: &ModuleConfig{
129 Name: "other",
130 },
131 },
132 } {
133 t.Run(test.name, func(t *testing.T) {
134 got := config.GetModuleConfig(test.moduleName)
135 if diff := cmp.Diff(test.want, got); diff != "" {
136 t.Errorf("Transform() mismatch (-want +got):\n%s", diff)
137 }
138 })
139 }
140 }
141
142 func TestGetAPIConfig(t *testing.T) {
143 config := &ModuleConfig{
144 Name: "spanner",
145 APIs: []*APIConfig{
146 {
147 Path: "google/spanner/v1",
148 ProtoPackage: "spanner.v1",
149 },
150 {
151 Path: "google/spanner/admin/instance/v1",
152 },
153 },
154 }
155
156 for _, test := range []struct {
157 name string
158 apiPath string
159 want *APIConfig
160 }{
161 {
162 name: "present in config",
163 apiPath: "google/spanner/v1",
164 want: &APIConfig{
165 Path: "google/spanner/v1",
166 ProtoPackage: "spanner.v1",
167 ModuleName: "spanner",
168 },
169 },
170 {
171 name: "absent in config",
172 apiPath: "google/spanner/other/v1",
173 want: &APIConfig{
174 Path: "google/spanner/other/v1",
175 ModuleName: "spanner",
176 },
177 },
178 } {
179 t.Run(test.name, func(t *testing.T) {
180 got := config.GetAPIConfig(test.apiPath)
181 if diff := cmp.Diff(test.want, got); diff != "" {
182 t.Errorf("GetAPIConfig() mismatch (-want +got):\n%s", diff)
183 }
184 })
185 }
186 }
187
188 func TestGetModulePath(t *testing.T) {
189 for _, test := range []struct {
190 name string
191 config *ModuleConfig
192 want string
193 }{
194 {
195 name: "version specified",
196 config: &ModuleConfig{
197 Name: "spanner",
198 ModulePathVersion: "v2",
199 },
200 want: "cloud.google.com/go/spanner/v2",
201 },
202 {
203 name: "no version specified",
204 config: &ModuleConfig{
205 Name: "spanner",
206 },
207 want: "cloud.google.com/go/spanner",
208 },
209 } {
210 t.Run(test.name, func(t *testing.T) {
211 got := test.config.GetModulePath()
212 if got != test.want {
213 t.Errorf("GetModulePath() = %q, want %q", got, test.want)
214 }
215 })
216 }
217 }
218
219 func TestGetProtoPackage(t *testing.T) {
220 for _, test := range []struct {
221 name string
222 config *APIConfig
223 want string
224 }{
225 {
226 name: "override present",
227 config: &APIConfig{
228 Path: "google/spanner/v1",
229 ProtoPackage: "override.package",
230 },
231 want: "override.package",
232 },
233 {
234 name: "no override",
235 config: &APIConfig{
236 Path: "google/spanner/v1",
237 },
238 want: "google.spanner.v1",
239 },
240 } {
241 t.Run(test.name, func(t *testing.T) {
242 got := test.config.GetProtoPackage()
243 if got != test.want {
244 t.Errorf("GetProtoPackage() = %q, want %q", got, test.want)
245 }
246 })
247 }
248 }
249
250 func TestGetClientDirectory(t *testing.T) {
251 for _, test := range []struct {
252 name string
253 config *APIConfig
254 want string
255 wantError bool
256 }{
257 {
258 name: "override present",
259 config: &APIConfig{
260 ClientDirectory: "override/directory",
261 },
262 want: "override/directory",
263 },
264 {
265 name: "simple path",
266 config: &APIConfig{
267 Path: "google/spanner/v1",
268 ModuleName: "spanner",
269 },
270 want: "apiv1",
271 },
272 {
273 name: "complex path",
274 config: &APIConfig{
275 Path: "google/spanner/admin/instance/v1",
276 ModuleName: "spanner",
277 },
278 want: "admin/instance/apiv1",
279 },
280 {
281 name: "module not in path",
282 config: &APIConfig{
283 Path: "google/spanner/v1",
284 ModuleName: "wrongmodule",
285 },
286 wantError: true,
287 },
288 } {
289 t.Run(test.name, func(t *testing.T) {
290 got, err := test.config.GetClientDirectory()
291 if (err != nil) != test.wantError {
292 t.Fatalf("GetClientDirectory() error = %v, wantError %v", err, test.wantError)
293 }
294 if got != test.want {
295 t.Errorf("GetClientDirectory() = %q, want %q", got, test.want)
296 }
297 })
298 }
299 }
300
301 func TestHasDisableGAPIC(t *testing.T) {
302 for _, test := range []struct {
303 name string
304 config *APIConfig
305 want bool
306 }{
307 {
308 name: "true",
309 config: &APIConfig{
310 DisableGAPIC: true,
311 },
312 want: true,
313 },
314 {
315 name: "default",
316 config: &APIConfig{},
317 want: false,
318 },
319 } {
320 t.Run(test.name, func(t *testing.T) {
321 if got := test.config.HasDisableGAPIC(); got != test.want {
322 t.Errorf("HasDisableGAPIC() = %v, want %v", got, test.want)
323 }
324 })
325 }
326 }