2017-02-13 00:58:16 +03:00
|
|
|
// 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 (
|
2017-03-15 20:51:41 +03:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2017-02-13 00:58:16 +03:00
|
|
|
"reflect"
|
2017-03-15 20:51:41 +03:00
|
|
|
"strings"
|
2017-02-13 00:58:16 +03:00
|
|
|
"testing"
|
2017-02-26 21:35:44 +03:00
|
|
|
"time"
|
2017-02-13 00:58:16 +03:00
|
|
|
|
2017-03-15 20:51:41 +03:00
|
|
|
"github.com/davecgh/go-spew/spew"
|
2017-02-26 21:35:44 +03:00
|
|
|
"github.com/golang/protobuf/ptypes"
|
|
|
|
google_protobuf "github.com/golang/protobuf/ptypes/timestamp"
|
|
|
|
"github.com/google/go-github/github"
|
2017-02-13 00:58:16 +03:00
|
|
|
"golang.org/x/build/maintner/maintpb"
|
|
|
|
)
|
|
|
|
|
2017-03-20 22:58:04 +03:00
|
|
|
var u1 = &GitHubUser{
|
2017-03-03 22:56:12 +03:00
|
|
|
Login: "gopherbot",
|
|
|
|
ID: 100,
|
|
|
|
}
|
2017-03-20 22:58:04 +03:00
|
|
|
var u2 = &GitHubUser{
|
2017-03-03 22:56:12 +03:00
|
|
|
Login: "kevinburke",
|
|
|
|
ID: 101,
|
|
|
|
}
|
|
|
|
|
2017-03-01 00:31:57 +03:00
|
|
|
type dummyMutationLogger struct {
|
|
|
|
Mutations []*maintpb.Mutation
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *dummyMutationLogger) Log(m *maintpb.Mutation) error {
|
|
|
|
if d.Mutations == nil {
|
|
|
|
d.Mutations = []*maintpb.Mutation{}
|
|
|
|
}
|
|
|
|
d.Mutations = append(d.Mutations, m)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-02-13 00:58:16 +03:00
|
|
|
type mutationTest struct {
|
2017-02-21 00:59:38 +03:00
|
|
|
corpus *Corpus
|
|
|
|
want *Corpus
|
2017-02-13 00:58:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (mt mutationTest) test(t *testing.T, muts ...*maintpb.Mutation) {
|
|
|
|
c := mt.corpus
|
2017-02-26 21:35:44 +03:00
|
|
|
if c == nil {
|
2017-04-30 00:15:37 +03:00
|
|
|
c = new(Corpus)
|
2017-02-26 21:35:44 +03:00
|
|
|
}
|
2017-02-13 00:58:16 +03:00
|
|
|
for _, m := range muts {
|
|
|
|
c.processMutationLocked(m)
|
|
|
|
}
|
2017-03-15 20:51:41 +03:00
|
|
|
c.github.c = nil
|
|
|
|
mt.want.github.c = nil
|
|
|
|
if !reflect.DeepEqual(c.github, mt.want.github) {
|
|
|
|
t.Errorf("corpus mismatch:\n got: %s\n\nwant: %s\n\ndiff: %v",
|
|
|
|
spew.Sdump(c.github),
|
|
|
|
spew.Sdump(mt.want.github),
|
|
|
|
diffPath(reflect.ValueOf(c.github), reflect.ValueOf(mt.want.github)))
|
2017-02-13 00:58:16 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-26 21:35:44 +03:00
|
|
|
var t1, t2 time.Time
|
|
|
|
var tp1, tp2 *google_protobuf.Timestamp
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
t1, _ = time.Parse(time.RFC3339, "2016-01-02T15:04:00Z")
|
|
|
|
t2, _ = time.Parse(time.RFC3339, "2016-01-02T15:30:00Z")
|
|
|
|
tp1, _ = ptypes.TimestampProto(t1)
|
|
|
|
tp2, _ = ptypes.TimestampProto(t2)
|
|
|
|
}
|
|
|
|
|
2017-02-13 00:58:16 +03:00
|
|
|
func TestProcessMutation_Github_NewIssue(t *testing.T) {
|
2017-04-30 00:15:37 +03:00
|
|
|
c := new(Corpus)
|
2017-03-20 22:58:04 +03:00
|
|
|
github := &GitHub{c: c}
|
2017-03-15 20:51:41 +03:00
|
|
|
c.github = github
|
2017-03-20 22:58:04 +03:00
|
|
|
github.users = map[int64]*GitHubUser{
|
2017-03-03 22:56:12 +03:00
|
|
|
u1.ID: u1,
|
2017-02-26 21:35:44 +03:00
|
|
|
}
|
2017-04-03 23:01:31 +03:00
|
|
|
github.repos = map[GithubRepoID]*GitHubRepo{
|
|
|
|
GithubRepoID{"golang", "go"}: &GitHubRepo{
|
2017-03-15 20:51:41 +03:00
|
|
|
github: github,
|
2017-04-03 23:01:31 +03:00
|
|
|
id: GithubRepoID{"golang", "go"},
|
2017-03-20 22:58:04 +03:00
|
|
|
issues: map[int32]*GitHubIssue{
|
|
|
|
3: &GitHubIssue{
|
2017-03-15 20:51:41 +03:00
|
|
|
Number: 3,
|
|
|
|
User: u1,
|
|
|
|
Title: "some title",
|
|
|
|
Body: "some body",
|
|
|
|
Created: t1,
|
|
|
|
Assignees: nil,
|
|
|
|
},
|
2017-02-13 00:58:16 +03:00
|
|
|
},
|
2017-02-26 21:35:44 +03:00
|
|
|
},
|
|
|
|
}
|
|
|
|
mutationTest{want: c}.test(t, &maintpb.Mutation{
|
|
|
|
GithubIssue: &maintpb.GithubIssueMutation{
|
|
|
|
Owner: "golang",
|
|
|
|
Repo: "go",
|
|
|
|
Number: 3,
|
|
|
|
User: &maintpb.GithubUser{
|
|
|
|
Login: "gopherbot",
|
|
|
|
Id: 100,
|
2017-02-13 00:58:16 +03:00
|
|
|
},
|
2017-03-03 22:56:12 +03:00
|
|
|
Title: "some title",
|
2017-02-26 21:35:44 +03:00
|
|
|
Body: "some body",
|
|
|
|
Created: tp1,
|
2017-02-13 00:58:16 +03:00
|
|
|
},
|
2017-02-26 21:35:44 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-04-21 08:46:02 +03:00
|
|
|
func TestProcessMutation_Github(t *testing.T) {
|
2017-04-30 00:15:37 +03:00
|
|
|
c := new(Corpus)
|
2017-04-21 08:46:02 +03:00
|
|
|
github := &GitHub{c: c}
|
|
|
|
c.github = github
|
|
|
|
github.repos = map[GithubRepoID]*GitHubRepo{
|
|
|
|
GithubRepoID{"golang", "go"}: &GitHubRepo{
|
|
|
|
github: github,
|
|
|
|
id: GithubRepoID{"golang", "go"},
|
|
|
|
issues: make(map[int32]*GitHubIssue),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
mutationTest{want: c}.test(t, &maintpb.Mutation{
|
|
|
|
Github: &maintpb.GithubMutation{
|
|
|
|
Owner: "golang",
|
|
|
|
Repo: "go",
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-02-26 21:35:44 +03:00
|
|
|
func TestNewMutationsFromIssue(t *testing.T) {
|
|
|
|
gh := &github.Issue{
|
|
|
|
Number: github.Int(5),
|
|
|
|
CreatedAt: &t1,
|
|
|
|
UpdatedAt: &t2,
|
|
|
|
Body: github.String("body of the issue"),
|
|
|
|
State: github.String("closed"),
|
|
|
|
}
|
2017-03-20 22:58:04 +03:00
|
|
|
gr := &GitHubRepo{
|
2017-04-03 23:01:31 +03:00
|
|
|
id: GithubRepoID{"golang", "go"},
|
2017-03-15 20:51:41 +03:00
|
|
|
}
|
|
|
|
is := gr.newMutationFromIssue(nil, gh)
|
2017-03-01 00:31:57 +03:00
|
|
|
want := &maintpb.Mutation{GithubIssue: &maintpb.GithubIssueMutation{
|
2017-03-15 20:51:41 +03:00
|
|
|
Owner: "golang",
|
|
|
|
Repo: "go",
|
|
|
|
Number: 5,
|
|
|
|
Body: "body of the issue",
|
|
|
|
Created: tp1,
|
|
|
|
Updated: tp2,
|
|
|
|
Assignees: []*maintpb.GithubUser{},
|
|
|
|
NoMilestone: true,
|
|
|
|
Closed: &maintpb.BoolChange{Val: true},
|
2017-03-01 00:31:57 +03:00
|
|
|
}}
|
2017-02-26 21:35:44 +03:00
|
|
|
if !reflect.DeepEqual(is, want) {
|
2017-03-15 20:51:41 +03:00
|
|
|
t.Errorf("issue mismatch\n got: %v\nwant: %v\ndiff path: %v", spew.Sdump(is), spew.Sdump(want),
|
|
|
|
diffPath(reflect.ValueOf(is), reflect.ValueOf(want)))
|
2017-02-26 21:35:44 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-03 22:56:12 +03:00
|
|
|
func TestNewAssigneesHandlesNil(t *testing.T) {
|
|
|
|
users := []*github.User{
|
|
|
|
&github.User{Login: github.String("foo"), ID: github.Int(3)},
|
|
|
|
}
|
|
|
|
got := newAssignees(nil, users)
|
|
|
|
want := []*maintpb.GithubUser{&maintpb.GithubUser{
|
|
|
|
Id: 3,
|
|
|
|
Login: "foo",
|
|
|
|
}}
|
|
|
|
if !reflect.DeepEqual(got, want) {
|
|
|
|
t.Errorf("assignee mismatch\n got: %#v\nwant: %#v", got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAssigneesDeleted(t *testing.T) {
|
2017-04-30 00:15:37 +03:00
|
|
|
c := new(Corpus)
|
2017-03-20 22:58:04 +03:00
|
|
|
assignees := []*GitHubUser{u1, u2}
|
|
|
|
issue := &GitHubIssue{
|
2017-03-03 22:56:12 +03:00
|
|
|
Number: 3,
|
|
|
|
User: u1,
|
|
|
|
Body: "some body",
|
|
|
|
Created: t2,
|
|
|
|
Updated: t2,
|
|
|
|
Assignees: assignees,
|
|
|
|
}
|
2017-03-20 22:58:04 +03:00
|
|
|
gr := &GitHubRepo{
|
2017-04-03 23:01:31 +03:00
|
|
|
id: GithubRepoID{"golang", "go"},
|
2017-03-20 22:58:04 +03:00
|
|
|
issues: map[int32]*GitHubIssue{
|
2017-03-03 22:56:12 +03:00
|
|
|
3: issue,
|
|
|
|
},
|
|
|
|
}
|
2017-03-20 22:58:04 +03:00
|
|
|
c.github = &GitHub{
|
|
|
|
users: map[int64]*GitHubUser{
|
2017-03-15 20:51:41 +03:00
|
|
|
u1.ID: u1,
|
|
|
|
},
|
2017-04-03 23:01:31 +03:00
|
|
|
repos: map[GithubRepoID]*GitHubRepo{
|
|
|
|
GithubRepoID{"golang", "go"}: gr,
|
2017-03-15 20:51:41 +03:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
mutation := gr.newMutationFromIssue(issue, &github.Issue{
|
2017-03-03 22:56:12 +03:00
|
|
|
Number: github.Int(3),
|
|
|
|
Assignees: []*github.User{&github.User{ID: github.Int(int(u2.ID))}},
|
2017-03-15 20:51:41 +03:00
|
|
|
})
|
2017-03-25 06:56:50 +03:00
|
|
|
c.addMutation(mutation)
|
2017-03-15 20:51:41 +03:00
|
|
|
gi := gr.issues[3]
|
2017-03-03 22:56:12 +03:00
|
|
|
if len(gi.Assignees) != 1 || gi.Assignees[0].ID != u2.ID {
|
|
|
|
t.Errorf("expected u1 to be deleted, got %v", gi.Assignees)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-20 03:24:29 +03:00
|
|
|
func DeepDiff(got, want interface{}) error {
|
|
|
|
return diffPath(reflect.ValueOf(got), reflect.ValueOf(want))
|
|
|
|
}
|
|
|
|
|
2017-03-15 20:51:41 +03:00
|
|
|
func diffPath(got, want reflect.Value) error {
|
|
|
|
if !got.IsValid() {
|
|
|
|
return errors.New("'got' value invalid")
|
2017-02-26 21:35:44 +03:00
|
|
|
}
|
2017-03-15 20:51:41 +03:00
|
|
|
if !want.IsValid() {
|
|
|
|
return errors.New("'want' value invalid")
|
2017-02-26 21:35:44 +03:00
|
|
|
}
|
|
|
|
|
2017-03-15 20:51:41 +03:00
|
|
|
t := got.Type()
|
|
|
|
if t != want.Type() {
|
|
|
|
return fmt.Errorf("got=%s, want=%s", got.Type(), want.Type())
|
2017-02-26 21:35:44 +03:00
|
|
|
}
|
2017-03-15 20:51:41 +03:00
|
|
|
|
|
|
|
switch t.Kind() {
|
|
|
|
case reflect.Ptr, reflect.Chan, reflect.Func, reflect.Interface, reflect.Map, reflect.Slice:
|
|
|
|
if got.IsNil() != want.IsNil() {
|
|
|
|
if got.IsNil() {
|
|
|
|
return fmt.Errorf("got = (%s)(nil), want = non-nil", t)
|
|
|
|
}
|
|
|
|
return fmt.Errorf("got = (%s)(non-nil), want = nil", t)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch t.Kind() {
|
|
|
|
case reflect.Ptr:
|
|
|
|
if got.IsNil() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return diffPath(got.Elem(), want.Elem())
|
|
|
|
|
|
|
|
case reflect.Struct:
|
|
|
|
nf := t.NumField()
|
|
|
|
for i := 0; i < nf; i++ {
|
|
|
|
sf := t.Field(i)
|
|
|
|
if err := diffPath(got.Field(i), want.Field(i)); err != nil {
|
|
|
|
inner := err.Error()
|
|
|
|
sep := "."
|
|
|
|
if strings.HasPrefix(inner, "field ") {
|
|
|
|
inner = strings.TrimPrefix(inner, "field ")
|
|
|
|
} else {
|
|
|
|
sep = ": "
|
|
|
|
}
|
|
|
|
return fmt.Errorf("field %s%s%v", sf.Name, sep, inner)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
case reflect.String:
|
|
|
|
if got.String() != want.String() {
|
|
|
|
return fmt.Errorf("got = %q; want = %q", got.String(), want.String())
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
|
|
|
|
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
|
|
|
if got.Int() != want.Int() {
|
|
|
|
return fmt.Errorf("got = %v; want = %v", got.Int(), want.Int())
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
|
|
|
|
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
|
|
|
|
if got.Uint() != want.Uint() {
|
|
|
|
return fmt.Errorf("got = %v; want = %v", got.Uint(), want.Uint())
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
|
|
|
|
case reflect.Bool:
|
|
|
|
if got.Bool() != want.Bool() {
|
|
|
|
return fmt.Errorf("got = %v; want = %v", got.Bool(), want.Bool())
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
|
|
|
|
case reflect.Slice:
|
|
|
|
gl, wl := got.Len(), want.Len()
|
|
|
|
if gl != wl {
|
|
|
|
return fmt.Errorf("slice len %v; want %v", gl, wl)
|
|
|
|
}
|
|
|
|
for i := 0; i < gl; i++ {
|
|
|
|
if err := diffPath(got.Index(i), want.Index(i)); err != nil {
|
|
|
|
return fmt.Errorf("index[%d] differs: %v", i, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("unhandled kind %v", t.Kind())
|
2017-02-26 21:35:44 +03:00
|
|
|
}
|
|
|
|
}
|