@@ -32,38 +32,38 @@ func TestAddLibrary(t *testing.T) {
3232 copyrightYear := strconv .Itoa (time .Now ().Year ())
3333 for _ , test := range []struct {
3434 name string
35- libName string
35+ apis [] string
3636 initialLibraries []* config.Library
3737 wantFinalLibraries []* config.Library
3838 wantGeneratedOutputDir string
3939 wantError error
4040 }{
4141 {
4242 name : "create new library" ,
43- libName : "google- cloud- secretmanager" ,
43+ apis : [] string { "google/ cloud/ secretmanager/v1" } ,
4444 initialLibraries : []* config.Library {},
4545 wantGeneratedOutputDir : "newlib-output" ,
4646 wantFinalLibraries : []* config.Library {
4747 {
48- Name : "google-cloud-secretmanager" ,
48+ Name : "google-cloud-secretmanager-v1 " ,
4949 CopyrightYear : copyrightYear ,
5050 },
5151 },
5252 },
5353 {
54- name : "fail create existing library" ,
55- libName : "google- cloud- secretmanager" ,
54+ name : "fail create existing library" ,
55+ apis : [] string { "google/ cloud/ secretmanager/v1" } ,
5656 initialLibraries : []* config.Library {
5757 {
58- Name : "google-cloud-secretmanager" ,
58+ Name : "google-cloud-secretmanager-v1 " ,
5959 },
6060 },
6161 wantGeneratedOutputDir : "existing-output" ,
6262 wantError : errLibraryAlreadyExists ,
6363 },
6464 {
65- name : "create new library and tidy existing" ,
66- libName : "google- cloud-secretmanager" ,
65+ name : "create new library and tidy existing" ,
66+ apis : [] string { "google/ cloud/orgpolicy/v1" } ,
6767 initialLibraries : []* config.Library {
6868 {
6969 Name : "existinglib" ,
@@ -81,7 +81,7 @@ func TestAddLibrary(t *testing.T) {
8181 },
8282 },
8383 {
84- Name : "google-cloud-secretmanager " ,
84+ Name : "google-cloud-orgpolicy-v1 " ,
8585 CopyrightYear : copyrightYear ,
8686 },
8787 },
@@ -102,7 +102,7 @@ func TestAddLibrary(t *testing.T) {
102102 if err := yaml .Write (librarianConfigPath , cfg ); err != nil {
103103 t .Fatal (err )
104104 }
105- err = runAdd (t .Context (), cfg , test .libName )
105+ err = runAdd (t .Context (), cfg , test .apis ... )
106106 if test .wantError != nil {
107107 if ! errors .Is (err , test .wantError ) {
108108 t .Errorf ("expected error %v, got %v" , test .wantError , err )
@@ -135,60 +135,34 @@ func TestAddCommand(t *testing.T) {
135135 t .Fatal (err )
136136 }
137137
138- testName := "google-cloud-secret-manager"
139138 for _ , test := range []struct {
140139 name string
141- args []string
142- wantErr error
140+ apis []string
141+ wantName string
143142 wantAPIs []* config.API
143+ wantErr error
144144 }{
145145 {
146146 name : "no args" ,
147- args : []string {"librarian" , "add" },
148- wantErr : errMissingLibraryName ,
147+ wantErr : errMissingAPI ,
149148 },
150149 {
151- name : "library name only" ,
152- args : []string {
153- "librarian" ,
154- "add" ,
155- testName ,
156- },
150+ name : "single API" ,
151+ apis : []string {"google/cloud/secretmanager/v1" },
152+ wantName : "google-cloud-secretmanager-v1" ,
157153 },
158154 {
159- name : "library with single API" ,
160- args : []string {
161- "librarian" ,
162- "add" ,
163- testName ,
164- "google/cloud/secretmanager/v1" ,
165- },
166- wantAPIs : []* config.API {
167- {
168- Path : "google/cloud/secretmanager/v1" ,
169- },
170- },
171- },
172- {
173- name : "library with multiple APIs" ,
174- args : []string {
175- "librarian" ,
176- "add" ,
177- testName ,
155+ name : "multiple APIs" ,
156+ apis : []string {
178157 "google/cloud/secretmanager/v1" ,
179158 "google/cloud/secretmanager/v1beta2" ,
180159 "google/cloud/secrets/v1beta1" ,
181160 },
161+ wantName : "google-cloud-secretmanager-v1" ,
182162 wantAPIs : []* config.API {
183- {
184- Path : "google/cloud/secretmanager/v1" ,
185- },
186- {
187- Path : "google/cloud/secretmanager/v1beta2" ,
188- },
189- {
190- Path : "google/cloud/secrets/v1beta1" ,
191- },
163+ {Path : "google/cloud/secretmanager/v1" },
164+ {Path : "google/cloud/secretmanager/v1beta2" },
165+ {Path : "google/cloud/secrets/v1beta1" },
192166 },
193167 },
194168 } {
@@ -203,7 +177,8 @@ func TestAddCommand(t *testing.T) {
203177 if err := yaml .Write (librarianConfigPath , cfg ); err != nil {
204178 t .Fatal (err )
205179 }
206- err := Run (t .Context (), test .args ... )
180+ args := append ([]string {"librarian" , "add" }, test .apis ... )
181+ err := Run (t .Context (), args ... )
207182 if test .wantErr != nil {
208183 if ! errors .Is (err , test .wantErr ) {
209184 t .Fatalf ("want error %v, got %v" , test .wantErr , err )
@@ -218,14 +193,12 @@ func TestAddCommand(t *testing.T) {
218193 if err != nil {
219194 t .Fatal (err )
220195 }
221- got , err := findLibrary (gotCfg , testName )
196+ got , err := findLibrary (gotCfg , test . wantName )
222197 if err != nil {
223198 t .Fatal (err )
224199 }
225- if test .wantAPIs != nil {
226- if diff := cmp .Diff (test .wantAPIs , got .APIs ); diff != "" {
227- t .Errorf ("apis mismatch (-want +got):\n %s" , diff )
228- }
200+ if diff := cmp .Diff (test .wantAPIs , got .APIs ); diff != "" {
201+ t .Errorf ("apis mismatch (-want +got):\n %s" , diff )
229202 }
230203 })
231204 }
@@ -238,10 +211,6 @@ func TestAddLibraryToLibrarianYaml(t *testing.T) {
238211 apis []string
239212 want []* config.API
240213 }{
241- {
242- name : "library with no specification-source" ,
243- libraryName : "newlib" ,
244- },
245214 {
246215 name : "library with single API" ,
247216 libraryName : "newlib" ,
@@ -305,3 +274,27 @@ func TestAddLibraryToLibrarianYaml(t *testing.T) {
305274 })
306275 }
307276}
277+
278+ func TestDeriveLibraryName (t * testing.T ) {
279+ for _ , test := range []struct {
280+ language string
281+ apiPath string
282+ want string
283+ }{
284+ {"python" , "google/cloud/secretmanager/v1" , "google-cloud-secretmanager" },
285+ {"python" , "google/cloud/secretmanager/v1beta2" , "google-cloud-secretmanager" },
286+ {"python" , "google/cloud/storage/v2alpha" , "google-cloud-storage" },
287+ {"python" , "google/maps/addressvalidation/v1" , "google-maps-addressvalidation" },
288+ {"python" , "google/api/v1" , "google-api" },
289+ {"rust" , "google/cloud/secretmanager/v1" , "google-cloud-secretmanager-v1" },
290+ {"rust" , "google/cloud/secretmanager/v1beta2" , "google-cloud-secretmanager-v1beta2" },
291+ {"fake" , "google/cloud/secretmanager/v1" , "google-cloud-secretmanager-v1" },
292+ } {
293+ t .Run (test .language + "/" + test .apiPath , func (t * testing.T ) {
294+ got := deriveLibraryName (test .language , test .apiPath )
295+ if got != test .want {
296+ t .Errorf ("deriveLibraryName(%q, %q) = %q, want %q" , test .language , test .apiPath , got , test .want )
297+ }
298+ })
299+ }
300+ }
0 commit comments