blob: 79cfce143aa0078e37b7ecd033dbda4ff4763885 [file] [log] [blame]
// Copyright 2017 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package maintner
import (
"testing"
"time"
)
var statusTests = []struct {
msg string
want string
}{
{`Create change
Uploaded patch set 1.
Patch-set: 1 (draft)
Change-id: I38a08cacc17bcd9587475495111fe98f10d6875c
Subject: test: test
Branch: refs/heads/master
Status: draft
Topic:
Commit: fee468c613a70d89f60fb5d683b0f796aabecaac`, "draft"},
{`Update patch set 1
Change has been successfully cherry-picked as 117ac82c422a11e4dd5f4c14b50bafc1df840481 by Brad Fitzpatrick
Patch-set: 1
Status: merged
Submission-id: 16401-1446004855021-a20b3823`, "merged"},
{`Create patch set 8
Uploaded patch set 8: Patch Set 7 was rebased.
Patch-set: 8
Subject: devapp: initial support for App Engine Flex
Commit: 17839a9f284b473986f235ad2757a2b445d05068
Tag: autogenerated:gerrit:newPatchSet
Groups: 17839a9f284b473986f235ad2757a2b445d05068`, ""},
}
func TestGetGerritStatus(t *testing.T) {
for _, tt := range statusTests {
gc := &GitCommit{Msg: tt.msg}
got := getGerritStatus(gc)
if got != tt.want {
t.Errorf("getGerritStatus msg:\n%s\ngot %s, want %s", tt.msg, got, tt.want)
}
}
}
var normalizeTests = []struct {
in string
out string
}{
{"foo", "foo"},
{"http://foo", "foo"},
{"upspin-review.googlesource.com", "upspin.googlesource.com"},
{"go-review.googlesource.com", "go.googlesource.com"},
}
func TestNormalizeServer(t *testing.T) {
for _, tt := range normalizeTests {
got := normalizeGerritServer(tt.in)
if got != tt.out {
t.Errorf("normalizeGerritServer(%q) = %q, want %q", tt.in, got, tt.out)
}
}
}
func TestGerritProject(t *testing.T) {
var c Corpus
c.EnableLeaderMode(new(dummyMutationLogger), "/fake/dir")
c.TrackGerrit("go.googlesource.com/build")
gp := c.Gerrit().Project("go-review.googlesource.com", "build")
if gp == nil {
t.Errorf("expected go-review.googlesource.com to return a project, got nil")
}
gp = c.Gerrit().Project("go-review.googlesource.com", "unknown")
if gp != nil {
t.Errorf("expected go-review.googlesource.com to return nil, got a project")
}
}
var messageTests = []struct {
in string
want string
wantNil bool
}{
{
in: `Update patch set 1
Patch Set 1: Code-Review+2
Just to confirm, "go test" will consider an empty test file to be passing?
Patch-set: 1
Reviewer: Quentin Smith <13020@62eb7196-b449-3ce5-99f1-c037f21e1705>
Label: Code-Review=+2
`,
want: "Patch Set 1: Code-Review+2\n\nJust to confirm, \"go test\" will consider an empty test file to be passing?",
},
{
in: `Create change
Uploaded patch set 1: Run-TryBot+1.
Patch-set: 1
Change-id: I1e0035ffba986c3551479d5742809e43da5e7c73
Subject: runtime: fall back to small mmaps if we fail to grow reservation
Branch: refs/heads/master
Status: new
Topic:
Commit: 8776f8d725c001456037e8888a72885d46cd6744
Tag: autogenerated:gerrit:newPatchSet
Groups: 8776f8d725c001456037e8888a72885d46cd6744
Reviewer: Keith Randall <5200@62eb7196-b449-3ce5-99f1-c037f21e1705>
Reviewer: Rick Hudson <5186@62eb7196-b449-3ce5-99f1-c037f21e1705>
Reviewer: Austin Clements <5167@62eb7196-b449-3ce5-99f1-c037f21e1705>
Label: Run-TryBot=+1
Private: false
Work-in-progress: false
`,
want: "Uploaded patch set 1: Run-TryBot+1.",
},
{
in: `Uploaded patch set 1.
Patch-set: 1
`,
wantNil: true,
},
{
in: `Create change
Uploaded patch set 1.
Patch-set: 1
Change-id: I3799148a111f1ab6bfee24c9e03e6ebbf9e9595b
Subject: net: make error messages consistent for invalid ports
Branch: refs/heads/master
Commit: 8a7de7048dc194d5e6f761add433b915beebb2e0
Groups: 8a7de7048dc194d5e6f761add433b915beebb2e0
`,
wantNil: true,
},
{
in: `Create patch set 7
Uploaded patch set 7.: Commit message was updated
Patch-set: 7
Subject: cmd/vet: -lostcancel: check for discarded result of context.WithCancel
Commit: 5487cc78ea332c7b49d43ef5955211387aca73bb
Groups: 5487cc78ea332c7b49d43ef5955211387aca73bb
`,
wantNil: true,
},
}
func TestGetGerritMessage(t *testing.T) {
var c Corpus
c.EnableLeaderMode(new(dummyMutationLogger), "/fake/dir")
c.TrackGerrit("go.googlesource.com/build")
gp := c.gerrit.projects["go.googlesource.com/build"]
for i, tt := range messageTests {
gc := &GitCommit{
Msg: tt.in,
CommitTime: time.Now().UTC(),
}
msg := gp.getGerritMessage(gc)
if msg == nil != tt.wantNil {
if tt.wantNil {
t.Errorf("%d. getGerritMessage returned item; want nil", i)
} else {
t.Errorf("%d. getGerritMessage = nil; want a message", i)
}
continue
}
if msg == nil {
continue
}
// just checking these get copied through appropriately
if msg.Version != 1 {
t.Errorf("%d. getGerritMessage: want Version 1, got %d", i, msg.Version)
}
if msg.Date.IsZero() {
t.Errorf("%d. getGerritMessage: expected Date to be non-zero, got zero", i)
}
if msg.Message != tt.want {
t.Errorf("%d. getGerritMessage = %q; want %q", i, msg.Message, tt.want)
}
}
}
func TestOwnerNameAndID(t *testing.T) {
testCases := []struct {
cl *GerritCL
OwnerID int
OwnerName string
}{
{&GerritCL{}, -1, ""},
{&GerritCL{
Meta: &GitCommit{
Parents: []*GitCommit{
&GitCommit{
Author: &GitPerson{
Str: "Rick Sanchez <137@62eb7196-b449-3ce5-99f1-c037f21e1705>",
},
},
},
},
}, 137, "Rick Sanchez"},
}
for _, tc := range testCases {
if got := tc.cl.OwnerID(); got != tc.OwnerID {
t.Errorf("cl.OwnerID() = %d; want %d", got, tc.OwnerID)
}
if got := tc.cl.OwnerName(); got != tc.OwnerName {
t.Errorf("cl.OwnerName() = %q; want %q", got, tc.OwnerName)
}
}
}
func TestSubject(t *testing.T) {
cl := &GerritCL{}
if w, e := cl.Subject(), ""; w != e {
t.Errorf("cl.Subject() = %q; want %q", w, e)
}
testcases := []struct{ msg, subject string }{
{"maintner: slurp up all the things", "maintner: slurp up all the things"},
{"cmd/go: build stuff\n\nand do other stuff, too.", "cmd/go: build stuff"},
}
for _, tc := range testcases {
cl = &GerritCL{Commit: &GitCommit{Msg: tc.msg}}
if cl.Subject() != tc.subject {
t.Errorf("cl.Subject() = %q; want %q", cl.Subject(), tc.subject)
}
}
}
func TestLineValue(t *testing.T) {
tests := []struct {
all, prefix, want, wantRest string
}{
{
all: "foo: value ",
prefix: "foo:",
want: "value",
wantRest: "",
},
{
all: "foo: value\n",
prefix: "foo:",
want: "value",
wantRest: "",
},
{
all: "bar: other\nfoo: value\n",
prefix: "foo:",
want: "value",
wantRest: "",
},
{
all: "notfoo: other\nfoo: value\n",
prefix: "foo:",
want: "value",
wantRest: "",
},
{
all: "Foo: bar\nLabel: Vote=+1\nLabel: Vote=+2\n",
prefix: "Label: ",
want: "Vote=+1",
wantRest: "Label: Vote=+2\n",
},
{
all: "Label: Vote=+2\n",
prefix: "Label: ",
want: "Vote=+2",
wantRest: "",
},
}
for _, tt := range tests {
got, gotRest := lineValue(tt.all, tt.prefix)
if got != tt.want {
t.Errorf("lineValue(%q, %q) returned value %q; want %q", tt.all, tt.prefix, got, tt.want)
}
if gotRest != tt.wantRest {
t.Errorf("lineValue(%q, %q) returned rest %q; want %q", tt.all, tt.prefix, gotRest, tt.wantRest)
}
}
}
func TestParseGerritLabelValue(t *testing.T) {
tests := []struct {
in string
wantLabel string
wantValue int8
wantWhose string
}{
{"Run-TryBot=+1", "Run-TryBot", 1, ""},
{"-Run-TryBot", "-Run-TryBot", 0, ""},
{"-TryBot-Result Gobot Gobot <5976@62eb7196-b449-3ce5-99f1-c037f21e1705>", "-TryBot-Result", 0, "5976@62eb7196-b449-3ce5-99f1-c037f21e1705"},
{"Run-TryBot=+1 Brad Fitzpatrick <5065@62eb7196-b449-3ce5-99f1-c037f21e1705>", "Run-TryBot", 1, "5065@62eb7196-b449-3ce5-99f1-c037f21e1705"},
{"TryBot-Result=-1 Gobot Gobot <5976@62eb7196-b449-3ce5-99f1-c037f21e1705>", "TryBot-Result", -1, "5976@62eb7196-b449-3ce5-99f1-c037f21e1705"},
}
for _, tt := range tests {
label, value, whose := parseGerritLabelValue(tt.in)
if label != tt.wantLabel || value != tt.wantValue || whose != tt.wantWhose {
t.Errorf("parseGerritLabelValue(%q) = %q, %v, %q; want %q, %v, %q",
tt.in,
label, value, whose,
tt.wantLabel, tt.wantValue, tt.wantWhose)
}
}
}