@@ -44,6 +44,7 @@ func TestRunGenerate(t *testing.T) {
4444 for _ , test := range []struct {
4545 name string
4646 api string
47+ push bool
4748 wantErr bool
4849 }{
4950 {
@@ -55,6 +56,11 @@ func TestRunGenerate(t *testing.T) {
5556 api : "google/cloud/future/v2" ,
5657 wantErr : true ,
5758 },
59+ {
60+ name : "testRunSuccess with push" ,
61+ api : "google/cloud/pubsub/v1" ,
62+ push : true ,
63+ },
5864 } {
5965 t .Run (test .name , func (t * testing.T ) {
6066 workRoot := t .TempDir ()
@@ -67,16 +73,37 @@ func TestRunGenerate(t *testing.T) {
6773 t .Fatalf ("APISouceRepo prepare test error = %v" , err )
6874 }
6975
70- cmd := exec .Command (
71- "go" ,
76+ // Create a local bare repository to act as the remote for the push.
77+ if test .push {
78+ bareRepoDir := filepath .Join (t .TempDir (), "remote.git" )
79+ if err := os .MkdirAll (bareRepoDir , 0755 ); err != nil {
80+ t .Fatalf ("Failed to create bare repo dir: %v" , err )
81+ }
82+ runGit (t , bareRepoDir , "init" , "--bare" )
83+ runGit (t , repo , "remote" , "set-url" , "origin" , bareRepoDir )
84+ }
85+
86+ // Setup mock GitHub server.
87+ server := newMockGitHubServer (t , "generate" )
88+ defer server .Close ()
89+
90+ cmdArgs := []string {
7291 "run" ,
92+ "-tags" , "e2etest" ,
7393 "github.com/googleapis/librarian/cmd/librarian" ,
7494 "generate" ,
7595 fmt .Sprintf ("--api=%s" , test .api ),
7696 fmt .Sprintf ("--output=%s" , workRoot ),
7797 fmt .Sprintf ("--repo=%s" , repo ),
7898 fmt .Sprintf ("--api-source=%s" , apiSourceRepo ),
79- )
99+ }
100+ if test .push {
101+ cmdArgs = append (cmdArgs , "--push" )
102+ }
103+
104+ cmd := exec .Command ("go" , cmdArgs ... )
105+ cmd .Env = append (os .Environ (), "LIBRARIAN_GITHUB_TOKEN=fake-token" )
106+ cmd .Env = append (cmd .Env , "LIBRARIAN_GITHUB_BASE_URL=" + server .URL )
80107 cmd .Stderr = os .Stderr
81108 cmd .Stdout = os .Stdout
82109 err := cmd .Run ()
@@ -490,37 +517,7 @@ END_COMMIT_OVERRIDE
490517 runGit (t , repo , "commit" , "-m" , commitMsg )
491518 runGit (t , repo , "log" , "--oneline" , "go-google-cloud-pubsub-v1-1.0.0..HEAD" , "--" , "google-cloud-pubsub/v1" )
492519
493- // Setup mock GitHub server for --push case
494- server := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
495- if r .Header .Get ("Authorization" ) != "Bearer fake-token" {
496- t .Errorf ("missing or wrong authorization header: got %q" , r .Header .Get ("Authorization" ))
497- }
498-
499- // Mock endpoint for POST /repos/{owner}/{repo}/pulls
500- if r .Method == "POST" && strings .HasSuffix (r .URL .Path , "/pulls" ) {
501- var newPR github.NewPullRequest
502- if err := json .NewDecoder (r .Body ).Decode (& newPR ); err != nil {
503- t .Fatalf ("failed to decode request body: %v" , err )
504- }
505- if ! strings .Contains (* newPR .Title , "chore: librarian release pull request" ) {
506- t .Errorf ("unexpected PR title: got %q" , * newPR .Title )
507- }
508- if * newPR .Base != "main" { // Assuming default branch
509- t .Errorf ("unexpected PR base: got %q" , * newPR .Base )
510- }
511- w .WriteHeader (http .StatusCreated )
512- fmt .Fprint (w , `{"number": 123, "html_url": "https://github.com/googleapis/librarian/pull/123"}` )
513- return
514- }
515-
516- // Mock endpoint for POST /repos/{owner}/{repo}/issues/{number}/labels
517- if r .Method == "POST" && strings .Contains (r .URL .Path , "/issues/123/labels" ) {
518- w .WriteHeader (http .StatusOK )
519- fmt .Fprint (w , `[]` )
520- return
521- }
522- t .Fatalf ("unexpected request: %s %s" , r .Method , r .URL .Path )
523- }))
520+ server := newMockGitHubServer (t , "release" )
524521 defer server .Close ()
525522
526523 cmdArgs := []string {
@@ -734,6 +731,43 @@ libraries:
734731 }
735732}
736733
734+ // newMockGitHubServer creates a mock GitHub API server for testing --push functionality.
735+ func newMockGitHubServer (t * testing.T , prTitleFragment string ) * httptest.Server {
736+ t .Helper ()
737+ return httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
738+ if r .Header .Get ("Authorization" ) != "Bearer fake-token" {
739+ t .Errorf ("missing or wrong authorization header: got %q" , r .Header .Get ("Authorization" ))
740+ }
741+
742+ // Mock endpoint for POST /repos/{owner}/{repo}/pulls
743+ if r .Method == "POST" && strings .HasSuffix (r .URL .Path , "/pulls" ) {
744+ var newPR github.NewPullRequest
745+ if err := json .NewDecoder (r .Body ).Decode (& newPR ); err != nil {
746+ t .Fatalf ("failed to decode request body: %v" , err )
747+ }
748+ expectedTitle := fmt .Sprintf ("chore: librarian %s pull request" , prTitleFragment )
749+ if ! strings .Contains (* newPR .Title , expectedTitle ) {
750+ t .Errorf ("unexpected PR title: got %q, want to contain %q" , * newPR .Title , expectedTitle )
751+ }
752+ if * newPR .Base != "main" {
753+ t .Errorf ("unexpected PR base: got %q" , * newPR .Base )
754+ }
755+ w .WriteHeader (http .StatusCreated )
756+ fmt .Fprint (w , `{"number": 123, "html_url": "https://github.com/googleapis/librarian/pull/123"}` )
757+ return
758+ }
759+
760+ // Mock endpoint for POST /repos/{owner}/{repo}/issues/{number}/labels
761+ if r .Method == "POST" && strings .Contains (r .URL .Path , "/issues/123/labels" ) {
762+ w .WriteHeader (http .StatusOK )
763+ fmt .Fprint (w , `[]` )
764+ return
765+ }
766+
767+ t .Fatalf ("unexpected request: %s %s" , r .Method , r .URL .Path )
768+ }))
769+ }
770+
737771// initRepo initiates a git repo in the given directory, copy
738772// files from source directory and create a commit.
739773func initRepo (t * testing.T , dir , source string ) error {
0 commit comments