|
| 1 | +package root |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/cli/cli/v2/internal/ghrepo" |
| 8 | + "github.com/cli/cli/v2/internal/prompter" |
| 9 | + "github.com/cli/cli/v2/pkg/extensions" |
| 10 | + "github.com/cli/cli/v2/pkg/iostreams" |
| 11 | + "github.com/stretchr/testify/assert" |
| 12 | + "github.com/stretchr/testify/require" |
| 13 | +) |
| 14 | + |
| 15 | +func TestOfficialExtensionStubRun(t *testing.T) { |
| 16 | + ext := &extensions.OfficialExtension{Name: "cool", Owner: "github", Repo: "gh-cool"} |
| 17 | + |
| 18 | + tests := []struct { |
| 19 | + name string |
| 20 | + isTTY bool |
| 21 | + confirmResult bool |
| 22 | + confirmErr error |
| 23 | + installErr error |
| 24 | + wantErr string |
| 25 | + wantStderr string |
| 26 | + wantInstalled bool |
| 27 | + }{ |
| 28 | + { |
| 29 | + name: "non-TTY prints install instructions", |
| 30 | + isTTY: false, |
| 31 | + wantStderr: "gh extension install github/gh-cool", |
| 32 | + }, |
| 33 | + { |
| 34 | + name: "TTY confirmed installs", |
| 35 | + isTTY: true, |
| 36 | + confirmResult: true, |
| 37 | + wantStderr: "Successfully installed github/gh-cool", |
| 38 | + wantInstalled: true, |
| 39 | + }, |
| 40 | + { |
| 41 | + name: "TTY declined does not install", |
| 42 | + isTTY: true, |
| 43 | + confirmResult: false, |
| 44 | + }, |
| 45 | + { |
| 46 | + name: "TTY prompt error is propagated", |
| 47 | + isTTY: true, |
| 48 | + confirmErr: fmt.Errorf("prompt interrupted"), |
| 49 | + wantErr: "prompt interrupted", |
| 50 | + }, |
| 51 | + { |
| 52 | + name: "TTY install error is propagated", |
| 53 | + isTTY: true, |
| 54 | + confirmResult: true, |
| 55 | + installErr: fmt.Errorf("network error"), |
| 56 | + wantErr: "network error", |
| 57 | + wantInstalled: true, |
| 58 | + }, |
| 59 | + } |
| 60 | + |
| 61 | + for _, tt := range tests { |
| 62 | + t.Run(tt.name, func(t *testing.T) { |
| 63 | + ios, _, _, stderr := iostreams.Test() |
| 64 | + if tt.isTTY { |
| 65 | + ios.SetStdinTTY(true) |
| 66 | + ios.SetStdoutTTY(true) |
| 67 | + ios.SetStderrTTY(true) |
| 68 | + } |
| 69 | + |
| 70 | + em := &extensions.ExtensionManagerMock{ |
| 71 | + InstallFunc: func(_ ghrepo.Interface, _ string) error { |
| 72 | + return tt.installErr |
| 73 | + }, |
| 74 | + } |
| 75 | + p := &prompter.PrompterMock{ |
| 76 | + ConfirmFunc: func(_ string, _ bool) (bool, error) { |
| 77 | + return tt.confirmResult, tt.confirmErr |
| 78 | + }, |
| 79 | + } |
| 80 | + |
| 81 | + err := officialExtensionStubRun(ios, p, em, ext) |
| 82 | + |
| 83 | + if tt.wantErr != "" { |
| 84 | + require.Error(t, err) |
| 85 | + assert.Contains(t, err.Error(), tt.wantErr) |
| 86 | + } else { |
| 87 | + require.NoError(t, err) |
| 88 | + } |
| 89 | + |
| 90 | + if tt.wantStderr != "" { |
| 91 | + assert.Contains(t, stderr.String(), tt.wantStderr) |
| 92 | + } |
| 93 | + |
| 94 | + if tt.wantInstalled { |
| 95 | + require.NotEmpty(t, em.InstallCalls()) |
| 96 | + repo := em.InstallCalls()[0].InterfaceMoqParam |
| 97 | + assert.Equal(t, "github", repo.RepoOwner()) |
| 98 | + assert.Equal(t, "gh-cool", repo.RepoName()) |
| 99 | + assert.Equal(t, "github.com", repo.RepoHost()) |
| 100 | + } else if tt.isTTY && !tt.confirmResult && tt.confirmErr == nil { |
| 101 | + assert.Empty(t, em.InstallCalls()) |
| 102 | + } |
| 103 | + }) |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +func TestNewCmdOfficialExtensionStub_Properties(t *testing.T) { |
| 108 | + ios, _, _, _ := iostreams.Test() |
| 109 | + ext := &extensions.OfficialExtension{Name: "cool", Owner: "github", Repo: "gh-cool"} |
| 110 | + em := &extensions.ExtensionManagerMock{} |
| 111 | + p := &prompter.PrompterMock{} |
| 112 | + |
| 113 | + cmd := NewCmdOfficialExtensionStub(ios, p, em, ext) |
| 114 | + |
| 115 | + assert.Equal(t, "cool", cmd.Use) |
| 116 | + assert.True(t, cmd.Hidden) |
| 117 | + assert.Equal(t, "extension", cmd.GroupID) |
| 118 | + assert.True(t, cmd.DisableFlagParsing) |
| 119 | +} |
0 commit comments