@@ -17,7 +17,6 @@ package librarian
17
17
import (
18
18
"context"
19
19
"errors"
20
- "fmt"
21
20
"os"
22
21
"os/exec"
23
22
"path/filepath"
@@ -204,6 +203,7 @@ func TestRunConfigureCommand(t *testing.T) {
204
203
container * mockContainerClient
205
204
wantConfigureCalls int
206
205
wantErr bool
206
+ wantErrMsg string
207
207
}{
208
208
{
209
209
name : "configures library successfully" ,
@@ -235,6 +235,7 @@ func TestRunConfigureCommand(t *testing.T) {
235
235
container : & mockContainerClient {},
236
236
wantConfigureCalls : 1 ,
237
237
wantErr : true ,
238
+ wantErrMsg : "failed to read dir" ,
238
239
},
239
240
{
240
241
name : "configures library with error message in response" ,
@@ -253,6 +254,7 @@ func TestRunConfigureCommand(t *testing.T) {
253
254
},
254
255
wantConfigureCalls : 1 ,
255
256
wantErr : true ,
257
+ wantErrMsg : "failed with error message" ,
256
258
},
257
259
{
258
260
name : "configures library with no response" ,
@@ -271,6 +273,24 @@ func TestRunConfigureCommand(t *testing.T) {
271
273
},
272
274
wantConfigureCalls : 1 ,
273
275
wantErr : true ,
276
+ wantErrMsg : "failed to read response file" ,
277
+ },
278
+ {
279
+ name : "configures library without initial version" ,
280
+ api : "some/api" ,
281
+ repo : newTestGitRepo (t ),
282
+ state : & config.LibrarianState {
283
+ Libraries : []* config.LibraryState {
284
+ {
285
+ ID : "some-library" ,
286
+ APIs : []* config.API {{Path : "some/api" }},
287
+ },
288
+ },
289
+ },
290
+ container : & mockContainerClient {
291
+ noInitVersion : true ,
292
+ },
293
+ wantConfigureCalls : 1 ,
274
294
},
275
295
{
276
296
name : "configure command failed" ,
@@ -285,10 +305,12 @@ func TestRunConfigureCommand(t *testing.T) {
285
305
},
286
306
},
287
307
container : & mockContainerClient {
288
- configureErr : errors .New ("simulated configure command error" ),
308
+ configureErr : errors .New ("simulated configure command error" ),
309
+ noConfigureResponse : true ,
289
310
},
290
311
wantConfigureCalls : 1 ,
291
312
wantErr : true ,
313
+ wantErrMsg : "simulated configure command error" ,
292
314
},
293
315
} {
294
316
t .Run (test .name , func (t * testing.T ) {
@@ -305,30 +327,22 @@ func TestRunConfigureCommand(t *testing.T) {
305
327
containerClient : test .container ,
306
328
}
307
329
308
- if test .name == "configures library successfully" ||
309
- test .name == "configures library with error message in response" {
310
- if err := os .MkdirAll (filepath .Join (cfg .APISource , test .api ), 0755 ); err != nil {
311
- t .Fatal (err )
312
- }
313
-
314
- data := []byte ("type: google.api.Service" )
315
- if err := os .WriteFile (filepath .Join (cfg .APISource , test .api , "example_service_v2.yaml" ), data , 0755 ); err != nil {
316
- t .Fatal (err )
317
- }
330
+ // Create a service config
331
+ if err := os .MkdirAll (filepath .Join (cfg .APISource , test .api ), 0755 ); err != nil {
332
+ t .Fatal (err )
318
333
}
319
334
320
- if test .name == "configures library with no response" ||
321
- test .name == "configure command failed" {
322
- if err := os .MkdirAll (filepath .Join (cfg .APISource , test .api ), 0755 ); err != nil {
323
- t .Fatal (err )
324
- }
335
+ data := []byte ("type: google.api.Service" )
336
+ if err := os .WriteFile (filepath .Join (cfg .APISource , test .api , "example_service_v2.yaml" ), data , 0755 ); err != nil {
337
+ t .Fatal (err )
338
+ }
325
339
326
- data := []byte ("type: google.api.Service" )
327
- if err := os .WriteFile (filepath .Join (cfg .APISource , test .api , "example_service_v2.yaml" ), data , 0755 ); err != nil {
340
+ if test .name == "configures library with non-existent api source" {
341
+ // This test verifies the scenario of no service config is found
342
+ // in api path.
343
+ if err := os .RemoveAll (filepath .Join (cfg .APISource )); err != nil {
328
344
t .Fatal (err )
329
345
}
330
-
331
- // Do not create response file
332
346
}
333
347
334
348
_ , err := r .runConfigureCommand (context .Background ())
@@ -338,6 +352,10 @@ func TestRunConfigureCommand(t *testing.T) {
338
352
t .Errorf ("runConfigureCommand() should return error" )
339
353
}
340
354
355
+ if ! strings .Contains (err .Error (), test .wantErrMsg ) {
356
+ t .Errorf ("runConfigureCommand() err = %v, want error containing %q" , err , test .wantErrMsg )
357
+ }
358
+
341
359
return
342
360
}
343
361
@@ -501,52 +519,6 @@ func TestNewGenerateRunner(t *testing.T) {
501
519
}
502
520
}
503
521
504
- // newTestGitRepo creates a new git repository in a temporary directory.
505
- func newTestGitRepo (t * testing.T ) gitrepo.Repository {
506
- return newTestGitRepoWithState (t , true )
507
- }
508
-
509
- // newTestGitRepo creates a new git repository in a temporary directory.
510
- func newTestGitRepoWithState (t * testing.T , writeState bool ) gitrepo.Repository {
511
- t .Helper ()
512
- dir := t .TempDir ()
513
- remoteURL := "https://github.com/googleapis/librarian.git"
514
- runGit (t , dir , "init" )
515
- runGit (
t ,
dir ,
"config" ,
"user.email" ,
"[email protected] " )
516
- runGit (t , dir , "config" , "user.name" , "Test User" )
517
- if err := os .WriteFile (filepath .Join (dir , "README.md" ), []byte ("test" ), 0644 ); err != nil {
518
- t .Fatalf ("os.WriteFile: %v" , err )
519
- }
520
- if writeState {
521
- // Create an empty state.yaml file
522
- stateDir := filepath .Join (dir , config .LibrarianDir )
523
- if err := os .MkdirAll (stateDir , 0755 ); err != nil {
524
- t .Fatalf ("os.MkdirAll: %v" , err )
525
- }
526
- stateFile := filepath .Join (stateDir , "state.yaml" )
527
- if err := os .WriteFile (stateFile , []byte ("" ), 0644 ); err != nil {
528
- t .Fatalf ("os.WriteFile: %v" , err )
529
- }
530
- }
531
- runGit (t , dir , "add" , "." )
532
- runGit (t , dir , "commit" , "-m" , "initial commit" )
533
- runGit (t , dir , "remote" , "add" , "origin" , remoteURL )
534
- repo , err := gitrepo .NewRepository (& gitrepo.RepositoryOptions {Dir : dir })
535
- if err != nil {
536
- t .Fatalf ("gitrepo.Open(%q) = %v" , dir , err )
537
- }
538
- return repo
539
- }
540
-
541
- func runGit (t * testing.T , dir string , args ... string ) {
542
- t .Helper ()
543
- cmd := exec .Command ("git" , args ... )
544
- cmd .Dir = dir
545
- if err := cmd .Run (); err != nil {
546
- t .Fatalf ("git %v: %v" , args , err )
547
- }
548
- }
549
-
550
522
func TestGenerateRun (t * testing.T ) {
551
523
t .Parallel ()
552
524
for _ , test := range []struct {
@@ -967,28 +939,15 @@ func TestGenerateScenarios(t *testing.T) {
967
939
workRoot : t .TempDir (),
968
940
}
969
941
942
+ // Create a service config in api path.
970
943
if err := os .MkdirAll (filepath .Join (cfg .APISource , test .api ), 0755 ); err != nil {
971
944
t .Fatal (err )
972
945
}
973
-
974
946
data := []byte ("type: google.api.Service" )
975
947
if err := os .WriteFile (filepath .Join (cfg .APISource , test .api , "example_service_v2.yaml" ), data , 0755 ); err != nil {
976
948
t .Fatal (err )
977
949
}
978
950
979
- // Write a configure-response.json because it is required by configure
980
- // command.
981
- if err := os .MkdirAll (filepath .Join (r .repo .GetDir (), config .LibrarianDir ), 0755 ); err != nil {
982
- t .Fatal (err )
983
- }
984
-
985
- libraryStr := fmt .Sprintf (`{
986
- "ID": "%s"
987
- }` , test .library )
988
- if err := os .WriteFile (filepath .Join (r .repo .GetDir (), config .LibrarianDir , config .ConfigureResponse ), []byte (libraryStr ), 0755 ); err != nil {
989
- t .Fatal (err )
990
- }
991
-
992
951
err := r .run (context .Background ())
993
952
if test .wantErr {
994
953
if err == nil {
@@ -1662,3 +1621,50 @@ func TestCleanAndCopyLibrary(t *testing.T) {
1662
1621
})
1663
1622
}
1664
1623
}
1624
+
1625
+ // newTestGitRepo creates a new git repository in a temporary directory.
1626
+ func newTestGitRepo (t * testing.T ) gitrepo.Repository {
1627
+ t .Helper ()
1628
+ return newTestGitRepoWithState (t , true )
1629
+ }
1630
+
1631
+ // newTestGitRepo creates a new git repository in a temporary directory.
1632
+ func newTestGitRepoWithState (t * testing.T , writeState bool ) gitrepo.Repository {
1633
+ t .Helper ()
1634
+ dir := t .TempDir ()
1635
+ remoteURL := "https://github.com/googleapis/librarian.git"
1636
+ runGit (t , dir , "init" )
1637
+ runGit (
t ,
dir ,
"config" ,
"user.email" ,
"[email protected] " )
1638
+ runGit (t , dir , "config" , "user.name" , "Test User" )
1639
+ if err := os .WriteFile (filepath .Join (dir , "README.md" ), []byte ("test" ), 0644 ); err != nil {
1640
+ t .Fatalf ("os.WriteFile: %v" , err )
1641
+ }
1642
+ if writeState {
1643
+ // Create an empty state.yaml file
1644
+ stateDir := filepath .Join (dir , config .LibrarianDir )
1645
+ if err := os .MkdirAll (stateDir , 0755 ); err != nil {
1646
+ t .Fatalf ("os.MkdirAll: %v" , err )
1647
+ }
1648
+ stateFile := filepath .Join (stateDir , "state.yaml" )
1649
+ if err := os .WriteFile (stateFile , []byte ("" ), 0644 ); err != nil {
1650
+ t .Fatalf ("os.WriteFile: %v" , err )
1651
+ }
1652
+ }
1653
+ runGit (t , dir , "add" , "." )
1654
+ runGit (t , dir , "commit" , "-m" , "initial commit" )
1655
+ runGit (t , dir , "remote" , "add" , "origin" , remoteURL )
1656
+ repo , err := gitrepo .NewRepository (& gitrepo.RepositoryOptions {Dir : dir })
1657
+ if err != nil {
1658
+ t .Fatalf ("gitrepo.Open(%q) = %v" , dir , err )
1659
+ }
1660
+ return repo
1661
+ }
1662
+
1663
+ func runGit (t * testing.T , dir string , args ... string ) {
1664
+ t .Helper ()
1665
+ cmd := exec .Command ("git" , args ... )
1666
+ cmd .Dir = dir
1667
+ if err := cmd .Run (); err != nil {
1668
+ t .Fatalf ("git %v: %v" , args , err )
1669
+ }
1670
+ }
0 commit comments