| OLD | NEW |
| (Empty) | |
| 1 // copyright 2014 the chromium authors. all rights reserved. |
| 2 // use of this source code is governed by a bsd-style license that can be |
| 3 // found in the license file. |
| 4 |
| 5 package git |
| 6 |
| 7 import ( |
| 8 "fmt" |
| 9 "strings" |
| 10 "testing" |
| 11 "time" |
| 12 |
| 13 . "github.com/smartystreets/goconvey/convey" |
| 14 ) |
| 15 |
| 16 var goodCommit string = `tree b966f77e58b8a3cf7c02dd0271a4d636c0857af4 |
| 17 parent 1b346cb5145e1fe4c074611e335d8ac96e18c686 |
| 18 parent ab8d80f57839fb03674c0fc69ff5ccf2145fa6e2 |
| 19 author Bob Boberton <bob@chromium.org> 1399330903 -0700 |
| 20 committer Jane January <jane@chromium.org> 1399330903 -0700 |
| 21 weird CoolData |
| 22 |
| 23 Cool commit message |
| 24 |
| 25 Awesome-footer: cowabunga! |
| 26 ` |
| 27 |
| 28 func TestGoodCommits(t *testing.T) { |
| 29 t.Parallel() |
| 30 |
| 31 Convey("NewCommitFromRaw(goodCommit)", t, func() { |
| 32 c, err := NewCommitFromRaw([]byte(goodCommit)) |
| 33 So(err, ShouldBeNil) |
| 34 |
| 35 Convey("Should be able to round-trip", func() { |
| 36 So(c.RawString(), ShouldEqual, goodCommit) |
| 37 }) |
| 38 |
| 39 Convey("Should process commit correctly", FailureContinues, func
() { |
| 40 correctHash := "1b5ba53e883138640ff6281ef6b3477588a688e2
" |
| 41 So(c.ID().String(), ShouldEqual, correctHash) |
| 42 So(c, ShouldImplement, (*InternableObject)(nil)) |
| 43 So(c.String(), ShouldEqual, fmt.Sprintf("Commit(%s, ...)
", correctHash)) |
| 44 So(c.Type(), ShouldEqual, CommitType) |
| 45 So(c.Complete(), ShouldBeTrue) |
| 46 }) |
| 47 |
| 48 Convey("And fields should all parse correctly", FailureContinues
, func() { |
| 49 So(c.Tree().String(), ShouldEqual, "b966f77e58b8a3cf7c02
dd0271a4d636c0857af4") |
| 50 So(c.Parents(), ShouldResemble, []*ObjectID{ |
| 51 MakeObjectID("1b346cb5145e1fe4c074611e335d8ac96e
18c686"), |
| 52 MakeObjectID("ab8d80f57839fb03674c0fc69ff5ccf214
5fa6e2"), |
| 53 }) |
| 54 loc := time.FixedZone("-0700", int((-7 * time.Hour).Seco
nds())) |
| 55 So(c.Author().RawString(), ShouldEqual, (&User{ |
| 56 Name: "Bob Boberton", |
| 57 Email: "bob@chromium.org", |
| 58 Time: time.Unix(1399330903, 0).In(loc), |
| 59 }).RawString()) |
| 60 So(c.Committer().RawString(), ShouldEqual, (&User{ |
| 61 Name: "Jane January", |
| 62 Email: "jane@chromium.org", |
| 63 Time: time.Unix(1399330903, 0).In(loc), |
| 64 }).RawString()) |
| 65 So(c.MessageLines(), ShouldResemble, []string{"Cool comm
it message"}) |
| 66 So(c.ExtraHeaders(), ShouldResemble, []string{"weird Co
olData"}) |
| 67 So(c.Footers(), ShouldResemble, map[string][]string{"Awe
some-footer": {"cowabunga!"}}) |
| 68 So(c.FooterPairs(), ShouldResemble, []Footer{{"Awesome-f
ooter", "cowabunga!"}}) |
| 69 }) |
| 70 }) |
| 71 |
| 72 Convey("NewCommitFromRaw(goodCommitNoNL)", t, func() { |
| 73 noNl := strings.TrimRight(goodCommit, "\n") |
| 74 c, err := NewCommitFromRaw([]byte(noNl)) |
| 75 So(err, ShouldBeNil) |
| 76 |
| 77 Convey("Should be able to round-trip", func() { |
| 78 So(c.RawString(), ShouldEqual, noNl) |
| 79 }) |
| 80 |
| 81 Convey("Should process commit correctly", FailureContinues, func
() { |
| 82 correctHash := "d4c5fe94cec9078ae8dd5d2158b9f6bf78e92880
" |
| 83 So(c.ID().String(), ShouldEqual, correctHash) |
| 84 |
| 85 So(c.MessageLines(), ShouldResemble, []string{"Cool comm
it message"}) |
| 86 So(c.Footers(), ShouldResemble, map[string][]string{"Awe
some-footer": {"cowabunga!"}}) |
| 87 So(c.FooterPairs(), ShouldResemble, []Footer{{"Awesome-f
ooter", "cowabunga!"}}) |
| 88 }) |
| 89 |
| 90 }) |
| 91 } |
| 92 |
| 93 func TestCommitMutate(t *testing.T) { |
| 94 t.Parallel() |
| 95 |
| 96 Convey("Commits can be mutated", t, func() { |
| 97 c, err := NewCommitFromRaw([]byte(goodCommit)) |
| 98 So(err, ShouldBeNil) |
| 99 |
| 100 id := MakeObjectID("deadbeefdeadbeefdeadbeefdeadbeefdeadbeef") |
| 101 nc := c.SetTree(id).SetParents(nil).SetRawMessage("I am a banana
!\n") |
| 102 |
| 103 Convey("but it doesn't affect the original", func() { |
| 104 So(c.ID().String(), ShouldEqual, "1b5ba53e883138640ff628
1ef6b3477588a688e2") |
| 105 }) |
| 106 |
| 107 Convey("but the modified commits lose their completeness & ident
ity", func() { |
| 108 So(nc.Complete(), ShouldBeFalse) |
| 109 So(nc.ID(), ShouldEqual, &NoID) |
| 110 }) |
| 111 |
| 112 Convey("and their raw representation is different", func() { |
| 113 expected := `tree deadbeefdeadbeefdeadbeefdeadbeefdeadbe
ef |
| 114 author Bob Boberton <bob@chromium.org> 1399330903 -0700 |
| 115 committer Jane January <jane@chromium.org> 1399330903 -0700 |
| 116 weird CoolData |
| 117 |
| 118 I am a banana! |
| 119 ` |
| 120 So(nc.RawString(), ShouldEqual, expected) |
| 121 |
| 122 Convey("which calculates a new identity and completeness
", func() { |
| 123 So(nc.ID().String(), ShouldEqual, "8342205f2453e
f852da3b46a2bc43042e5e51ca7") |
| 124 }) |
| 125 }) |
| 126 }) |
| 127 } |
| 128 |
| 129 func TestBadCommits(t *testing.T) { |
| 130 t.Parallel() |
| 131 |
| 132 Convey("Commit with garbage after footer doesn't parse footers", t, func
() { |
| 133 cdata := `tree b966f77e58b8a3cf7c02dd0271a4d636c0857af4 |
| 134 parent 1b346cb5145e1fe4c074611e335d8ac96e18c686 |
| 135 author Bob Boberton <bob@chromium.org> 1399330903 -0700 |
| 136 committer Jane January <jane@chromium.org> 1399330903 -0700 |
| 137 |
| 138 Cool commit message |
| 139 |
| 140 Awesome-footer: cowabunga! |
| 141 totes not a footer, yo! |
| 142 ` |
| 143 c, err := NewCommitFromRaw([]byte(cdata)) |
| 144 So(err, ShouldBeNil) |
| 145 Convey("but it should still round trip", func() { |
| 146 So(c.RawString(), ShouldEqual, cdata) |
| 147 }) |
| 148 |
| 149 Convey("and no footers were parsed", func() { |
| 150 So(c.Footers(), ShouldBeEmpty) |
| 151 So(c.FooterPairs(), ShouldBeEmpty) |
| 152 }) |
| 153 |
| 154 Convey("and the message contains everything", func() { |
| 155 So(c.MessageLines(), ShouldResemble, []string{ |
| 156 "Cool commit message", |
| 157 "", |
| 158 "Awesome-footer: cowabunga!", |
| 159 "totes not a footer, yo!", |
| 160 }) |
| 161 }) |
| 162 }) |
| 163 } |
| OLD | NEW |