| 1 |
package packp |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
|
| 6 |
"github.com/go-git/go-git/v5/plumbing" |
| 7 |
"github.com/go-git/go-git/v5/plumbing/format/pktline" |
| 8 |
|
| 9 |
. "gopkg.in/check.v1" |
| 10 |
) |
| 11 |
|
| 12 |
type ReportStatusSuite struct{} |
| 13 |
|
| 14 |
var _ = Suite(&ReportStatusSuite{}) |
| 15 |
|
| 16 |
func (s *ReportStatusSuite) TestError(c *C) { |
| 17 |
rs := NewReportStatus() |
| 18 |
rs.UnpackStatus = "ok" |
| 19 |
c.Assert(rs.Error(), IsNil) |
| 20 |
rs.UnpackStatus = "OK" |
| 21 |
c.Assert(rs.Error(), ErrorMatches, "unpack error: OK") |
| 22 |
rs.UnpackStatus = "" |
| 23 |
c.Assert(rs.Error(), ErrorMatches, "unpack error: ") |
| 24 |
|
| 25 |
cs := &CommandStatus{ReferenceName: plumbing.ReferenceName("ref")} |
| 26 |
rs.UnpackStatus = "ok" |
| 27 |
rs.CommandStatuses = append(rs.CommandStatuses, cs) |
| 28 |
|
| 29 |
cs.Status = "ok" |
| 30 |
c.Assert(rs.Error(), IsNil) |
| 31 |
cs.Status = "OK" |
| 32 |
c.Assert(rs.Error(), ErrorMatches, "command error on ref: OK") |
| 33 |
cs.Status = "" |
| 34 |
c.Assert(rs.Error(), ErrorMatches, "command error on ref: ") |
| 35 |
} |
| 36 |
|
| 37 |
func (s *ReportStatusSuite) testEncodeDecodeOk(c *C, rs *ReportStatus, lines ...string) { |
| 38 |
s.testDecodeOk(c, rs, lines...) |
| 39 |
s.testEncodeOk(c, rs, lines...) |
| 40 |
} |
| 41 |
|
| 42 |
func (s *ReportStatusSuite) testDecodeOk(c *C, expected *ReportStatus, lines ...string) { |
| 43 |
r := toPktLines(c, lines) |
| 44 |
rs := NewReportStatus() |
| 45 |
c.Assert(rs.Decode(r), IsNil) |
| 46 |
c.Assert(rs, DeepEquals, expected) |
| 47 |
} |
| 48 |
|
| 49 |
func (s *ReportStatusSuite) testDecodeError(c *C, errorMatch string, lines ...string) { |
| 50 |
r := toPktLines(c, lines) |
| 51 |
rs := NewReportStatus() |
| 52 |
c.Assert(rs.Decode(r), ErrorMatches, errorMatch) |
| 53 |
} |
| 54 |
|
| 55 |
func (s *ReportStatusSuite) testEncodeOk(c *C, input *ReportStatus, lines ...string) { |
| 56 |
expected := pktlines(c, lines...) |
| 57 |
var buf bytes.Buffer |
| 58 |
c.Assert(input.Encode(&buf), IsNil) |
| 59 |
obtained := buf.Bytes() |
| 60 |
|
| 61 |
comment := Commentf("\nobtained = %s\nexpected = %s\n", string(obtained), string(expected)) |
| 62 |
|
| 63 |
c.Assert(obtained, DeepEquals, expected, comment) |
| 64 |
} |
| 65 |
|
| 66 |
func (s *ReportStatusSuite) TestEncodeDecodeOkOneReference(c *C) { |
| 67 |
rs := NewReportStatus() |
| 68 |
rs.UnpackStatus = "ok" |
| 69 |
rs.CommandStatuses = []*CommandStatus{{ |
| 70 |
ReferenceName: plumbing.ReferenceName("refs/heads/master"), |
| 71 |
Status: "ok", |
| 72 |
}} |
| 73 |
|
| 74 |
s.testEncodeDecodeOk(c, rs, |
| 75 |
"unpack ok\n", |
| 76 |
"ok refs/heads/master\n", |
| 77 |
pktline.FlushString, |
| 78 |
) |
| 79 |
} |
| 80 |
|
| 81 |
func (s *ReportStatusSuite) TestEncodeDecodeOkOneReferenceFailed(c *C) { |
| 82 |
rs := NewReportStatus() |
| 83 |
rs.UnpackStatus = "my error" |
| 84 |
rs.CommandStatuses = []*CommandStatus{{ |
| 85 |
ReferenceName: plumbing.ReferenceName("refs/heads/master"), |
| 86 |
Status: "command error", |
| 87 |
}} |
| 88 |
|
| 89 |
s.testEncodeDecodeOk(c, rs, |
| 90 |
"unpack my error\n", |
| 91 |
"ng refs/heads/master command error\n", |
| 92 |
pktline.FlushString, |
| 93 |
) |
| 94 |
} |
| 95 |
|
| 96 |
func (s *ReportStatusSuite) TestEncodeDecodeOkMoreReferences(c *C) { |
| 97 |
rs := NewReportStatus() |
| 98 |
rs.UnpackStatus = "ok" |
| 99 |
rs.CommandStatuses = []*CommandStatus{{ |
| 100 |
ReferenceName: plumbing.ReferenceName("refs/heads/master"), |
| 101 |
Status: "ok", |
| 102 |
}, { |
| 103 |
ReferenceName: plumbing.ReferenceName("refs/heads/a"), |
| 104 |
Status: "ok", |
| 105 |
}, { |
| 106 |
ReferenceName: plumbing.ReferenceName("refs/heads/b"), |
| 107 |
Status: "ok", |
| 108 |
}} |
| 109 |
|
| 110 |
s.testEncodeDecodeOk(c, rs, |
| 111 |
"unpack ok\n", |
| 112 |
"ok refs/heads/master\n", |
| 113 |
"ok refs/heads/a\n", |
| 114 |
"ok refs/heads/b\n", |
| 115 |
pktline.FlushString, |
| 116 |
) |
| 117 |
} |
| 118 |
|
| 119 |
func (s *ReportStatusSuite) TestEncodeDecodeOkMoreReferencesFailed(c *C) { |
| 120 |
rs := NewReportStatus() |
| 121 |
rs.UnpackStatus = "my error" |
| 122 |
rs.CommandStatuses = []*CommandStatus{{ |
| 123 |
ReferenceName: plumbing.ReferenceName("refs/heads/master"), |
| 124 |
Status: "ok", |
| 125 |
}, { |
| 126 |
ReferenceName: plumbing.ReferenceName("refs/heads/a"), |
| 127 |
Status: "command error", |
| 128 |
}, { |
| 129 |
ReferenceName: plumbing.ReferenceName("refs/heads/b"), |
| 130 |
Status: "ok", |
| 131 |
}} |
| 132 |
|
| 133 |
s.testEncodeDecodeOk(c, rs, |
| 134 |
"unpack my error\n", |
| 135 |
"ok refs/heads/master\n", |
| 136 |
"ng refs/heads/a command error\n", |
| 137 |
"ok refs/heads/b\n", |
| 138 |
pktline.FlushString, |
| 139 |
) |
| 140 |
} |
| 141 |
|
| 142 |
func (s *ReportStatusSuite) TestEncodeDecodeOkNoReferences(c *C) { |
| 143 |
expected := NewReportStatus() |
| 144 |
expected.UnpackStatus = "ok" |
| 145 |
|
| 146 |
s.testEncodeDecodeOk(c, expected, |
| 147 |
"unpack ok\n", |
| 148 |
pktline.FlushString, |
| 149 |
) |
| 150 |
} |
| 151 |
|
| 152 |
func (s *ReportStatusSuite) TestEncodeDecodeOkNoReferencesFailed(c *C) { |
| 153 |
rs := NewReportStatus() |
| 154 |
rs.UnpackStatus = "my error" |
| 155 |
|
| 156 |
s.testEncodeDecodeOk(c, rs, |
| 157 |
"unpack my error\n", |
| 158 |
pktline.FlushString, |
| 159 |
) |
| 160 |
} |
| 161 |
|
| 162 |
func (s *ReportStatusSuite) TestDecodeErrorOneReferenceNoFlush(c *C) { |
| 163 |
expected := NewReportStatus() |
| 164 |
expected.UnpackStatus = "ok" |
| 165 |
expected.CommandStatuses = []*CommandStatus{{ |
| 166 |
ReferenceName: plumbing.ReferenceName("refs/heads/master"), |
| 167 |
Status: "ok", |
| 168 |
}} |
| 169 |
|
| 170 |
s.testDecodeError(c, "missing flush", |
| 171 |
"unpack ok\n", |
| 172 |
"ok refs/heads/master\n", |
| 173 |
) |
| 174 |
} |
| 175 |
|
| 176 |
func (s *ReportStatusSuite) TestDecodeErrorEmpty(c *C) { |
| 177 |
expected := NewReportStatus() |
| 178 |
expected.UnpackStatus = "ok" |
| 179 |
expected.CommandStatuses = []*CommandStatus{{ |
| 180 |
ReferenceName: plumbing.ReferenceName("refs/heads/master"), |
| 181 |
Status: "ok", |
| 182 |
}} |
| 183 |
|
| 184 |
s.testDecodeError(c, "unexpected EOF") |
| 185 |
} |
| 186 |
|
| 187 |
func (s *ReportStatusSuite) TestDecodeErrorMalformed(c *C) { |
| 188 |
expected := NewReportStatus() |
| 189 |
expected.UnpackStatus = "ok" |
| 190 |
expected.CommandStatuses = []*CommandStatus{{ |
| 191 |
ReferenceName: plumbing.ReferenceName("refs/heads/master"), |
| 192 |
Status: "ok", |
| 193 |
}} |
| 194 |
|
| 195 |
s.testDecodeError(c, "malformed unpack status: unpackok", |
| 196 |
"unpackok\n", |
| 197 |
pktline.FlushString, |
| 198 |
) |
| 199 |
} |
| 200 |
|
| 201 |
func (s *ReportStatusSuite) TestDecodeErrorMalformed2(c *C) { |
| 202 |
expected := NewReportStatus() |
| 203 |
expected.UnpackStatus = "ok" |
| 204 |
expected.CommandStatuses = []*CommandStatus{{ |
| 205 |
ReferenceName: plumbing.ReferenceName("refs/heads/master"), |
| 206 |
Status: "ok", |
| 207 |
}} |
| 208 |
|
| 209 |
s.testDecodeError(c, "malformed unpack status: UNPACK OK", |
| 210 |
"UNPACK OK\n", |
| 211 |
pktline.FlushString, |
| 212 |
) |
| 213 |
} |
| 214 |
|
| 215 |
func (s *ReportStatusSuite) TestDecodeErrorMalformedCommandStatus(c *C) { |
| 216 |
expected := NewReportStatus() |
| 217 |
expected.UnpackStatus = "ok" |
| 218 |
expected.CommandStatuses = []*CommandStatus{{ |
| 219 |
ReferenceName: plumbing.ReferenceName("refs/heads/master"), |
| 220 |
Status: "ok", |
| 221 |
}} |
| 222 |
|
| 223 |
s.testDecodeError(c, "malformed command status: ko refs/heads/master", |
| 224 |
"unpack ok\n", |
| 225 |
"ko refs/heads/master\n", |
| 226 |
pktline.FlushString, |
| 227 |
) |
| 228 |
} |
| 229 |
|
| 230 |
func (s *ReportStatusSuite) TestDecodeErrorMalformedCommandStatus2(c *C) { |
| 231 |
expected := NewReportStatus() |
| 232 |
expected.UnpackStatus = "ok" |
| 233 |
expected.CommandStatuses = []*CommandStatus{{ |
| 234 |
ReferenceName: plumbing.ReferenceName("refs/heads/master"), |
| 235 |
Status: "ok", |
| 236 |
}} |
| 237 |
|
| 238 |
s.testDecodeError(c, "malformed command status: ng refs/heads/master", |
| 239 |
"unpack ok\n", |
| 240 |
"ng refs/heads/master\n", |
| 241 |
pktline.FlushString, |
| 242 |
) |
| 243 |
} |
| 244 |
|
| 245 |
func (s *ReportStatusSuite) TestDecodeErrorPrematureFlush(c *C) { |
| 246 |
expected := NewReportStatus() |
| 247 |
expected.UnpackStatus = "ok" |
| 248 |
expected.CommandStatuses = []*CommandStatus{{ |
| 249 |
ReferenceName: plumbing.ReferenceName("refs/heads/master"), |
| 250 |
Status: "ok", |
| 251 |
}} |
| 252 |
|
| 253 |
s.testDecodeError(c, "premature flush", |
| 254 |
pktline.FlushString, |
| 255 |
) |
| 256 |
} |