(t *testing.T)
| 173 | } |
| 174 | |
| 175 | func TestCommentableRunUploadsAndWritesBodies(t *testing.T) { |
| 176 | tests := []struct { |
| 177 | name string |
| 178 | opts CommentableOptions |
| 179 | attach []string |
| 180 | host string |
| 181 | hostTokens map[string]string |
| 182 | // Empty means the default, which can upload. |
| 183 | viewerPermission string |
| 184 | repositoryDatabaseID int64 |
| 185 | uploads []attachments.UploadStub |
| 186 | wantQuery string |
| 187 | writeFails bool |
| 188 | wantBody string |
| 189 | wantStdout string |
| 190 | wantErr string |
| 191 | wantUploads int |
| 192 | }{ |
| 193 | { |
| 194 | name: "creating with no asset uploads nothing", |
| 195 | opts: CommentableOptions{Body: "just text", InputType: InputTypeInline}, |
| 196 | wantQuery: `mutation CommentCreate\b`, |
| 197 | wantBody: "just text", |
| 198 | wantStdout: "https://github.com/OWNER/REPO/pull/123#issuecomment-456\n", |
| 199 | }, |
| 200 | { |
| 201 | name: "creating uploads the asset and writes the reference", |
| 202 | opts: CommentableOptions{Body: "see below", InputType: InputTypeInline}, |
| 203 | attach: []string{"shot.png"}, |
| 204 | repositoryDatabaseID: 1234, |
| 205 | uploads: []attachments.UploadStub{{Name: "shot.png", Status: 201, Body: `{"url":"https://example.com/1"}`}}, |
| 206 | wantQuery: `mutation CommentCreate\b`, |
| 207 | wantBody: "see below\n\n", |
| 208 | wantStdout: "https://github.com/OWNER/REPO/pull/123#issuecomment-456\n", |
| 209 | wantUploads: 1, |
| 210 | }, |
| 211 | { |
| 212 | name: "creating writes what uploaded when one upload fails", |
| 213 | opts: CommentableOptions{Body: "see below", InputType: InputTypeInline}, |
| 214 | attach: []string{"a.png", "b.png"}, |
| 215 | repositoryDatabaseID: 1234, |
| 216 | uploads: []attachments.UploadStub{ |
| 217 | {Name: "a.png", Status: 201, Body: `{"url":"https://example.com/1"}`}, |
| 218 | {Name: "b.png", Status: 404, Body: `{"message":"Not Found"}`}, |
| 219 | }, |
| 220 | wantQuery: `mutation CommentCreate\b`, |
| 221 | wantBody: "see below\n\n", |
| 222 | wantStdout: "https://github.com/OWNER/REPO/pull/123#issuecomment-456\n", |
| 223 | wantErr: "could not upload ./b.png: attaching files requires write access to the repository", |
| 224 | wantUploads: 2, |
| 225 | }, |
| 226 | { |
| 227 | name: "creating writes nothing when every upload fails", |
| 228 | opts: CommentableOptions{Body: "", InputType: InputTypeInline}, |
| 229 | attach: []string{"a.png"}, |
| 230 | repositoryDatabaseID: 1234, |
| 231 | uploads: []attachments.UploadStub{{Name: "a.png", Status: 404, Body: `{"message":"Not Found"}`}}, |
| 232 | wantErr: "could not upload ./a.png: attaching files requires write access to the repository\nno comment was posted", |
nothing calls this directly
no test coverage detected