github.com/ungtb10d/cli/v2@v2.0.0-20221110210412-98537dd9d6a1/pkg/cmd/repo/archive/archive_test.go (about) 1 package archive 2 3 import ( 4 "bytes" 5 "fmt" 6 "net/http" 7 "testing" 8 9 "github.com/ungtb10d/cli/v2/internal/ghrepo" 10 "github.com/ungtb10d/cli/v2/pkg/cmdutil" 11 "github.com/ungtb10d/cli/v2/pkg/httpmock" 12 "github.com/ungtb10d/cli/v2/pkg/iostreams" 13 "github.com/ungtb10d/cli/v2/pkg/prompt" 14 "github.com/google/shlex" 15 "github.com/stretchr/testify/assert" 16 ) 17 18 func TestNewCmdArchive(t *testing.T) { 19 tests := []struct { 20 name string 21 input string 22 wantErr bool 23 output ArchiveOptions 24 errMsg string 25 }{ 26 { 27 name: "no arguments no tty", 28 input: "", 29 errMsg: "--confirm required when not running interactively", 30 wantErr: true, 31 }, 32 { 33 name: "repo argument tty", 34 input: "OWNER/REPO --confirm", 35 output: ArchiveOptions{RepoArg: "OWNER/REPO", Confirmed: true}, 36 }, 37 } 38 for _, tt := range tests { 39 t.Run(tt.name, func(t *testing.T) { 40 ios, _, _, _ := iostreams.Test() 41 f := &cmdutil.Factory{ 42 IOStreams: ios, 43 } 44 argv, err := shlex.Split(tt.input) 45 assert.NoError(t, err) 46 var gotOpts *ArchiveOptions 47 cmd := NewCmdArchive(f, func(opts *ArchiveOptions) error { 48 gotOpts = opts 49 return nil 50 }) 51 cmd.SetArgs(argv) 52 cmd.SetIn(&bytes.Buffer{}) 53 cmd.SetOut(&bytes.Buffer{}) 54 cmd.SetErr(&bytes.Buffer{}) 55 56 _, err = cmd.ExecuteC() 57 if tt.wantErr { 58 assert.Error(t, err) 59 return 60 } 61 assert.NoError(t, err) 62 assert.Equal(t, tt.output.RepoArg, gotOpts.RepoArg) 63 assert.Equal(t, tt.output.Confirmed, gotOpts.Confirmed) 64 }) 65 } 66 } 67 68 func Test_ArchiveRun(t *testing.T) { 69 queryResponse := `{ "data": { "repository": { "id": "THE-ID","isArchived": %s} } }` 70 tests := []struct { 71 name string 72 opts ArchiveOptions 73 httpStubs func(*httpmock.Registry) 74 askStubs func(*prompt.AskStubber) 75 isTTY bool 76 wantStdout string 77 wantStderr string 78 }{ 79 { 80 name: "unarchived repo tty", 81 wantStdout: "✓ Archived repository OWNER/REPO\n", 82 askStubs: func(q *prompt.AskStubber) { 83 //nolint:staticcheck // SA1019: q.StubOne is deprecated: use StubPrompt 84 q.StubOne(true) 85 }, 86 isTTY: true, 87 opts: ArchiveOptions{RepoArg: "OWNER/REPO"}, 88 httpStubs: func(reg *httpmock.Registry) { 89 reg.Register( 90 httpmock.GraphQL(`query RepositoryInfo\b`), 91 httpmock.StringResponse(fmt.Sprintf(queryResponse, "false"))) 92 reg.Register( 93 httpmock.GraphQL(`mutation ArchiveRepository\b`), 94 httpmock.StringResponse(`{}`)) 95 }, 96 }, 97 { 98 name: "infer base repo", 99 wantStdout: "✓ Archived repository OWNER/REPO\n", 100 opts: ArchiveOptions{}, 101 askStubs: func(q *prompt.AskStubber) { 102 //nolint:staticcheck // SA1019: q.StubOne is deprecated: use StubPrompt 103 q.StubOne(true) 104 }, 105 isTTY: true, 106 httpStubs: func(reg *httpmock.Registry) { 107 reg.Register( 108 httpmock.GraphQL(`query RepositoryInfo\b`), 109 httpmock.StringResponse(fmt.Sprintf(queryResponse, "false"))) 110 reg.Register( 111 httpmock.GraphQL(`mutation ArchiveRepository\b`), 112 httpmock.StringResponse(`{}`)) 113 }, 114 }, 115 { 116 name: "archived repo tty", 117 wantStderr: "! Repository OWNER/REPO is already archived\n", 118 opts: ArchiveOptions{RepoArg: "OWNER/REPO"}, 119 httpStubs: func(reg *httpmock.Registry) { 120 reg.Register( 121 httpmock.GraphQL(`query RepositoryInfo\b`), 122 httpmock.StringResponse(fmt.Sprintf(queryResponse, "true"))) 123 }, 124 }, 125 } 126 127 for _, tt := range tests { 128 repo, _ := ghrepo.FromFullName("OWNER/REPO") 129 reg := &httpmock.Registry{} 130 if tt.httpStubs != nil { 131 tt.httpStubs(reg) 132 } 133 134 tt.opts.BaseRepo = func() (ghrepo.Interface, error) { 135 return repo, nil 136 } 137 tt.opts.HttpClient = func() (*http.Client, error) { 138 return &http.Client{Transport: reg}, nil 139 } 140 ios, _, stdout, stderr := iostreams.Test() 141 tt.opts.IO = ios 142 143 //nolint:staticcheck // SA1019: prompt.InitAskStubber is deprecated: use NewAskStubber 144 q, teardown := prompt.InitAskStubber() 145 defer teardown() 146 if tt.askStubs != nil { 147 tt.askStubs(q) 148 } 149 150 t.Run(tt.name, func(t *testing.T) { 151 defer reg.Verify(t) 152 ios.SetStdoutTTY(tt.isTTY) 153 ios.SetStderrTTY(tt.isTTY) 154 155 err := archiveRun(&tt.opts) 156 assert.NoError(t, err) 157 assert.Equal(t, tt.wantStdout, stdout.String()) 158 assert.Equal(t, tt.wantStderr, stderr.String()) 159 }) 160 } 161 }