Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1002)

Unified Diff: go/src/infra/libs/git/commit_test.go

Issue 662113003: Drover's back, baby! (Closed) Base URL: https://chromium.googlesource.com/infra/infra.git/+/master
Patch Set: more tests and refactors Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « go/src/infra/libs/git/commit.go ('k') | go/src/infra/libs/git/emptyObject.go » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: go/src/infra/libs/git/commit_test.go
diff --git a/go/src/infra/libs/git/commit_test.go b/go/src/infra/libs/git/commit_test.go
new file mode 100644
index 0000000000000000000000000000000000000000..a9a5d5b7effd4e04145037751793e618a95d915a
--- /dev/null
+++ b/go/src/infra/libs/git/commit_test.go
@@ -0,0 +1,163 @@
+// copyright 2014 the chromium 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 git
+
+import (
+ "fmt"
+ "strings"
+ "testing"
+ "time"
+
+ . "github.com/smartystreets/goconvey/convey"
+)
+
+var goodCommit string = `tree b966f77e58b8a3cf7c02dd0271a4d636c0857af4
+parent 1b346cb5145e1fe4c074611e335d8ac96e18c686
+parent ab8d80f57839fb03674c0fc69ff5ccf2145fa6e2
+author Bob Boberton <bob@chromium.org> 1399330903 -0700
+committer Jane January <jane@chromium.org> 1399330903 -0700
+weird CoolData
+
+Cool commit message
+
+Awesome-footer: cowabunga!
+`
+
+func TestGoodCommits(t *testing.T) {
+ t.Parallel()
+
+ Convey("NewCommitFromRaw(goodCommit)", t, func() {
+ c, err := NewCommitFromRaw([]byte(goodCommit))
+ So(err, ShouldBeNil)
+
+ Convey("Should be able to round-trip", func() {
+ So(c.RawString(), ShouldEqual, goodCommit)
+ })
+
+ Convey("Should process commit correctly", FailureContinues, func() {
+ correctHash := "1b5ba53e883138640ff6281ef6b3477588a688e2"
+ So(c.ID().String(), ShouldEqual, correctHash)
+ So(c, ShouldImplement, (*InternableObject)(nil))
+ So(c.String(), ShouldEqual, fmt.Sprintf("Commit(%s, ...)", correctHash))
+ So(c.Type(), ShouldEqual, CommitType)
+ So(c.Complete(), ShouldBeTrue)
+ })
+
+ Convey("And fields should all parse correctly", FailureContinues, func() {
+ So(c.Tree().String(), ShouldEqual, "b966f77e58b8a3cf7c02dd0271a4d636c0857af4")
+ So(c.Parents(), ShouldResemble, []*ObjectID{
+ MakeObjectID("1b346cb5145e1fe4c074611e335d8ac96e18c686"),
+ MakeObjectID("ab8d80f57839fb03674c0fc69ff5ccf2145fa6e2"),
+ })
+ loc := time.FixedZone("-0700", int((-7 * time.Hour).Seconds()))
+ So(c.Author().RawString(), ShouldEqual, (&User{
+ Name: "Bob Boberton",
+ Email: "bob@chromium.org",
+ Time: time.Unix(1399330903, 0).In(loc),
+ }).RawString())
+ So(c.Committer().RawString(), ShouldEqual, (&User{
+ Name: "Jane January",
+ Email: "jane@chromium.org",
+ Time: time.Unix(1399330903, 0).In(loc),
+ }).RawString())
+ So(c.MessageLines(), ShouldResemble, []string{"Cool commit message"})
+ So(c.ExtraHeaders(), ShouldResemble, []string{"weird CoolData"})
+ So(c.Footers(), ShouldResemble, map[string][]string{"Awesome-footer": {"cowabunga!"}})
+ So(c.FooterPairs(), ShouldResemble, []Footer{{"Awesome-footer", "cowabunga!"}})
+ })
+ })
+
+ Convey("NewCommitFromRaw(goodCommitNoNL)", t, func() {
+ noNl := strings.TrimRight(goodCommit, "\n")
+ c, err := NewCommitFromRaw([]byte(noNl))
+ So(err, ShouldBeNil)
+
+ Convey("Should be able to round-trip", func() {
+ So(c.RawString(), ShouldEqual, noNl)
+ })
+
+ Convey("Should process commit correctly", FailureContinues, func() {
+ correctHash := "d4c5fe94cec9078ae8dd5d2158b9f6bf78e92880"
+ So(c.ID().String(), ShouldEqual, correctHash)
+
+ So(c.MessageLines(), ShouldResemble, []string{"Cool commit message"})
+ So(c.Footers(), ShouldResemble, map[string][]string{"Awesome-footer": {"cowabunga!"}})
+ So(c.FooterPairs(), ShouldResemble, []Footer{{"Awesome-footer", "cowabunga!"}})
+ })
+
+ })
+}
+
+func TestCommitMutate(t *testing.T) {
+ t.Parallel()
+
+ Convey("Commits can be mutated", t, func() {
+ c, err := NewCommitFromRaw([]byte(goodCommit))
+ So(err, ShouldBeNil)
+
+ id := MakeObjectID("deadbeefdeadbeefdeadbeefdeadbeefdeadbeef")
+ nc := c.SetTree(id).SetParents(nil).SetRawMessage("I am a banana!\n")
+
+ Convey("but it doesn't affect the original", func() {
+ So(c.ID().String(), ShouldEqual, "1b5ba53e883138640ff6281ef6b3477588a688e2")
+ })
+
+ Convey("but the modified commits lose their completeness & identity", func() {
+ So(nc.Complete(), ShouldBeFalse)
+ So(nc.ID(), ShouldEqual, &NoID)
+ })
+
+ Convey("and their raw representation is different", func() {
+ expected := `tree deadbeefdeadbeefdeadbeefdeadbeefdeadbeef
+author Bob Boberton <bob@chromium.org> 1399330903 -0700
+committer Jane January <jane@chromium.org> 1399330903 -0700
+weird CoolData
+
+I am a banana!
+`
+ So(nc.RawString(), ShouldEqual, expected)
+
+ Convey("which calculates a new identity and completeness", func() {
+ So(nc.ID().String(), ShouldEqual, "8342205f2453ef852da3b46a2bc43042e5e51ca7")
+ })
+ })
+ })
+}
+
+func TestBadCommits(t *testing.T) {
+ t.Parallel()
+
+ Convey("Commit with garbage after footer doesn't parse footers", t, func() {
+ cdata := `tree b966f77e58b8a3cf7c02dd0271a4d636c0857af4
+parent 1b346cb5145e1fe4c074611e335d8ac96e18c686
+author Bob Boberton <bob@chromium.org> 1399330903 -0700
+committer Jane January <jane@chromium.org> 1399330903 -0700
+
+Cool commit message
+
+Awesome-footer: cowabunga!
+totes not a footer, yo!
+`
+ c, err := NewCommitFromRaw([]byte(cdata))
+ So(err, ShouldBeNil)
+ Convey("but it should still round trip", func() {
+ So(c.RawString(), ShouldEqual, cdata)
+ })
+
+ Convey("and no footers were parsed", func() {
+ So(c.Footers(), ShouldBeEmpty)
+ So(c.FooterPairs(), ShouldBeEmpty)
+ })
+
+ Convey("and the message contains everything", func() {
+ So(c.MessageLines(), ShouldResemble, []string{
+ "Cool commit message",
+ "",
+ "Awesome-footer: cowabunga!",
+ "totes not a footer, yo!",
+ })
+ })
+ })
+}
« no previous file with comments | « go/src/infra/libs/git/commit.go ('k') | go/src/infra/libs/git/emptyObject.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698