github.com/cli/cli@v1.14.1-0.20210902173923-1af6a669e342/pkg/cmd/repo/create/create_test.go (about) 1 package create 2 3 import ( 4 "bytes" 5 "encoding/json" 6 "io/ioutil" 7 "net/http" 8 "testing" 9 10 "github.com/MakeNowJust/heredoc" 11 "github.com/cli/cli/internal/config" 12 "github.com/cli/cli/internal/run" 13 "github.com/cli/cli/pkg/cmdutil" 14 "github.com/cli/cli/pkg/httpmock" 15 "github.com/cli/cli/pkg/iostreams" 16 "github.com/cli/cli/pkg/prompt" 17 "github.com/cli/cli/test" 18 "github.com/google/shlex" 19 "github.com/stretchr/testify/assert" 20 ) 21 22 func runCommand(httpClient *http.Client, cli string, isTTY bool) (*test.CmdOut, error) { 23 io, _, stdout, stderr := iostreams.Test() 24 io.SetStdoutTTY(isTTY) 25 io.SetStdinTTY(isTTY) 26 fac := &cmdutil.Factory{ 27 IOStreams: io, 28 HttpClient: func() (*http.Client, error) { 29 return httpClient, nil 30 }, 31 Config: func() (config.Config, error) { 32 return config.NewBlankConfig(), nil 33 }, 34 } 35 36 cmd := NewCmdCreate(fac, nil) 37 38 // TODO STUPID HACK 39 // cobra aggressively adds help to all commands. since we're not running through the root command 40 // (which manages help when running for real) and since create has a '-h' flag (for homepage), 41 // cobra blows up when it tried to add a help flag and -h is already in use. This hack adds a 42 // dummy help flag with a random shorthand to get around this. 43 cmd.Flags().BoolP("help", "x", false, "") 44 45 argv, err := shlex.Split(cli) 46 cmd.SetArgs(argv) 47 48 cmd.SetIn(&bytes.Buffer{}) 49 cmd.SetOut(&bytes.Buffer{}) 50 cmd.SetErr(&bytes.Buffer{}) 51 52 if err != nil { 53 panic(err) 54 } 55 56 _, err = cmd.ExecuteC() 57 58 if err != nil { 59 return nil, err 60 } 61 62 return &test.CmdOut{ 63 OutBuf: stdout, 64 ErrBuf: stderr}, nil 65 } 66 67 func TestRepoCreate(t *testing.T) { 68 reg := &httpmock.Registry{} 69 reg.Register( 70 httpmock.GraphQL(`mutation RepositoryCreate\b`), 71 httpmock.StringResponse(` 72 { "data": { "createRepository": { 73 "repository": { 74 "id": "REPOID", 75 "url": "https://github.com/OWNER/REPO", 76 "name": "REPO", 77 "owner": { 78 "login": "OWNER" 79 } 80 } 81 } } }`)) 82 83 httpClient := &http.Client{Transport: reg} 84 85 cs, cmdTeardown := run.Stub() 86 defer cmdTeardown(t) 87 88 cs.Register(`git remote add -f origin https://github\.com/OWNER/REPO\.git`, 0, "") 89 cs.Register(`git rev-parse --show-toplevel`, 0, "") 90 91 as, surveyTearDown := prompt.InitAskStubber() 92 defer surveyTearDown() 93 94 as.Stub([]*prompt.QuestionStub{ 95 { 96 Name: "repoVisibility", 97 Value: "PRIVATE", 98 }, 99 }) 100 101 as.Stub([]*prompt.QuestionStub{ 102 { 103 Name: "addGitIgnore", 104 Value: false, 105 }, 106 }) 107 108 as.Stub([]*prompt.QuestionStub{ 109 { 110 Name: "addLicense", 111 Value: false, 112 }, 113 }) 114 115 as.Stub([]*prompt.QuestionStub{ 116 { 117 Name: "confirmSubmit", 118 Value: true, 119 }, 120 }) 121 122 output, err := runCommand(httpClient, "REPO", true) 123 if err != nil { 124 t.Errorf("error running command `repo create`: %v", err) 125 } 126 127 assert.Equal(t, "", output.String()) 128 assert.Equal(t, "✓ Created repository OWNER/REPO on GitHub\n✓ Added remote https://github.com/OWNER/REPO.git\n", output.Stderr()) 129 130 var reqBody struct { 131 Query string 132 Variables struct { 133 Input map[string]interface{} 134 } 135 } 136 137 if len(reg.Requests) != 1 { 138 t.Fatalf("expected 1 HTTP request, got %d", len(reg.Requests)) 139 } 140 141 bodyBytes, _ := ioutil.ReadAll(reg.Requests[0].Body) 142 _ = json.Unmarshal(bodyBytes, &reqBody) 143 if repoName := reqBody.Variables.Input["name"].(string); repoName != "REPO" { 144 t.Errorf("expected %q, got %q", "REPO", repoName) 145 } 146 if repoVisibility := reqBody.Variables.Input["visibility"].(string); repoVisibility != "PRIVATE" { 147 t.Errorf("expected %q, got %q", "PRIVATE", repoVisibility) 148 } 149 if _, ownerSet := reqBody.Variables.Input["ownerId"]; ownerSet { 150 t.Error("expected ownerId not to be set") 151 } 152 } 153 154 func TestRepoCreate_outsideGitWorkDir(t *testing.T) { 155 reg := &httpmock.Registry{} 156 reg.Register( 157 httpmock.GraphQL(`mutation RepositoryCreate\b`), 158 httpmock.StringResponse(` 159 { "data": { "createRepository": { 160 "repository": { 161 "id": "REPOID", 162 "url": "https://github.com/OWNER/REPO", 163 "name": "REPO", 164 "owner": { 165 "login": "OWNER" 166 } 167 } 168 } } }`)) 169 170 httpClient := &http.Client{Transport: reg} 171 172 cs, cmdTeardown := run.Stub() 173 defer cmdTeardown(t) 174 175 cs.Register(`git rev-parse --show-toplevel`, 1, "") 176 cs.Register(`git init REPO`, 0, "") 177 cs.Register(`git -C REPO remote add origin https://github\.com/OWNER/REPO\.git`, 0, "") 178 179 output, err := runCommand(httpClient, "REPO --private --confirm", false) 180 if err != nil { 181 t.Errorf("error running command `repo create`: %v", err) 182 } 183 184 assert.Equal(t, "https://github.com/OWNER/REPO\n", output.String()) 185 assert.Equal(t, "", output.Stderr()) 186 187 var reqBody struct { 188 Query string 189 Variables struct { 190 Input map[string]interface{} 191 } 192 } 193 194 if len(reg.Requests) != 1 { 195 t.Fatalf("expected 1 HTTP request, got %d", len(reg.Requests)) 196 } 197 198 bodyBytes, _ := ioutil.ReadAll(reg.Requests[0].Body) 199 _ = json.Unmarshal(bodyBytes, &reqBody) 200 if repoName := reqBody.Variables.Input["name"].(string); repoName != "REPO" { 201 t.Errorf("expected %q, got %q", "REPO", repoName) 202 } 203 if repoVisibility := reqBody.Variables.Input["visibility"].(string); repoVisibility != "PRIVATE" { 204 t.Errorf("expected %q, got %q", "PRIVATE", repoVisibility) 205 } 206 if _, ownerSet := reqBody.Variables.Input["ownerId"]; ownerSet { 207 t.Error("expected ownerId not to be set") 208 } 209 } 210 211 func TestRepoCreate_org(t *testing.T) { 212 reg := &httpmock.Registry{} 213 reg.Register( 214 httpmock.REST("GET", "users/ORG"), 215 httpmock.StringResponse(` 216 { "node_id": "ORGID" 217 }`)) 218 reg.Register( 219 httpmock.GraphQL(`mutation RepositoryCreate\b`), 220 httpmock.StringResponse(` 221 { "data": { "createRepository": { 222 "repository": { 223 "id": "REPOID", 224 "url": "https://github.com/ORG/REPO", 225 "name": "REPO", 226 "owner": { 227 "login": "ORG" 228 } 229 } 230 } } }`)) 231 httpClient := &http.Client{Transport: reg} 232 233 cs, cmdTeardown := run.Stub() 234 defer cmdTeardown(t) 235 236 cs.Register(`git remote add -f origin https://github\.com/ORG/REPO\.git`, 0, "") 237 cs.Register(`git rev-parse --show-toplevel`, 0, "") 238 239 as, surveyTearDown := prompt.InitAskStubber() 240 defer surveyTearDown() 241 242 as.Stub([]*prompt.QuestionStub{ 243 { 244 Name: "repoVisibility", 245 Value: "PRIVATE", 246 }, 247 }) 248 249 as.Stub([]*prompt.QuestionStub{ 250 { 251 Name: "addGitIgnore", 252 Value: false, 253 }, 254 }) 255 256 as.Stub([]*prompt.QuestionStub{ 257 { 258 Name: "addLicense", 259 Value: false, 260 }, 261 }) 262 263 as.Stub([]*prompt.QuestionStub{ 264 { 265 Name: "confirmSubmit", 266 Value: true, 267 }, 268 }) 269 270 output, err := runCommand(httpClient, "ORG/REPO", true) 271 if err != nil { 272 t.Errorf("error running command `repo create`: %v", err) 273 } 274 275 assert.Equal(t, "", output.String()) 276 assert.Equal(t, "✓ Created repository ORG/REPO on GitHub\n✓ Added remote https://github.com/ORG/REPO.git\n", output.Stderr()) 277 278 var reqBody struct { 279 Query string 280 Variables struct { 281 Input map[string]interface{} 282 } 283 } 284 285 if len(reg.Requests) != 2 { 286 t.Fatalf("expected 2 HTTP requests, got %d", len(reg.Requests)) 287 } 288 289 assert.Equal(t, "/users/ORG", reg.Requests[0].URL.Path) 290 291 bodyBytes, _ := ioutil.ReadAll(reg.Requests[1].Body) 292 _ = json.Unmarshal(bodyBytes, &reqBody) 293 if orgID := reqBody.Variables.Input["ownerId"].(string); orgID != "ORGID" { 294 t.Errorf("expected %q, got %q", "ORGID", orgID) 295 } 296 if _, teamSet := reqBody.Variables.Input["teamId"]; teamSet { 297 t.Error("expected teamId not to be set") 298 } 299 } 300 301 func TestRepoCreate_orgWithTeam(t *testing.T) { 302 reg := &httpmock.Registry{} 303 reg.Register( 304 httpmock.REST("GET", "orgs/ORG/teams/monkeys"), 305 httpmock.StringResponse(` 306 { "node_id": "TEAMID", 307 "organization": { "node_id": "ORGID" } 308 }`)) 309 reg.Register( 310 httpmock.GraphQL(`mutation RepositoryCreate\b`), 311 httpmock.StringResponse(` 312 { "data": { "createRepository": { 313 "repository": { 314 "id": "REPOID", 315 "url": "https://github.com/ORG/REPO", 316 "name": "REPO", 317 "owner": { 318 "login": "ORG" 319 } 320 } 321 } } }`)) 322 httpClient := &http.Client{Transport: reg} 323 324 cs, cmdTeardown := run.Stub() 325 defer cmdTeardown(t) 326 327 cs.Register(`git remote add -f origin https://github\.com/ORG/REPO\.git`, 0, "") 328 cs.Register(`git rev-parse --show-toplevel`, 0, "") 329 330 as, surveyTearDown := prompt.InitAskStubber() 331 defer surveyTearDown() 332 333 as.Stub([]*prompt.QuestionStub{ 334 { 335 Name: "repoVisibility", 336 Value: "PRIVATE", 337 }, 338 }) 339 340 as.Stub([]*prompt.QuestionStub{ 341 { 342 Name: "addGitIgnore", 343 Value: false, 344 }, 345 }) 346 347 as.Stub([]*prompt.QuestionStub{ 348 { 349 Name: "addLicense", 350 Value: false, 351 }, 352 }) 353 354 as.Stub([]*prompt.QuestionStub{ 355 { 356 Name: "confirmSubmit", 357 Value: true, 358 }, 359 }) 360 361 output, err := runCommand(httpClient, "ORG/REPO --team monkeys", true) 362 if err != nil { 363 t.Errorf("error running command `repo create`: %v", err) 364 } 365 366 assert.Equal(t, "", output.String()) 367 assert.Equal(t, "✓ Created repository ORG/REPO on GitHub\n✓ Added remote https://github.com/ORG/REPO.git\n", output.Stderr()) 368 369 var reqBody struct { 370 Query string 371 Variables struct { 372 Input map[string]interface{} 373 } 374 } 375 376 if len(reg.Requests) != 2 { 377 t.Fatalf("expected 2 HTTP requests, got %d", len(reg.Requests)) 378 } 379 380 assert.Equal(t, "/orgs/ORG/teams/monkeys", reg.Requests[0].URL.Path) 381 382 bodyBytes, _ := ioutil.ReadAll(reg.Requests[1].Body) 383 _ = json.Unmarshal(bodyBytes, &reqBody) 384 if orgID := reqBody.Variables.Input["ownerId"].(string); orgID != "ORGID" { 385 t.Errorf("expected %q, got %q", "ORGID", orgID) 386 } 387 if teamID := reqBody.Variables.Input["teamId"].(string); teamID != "TEAMID" { 388 t.Errorf("expected %q, got %q", "TEAMID", teamID) 389 } 390 } 391 392 func TestRepoCreate_template(t *testing.T) { 393 reg := &httpmock.Registry{} 394 defer reg.Verify(t) 395 reg.Register( 396 httpmock.GraphQL(`mutation CloneTemplateRepository\b`), 397 httpmock.StringResponse(` 398 { "data": { "cloneTemplateRepository": { 399 "repository": { 400 "id": "REPOID", 401 "name": "REPO", 402 "owner": { 403 "login": "OWNER" 404 }, 405 "url": "https://github.com/OWNER/REPO" 406 } 407 } } }`)) 408 409 reg.StubRepoInfoResponse("OWNER", "REPO", "main") 410 411 reg.Register( 412 httpmock.GraphQL(`query UserCurrent\b`), 413 httpmock.StringResponse(`{"data":{"viewer":{"ID":"OWNERID"}}}`)) 414 415 httpClient := &http.Client{Transport: reg} 416 417 cs, cmdTeardown := run.Stub() 418 defer cmdTeardown(t) 419 420 cs.Register(`git rev-parse --show-toplevel`, 1, "") 421 cs.Register(`git init REPO`, 0, "") 422 cs.Register(`git -C REPO remote add`, 0, "") 423 cs.Register(`git -C REPO fetch origin \+refs/heads/main:refs/remotes/origin/main`, 0, "") 424 cs.Register(`git -C REPO checkout main`, 0, "") 425 426 _, surveyTearDown := prompt.InitAskStubber() 427 defer surveyTearDown() 428 429 output, err := runCommand(httpClient, "REPO -y --private --template='OWNER/REPO'", true) 430 if err != nil { 431 t.Errorf("error running command `repo create`: %v", err) 432 return 433 } 434 435 assert.Equal(t, "", output.String()) 436 assert.Equal(t, heredoc.Doc(` 437 ✓ Created repository OWNER/REPO on GitHub 438 ✓ Initialized repository in "REPO" 439 `), output.Stderr()) 440 441 var reqBody struct { 442 Query string 443 Variables struct { 444 Input map[string]interface{} 445 } 446 } 447 448 bodyBytes, _ := ioutil.ReadAll(reg.Requests[2].Body) 449 _ = json.Unmarshal(bodyBytes, &reqBody) 450 if repoName := reqBody.Variables.Input["name"].(string); repoName != "REPO" { 451 t.Errorf("expected %q, got %q", "REPO", repoName) 452 } 453 if repoVisibility := reqBody.Variables.Input["visibility"].(string); repoVisibility != "PRIVATE" { 454 t.Errorf("expected %q, got %q", "PRIVATE", repoVisibility) 455 } 456 if ownerId := reqBody.Variables.Input["ownerId"].(string); ownerId != "OWNERID" { 457 t.Errorf("expected %q, got %q", "OWNERID", ownerId) 458 } 459 } 460 461 func TestRepoCreate_withoutNameArg(t *testing.T) { 462 reg := &httpmock.Registry{} 463 reg.Register( 464 httpmock.REST("GET", "users/OWNER"), 465 httpmock.StringResponse(`{ "node_id": "OWNERID" }`)) 466 reg.Register( 467 httpmock.GraphQL(`mutation RepositoryCreate\b`), 468 httpmock.StringResponse(` 469 { "data": { "createRepository": { 470 "repository": { 471 "id": "REPOID", 472 "url": "https://github.com/OWNER/REPO", 473 "name": "REPO", 474 "owner": { 475 "login": "OWNER" 476 } 477 } 478 } } }`)) 479 httpClient := &http.Client{Transport: reg} 480 481 cs, cmdTeardown := run.Stub() 482 defer cmdTeardown(t) 483 484 cs.Register(`git remote add -f origin https://github\.com/OWNER/REPO\.git`, 0, "") 485 cs.Register(`git rev-parse --show-toplevel`, 0, "") 486 487 as, surveyTearDown := prompt.InitAskStubber() 488 defer surveyTearDown() 489 490 as.Stub([]*prompt.QuestionStub{ 491 { 492 Name: "repoName", 493 Value: "OWNER/REPO", 494 }, 495 { 496 Name: "repoDescription", 497 Value: "DESCRIPTION", 498 }, 499 { 500 Name: "repoVisibility", 501 Value: "PRIVATE", 502 }, 503 }) 504 505 as.Stub([]*prompt.QuestionStub{ 506 { 507 Name: "confirmSubmit", 508 Value: true, 509 }, 510 }) 511 512 output, err := runCommand(httpClient, "", true) 513 if err != nil { 514 t.Errorf("error running command `repo create`: %v", err) 515 } 516 517 assert.Equal(t, "", output.String()) 518 assert.Equal(t, "✓ Created repository OWNER/REPO on GitHub\n✓ Added remote https://github.com/OWNER/REPO.git\n", output.Stderr()) 519 520 var reqBody struct { 521 Query string 522 Variables struct { 523 Input map[string]interface{} 524 } 525 } 526 527 if len(reg.Requests) != 2 { 528 t.Fatalf("expected 2 HTTP request, got %d", len(reg.Requests)) 529 } 530 531 bodyBytes, _ := ioutil.ReadAll(reg.Requests[1].Body) 532 _ = json.Unmarshal(bodyBytes, &reqBody) 533 if repoName := reqBody.Variables.Input["name"].(string); repoName != "REPO" { 534 t.Errorf("expected %q, got %q", "REPO", repoName) 535 } 536 if repoVisibility := reqBody.Variables.Input["visibility"].(string); repoVisibility != "PRIVATE" { 537 t.Errorf("expected %q, got %q", "PRIVATE", repoVisibility) 538 } 539 if ownerId := reqBody.Variables.Input["ownerId"].(string); ownerId != "OWNERID" { 540 t.Errorf("expected %q, got %q", "OWNERID", ownerId) 541 } 542 } 543 544 func TestRepoCreate_WithGitIgnore(t *testing.T) { 545 cs, cmdTeardown := run.Stub() 546 defer cmdTeardown(t) 547 548 cs.Register(`git remote add -f origin https://github\.com/OWNER/REPO\.git`, 0, "") 549 cs.Register(`git rev-parse --show-toplevel`, 0, "") 550 551 as, surveyTearDown := prompt.InitAskStubber() 552 defer surveyTearDown() 553 554 as.Stub([]*prompt.QuestionStub{ 555 { 556 Name: "repoVisibility", 557 Value: "PRIVATE", 558 }, 559 }) 560 561 as.Stub([]*prompt.QuestionStub{ 562 { 563 Name: "addGitIgnore", 564 Value: true, 565 }, 566 }) 567 568 as.Stub([]*prompt.QuestionStub{ 569 { 570 Name: "chooseGitIgnore", 571 Value: "Go", 572 }, 573 }) 574 575 as.Stub([]*prompt.QuestionStub{ 576 { 577 Name: "addLicense", 578 Value: false, 579 }, 580 }) 581 582 as.Stub([]*prompt.QuestionStub{ 583 { 584 Name: "confirmSubmit", 585 Value: true, 586 }, 587 }) 588 589 reg := &httpmock.Registry{} 590 reg.Register( 591 httpmock.REST("GET", "users/OWNER"), 592 httpmock.StringResponse(`{ "node_id": "OWNERID" }`)) 593 reg.Register( 594 httpmock.REST("GET", "gitignore/templates"), 595 httpmock.StringResponse(`["Actionscript","Android","AppceleratorTitanium","Autotools","Bancha","C","C++","Go"]`)) 596 reg.Register( 597 httpmock.REST("POST", "user/repos"), 598 httpmock.StringResponse(`{"name":"REPO", "owner":{"login": "OWNER"}, "html_url":"https://github.com/OWNER/REPO"}`)) 599 httpClient := &http.Client{Transport: reg} 600 601 output, err := runCommand(httpClient, "OWNER/REPO", true) 602 if err != nil { 603 t.Errorf("error running command `repo create`: %v", err) 604 } 605 606 assert.Equal(t, "", output.String()) 607 assert.Equal(t, "✓ Created repository OWNER/REPO on GitHub\n✓ Added remote https://github.com/OWNER/REPO.git\n", output.Stderr()) 608 609 if len(reg.Requests) != 3 { 610 t.Fatalf("expected 3 HTTP request, got %d", len(reg.Requests)) 611 } 612 613 reqBody := make(map[string]interface{}) 614 dec := json.NewDecoder(reg.Requests[2].Body) 615 assert.NoError(t, dec.Decode(&reqBody)) 616 617 if gitignore := reqBody["gitignore_template"]; gitignore != "Go" { 618 t.Errorf("expected %q, got %q", "Go", gitignore) 619 } 620 if license := reqBody["license_template"]; license != nil { 621 t.Errorf("expected %v, got %v", nil, license) 622 } 623 } 624 625 func TestRepoCreate_WithBothGitIgnoreLicense(t *testing.T) { 626 cs, cmdTeardown := run.Stub() 627 defer cmdTeardown(t) 628 629 cs.Register(`git remote add -f origin https://github\.com/OWNER/REPO\.git`, 0, "") 630 cs.Register(`git rev-parse --show-toplevel`, 0, "") 631 632 as, surveyTearDown := prompt.InitAskStubber() 633 defer surveyTearDown() 634 635 as.Stub([]*prompt.QuestionStub{ 636 { 637 Name: "repoVisibility", 638 Value: "PRIVATE", 639 }, 640 }) 641 642 as.Stub([]*prompt.QuestionStub{ 643 { 644 Name: "addGitIgnore", 645 Value: true, 646 }, 647 }) 648 649 as.Stub([]*prompt.QuestionStub{ 650 { 651 Name: "chooseGitIgnore", 652 Value: "Go", 653 }, 654 }) 655 656 as.Stub([]*prompt.QuestionStub{ 657 { 658 Name: "addLicense", 659 Value: true, 660 }, 661 }) 662 663 as.Stub([]*prompt.QuestionStub{ 664 { 665 Name: "chooseLicense", 666 Value: "GNU Lesser General Public License v3.0", 667 }, 668 }) 669 670 as.Stub([]*prompt.QuestionStub{ 671 { 672 Name: "confirmSubmit", 673 Value: true, 674 }, 675 }) 676 677 reg := &httpmock.Registry{} 678 reg.Register( 679 httpmock.REST("GET", "users/OWNER"), 680 httpmock.StringResponse(`{ "node_id": "OWNERID" }`)) 681 reg.Register( 682 httpmock.REST("GET", "gitignore/templates"), 683 httpmock.StringResponse(`["Actionscript","Android","AppceleratorTitanium","Autotools","Bancha","C","C++","Go"]`)) 684 reg.Register( 685 httpmock.REST("GET", "licenses"), 686 httpmock.StringResponse(`[{"key": "mit","name": "MIT License"},{"key": "lgpl-3.0","name": "GNU Lesser General Public License v3.0"}]`)) 687 reg.Register( 688 httpmock.REST("POST", "user/repos"), 689 httpmock.StringResponse(`{"name":"REPO", "owner":{"login": "OWNER"}, "html_url":"https://github.com/OWNER/REPO"}`)) 690 httpClient := &http.Client{Transport: reg} 691 692 output, err := runCommand(httpClient, "OWNER/REPO", true) 693 if err != nil { 694 t.Errorf("error running command `repo create`: %v", err) 695 } 696 697 assert.Equal(t, "", output.String()) 698 assert.Equal(t, "✓ Created repository OWNER/REPO on GitHub\n✓ Added remote https://github.com/OWNER/REPO.git\n", output.Stderr()) 699 700 if len(reg.Requests) != 4 { 701 t.Fatalf("expected 4 HTTP request, got %d", len(reg.Requests)) 702 } 703 704 reqBody := make(map[string]interface{}) 705 dec := json.NewDecoder(reg.Requests[3].Body) 706 assert.NoError(t, dec.Decode(&reqBody)) 707 708 if gitignore := reqBody["gitignore_template"]; gitignore != "Go" { 709 t.Errorf("expected %q, got %q", "Go", gitignore) 710 } 711 if license := reqBody["license_template"]; license != "lgpl-3.0" { 712 t.Errorf("expected %q, got %q", "lgpl-3.0", license) 713 } 714 } 715 716 func TestRepoCreate_WithConfirmFlag(t *testing.T) { 717 cs, cmdTeardown := run.Stub() 718 defer cmdTeardown(t) 719 720 cs.Register(`git remote add -f origin https://github\.com/OWNER/REPO\.git`, 0, "") 721 cs.Register(`git rev-parse --show-toplevel`, 0, "") 722 723 reg := &httpmock.Registry{} 724 725 reg.Register( 726 httpmock.GraphQL(`mutation RepositoryCreate\b`), 727 httpmock.StringResponse(` 728 { "data": { "createRepository": { 729 "repository": { 730 "id": "REPOID", 731 "url": "https://github.com/OWNER/REPO", 732 "name": "REPO", 733 "owner": { 734 "login": "OWNER" 735 } 736 } 737 } } }`), 738 ) 739 740 reg.Register( 741 httpmock.REST("GET", "users/OWNER"), 742 httpmock.StringResponse(`{ "node_id": "OWNERID" }`), 743 ) 744 745 httpClient := &http.Client{Transport: reg} 746 747 in := "OWNER/REPO --confirm --private" 748 output, err := runCommand(httpClient, in, true) 749 if err != nil { 750 t.Errorf("error running command `repo create %v`: %v", in, err) 751 } 752 753 assert.Equal(t, "", output.String()) 754 assert.Equal(t, "✓ Created repository OWNER/REPO on GitHub\n✓ Added remote https://github.com/OWNER/REPO.git\n", output.Stderr()) 755 756 var reqBody struct { 757 Query string 758 Variables struct { 759 Input map[string]interface{} 760 } 761 } 762 763 if len(reg.Requests) != 2 { 764 t.Fatalf("expected 2 HTTP request, got %d", len(reg.Requests)) 765 } 766 767 bodyBytes, _ := ioutil.ReadAll(reg.Requests[1].Body) 768 _ = json.Unmarshal(bodyBytes, &reqBody) 769 if repoName := reqBody.Variables.Input["name"].(string); repoName != "REPO" { 770 t.Errorf("expected %q, got %q", "REPO", repoName) 771 } 772 if repoVisibility := reqBody.Variables.Input["visibility"].(string); repoVisibility != "PRIVATE" { 773 t.Errorf("expected %q, got %q", "PRIVATE", repoVisibility) 774 } 775 }