@@ -950,6 +950,115 @@ func TestGenerateScenarios(t *testing.T) {
950950 }
951951}
952952
953+ func TestGenerateSingleLibraryCommand (t * testing.T ) {
954+ t .Parallel ()
955+ for _ , test := range []struct {
956+ name string
957+ api string
958+ library string
959+ state * config.LibrarianState
960+ container * mockContainerClient
961+ ghClient GitHubClient
962+ build bool
963+ wantErr bool
964+ wantErrMsg string
965+ wantPRType pullRequestType
966+ }{
967+ {
968+ name : "onboard library returns pullRequestOnboard" ,
969+ api : "some/api" ,
970+ library : "some-library" ,
971+ state : & config.LibrarianState {
972+ Image : "gcr.io/test/image:v1.2.3" ,
973+ },
974+ container : & mockContainerClient {
975+ wantLibraryGen : true ,
976+ configureLibraryPaths : []string {
977+ "src/a" ,
978+ },
979+ },
980+ ghClient : & mockGitHubClient {},
981+ build : true ,
982+ wantPRType : pullRequestOnboard ,
983+ },
984+ {
985+ name : "generate existing library returns pullRequestGenerate" ,
986+ library : "some-library" ,
987+ state : & config.LibrarianState {
988+ Image : "gcr.io/test/image:v1.2.3" ,
989+ Libraries : []* config.LibraryState {
990+ {
991+ ID : "some-library" ,
992+ APIs : []* config.API {{Path : "some/api" }},
993+ SourceRoots : []string {
994+ "src/a" ,
995+ },
996+ },
997+ },
998+ },
999+ container : & mockContainerClient {
1000+ wantLibraryGen : true ,
1001+ },
1002+ ghClient : & mockGitHubClient {},
1003+ build : true ,
1004+ wantPRType : pullRequestGenerate ,
1005+ },
1006+ } {
1007+ t .Run (test .name , func (t * testing.T ) {
1008+ repo := newTestGitRepoWithState (t , test .state , true )
1009+ sourceRepo := newTestGitRepo (t )
1010+ r := & generateRunner {
1011+ api : test .api ,
1012+ library : test .library ,
1013+ build : test .build ,
1014+ repo : repo ,
1015+ sourceRepo : sourceRepo ,
1016+ state : test .state ,
1017+ containerClient : test .container ,
1018+ ghClient : test .ghClient ,
1019+ workRoot : t .TempDir (),
1020+ }
1021+
1022+ // Create a service config in api path.
1023+ if test .api != "" {
1024+ if err := os .MkdirAll (filepath .Join (r .sourceRepo .GetDir (), test .api ), 0755 ); err != nil {
1025+ t .Fatal (err )
1026+ }
1027+ data := []byte ("type: google.api.Service" )
1028+ if err := os .WriteFile (filepath .Join (r .sourceRepo .GetDir (), test .api , "example_service_v2.yaml" ), data , 0755 ); err != nil {
1029+ t .Fatal (err )
1030+ }
1031+ // Commit the service config file because configure command needs
1032+ // to find the piper id associated with the commit message.
1033+ if err := r .sourceRepo .AddAll (); err != nil {
1034+ t .Fatal (err )
1035+ }
1036+ message := "feat: add an api\n \n PiperOrigin-RevId: 123456"
1037+ if err := r .sourceRepo .Commit (message ); err != nil {
1038+ t .Fatal (err )
1039+ }
1040+ }
1041+
1042+ status , err := r .generateSingleLibrary (context .Background (), r .library , r .workRoot )
1043+ if test .wantErr {
1044+ if err == nil {
1045+ t .Fatalf ("%s should return error" , test .name )
1046+ }
1047+ if ! strings .Contains (err .Error (), test .wantErrMsg ) {
1048+ t .Errorf ("want error message %s, got %s" , test .wantErrMsg , err .Error ())
1049+ }
1050+ return
1051+ }
1052+ if err != nil {
1053+ t .Fatal (err )
1054+ }
1055+ if status .prType != test .wantPRType {
1056+ t .Errorf ("generateSingleLibrary() prType = %v, want %v" , status .prType , test .wantPRType )
1057+ }
1058+ })
1059+ }
1060+ }
1061+
9531062func TestUpdateLastGeneratedCommitState (t * testing.T ) {
9541063 t .Parallel ()
9551064 sourceRepo := newTestGitRepo (t )
0 commit comments