1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070
|
package extension
import (
"errors"
"fmt"
"io"
"net/http"
"net/url"
"os"
"strings"
"testing"
"github.com/MakeNowJust/heredoc"
"github.com/cli/cli/v2/internal/browser"
"github.com/cli/cli/v2/internal/config"
"github.com/cli/cli/v2/internal/ghrepo"
"github.com/cli/cli/v2/internal/prompter"
"github.com/cli/cli/v2/pkg/cmdutil"
"github.com/cli/cli/v2/pkg/extensions"
"github.com/cli/cli/v2/pkg/httpmock"
"github.com/cli/cli/v2/pkg/iostreams"
"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
)
func TestNewCmdExtension(t *testing.T) {
tempDir := t.TempDir()
oldWd, _ := os.Getwd()
assert.NoError(t, os.Chdir(tempDir))
t.Cleanup(func() { _ = os.Chdir(oldWd) })
tests := []struct {
name string
args []string
managerStubs func(em *extensions.ExtensionManagerMock) func(*testing.T)
prompterStubs func(pm *prompter.PrompterMock)
httpStubs func(reg *httpmock.Registry)
browseStubs func(*browser.Stub) func(*testing.T)
isTTY bool
wantErr bool
errMsg string
wantStdout string
wantStderr string
}{
{
name: "search for extensions",
args: []string{"search"},
managerStubs: func(em *extensions.ExtensionManagerMock) func(*testing.T) {
em.ListFunc = func() []extensions.Extension {
return []extensions.Extension{
&extensions.ExtensionMock{
URLFunc: func() string {
return "https://github.com/vilmibm/gh-screensaver"
},
},
&extensions.ExtensionMock{
URLFunc: func() string {
return "https://github.com/github/gh-gei"
},
},
}
}
return func(t *testing.T) {
listCalls := em.ListCalls()
assert.Equal(t, 1, len(listCalls))
}
},
httpStubs: func(reg *httpmock.Registry) {
values := url.Values{
"page": []string{"1"},
"per_page": []string{"30"},
"q": []string{"topic:gh-extension"},
}
reg.Register(
httpmock.QueryMatcher("GET", "search/repositories", values),
httpmock.JSONResponse(searchResults(4)),
)
},
isTTY: true,
wantStdout: "Showing 4 of 4 extensions\n\n REPO DESCRIPTION\n✓ vilmibm/gh-screensaver terminal animations\n cli/gh-cool it's just cool ok\n samcoe/gh-triage helps with triage\n✓ github/gh-gei something something enterprise\n",
},
{
name: "search for extensions non-tty",
args: []string{"search"},
managerStubs: func(em *extensions.ExtensionManagerMock) func(*testing.T) {
em.ListFunc = func() []extensions.Extension {
return []extensions.Extension{
&extensions.ExtensionMock{
URLFunc: func() string {
return "https://github.com/vilmibm/gh-screensaver"
},
},
&extensions.ExtensionMock{
URLFunc: func() string {
return "https://github.com/github/gh-gei"
},
},
}
}
return func(t *testing.T) {
listCalls := em.ListCalls()
assert.Equal(t, 1, len(listCalls))
}
},
httpStubs: func(reg *httpmock.Registry) {
values := url.Values{
"page": []string{"1"},
"per_page": []string{"30"},
"q": []string{"topic:gh-extension"},
}
reg.Register(
httpmock.QueryMatcher("GET", "search/repositories", values),
httpmock.JSONResponse(searchResults(4)),
)
},
wantStdout: "installed\tvilmibm/gh-screensaver\tterminal animations\n\tcli/gh-cool\tit's just cool ok\n\tsamcoe/gh-triage\thelps with triage\ninstalled\tgithub/gh-gei\tsomething something enterprise\n",
},
{
name: "search for extensions with keywords",
args: []string{"search", "screen"},
managerStubs: func(em *extensions.ExtensionManagerMock) func(*testing.T) {
em.ListFunc = func() []extensions.Extension {
return []extensions.Extension{
&extensions.ExtensionMock{
URLFunc: func() string {
return "https://github.com/vilmibm/gh-screensaver"
},
},
&extensions.ExtensionMock{
URLFunc: func() string {
return "https://github.com/github/gh-gei"
},
},
}
}
return func(t *testing.T) {
listCalls := em.ListCalls()
assert.Equal(t, 1, len(listCalls))
}
},
httpStubs: func(reg *httpmock.Registry) {
values := url.Values{
"page": []string{"1"},
"per_page": []string{"30"},
"q": []string{"screen topic:gh-extension"},
}
results := searchResults(1)
reg.Register(
httpmock.QueryMatcher("GET", "search/repositories", values),
httpmock.JSONResponse(results),
)
},
wantStdout: "installed\tvilmibm/gh-screensaver\tterminal animations\n",
},
{
name: "search for extensions with parameter flags",
args: []string{"search", "--limit", "1", "--order", "asc", "--sort", "stars"},
managerStubs: func(em *extensions.ExtensionManagerMock) func(*testing.T) {
em.ListFunc = func() []extensions.Extension {
return []extensions.Extension{}
}
return func(t *testing.T) {
listCalls := em.ListCalls()
assert.Equal(t, 1, len(listCalls))
}
},
httpStubs: func(reg *httpmock.Registry) {
values := url.Values{
"page": []string{"1"},
"order": []string{"asc"},
"sort": []string{"stars"},
"per_page": []string{"1"},
"q": []string{"topic:gh-extension"},
}
results := searchResults(1)
reg.Register(
httpmock.QueryMatcher("GET", "search/repositories", values),
httpmock.JSONResponse(results),
)
},
wantStdout: "\tvilmibm/gh-screensaver\tterminal animations\n",
},
{
name: "search for extensions with qualifier flags",
args: []string{"search", "--license", "GPLv3", "--owner", "jillvalentine"},
managerStubs: func(em *extensions.ExtensionManagerMock) func(*testing.T) {
em.ListFunc = func() []extensions.Extension {
return []extensions.Extension{}
}
return func(t *testing.T) {
listCalls := em.ListCalls()
assert.Equal(t, 1, len(listCalls))
}
},
httpStubs: func(reg *httpmock.Registry) {
values := url.Values{
"page": []string{"1"},
"per_page": []string{"30"},
"q": []string{"license:GPLv3 topic:gh-extension user:jillvalentine"},
}
results := searchResults(1)
reg.Register(
httpmock.QueryMatcher("GET", "search/repositories", values),
httpmock.JSONResponse(results),
)
},
wantStdout: "\tvilmibm/gh-screensaver\tterminal animations\n",
},
{
name: "search for extensions with web mode",
args: []string{"search", "--web"},
browseStubs: func(b *browser.Stub) func(*testing.T) {
return func(t *testing.T) {
b.Verify(t, "https://github.com/search?q=topic%3Agh-extension&type=repositories")
}
},
},
{
name: "install an extension",
args: []string{"install", "owner/gh-some-ext"},
managerStubs: func(em *extensions.ExtensionManagerMock) func(*testing.T) {
em.ListFunc = func() []extensions.Extension {
return []extensions.Extension{}
}
em.InstallFunc = func(_ ghrepo.Interface, _ string) error {
return nil
}
return func(t *testing.T) {
installCalls := em.InstallCalls()
assert.Equal(t, 1, len(installCalls))
assert.Equal(t, "gh-some-ext", installCalls[0].InterfaceMoqParam.RepoName())
listCalls := em.ListCalls()
assert.Equal(t, 1, len(listCalls))
}
},
},
{
name: "install an extension with same name as existing extension",
args: []string{"install", "owner/gh-existing-ext"},
managerStubs: func(em *extensions.ExtensionManagerMock) func(*testing.T) {
em.ListFunc = func() []extensions.Extension {
e := &Extension{path: "owner2/gh-existing-ext", owner: "owner2"}
return []extensions.Extension{e}
}
return func(t *testing.T) {
calls := em.ListCalls()
assert.Equal(t, 1, len(calls))
}
},
wantErr: true,
errMsg: "there is already an installed extension that provides the \"existing-ext\" command",
},
{
name: "install an already installed extension",
args: []string{"install", "owner/gh-existing-ext"},
managerStubs: func(em *extensions.ExtensionManagerMock) func(*testing.T) {
em.ListFunc = func() []extensions.Extension {
e := &Extension{path: "owner/gh-existing-ext", owner: "owner"}
return []extensions.Extension{e}
}
return func(t *testing.T) {
calls := em.ListCalls()
assert.Equal(t, 1, len(calls))
}
},
wantStderr: "! Extension owner/gh-existing-ext is already installed\n",
},
{
name: "install local extension",
args: []string{"install", "."},
managerStubs: func(em *extensions.ExtensionManagerMock) func(*testing.T) {
em.InstallLocalFunc = func(dir string) error {
return nil
}
return func(t *testing.T) {
calls := em.InstallLocalCalls()
assert.Equal(t, 1, len(calls))
assert.Equal(t, tempDir, normalizeDir(calls[0].Dir))
}
},
},
{
name: "error extension not found",
args: []string{"install", "owner/gh-some-ext"},
managerStubs: func(em *extensions.ExtensionManagerMock) func(*testing.T) {
em.ListFunc = func() []extensions.Extension {
return []extensions.Extension{}
}
em.InstallFunc = func(_ ghrepo.Interface, _ string) error {
return repositoryNotFoundErr
}
return func(t *testing.T) {
installCalls := em.InstallCalls()
assert.Equal(t, 1, len(installCalls))
assert.Equal(t, "gh-some-ext", installCalls[0].InterfaceMoqParam.RepoName())
}
},
wantErr: true,
errMsg: "X Could not find extension 'owner/gh-some-ext' on host github.com",
},
{
name: "install local extension with pin",
args: []string{"install", ".", "--pin", "v1.0.0"},
wantErr: true,
errMsg: "local extensions cannot be pinned",
isTTY: true,
},
{
name: "upgrade argument error",
args: []string{"upgrade"},
wantErr: true,
errMsg: "specify an extension to upgrade or `--all`",
},
{
name: "upgrade --all with extension name error",
args: []string{"upgrade", "test", "--all"},
wantErr: true,
errMsg: "cannot use `--all` with extension name",
},
{
name: "upgrade an extension",
args: []string{"upgrade", "hello"},
managerStubs: func(em *extensions.ExtensionManagerMock) func(*testing.T) {
em.UpgradeFunc = func(name string, force bool) error {
return nil
}
return func(t *testing.T) {
calls := em.UpgradeCalls()
assert.Equal(t, 1, len(calls))
assert.Equal(t, "hello", calls[0].Name)
}
},
isTTY: true,
wantStdout: "✓ Successfully upgraded extension\n",
},
{
name: "upgrade an extension dry run",
args: []string{"upgrade", "hello", "--dry-run"},
managerStubs: func(em *extensions.ExtensionManagerMock) func(*testing.T) {
em.EnableDryRunModeFunc = func() {}
em.UpgradeFunc = func(name string, force bool) error {
return nil
}
return func(t *testing.T) {
dryRunCalls := em.EnableDryRunModeCalls()
assert.Equal(t, 1, len(dryRunCalls))
upgradeCalls := em.UpgradeCalls()
assert.Equal(t, 1, len(upgradeCalls))
assert.Equal(t, "hello", upgradeCalls[0].Name)
assert.False(t, upgradeCalls[0].Force)
}
},
isTTY: true,
wantStdout: "✓ Would have upgraded extension\n",
},
{
name: "upgrade an extension notty",
args: []string{"upgrade", "hello"},
managerStubs: func(em *extensions.ExtensionManagerMock) func(*testing.T) {
em.UpgradeFunc = func(name string, force bool) error {
return nil
}
return func(t *testing.T) {
calls := em.UpgradeCalls()
assert.Equal(t, 1, len(calls))
assert.Equal(t, "hello", calls[0].Name)
}
},
isTTY: false,
},
{
name: "upgrade an up-to-date extension",
args: []string{"upgrade", "hello"},
managerStubs: func(em *extensions.ExtensionManagerMock) func(*testing.T) {
em.UpgradeFunc = func(name string, force bool) error {
// An already up to date extension returns the same response
// as an one that has been upgraded.
return nil
}
return func(t *testing.T) {
calls := em.UpgradeCalls()
assert.Equal(t, 1, len(calls))
assert.Equal(t, "hello", calls[0].Name)
}
},
isTTY: true,
wantStdout: "✓ Successfully upgraded extension\n",
},
{
name: "upgrade extension error",
args: []string{"upgrade", "hello"},
managerStubs: func(em *extensions.ExtensionManagerMock) func(*testing.T) {
em.UpgradeFunc = func(name string, force bool) error {
return errors.New("oh no")
}
return func(t *testing.T) {
calls := em.UpgradeCalls()
assert.Equal(t, 1, len(calls))
assert.Equal(t, "hello", calls[0].Name)
}
},
isTTY: false,
wantErr: true,
errMsg: "SilentError",
wantStdout: "",
wantStderr: "X Failed upgrading extension hello: oh no\n",
},
{
name: "upgrade an extension gh-prefix",
args: []string{"upgrade", "gh-hello"},
managerStubs: func(em *extensions.ExtensionManagerMock) func(*testing.T) {
em.UpgradeFunc = func(name string, force bool) error {
return nil
}
return func(t *testing.T) {
calls := em.UpgradeCalls()
assert.Equal(t, 1, len(calls))
assert.Equal(t, "hello", calls[0].Name)
}
},
isTTY: true,
wantStdout: "✓ Successfully upgraded extension\n",
},
{
name: "upgrade an extension full name",
args: []string{"upgrade", "monalisa/gh-hello"},
managerStubs: func(em *extensions.ExtensionManagerMock) func(*testing.T) {
em.UpgradeFunc = func(name string, force bool) error {
return nil
}
return func(t *testing.T) {
calls := em.UpgradeCalls()
assert.Equal(t, 1, len(calls))
assert.Equal(t, "hello", calls[0].Name)
}
},
isTTY: true,
wantStdout: "✓ Successfully upgraded extension\n",
},
{
name: "upgrade all",
args: []string{"upgrade", "--all"},
managerStubs: func(em *extensions.ExtensionManagerMock) func(*testing.T) {
em.UpgradeFunc = func(name string, force bool) error {
return nil
}
return func(t *testing.T) {
calls := em.UpgradeCalls()
assert.Equal(t, 1, len(calls))
assert.Equal(t, "", calls[0].Name)
}
},
isTTY: true,
wantStdout: "✓ Successfully upgraded extensions\n",
},
{
name: "upgrade all dry run",
args: []string{"upgrade", "--all", "--dry-run"},
managerStubs: func(em *extensions.ExtensionManagerMock) func(*testing.T) {
em.EnableDryRunModeFunc = func() {}
em.UpgradeFunc = func(name string, force bool) error {
return nil
}
return func(t *testing.T) {
dryRunCalls := em.EnableDryRunModeCalls()
assert.Equal(t, 1, len(dryRunCalls))
upgradeCalls := em.UpgradeCalls()
assert.Equal(t, 1, len(upgradeCalls))
assert.Equal(t, "", upgradeCalls[0].Name)
assert.False(t, upgradeCalls[0].Force)
}
},
isTTY: true,
wantStdout: "✓ Would have upgraded extensions\n",
},
{
name: "upgrade all none installed",
args: []string{"upgrade", "--all"},
managerStubs: func(em *extensions.ExtensionManagerMock) func(*testing.T) {
em.UpgradeFunc = func(name string, force bool) error {
return noExtensionsInstalledError
}
return func(t *testing.T) {
calls := em.UpgradeCalls()
assert.Equal(t, 1, len(calls))
assert.Equal(t, "", calls[0].Name)
}
},
isTTY: true,
wantErr: true,
errMsg: "no installed extensions found",
},
{
name: "upgrade all notty",
args: []string{"upgrade", "--all"},
managerStubs: func(em *extensions.ExtensionManagerMock) func(*testing.T) {
em.UpgradeFunc = func(name string, force bool) error {
return nil
}
return func(t *testing.T) {
calls := em.UpgradeCalls()
assert.Equal(t, 1, len(calls))
assert.Equal(t, "", calls[0].Name)
}
},
isTTY: false,
},
{
name: "remove extension tty",
args: []string{"remove", "hello"},
managerStubs: func(em *extensions.ExtensionManagerMock) func(*testing.T) {
em.RemoveFunc = func(name string) error {
return nil
}
return func(t *testing.T) {
calls := em.RemoveCalls()
assert.Equal(t, 1, len(calls))
assert.Equal(t, "hello", calls[0].Name)
}
},
isTTY: true,
wantStdout: "✓ Removed extension hello\n",
},
{
name: "remove extension nontty",
args: []string{"remove", "hello"},
managerStubs: func(em *extensions.ExtensionManagerMock) func(*testing.T) {
em.RemoveFunc = func(name string) error {
return nil
}
return func(t *testing.T) {
calls := em.RemoveCalls()
assert.Equal(t, 1, len(calls))
assert.Equal(t, "hello", calls[0].Name)
}
},
isTTY: false,
wantStdout: "",
},
{
name: "remove extension gh-prefix",
args: []string{"remove", "gh-hello"},
managerStubs: func(em *extensions.ExtensionManagerMock) func(*testing.T) {
em.RemoveFunc = func(name string) error {
return nil
}
return func(t *testing.T) {
calls := em.RemoveCalls()
assert.Equal(t, 1, len(calls))
assert.Equal(t, "hello", calls[0].Name)
}
},
isTTY: false,
wantStdout: "",
},
{
name: "remove extension full name",
args: []string{"remove", "monalisa/gh-hello"},
managerStubs: func(em *extensions.ExtensionManagerMock) func(*testing.T) {
em.RemoveFunc = func(name string) error {
return nil
}
return func(t *testing.T) {
calls := em.RemoveCalls()
assert.Equal(t, 1, len(calls))
assert.Equal(t, "hello", calls[0].Name)
}
},
isTTY: false,
wantStdout: "",
},
{
name: "list extensions",
args: []string{"list"},
managerStubs: func(em *extensions.ExtensionManagerMock) func(*testing.T) {
em.ListFunc = func() []extensions.Extension {
ex1 := &Extension{path: "cli/gh-test", url: "https://github.com/cli/gh-test", currentVersion: "1"}
ex2 := &Extension{path: "cli/gh-test2", url: "https://github.com/cli/gh-test2", currentVersion: "1"}
return []extensions.Extension{ex1, ex2}
}
return func(t *testing.T) {
calls := em.ListCalls()
assert.Equal(t, 1, len(calls))
}
},
wantStdout: "gh test\tcli/gh-test\t1\ngh test2\tcli/gh-test2\t1\n",
},
{
name: "create extension interactive",
args: []string{"create"},
managerStubs: func(em *extensions.ExtensionManagerMock) func(*testing.T) {
em.CreateFunc = func(name string, tmplType extensions.ExtTemplateType) error {
return nil
}
return func(t *testing.T) {
calls := em.CreateCalls()
assert.Equal(t, 1, len(calls))
assert.Equal(t, "gh-test", calls[0].Name)
}
},
isTTY: true,
prompterStubs: func(pm *prompter.PrompterMock) {
pm.InputFunc = func(prompt, defVal string) (string, error) {
if prompt == "Extension name:" {
return "test", nil
}
return "", nil
}
pm.SelectFunc = func(prompt, defVal string, opts []string) (int, error) {
return prompter.IndexFor(opts, "Script (Bash, Ruby, Python, etc)")
}
},
wantStdout: heredoc.Doc(`
✓ Created directory gh-test
✓ Initialized git repository
✓ Made initial commit
✓ Set up extension scaffolding
gh-test is ready for development!
Next Steps
- run 'cd gh-test; gh extension install .; gh test' to see your new extension in action
- run 'gh repo create' to share your extension with others
For more information on writing extensions:
https://docs.github.com/github-cli/github-cli/creating-github-cli-extensions
`),
},
{
name: "create extension with arg, --precompiled=go",
args: []string{"create", "test", "--precompiled", "go"},
managerStubs: func(em *extensions.ExtensionManagerMock) func(*testing.T) {
em.CreateFunc = func(name string, tmplType extensions.ExtTemplateType) error {
return nil
}
return func(t *testing.T) {
calls := em.CreateCalls()
assert.Equal(t, 1, len(calls))
assert.Equal(t, "gh-test", calls[0].Name)
}
},
isTTY: true,
wantStdout: heredoc.Doc(`
✓ Created directory gh-test
✓ Initialized git repository
✓ Made initial commit
✓ Set up extension scaffolding
✓ Downloaded Go dependencies
✓ Built gh-test binary
gh-test is ready for development!
Next Steps
- run 'cd gh-test; gh extension install .; gh test' to see your new extension in action
- run 'go build && gh test' to see changes in your code as you develop
- run 'gh repo create' to share your extension with others
For more information on writing extensions:
https://docs.github.com/github-cli/github-cli/creating-github-cli-extensions
`),
},
{
name: "create extension with arg, --precompiled=other",
args: []string{"create", "test", "--precompiled", "other"},
managerStubs: func(em *extensions.ExtensionManagerMock) func(*testing.T) {
em.CreateFunc = func(name string, tmplType extensions.ExtTemplateType) error {
return nil
}
return func(t *testing.T) {
calls := em.CreateCalls()
assert.Equal(t, 1, len(calls))
assert.Equal(t, "gh-test", calls[0].Name)
}
},
isTTY: true,
wantStdout: heredoc.Doc(`
✓ Created directory gh-test
✓ Initialized git repository
✓ Made initial commit
✓ Set up extension scaffolding
gh-test is ready for development!
Next Steps
- run 'cd gh-test; gh extension install .' to install your extension locally
- fill in script/build.sh with your compilation script for automated builds
- compile a gh-test binary locally and run 'gh test' to see changes
- run 'gh repo create' to share your extension with others
For more information on writing extensions:
https://docs.github.com/github-cli/github-cli/creating-github-cli-extensions
`),
},
{
name: "create extension tty with argument",
args: []string{"create", "test"},
managerStubs: func(em *extensions.ExtensionManagerMock) func(*testing.T) {
em.CreateFunc = func(name string, tmplType extensions.ExtTemplateType) error {
return nil
}
return func(t *testing.T) {
calls := em.CreateCalls()
assert.Equal(t, 1, len(calls))
assert.Equal(t, "gh-test", calls[0].Name)
}
},
isTTY: true,
wantStdout: heredoc.Doc(`
✓ Created directory gh-test
✓ Initialized git repository
✓ Made initial commit
✓ Set up extension scaffolding
gh-test is ready for development!
Next Steps
- run 'cd gh-test; gh extension install .; gh test' to see your new extension in action
- run 'gh repo create' to share your extension with others
For more information on writing extensions:
https://docs.github.com/github-cli/github-cli/creating-github-cli-extensions
`),
},
{
name: "create extension tty with argument commit fails",
args: []string{"create", "test"},
managerStubs: func(em *extensions.ExtensionManagerMock) func(*testing.T) {
em.CreateFunc = func(name string, tmplType extensions.ExtTemplateType) error {
return ErrInitialCommitFailed
}
return func(t *testing.T) {
calls := em.CreateCalls()
assert.Equal(t, 1, len(calls))
assert.Equal(t, "gh-test", calls[0].Name)
}
},
isTTY: true,
wantStdout: heredoc.Doc(`
✓ Created directory gh-test
✓ Initialized git repository
X Made initial commit
✓ Set up extension scaffolding
gh-test is ready for development!
Next Steps
- run 'cd gh-test; gh extension install .; gh test' to see your new extension in action
- run 'gh repo create' to share your extension with others
For more information on writing extensions:
https://docs.github.com/github-cli/github-cli/creating-github-cli-extensions
`),
},
{
name: "create extension notty",
args: []string{"create", "gh-test"},
managerStubs: func(em *extensions.ExtensionManagerMock) func(*testing.T) {
em.CreateFunc = func(name string, tmplType extensions.ExtTemplateType) error {
return nil
}
return func(t *testing.T) {
calls := em.CreateCalls()
assert.Equal(t, 1, len(calls))
assert.Equal(t, "gh-test", calls[0].Name)
}
},
isTTY: false,
wantStdout: "",
},
{
name: "exec extension missing",
args: []string{"exec", "invalid"},
managerStubs: func(em *extensions.ExtensionManagerMock) func(*testing.T) {
em.DispatchFunc = func(args []string, stdin io.Reader, stdout, stderr io.Writer) (bool, error) {
return false, nil
}
return func(t *testing.T) {
calls := em.DispatchCalls()
assert.Equal(t, 1, len(calls))
assert.EqualValues(t, []string{"invalid"}, calls[0].Args)
}
},
wantErr: true,
errMsg: `extension "invalid" not found`,
},
{
name: "exec extension with arguments",
args: []string{"exec", "test", "arg1", "arg2", "--flag1"},
managerStubs: func(em *extensions.ExtensionManagerMock) func(*testing.T) {
em.DispatchFunc = func(args []string, stdin io.Reader, stdout, stderr io.Writer) (bool, error) {
fmt.Fprintf(stdout, "test output")
return true, nil
}
return func(t *testing.T) {
calls := em.DispatchCalls()
assert.Equal(t, 1, len(calls))
assert.EqualValues(t, []string{"test", "arg1", "arg2", "--flag1"}, calls[0].Args)
}
},
wantStdout: "test output",
},
{
name: "browse",
args: []string{"browse"},
wantErr: true,
errMsg: "this command runs an interactive UI and needs to be run in a terminal",
},
{
name: "force install when absent",
args: []string{"install", "owner/gh-hello", "--force"},
managerStubs: func(em *extensions.ExtensionManagerMock) func(*testing.T) {
em.ListFunc = func() []extensions.Extension {
return []extensions.Extension{}
}
em.InstallFunc = func(_ ghrepo.Interface, _ string) error {
return nil
}
return func(t *testing.T) {
listCalls := em.ListCalls()
assert.Equal(t, 1, len(listCalls))
installCalls := em.InstallCalls()
assert.Equal(t, 1, len(installCalls))
assert.Equal(t, "gh-hello", installCalls[0].InterfaceMoqParam.RepoName())
}
},
isTTY: true,
wantStdout: "✓ Installed extension owner/gh-hello\n",
},
{
name: "force install when present",
args: []string{"install", "owner/gh-hello", "--force"},
managerStubs: func(em *extensions.ExtensionManagerMock) func(*testing.T) {
em.ListFunc = func() []extensions.Extension {
return []extensions.Extension{
&Extension{path: "owner/gh-hello", owner: "owner"},
}
}
em.InstallFunc = func(_ ghrepo.Interface, _ string) error {
return nil
}
em.UpgradeFunc = func(name string, force bool) error {
return nil
}
return func(t *testing.T) {
listCalls := em.ListCalls()
assert.Equal(t, 1, len(listCalls))
installCalls := em.InstallCalls()
assert.Equal(t, 0, len(installCalls))
upgradeCalls := em.UpgradeCalls()
assert.Equal(t, 1, len(upgradeCalls))
assert.Equal(t, "hello", upgradeCalls[0].Name)
}
},
isTTY: true,
wantStdout: "✓ Successfully upgraded extension\n",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ios, _, stdout, stderr := iostreams.Test()
ios.SetStdoutTTY(tt.isTTY)
ios.SetStderrTTY(tt.isTTY)
var assertFunc func(*testing.T)
em := &extensions.ExtensionManagerMock{}
if tt.managerStubs != nil {
assertFunc = tt.managerStubs(em)
}
pm := &prompter.PrompterMock{}
if tt.prompterStubs != nil {
tt.prompterStubs(pm)
}
reg := httpmock.Registry{}
defer reg.Verify(t)
client := http.Client{Transport: ®}
if tt.httpStubs != nil {
tt.httpStubs(®)
}
var assertBrowserFunc func(*testing.T)
browseStub := &browser.Stub{}
if tt.browseStubs != nil {
assertBrowserFunc = tt.browseStubs(browseStub)
}
f := cmdutil.Factory{
Config: func() (config.Config, error) {
return config.NewBlankConfig(), nil
},
IOStreams: ios,
ExtensionManager: em,
Prompter: pm,
Browser: browseStub,
HttpClient: func() (*http.Client, error) {
return &client, nil
},
}
cmd := NewCmdExtension(&f)
cmd.SetArgs(tt.args)
cmd.SetOut(io.Discard)
cmd.SetErr(io.Discard)
_, err := cmd.ExecuteC()
if tt.wantErr {
assert.EqualError(t, err, tt.errMsg)
} else {
assert.NoError(t, err)
}
if assertFunc != nil {
assertFunc(t)
}
if assertBrowserFunc != nil {
assertBrowserFunc(t)
}
assert.Equal(t, tt.wantStdout, stdout.String())
assert.Equal(t, tt.wantStderr, stderr.String())
})
}
}
func normalizeDir(d string) string {
return strings.TrimPrefix(d, "/private")
}
func Test_checkValidExtension(t *testing.T) {
rootCmd := &cobra.Command{}
rootCmd.AddCommand(&cobra.Command{Use: "help"})
rootCmd.AddCommand(&cobra.Command{Use: "auth"})
m := &extensions.ExtensionManagerMock{
ListFunc: func() []extensions.Extension {
return []extensions.Extension{
&extensions.ExtensionMock{
OwnerFunc: func() string { return "monalisa" },
NameFunc: func() string { return "screensaver" },
},
&extensions.ExtensionMock{
OwnerFunc: func() string { return "monalisa" },
NameFunc: func() string { return "triage" },
},
}
},
}
type args struct {
rootCmd *cobra.Command
manager extensions.ExtensionManager
extName string
extOwner string
}
tests := []struct {
name string
args args
wantError string
}{
{
name: "valid extension",
args: args{
rootCmd: rootCmd,
manager: m,
extOwner: "monalisa",
extName: "gh-hello",
},
},
{
name: "invalid extension name",
args: args{
rootCmd: rootCmd,
manager: m,
extOwner: "monalisa",
extName: "gherkins",
},
wantError: "extension repository name must start with `gh-`",
},
{
name: "clashes with built-in command",
args: args{
rootCmd: rootCmd,
manager: m,
extOwner: "monalisa",
extName: "gh-auth",
},
wantError: "\"auth\" matches the name of a built-in command or alias",
},
{
name: "clashes with an installed extension",
args: args{
rootCmd: rootCmd,
manager: m,
extOwner: "cli",
extName: "gh-triage",
},
wantError: "there is already an installed extension that provides the \"triage\" command",
},
{
name: "clashes with same extension",
args: args{
rootCmd: rootCmd,
manager: m,
extOwner: "monalisa",
extName: "gh-triage",
},
wantError: "alreadyInstalledError",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_, err := checkValidExtension(tt.args.rootCmd, tt.args.manager, tt.args.extName, tt.args.extOwner)
if tt.wantError == "" {
assert.NoError(t, err)
} else {
assert.EqualError(t, err, tt.wantError)
}
})
}
}
func searchResults(numResults int) interface{} {
result := map[string]interface{}{
"incomplete_results": false,
"total_count": 4,
"items": []interface{}{
map[string]interface{}{
"name": "gh-screensaver",
"full_name": "vilmibm/gh-screensaver",
"description": "terminal animations",
"owner": map[string]interface{}{
"login": "vilmibm",
},
},
map[string]interface{}{
"name": "gh-cool",
"full_name": "cli/gh-cool",
"description": "it's just cool ok",
"owner": map[string]interface{}{
"login": "cli",
},
},
map[string]interface{}{
"name": "gh-triage",
"full_name": "samcoe/gh-triage",
"description": "helps with triage",
"owner": map[string]interface{}{
"login": "samcoe",
},
},
map[string]interface{}{
"name": "gh-gei",
"full_name": "github/gh-gei",
"description": "something something enterprise",
"owner": map[string]interface{}{
"login": "github",
},
},
},
}
if len(result["items"].([]interface{})) > numResults {
fewerItems := result["items"].([]interface{})[0:numResults]
result["items"] = fewerItems
}
return result
}
|