| 1 |
// Copyright 2016 The go-github AUTHORS. All rights reserved. |
| 2 |
// |
| 3 |
// Use of this source code is governed by a BSD-style |
| 4 |
// license that can be found in the LICENSE file. |
| 5 |
|
| 6 |
package github |
| 7 |
|
| 8 |
import ( |
| 9 |
"context" |
| 10 |
"encoding/json" |
| 11 |
"fmt" |
| 12 |
"net/http" |
| 13 |
"testing" |
| 14 |
|
| 15 |
"github.com/google/go-cmp/cmp" |
| 16 |
) |
| 17 |
|
| 18 |
func TestMigrationService_StartImport(t *testing.T) { |
| 19 |
client, mux, _, teardown := setup() |
| 20 |
defer teardown() |
| 21 |
|
| 22 |
input := &Import{ |
| 23 |
VCS: String("git"), |
| 24 |
VCSURL: String("url"), |
| 25 |
VCSUsername: String("u"), |
| 26 |
VCSPassword: String("p"), |
| 27 |
} |
| 28 |
|
| 29 |
mux.HandleFunc("/repos/o/r/import", func(w http.ResponseWriter, r *http.Request) { |
| 30 |
v := new(Import) |
| 31 |
assertNilError(t, json.NewDecoder(r.Body).Decode(v)) |
| 32 |
|
| 33 |
testMethod(t, r, "PUT") |
| 34 |
if !cmp.Equal(v, input) { |
| 35 |
t.Errorf("Request body = %+v, want %+v", v, input) |
| 36 |
} |
| 37 |
|
| 38 |
w.WriteHeader(http.StatusCreated) |
| 39 |
fmt.Fprint(w, `{"status":"importing"}`) |
| 40 |
}) |
| 41 |
|
| 42 |
ctx := context.Background() |
| 43 |
got, _, err := client.Migrations.StartImport(ctx, "o", "r", input) |
| 44 |
if err != nil { |
| 45 |
t.Errorf("StartImport returned error: %v", err) |
| 46 |
} |
| 47 |
want := &Import{Status: String("importing")} |
| 48 |
if !cmp.Equal(got, want) { |
| 49 |
t.Errorf("StartImport = %+v, want %+v", got, want) |
| 50 |
} |
| 51 |
|
| 52 |
const methodName = "StartImport" |
| 53 |
testBadOptions(t, methodName, func() (err error) { |
| 54 |
_, _, err = client.Migrations.StartImport(ctx, "\n", "\n", input) |
| 55 |
return err |
| 56 |
}) |
| 57 |
|
| 58 |
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { |
| 59 |
got, resp, err := client.Migrations.StartImport(ctx, "o", "r", input) |
| 60 |
if got != nil { |
| 61 |
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) |
| 62 |
} |
| 63 |
return resp, err |
| 64 |
}) |
| 65 |
} |
| 66 |
|
| 67 |
func TestMigrationService_ImportProgress(t *testing.T) { |
| 68 |
client, mux, _, teardown := setup() |
| 69 |
defer teardown() |
| 70 |
|
| 71 |
mux.HandleFunc("/repos/o/r/import", func(w http.ResponseWriter, r *http.Request) { |
| 72 |
testMethod(t, r, "GET") |
| 73 |
fmt.Fprint(w, `{"status":"complete"}`) |
| 74 |
}) |
| 75 |
|
| 76 |
ctx := context.Background() |
| 77 |
got, _, err := client.Migrations.ImportProgress(ctx, "o", "r") |
| 78 |
if err != nil { |
| 79 |
t.Errorf("ImportProgress returned error: %v", err) |
| 80 |
} |
| 81 |
want := &Import{Status: String("complete")} |
| 82 |
if !cmp.Equal(got, want) { |
| 83 |
t.Errorf("ImportProgress = %+v, want %+v", got, want) |
| 84 |
} |
| 85 |
|
| 86 |
const methodName = "ImportProgress" |
| 87 |
testBadOptions(t, methodName, func() (err error) { |
| 88 |
_, _, err = client.Migrations.ImportProgress(ctx, "\n", "\n") |
| 89 |
return err |
| 90 |
}) |
| 91 |
|
| 92 |
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { |
| 93 |
got, resp, err := client.Migrations.ImportProgress(ctx, "o", "r") |
| 94 |
if got != nil { |
| 95 |
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) |
| 96 |
} |
| 97 |
return resp, err |
| 98 |
}) |
| 99 |
} |
| 100 |
|
| 101 |
func TestMigrationService_UpdateImport(t *testing.T) { |
| 102 |
client, mux, _, teardown := setup() |
| 103 |
defer teardown() |
| 104 |
|
| 105 |
input := &Import{ |
| 106 |
VCS: String("git"), |
| 107 |
VCSURL: String("url"), |
| 108 |
VCSUsername: String("u"), |
| 109 |
VCSPassword: String("p"), |
| 110 |
} |
| 111 |
|
| 112 |
mux.HandleFunc("/repos/o/r/import", func(w http.ResponseWriter, r *http.Request) { |
| 113 |
v := new(Import) |
| 114 |
assertNilError(t, json.NewDecoder(r.Body).Decode(v)) |
| 115 |
|
| 116 |
testMethod(t, r, "PATCH") |
| 117 |
if !cmp.Equal(v, input) { |
| 118 |
t.Errorf("Request body = %+v, want %+v", v, input) |
| 119 |
} |
| 120 |
|
| 121 |
w.WriteHeader(http.StatusCreated) |
| 122 |
fmt.Fprint(w, `{"status":"importing"}`) |
| 123 |
}) |
| 124 |
|
| 125 |
ctx := context.Background() |
| 126 |
got, _, err := client.Migrations.UpdateImport(ctx, "o", "r", input) |
| 127 |
if err != nil { |
| 128 |
t.Errorf("UpdateImport returned error: %v", err) |
| 129 |
} |
| 130 |
want := &Import{Status: String("importing")} |
| 131 |
if !cmp.Equal(got, want) { |
| 132 |
t.Errorf("UpdateImport = %+v, want %+v", got, want) |
| 133 |
} |
| 134 |
|
| 135 |
const methodName = "UpdateImport" |
| 136 |
testBadOptions(t, methodName, func() (err error) { |
| 137 |
_, _, err = client.Migrations.UpdateImport(ctx, "\n", "\n", input) |
| 138 |
return err |
| 139 |
}) |
| 140 |
|
| 141 |
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { |
| 142 |
got, resp, err := client.Migrations.UpdateImport(ctx, "o", "r", input) |
| 143 |
if got != nil { |
| 144 |
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) |
| 145 |
} |
| 146 |
return resp, err |
| 147 |
}) |
| 148 |
} |
| 149 |
|
| 150 |
func TestMigrationService_CommitAuthors(t *testing.T) { |
| 151 |
client, mux, _, teardown := setup() |
| 152 |
defer teardown() |
| 153 |
|
| 154 |
mux.HandleFunc("/repos/o/r/import/authors", func(w http.ResponseWriter, r *http.Request) { |
| 155 |
testMethod(t, r, "GET") |
| 156 |
fmt.Fprint(w, `[{"id":1,"name":"a"},{"id":2,"name":"b"}]`) |
| 157 |
}) |
| 158 |
|
| 159 |
ctx := context.Background() |
| 160 |
got, _, err := client.Migrations.CommitAuthors(ctx, "o", "r") |
| 161 |
if err != nil { |
| 162 |
t.Errorf("CommitAuthors returned error: %v", err) |
| 163 |
} |
| 164 |
want := []*SourceImportAuthor{ |
| 165 |
{ID: Int64(1), Name: String("a")}, |
| 166 |
{ID: Int64(2), Name: String("b")}, |
| 167 |
} |
| 168 |
if !cmp.Equal(got, want) { |
| 169 |
t.Errorf("CommitAuthors = %+v, want %+v", got, want) |
| 170 |
} |
| 171 |
|
| 172 |
const methodName = "CommitAuthors" |
| 173 |
testBadOptions(t, methodName, func() (err error) { |
| 174 |
_, _, err = client.Migrations.CommitAuthors(ctx, "\n", "\n") |
| 175 |
return err |
| 176 |
}) |
| 177 |
|
| 178 |
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { |
| 179 |
got, resp, err := client.Migrations.CommitAuthors(ctx, "o", "r") |
| 180 |
if got != nil { |
| 181 |
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) |
| 182 |
} |
| 183 |
return resp, err |
| 184 |
}) |
| 185 |
} |
| 186 |
|
| 187 |
func TestMigrationService_MapCommitAuthor(t *testing.T) { |
| 188 |
client, mux, _, teardown := setup() |
| 189 |
defer teardown() |
| 190 |
|
| 191 |
input := &SourceImportAuthor{Name: String("n"), Email: String("e")} |
| 192 |
|
| 193 |
mux.HandleFunc("/repos/o/r/import/authors/1", func(w http.ResponseWriter, r *http.Request) { |
| 194 |
v := new(SourceImportAuthor) |
| 195 |
assertNilError(t, json.NewDecoder(r.Body).Decode(v)) |
| 196 |
|
| 197 |
testMethod(t, r, "PATCH") |
| 198 |
if !cmp.Equal(v, input) { |
| 199 |
t.Errorf("Request body = %+v, want %+v", v, input) |
| 200 |
} |
| 201 |
|
| 202 |
fmt.Fprint(w, `{"id": 1}`) |
| 203 |
}) |
| 204 |
|
| 205 |
ctx := context.Background() |
| 206 |
got, _, err := client.Migrations.MapCommitAuthor(ctx, "o", "r", 1, input) |
| 207 |
if err != nil { |
| 208 |
t.Errorf("MapCommitAuthor returned error: %v", err) |
| 209 |
} |
| 210 |
want := &SourceImportAuthor{ID: Int64(1)} |
| 211 |
if !cmp.Equal(got, want) { |
| 212 |
t.Errorf("MapCommitAuthor = %+v, want %+v", got, want) |
| 213 |
} |
| 214 |
|
| 215 |
const methodName = "MapCommitAuthor" |
| 216 |
testBadOptions(t, methodName, func() (err error) { |
| 217 |
_, _, err = client.Migrations.MapCommitAuthor(ctx, "\n", "\n", 1, input) |
| 218 |
return err |
| 219 |
}) |
| 220 |
|
| 221 |
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { |
| 222 |
got, resp, err := client.Migrations.MapCommitAuthor(ctx, "o", "r", 1, input) |
| 223 |
if got != nil { |
| 224 |
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) |
| 225 |
} |
| 226 |
return resp, err |
| 227 |
}) |
| 228 |
} |
| 229 |
|
| 230 |
func TestMigrationService_SetLFSPreference(t *testing.T) { |
| 231 |
client, mux, _, teardown := setup() |
| 232 |
defer teardown() |
| 233 |
|
| 234 |
input := &Import{UseLFS: String("opt_in")} |
| 235 |
|
| 236 |
mux.HandleFunc("/repos/o/r/import/lfs", func(w http.ResponseWriter, r *http.Request) { |
| 237 |
v := new(Import) |
| 238 |
assertNilError(t, json.NewDecoder(r.Body).Decode(v)) |
| 239 |
|
| 240 |
testMethod(t, r, "PATCH") |
| 241 |
if !cmp.Equal(v, input) { |
| 242 |
t.Errorf("Request body = %+v, want %+v", v, input) |
| 243 |
} |
| 244 |
|
| 245 |
w.WriteHeader(http.StatusCreated) |
| 246 |
fmt.Fprint(w, `{"status":"importing"}`) |
| 247 |
}) |
| 248 |
|
| 249 |
ctx := context.Background() |
| 250 |
got, _, err := client.Migrations.SetLFSPreference(ctx, "o", "r", input) |
| 251 |
if err != nil { |
| 252 |
t.Errorf("SetLFSPreference returned error: %v", err) |
| 253 |
} |
| 254 |
want := &Import{Status: String("importing")} |
| 255 |
if !cmp.Equal(got, want) { |
| 256 |
t.Errorf("SetLFSPreference = %+v, want %+v", got, want) |
| 257 |
} |
| 258 |
|
| 259 |
const methodName = "SetLFSPreference" |
| 260 |
testBadOptions(t, methodName, func() (err error) { |
| 261 |
_, _, err = client.Migrations.SetLFSPreference(ctx, "\n", "\n", input) |
| 262 |
return err |
| 263 |
}) |
| 264 |
|
| 265 |
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { |
| 266 |
got, resp, err := client.Migrations.SetLFSPreference(ctx, "o", "r", input) |
| 267 |
if got != nil { |
| 268 |
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) |
| 269 |
} |
| 270 |
return resp, err |
| 271 |
}) |
| 272 |
} |
| 273 |
|
| 274 |
func TestMigrationService_LargeFiles(t *testing.T) { |
| 275 |
client, mux, _, teardown := setup() |
| 276 |
defer teardown() |
| 277 |
|
| 278 |
mux.HandleFunc("/repos/o/r/import/large_files", func(w http.ResponseWriter, r *http.Request) { |
| 279 |
testMethod(t, r, "GET") |
| 280 |
fmt.Fprint(w, `[{"oid":"a"},{"oid":"b"}]`) |
| 281 |
}) |
| 282 |
|
| 283 |
ctx := context.Background() |
| 284 |
got, _, err := client.Migrations.LargeFiles(ctx, "o", "r") |
| 285 |
if err != nil { |
| 286 |
t.Errorf("LargeFiles returned error: %v", err) |
| 287 |
} |
| 288 |
want := []*LargeFile{ |
| 289 |
{OID: String("a")}, |
| 290 |
{OID: String("b")}, |
| 291 |
} |
| 292 |
if !cmp.Equal(got, want) { |
| 293 |
t.Errorf("LargeFiles = %+v, want %+v", got, want) |
| 294 |
} |
| 295 |
|
| 296 |
const methodName = "LargeFiles" |
| 297 |
testBadOptions(t, methodName, func() (err error) { |
| 298 |
_, _, err = client.Migrations.LargeFiles(ctx, "\n", "\n") |
| 299 |
return err |
| 300 |
}) |
| 301 |
|
| 302 |
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { |
| 303 |
got, resp, err := client.Migrations.LargeFiles(ctx, "o", "r") |
| 304 |
if got != nil { |
| 305 |
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) |
| 306 |
} |
| 307 |
return resp, err |
| 308 |
}) |
| 309 |
} |
| 310 |
|
| 311 |
func TestMigrationService_CancelImport(t *testing.T) { |
| 312 |
client, mux, _, teardown := setup() |
| 313 |
defer teardown() |
| 314 |
|
| 315 |
mux.HandleFunc("/repos/o/r/import", func(w http.ResponseWriter, r *http.Request) { |
| 316 |
testMethod(t, r, "DELETE") |
| 317 |
w.WriteHeader(http.StatusNoContent) |
| 318 |
}) |
| 319 |
|
| 320 |
ctx := context.Background() |
| 321 |
_, err := client.Migrations.CancelImport(ctx, "o", "r") |
| 322 |
if err != nil { |
| 323 |
t.Errorf("CancelImport returned error: %v", err) |
| 324 |
} |
| 325 |
|
| 326 |
const methodName = "CancelImport" |
| 327 |
testBadOptions(t, methodName, func() (err error) { |
| 328 |
_, err = client.Migrations.CancelImport(ctx, "\n", "\n") |
| 329 |
return err |
| 330 |
}) |
| 331 |
|
| 332 |
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { |
| 333 |
return client.Migrations.CancelImport(ctx, "o", "r") |
| 334 |
}) |
| 335 |
} |
| 336 |
|
| 337 |
func TestLargeFile_Marshal(t *testing.T) { |
| 338 |
testJSONMarshal(t, &LargeFile{}, "{}") |
| 339 |
|
| 340 |
u := &LargeFile{ |
| 341 |
RefName: String("rn"), |
| 342 |
Path: String("p"), |
| 343 |
OID: String("oid"), |
| 344 |
Size: Int(1), |
| 345 |
} |
| 346 |
|
| 347 |
want := `{ |
| 348 |
"ref_name": "rn", |
| 349 |
"path": "p", |
| 350 |
"oid": "oid", |
| 351 |
"size": 1 |
| 352 |
}` |
| 353 |
|
| 354 |
testJSONMarshal(t, u, want) |
| 355 |
} |
| 356 |
|
| 357 |
func TestSourceImportAuthor_Marshal(t *testing.T) { |
| 358 |
testJSONMarshal(t, &SourceImportAuthor{}, "{}") |
| 359 |
|
| 360 |
u := &SourceImportAuthor{ |
| 361 |
ID: Int64(1), |
| 362 |
RemoteID: String("rid"), |
| 363 |
RemoteName: String("rn"), |
| 364 |
Email: String("e"), |
| 365 |
Name: String("n"), |
| 366 |
URL: String("url"), |
| 367 |
ImportURL: String("iurl"), |
| 368 |
} |
| 369 |
|
| 370 |
want := `{ |
| 371 |
"id": 1, |
| 372 |
"remote_id": "rid", |
| 373 |
"remote_name": "rn", |
| 374 |
"email": "e", |
| 375 |
"name": "n", |
| 376 |
"url": "url", |
| 377 |
"import_url": "iurl" |
| 378 |
}` |
| 379 |
|
| 380 |
testJSONMarshal(t, u, want) |
| 381 |
} |
| 382 |
|
| 383 |
func TestImport_Marshal(t *testing.T) { |
| 384 |
testJSONMarshal(t, &Import{}, "{}") |
| 385 |
|
| 386 |
u := &Import{ |
| 387 |
VCSURL: String("vcsurl"), |
| 388 |
VCS: String("vcs"), |
| 389 |
VCSUsername: String("vcsusr"), |
| 390 |
VCSPassword: String("vcspass"), |
| 391 |
TFVCProject: String("tfvcp"), |
| 392 |
UseLFS: String("uselfs"), |
| 393 |
HasLargeFiles: Bool(false), |
| 394 |
LargeFilesSize: Int(1), |
| 395 |
LargeFilesCount: Int(1), |
| 396 |
Status: String("status"), |
| 397 |
CommitCount: Int(1), |
| 398 |
StatusText: String("statustxt"), |
| 399 |
AuthorsCount: Int(1), |
| 400 |
Percent: Int(1), |
| 401 |
PushPercent: Int(1), |
| 402 |
URL: String("url"), |
| 403 |
HTMLURL: String("hurl"), |
| 404 |
AuthorsURL: String("aurl"), |
| 405 |
RepositoryURL: String("rurl"), |
| 406 |
Message: String("msg"), |
| 407 |
FailedStep: String("fs"), |
| 408 |
HumanName: String("hn"), |
| 409 |
ProjectChoices: []*Import{{VCSURL: String("vcsurl")}}, |
| 410 |
} |
| 411 |
|
| 412 |
want := `{ |
| 413 |
"vcs_url": "vcsurl", |
| 414 |
"vcs": "vcs", |
| 415 |
"vcs_username": "vcsusr", |
| 416 |
"vcs_password": "vcspass", |
| 417 |
"tfvc_project": "tfvcp", |
| 418 |
"use_lfs": "uselfs", |
| 419 |
"has_large_files": false, |
| 420 |
"large_files_size": 1, |
| 421 |
"large_files_count": 1, |
| 422 |
"status": "status", |
| 423 |
"commit_count": 1, |
| 424 |
"status_text": "statustxt", |
| 425 |
"authors_count": 1, |
| 426 |
"percent": 1, |
| 427 |
"push_percent": 1, |
| 428 |
"url": "url", |
| 429 |
"html_url": "hurl", |
| 430 |
"authors_url": "aurl", |
| 431 |
"repository_url": "rurl", |
| 432 |
"message": "msg", |
| 433 |
"failed_step": "fs", |
| 434 |
"human_name": "hn", |
| 435 |
"project_choices": [ |
| 436 |
{ |
| 437 |
"vcs_url": "vcsurl" |
| 438 |
} |
| 439 |
] |
| 440 |
}` |
| 441 |
|
| 442 |
testJSONMarshal(t, u, want) |
| 443 |
} |