github.com/cli/cli@v1.14.1-0.20210902173923-1af6a669e342/pkg/cmd/pr/checks/checks_test.go (about) 1 package checks 2 3 import ( 4 "bytes" 5 "encoding/json" 6 "io" 7 "os" 8 "testing" 9 10 "github.com/cli/cli/api" 11 "github.com/cli/cli/internal/ghrepo" 12 "github.com/cli/cli/internal/run" 13 "github.com/cli/cli/pkg/cmd/pr/shared" 14 "github.com/cli/cli/pkg/cmdutil" 15 "github.com/cli/cli/pkg/iostreams" 16 "github.com/google/shlex" 17 "github.com/stretchr/testify/assert" 18 "github.com/stretchr/testify/require" 19 ) 20 21 func TestNewCmdChecks(t *testing.T) { 22 tests := []struct { 23 name string 24 cli string 25 wants ChecksOptions 26 }{ 27 { 28 name: "no arguments", 29 cli: "", 30 wants: ChecksOptions{}, 31 }, 32 { 33 name: "pr argument", 34 cli: "1234", 35 wants: ChecksOptions{ 36 SelectorArg: "1234", 37 }, 38 }, 39 } 40 41 for _, tt := range tests { 42 t.Run(tt.name, func(t *testing.T) { 43 io, _, _, _ := iostreams.Test() 44 f := &cmdutil.Factory{ 45 IOStreams: io, 46 } 47 48 argv, err := shlex.Split(tt.cli) 49 assert.NoError(t, err) 50 51 var gotOpts *ChecksOptions 52 cmd := NewCmdChecks(f, func(opts *ChecksOptions) error { 53 gotOpts = opts 54 return nil 55 }) 56 cmd.SetArgs(argv) 57 cmd.SetIn(&bytes.Buffer{}) 58 cmd.SetOut(&bytes.Buffer{}) 59 cmd.SetErr(&bytes.Buffer{}) 60 61 _, err = cmd.ExecuteC() 62 assert.NoError(t, err) 63 64 assert.Equal(t, tt.wants.SelectorArg, gotOpts.SelectorArg) 65 }) 66 } 67 } 68 69 func Test_checksRun(t *testing.T) { 70 tests := []struct { 71 name string 72 fixture string 73 prJSON string 74 nontty bool 75 wantOut string 76 wantErr string 77 }{ 78 { 79 name: "no commits", 80 prJSON: `{ "number": 123 }`, 81 wantOut: "", 82 wantErr: "no commit found on the pull request", 83 }, 84 { 85 name: "no checks", 86 prJSON: `{ "number": 123, "statusCheckRollup": { "nodes": [{"commit": {"oid": "abc"}}]}, "baseRefName": "master" }`, 87 wantOut: "", 88 wantErr: "no checks reported on the 'master' branch", 89 }, 90 { 91 name: "some failing", 92 fixture: "./fixtures/someFailing.json", 93 wantOut: "Some checks were not successful\n1 failing, 1 successful, and 1 pending checks\n\nX sad tests 1m26s sweet link\n✓ cool tests 1m26s sweet link\n- slow tests 1m26s sweet link\n", 94 wantErr: "SilentError", 95 }, 96 { 97 name: "some pending", 98 fixture: "./fixtures/somePending.json", 99 wantOut: "Some checks are still pending\n0 failing, 2 successful, and 1 pending checks\n\n✓ cool tests 1m26s sweet link\n✓ rad tests 1m26s sweet link\n- slow tests 1m26s sweet link\n", 100 wantErr: "SilentError", 101 }, 102 { 103 name: "all passing", 104 fixture: "./fixtures/allPassing.json", 105 wantOut: "All checks were successful\n0 failing, 3 successful, and 0 pending checks\n\n✓ awesome tests 1m26s sweet link\n✓ cool tests 1m26s sweet link\n✓ rad tests 1m26s sweet link\n", 106 wantErr: "", 107 }, 108 { 109 name: "with statuses", 110 fixture: "./fixtures/withStatuses.json", 111 wantOut: "Some checks were not successful\n1 failing, 2 successful, and 0 pending checks\n\nX a status sweet link\n✓ cool tests 1m26s sweet link\n✓ rad tests 1m26s sweet link\n", 112 wantErr: "SilentError", 113 }, 114 { 115 name: "no checks", 116 nontty: true, 117 prJSON: `{ "number": 123, "statusCheckRollup": { "nodes": [{"commit": {"oid": "abc"}}]}, "baseRefName": "master" }`, 118 wantOut: "", 119 wantErr: "no checks reported on the 'master' branch", 120 }, 121 { 122 name: "some failing", 123 nontty: true, 124 fixture: "./fixtures/someFailing.json", 125 wantOut: "sad tests\tfail\t1m26s\tsweet link\ncool tests\tpass\t1m26s\tsweet link\nslow tests\tpending\t1m26s\tsweet link\n", 126 wantErr: "SilentError", 127 }, 128 { 129 name: "some pending", 130 nontty: true, 131 fixture: "./fixtures/somePending.json", 132 wantOut: "cool tests\tpass\t1m26s\tsweet link\nrad tests\tpass\t1m26s\tsweet link\nslow tests\tpending\t1m26s\tsweet link\n", 133 wantErr: "SilentError", 134 }, 135 { 136 name: "all passing", 137 nontty: true, 138 fixture: "./fixtures/allPassing.json", 139 wantOut: "awesome tests\tpass\t1m26s\tsweet link\ncool tests\tpass\t1m26s\tsweet link\nrad tests\tpass\t1m26s\tsweet link\n", 140 wantErr: "", 141 }, 142 { 143 name: "with statuses", 144 nontty: true, 145 fixture: "./fixtures/withStatuses.json", 146 wantOut: "a status\tfail\t0\tsweet link\ncool tests\tpass\t1m26s\tsweet link\nrad tests\tpass\t1m26s\tsweet link\n", 147 wantErr: "SilentError", 148 }, 149 } 150 151 for _, tt := range tests { 152 t.Run(tt.name, func(t *testing.T) { 153 ios, _, stdout, _ := iostreams.Test() 154 ios.SetStdoutTTY(!tt.nontty) 155 156 var response *api.PullRequest 157 var jsonReader io.Reader 158 if tt.fixture != "" { 159 ff, err := os.Open(tt.fixture) 160 require.NoError(t, err) 161 defer ff.Close() 162 jsonReader = ff 163 } else { 164 jsonReader = bytes.NewBufferString(tt.prJSON) 165 } 166 dec := json.NewDecoder(jsonReader) 167 require.NoError(t, dec.Decode(&response)) 168 169 opts := &ChecksOptions{ 170 IO: ios, 171 SelectorArg: "123", 172 Finder: shared.NewMockFinder("123", response, ghrepo.New("OWNER", "REPO")), 173 } 174 175 err := checksRun(opts) 176 if tt.wantErr != "" { 177 assert.EqualError(t, err, tt.wantErr) 178 } else { 179 assert.NoError(t, err) 180 } 181 182 assert.Equal(t, tt.wantOut, stdout.String()) 183 }) 184 } 185 } 186 187 func TestChecksRun_web(t *testing.T) { 188 tests := []struct { 189 name string 190 isTTY bool 191 wantStderr string 192 wantStdout string 193 wantBrowse string 194 }{ 195 { 196 name: "tty", 197 isTTY: true, 198 wantStderr: "Opening github.com/OWNER/REPO/pull/123/checks in your browser.\n", 199 wantStdout: "", 200 wantBrowse: "https://github.com/OWNER/REPO/pull/123/checks", 201 }, 202 { 203 name: "nontty", 204 isTTY: false, 205 wantStderr: "", 206 wantStdout: "", 207 wantBrowse: "https://github.com/OWNER/REPO/pull/123/checks", 208 }, 209 } 210 for _, tc := range tests { 211 t.Run(tc.name, func(t *testing.T) { 212 browser := &cmdutil.TestBrowser{} 213 214 io, _, stdout, stderr := iostreams.Test() 215 io.SetStdoutTTY(tc.isTTY) 216 io.SetStdinTTY(tc.isTTY) 217 io.SetStderrTTY(tc.isTTY) 218 219 _, teardown := run.Stub() 220 defer teardown(t) 221 222 err := checksRun(&ChecksOptions{ 223 IO: io, 224 Browser: browser, 225 WebMode: true, 226 SelectorArg: "123", 227 Finder: shared.NewMockFinder("123", &api.PullRequest{Number: 123}, ghrepo.New("OWNER", "REPO")), 228 }) 229 assert.NoError(t, err) 230 assert.Equal(t, tc.wantStdout, stdout.String()) 231 assert.Equal(t, tc.wantStderr, stderr.String()) 232 browser.Verify(t, tc.wantBrowse) 233 }) 234 } 235 }