golang.org/x/build@v0.0.0-20240506185731-218518f32b70/cmd/gerritbot/gerritbot_test.go (about)

     1  // Copyright 2018 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package main
     6  
     7  import (
     8  	"net/url"
     9  	"os/exec"
    10  	"testing"
    11  
    12  	"github.com/google/go-cmp/cmp"
    13  	"github.com/google/go-github/v48/github"
    14  	"golang.org/x/build/maintner"
    15  	"golang.org/x/build/repos"
    16  )
    17  
    18  func newPullRequest(title, body string) *github.PullRequest {
    19  	return &github.PullRequest{
    20  		Title:  github.String(title),
    21  		Body:   github.String(body),
    22  		Number: github.Int(42),
    23  		Head:   &github.PullRequestBranch{SHA: github.String("deadbeef")},
    24  		Base: &github.PullRequestBranch{
    25  			Repo: &github.Repository{
    26  				Owner: &github.User{
    27  					Login: github.String("golang"),
    28  				},
    29  				Name: github.String("go"),
    30  			},
    31  		},
    32  	}
    33  }
    34  
    35  func newMaintnerCL() *maintner.GerritCL {
    36  	return &maintner.GerritCL{
    37  		Commit: &maintner.GitCommit{
    38  			Msg: `cmd/gerritbot: previous commit messsage
    39  
    40  Hello there
    41  
    42  Change-Id: If751ce3ffa3a4d5e00a3138211383d12cb6b23fc
    43  `,
    44  		},
    45  	}
    46  }
    47  
    48  func TestCommitMessage(t *testing.T) {
    49  	if _, err := exec.LookPath("git"); err != nil {
    50  		t.Skipf("skipping; 'git' not in PATH")
    51  	}
    52  	testCases := []struct {
    53  		desc     string
    54  		pr       *github.PullRequest
    55  		cl       *maintner.GerritCL
    56  		expected string
    57  	}{
    58  		{
    59  			"simple",
    60  			newPullRequest("cmd/gerritbot: title of change", "Body text"),
    61  			nil,
    62  			`cmd/gerritbot: title of change
    63  
    64  Body text
    65  
    66  Change-Id: I8ef4fc7aa2b40846583a9cbf175d75d023b5564e
    67  GitHub-Last-Rev: deadbeef
    68  GitHub-Pull-Request: golang/go#42
    69  `,
    70  		},
    71  		{
    72  			"change with Change-Id",
    73  			newPullRequest("x/build/cmd/gerritbot: change with change ID", "Body text"),
    74  			newMaintnerCL(),
    75  			`cmd/gerritbot: change with change ID
    76  
    77  Body text
    78  
    79  Change-Id: If751ce3ffa3a4d5e00a3138211383d12cb6b23fc
    80  GitHub-Last-Rev: deadbeef
    81  GitHub-Pull-Request: golang/go#42
    82  `,
    83  		},
    84  		{
    85  			"Change-Id in body text",
    86  			newPullRequest("cmd/gerritbot: change with change ID in body text",
    87  				"Change-Id: I30e0a6ec666a06eae3e8444490d96fabcab3333e"),
    88  			nil,
    89  			`cmd/gerritbot: change with change ID in body text
    90  
    91  Change-Id: I30e0a6ec666a06eae3e8444490d96fabcab3333e
    92  GitHub-Last-Rev: deadbeef
    93  GitHub-Pull-Request: golang/go#42
    94  `,
    95  		},
    96  		{
    97  			"Change-Id in body text with an existing CL",
    98  			newPullRequest("cmd/gerritbot: change with change ID in body text and an existing CL",
    99  				"Change-Id: I30e0a6ec666a06eae3e8444490d96fabcab3333e"),
   100  			newMaintnerCL(),
   101  			`cmd/gerritbot: change with change ID in body text and an existing CL
   102  
   103  Change-Id: I30e0a6ec666a06eae3e8444490d96fabcab3333e
   104  
   105  Change-Id: If751ce3ffa3a4d5e00a3138211383d12cb6b23fc
   106  GitHub-Last-Rev: deadbeef
   107  GitHub-Pull-Request: golang/go#42
   108  `,
   109  		},
   110  		{
   111  			"cq-include-trybots",
   112  			newPullRequest("cmd/gerritbot: title of change",
   113  				`Body text
   114  Cq-Include-Trybots: bot-a-123,bot-b-123`),
   115  			nil,
   116  			`cmd/gerritbot: title of change
   117  
   118  Body text
   119  
   120  Change-Id: I8ef4fc7aa2b40846583a9cbf175d75d023b5564e
   121  GitHub-Last-Rev: deadbeef
   122  GitHub-Pull-Request: golang/go#42
   123  Cq-Include-Trybots: bot-a-123,bot-b-123
   124  `,
   125  		},
   126  		{
   127  			"cq-include-trybots-middle",
   128  			newPullRequest("cmd/gerritbot: title of change",
   129  				`Body text
   130  
   131  Cq-Include-Trybots: bot-a-123,bot-b-123
   132  
   133  More text`),
   134  			nil,
   135  			`cmd/gerritbot: title of change
   136  
   137  Body text
   138  
   139  More text
   140  
   141  Change-Id: I8ef4fc7aa2b40846583a9cbf175d75d023b5564e
   142  GitHub-Last-Rev: deadbeef
   143  GitHub-Pull-Request: golang/go#42
   144  Cq-Include-Trybots: bot-a-123,bot-b-123
   145  `,
   146  		},
   147  	}
   148  
   149  	for _, tc := range testCases {
   150  		t.Run(tc.desc, func(t *testing.T) {
   151  			msg, err := commitMessage(tc.pr, tc.cl)
   152  			if err != nil {
   153  				t.Fatalf("got unexpected error from commitMessage: %v", err)
   154  			}
   155  			if diff := cmp.Diff(msg, tc.expected); diff != "" {
   156  				t.Errorf("got unexpected commit message (-got +want)\n%s", diff)
   157  			}
   158  		})
   159  	}
   160  }
   161  
   162  // Test that gerritChangeRE matches the URL to the Change within
   163  // the git output from Gerrit after successfully creating a new CL.
   164  // Whenever Gerrit changes the Change URL format in its output,
   165  // we need to update gerritChangeRE and this test accordingly.
   166  //
   167  // See https://golang.org/issue/27561.
   168  func TestFindChangeURL(t *testing.T) {
   169  	for _, tc := range [...]struct {
   170  		name string
   171  		in   string // Output from git (and input to the regexp).
   172  		want string
   173  	}{
   174  		{
   175  			name: "verbatim", // Verbatim git output from Gerrit, extracted from production logs on 2018/09/07.
   176  			in:   "remote: \rremote: Processing changes: new: 1 (\\)\rremote: Processing changes: new: 1 (|)\rremote: Processing changes: refs: 1, new: 1 (|)\rremote: Processing changes: refs: 1, new: 1 (|)        \rremote: Processing changes: refs: 1, new: 1, done            \nremote: \nremote: SUCCESS        \nremote: \nremote: New Changes:        \nremote:   https://go-review.googlesource.com/c/dl/+/134117 remove blank line from codereview.cfg        \nTo https://go.googlesource.com/dl\n * [new branch]      HEAD -> refs/for/master",
   177  			want: "https://go-review.googlesource.com/c/dl/+/134117",
   178  		},
   179  		{
   180  			name: "repo-with-dash", // A Gerrit repository with a dash in its name (shortened git output).
   181  			in:   "remote: \rremote: Processing changes: (\\) [...] https://go-review.googlesource.com/c/vscode-go/+/222417 [...]",
   182  			want: "https://go-review.googlesource.com/c/vscode-go/+/222417",
   183  		},
   184  	} {
   185  		t.Run(tc.name, func(t *testing.T) {
   186  			got := gerritChangeRE.FindString(tc.in)
   187  			if got != tc.want {
   188  				t.Errorf("could not find change URL in command output: %q\n\ngot %q, want %q", tc.in, got, tc.want)
   189  			}
   190  		})
   191  	}
   192  }
   193  
   194  // TestFindChangeURLInAllRepos tests that gerritChangeRE
   195  // works for names of all Gerrit repositories.
   196  //
   197  // See https://golang.org/issue/37725.
   198  func TestFindChangeURLInAllRepos(t *testing.T) {
   199  	for proj := range repos.ByGerritProject {
   200  		u := (&url.URL{
   201  			Scheme: "https",
   202  			Host:   "go-review.googlesource.com",
   203  			Path:   "/c/" + proj + "/+/1337",
   204  		}).String()
   205  		if gerritChangeRE.FindString("... "+u+" ...") != u {
   206  			t.Errorf("gerritChangeRE regexp didn't work for Gerrit repository named %q, does the regexp need to be adjusted to match some additional characters?", proj)
   207  		}
   208  	}
   209  }