|
| 1 | +package shared |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + "time" |
| 6 | + |
| 7 | + "github.com/MakeNowJust/heredoc" |
| 8 | + "github.com/cli/cli/v2/pkg/iostreams" |
| 9 | + "github.com/stretchr/testify/assert" |
| 10 | + "github.com/stretchr/testify/require" |
| 11 | +) |
| 12 | + |
| 13 | +func TestRenderJobs(t *testing.T) { |
| 14 | + startedAt, err := time.Parse(time.RFC3339, "2009-03-19T00:00:00Z") |
| 15 | + require.NoError(t, err) |
| 16 | + completedAt, err := time.Parse(time.RFC3339, "2009-03-19T00:01:00Z") |
| 17 | + require.NoError(t, err) |
| 18 | + |
| 19 | + tests := []struct { |
| 20 | + name string |
| 21 | + jobs []Job |
| 22 | + wantVerbose string |
| 23 | + wantNormal string |
| 24 | + wantCompact string |
| 25 | + }{ |
| 26 | + { |
| 27 | + name: "nil jobs", |
| 28 | + jobs: nil, |
| 29 | + }, |
| 30 | + { |
| 31 | + name: "empty jobs", |
| 32 | + jobs: []Job{}, |
| 33 | + }, |
| 34 | + { |
| 35 | + // This is not a real-world case, but nevertheless the code should |
| 36 | + // be able to handle that without error/panic. |
| 37 | + name: "in-progress job without steps", |
| 38 | + jobs: []Job{ |
| 39 | + { |
| 40 | + Name: "foo", |
| 41 | + ID: 999, |
| 42 | + StartedAt: startedAt, |
| 43 | + Status: InProgress, |
| 44 | + }, |
| 45 | + }, |
| 46 | + wantCompact: heredoc.Doc(` |
| 47 | + * foo (ID 999)`), |
| 48 | + wantNormal: heredoc.Doc(` |
| 49 | + * foo (ID 999)`), |
| 50 | + wantVerbose: heredoc.Doc(` |
| 51 | + * foo (ID 999)`), |
| 52 | + }, |
| 53 | + { |
| 54 | + // This is not a real-world case, but nevertheless the code should |
| 55 | + // be able to handle that without error/panic. |
| 56 | + name: "successful job without steps", |
| 57 | + jobs: []Job{ |
| 58 | + { |
| 59 | + Name: "foo", |
| 60 | + ID: 999, |
| 61 | + StartedAt: startedAt, |
| 62 | + CompletedAt: completedAt, |
| 63 | + Status: Completed, |
| 64 | + Conclusion: Success, |
| 65 | + }, |
| 66 | + }, |
| 67 | + wantCompact: heredoc.Doc(` |
| 68 | + ✓ foo in 1m0s (ID 999)`), |
| 69 | + wantNormal: heredoc.Doc(` |
| 70 | + ✓ foo in 1m0s (ID 999)`), |
| 71 | + wantVerbose: heredoc.Doc(` |
| 72 | + ✓ foo in 1m0s (ID 999)`), |
| 73 | + }, |
| 74 | + { |
| 75 | + // This is not a real-world case, but nevertheless the code should |
| 76 | + // be able to handle that without error/panic. |
| 77 | + name: "failed job without steps", |
| 78 | + jobs: []Job{ |
| 79 | + { |
| 80 | + Name: "foo", |
| 81 | + ID: 999, |
| 82 | + StartedAt: startedAt, |
| 83 | + CompletedAt: completedAt, |
| 84 | + Status: Completed, |
| 85 | + Conclusion: Failure, |
| 86 | + }, |
| 87 | + }, |
| 88 | + wantCompact: heredoc.Doc(` |
| 89 | + X foo in 1m0s (ID 999)`), |
| 90 | + wantNormal: heredoc.Doc(` |
| 91 | + X foo in 1m0s (ID 999)`), |
| 92 | + wantVerbose: heredoc.Doc(` |
| 93 | + X foo in 1m0s (ID 999)`), |
| 94 | + }, |
| 95 | + { |
| 96 | + name: "in-progress job with various step status values", |
| 97 | + jobs: []Job{ |
| 98 | + { |
| 99 | + Name: "foo", |
| 100 | + ID: 999, |
| 101 | + StartedAt: startedAt, |
| 102 | + Status: InProgress, |
| 103 | + Steps: []Step{ |
| 104 | + { |
| 105 | + Name: "passed", |
| 106 | + StartedAt: startedAt, |
| 107 | + CompletedAt: completedAt, |
| 108 | + Status: Completed, |
| 109 | + Conclusion: Success, |
| 110 | + Number: 1, |
| 111 | + }, |
| 112 | + { |
| 113 | + Name: "skipped", |
| 114 | + Status: Completed, |
| 115 | + Conclusion: Skipped, |
| 116 | + Number: 2, |
| 117 | + }, |
| 118 | + { |
| 119 | + Name: "failed 1", |
| 120 | + StartedAt: startedAt, |
| 121 | + CompletedAt: completedAt, |
| 122 | + Status: Completed, |
| 123 | + Conclusion: Failure, |
| 124 | + Number: 3, |
| 125 | + }, |
| 126 | + { |
| 127 | + Name: "failed 2", |
| 128 | + StartedAt: startedAt, |
| 129 | + CompletedAt: completedAt, |
| 130 | + Status: Completed, |
| 131 | + Conclusion: Failure, |
| 132 | + Number: 4, |
| 133 | + }, |
| 134 | + { |
| 135 | + Name: "in-progress", |
| 136 | + StartedAt: startedAt, |
| 137 | + Status: InProgress, |
| 138 | + Number: 5, |
| 139 | + }, |
| 140 | + { |
| 141 | + Name: "pending", |
| 142 | + Status: Pending, |
| 143 | + Number: 6, |
| 144 | + }, |
| 145 | + }, |
| 146 | + }, |
| 147 | + }, |
| 148 | + wantCompact: heredoc.Doc(` |
| 149 | + * foo (ID 999) |
| 150 | + X failed 1 |
| 151 | + X failed 2 |
| 152 | + * in-progress`), |
| 153 | + wantNormal: heredoc.Doc(` |
| 154 | + * foo (ID 999)`), |
| 155 | + wantVerbose: heredoc.Doc(` |
| 156 | + * foo (ID 999) |
| 157 | + ✓ passed |
| 158 | + - skipped |
| 159 | + X failed 1 |
| 160 | + X failed 2 |
| 161 | + * in-progress |
| 162 | + * pending`), |
| 163 | + }, |
| 164 | + { |
| 165 | + // As of my observations (babakks) when there is a failed step, the |
| 166 | + // job run is marked as failed. In other words, a successful job run |
| 167 | + // cannot have any failed steps. That's why there's no failed steps |
| 168 | + // in this test case. |
| 169 | + name: "successful job with various step status values", |
| 170 | + jobs: []Job{ |
| 171 | + { |
| 172 | + Name: "foo", |
| 173 | + ID: 999, |
| 174 | + StartedAt: startedAt, |
| 175 | + CompletedAt: completedAt, |
| 176 | + Status: Completed, |
| 177 | + Conclusion: Success, |
| 178 | + Steps: []Step{ |
| 179 | + { |
| 180 | + Name: "passed 1", |
| 181 | + StartedAt: startedAt, |
| 182 | + CompletedAt: completedAt, |
| 183 | + Status: Completed, |
| 184 | + Conclusion: Success, |
| 185 | + Number: 1, |
| 186 | + }, |
| 187 | + { |
| 188 | + Name: "skipped", |
| 189 | + Status: Completed, |
| 190 | + Conclusion: Skipped, |
| 191 | + Number: 2, |
| 192 | + }, |
| 193 | + { |
| 194 | + Name: "passed 2", |
| 195 | + StartedAt: startedAt, |
| 196 | + CompletedAt: completedAt, |
| 197 | + Status: Completed, |
| 198 | + Conclusion: Success, |
| 199 | + Number: 3, |
| 200 | + }, |
| 201 | + }, |
| 202 | + }, |
| 203 | + }, |
| 204 | + wantCompact: heredoc.Doc(` |
| 205 | + ✓ foo in 1m0s (ID 999)`), |
| 206 | + wantNormal: heredoc.Doc(` |
| 207 | + ✓ foo in 1m0s (ID 999)`), |
| 208 | + wantVerbose: heredoc.Doc(` |
| 209 | + ✓ foo in 1m0s (ID 999) |
| 210 | + ✓ passed 1 |
| 211 | + - skipped |
| 212 | + ✓ passed 2`), |
| 213 | + }, |
| 214 | + { |
| 215 | + name: "failed job with various step status values", |
| 216 | + jobs: []Job{ |
| 217 | + { |
| 218 | + Name: "foo", |
| 219 | + ID: 999, |
| 220 | + StartedAt: startedAt, |
| 221 | + CompletedAt: completedAt, |
| 222 | + Status: Completed, |
| 223 | + Conclusion: Failure, |
| 224 | + Steps: []Step{ |
| 225 | + { |
| 226 | + Name: "passed 1", |
| 227 | + StartedAt: startedAt, |
| 228 | + CompletedAt: completedAt, |
| 229 | + Status: Completed, |
| 230 | + Conclusion: Success, |
| 231 | + Number: 1, |
| 232 | + }, |
| 233 | + { |
| 234 | + Name: "skipped", |
| 235 | + Status: Completed, |
| 236 | + Conclusion: Skipped, |
| 237 | + Number: 2, |
| 238 | + }, |
| 239 | + { |
| 240 | + Name: "failed 1", |
| 241 | + StartedAt: startedAt, |
| 242 | + CompletedAt: completedAt, |
| 243 | + Status: Completed, |
| 244 | + Conclusion: Failure, |
| 245 | + Number: 3, |
| 246 | + }, |
| 247 | + { |
| 248 | + Name: "failed 2", |
| 249 | + StartedAt: startedAt, |
| 250 | + CompletedAt: completedAt, |
| 251 | + Status: Completed, |
| 252 | + Conclusion: Failure, |
| 253 | + Number: 4, |
| 254 | + }, |
| 255 | + { |
| 256 | + Name: "passed 2", |
| 257 | + StartedAt: startedAt, |
| 258 | + CompletedAt: completedAt, |
| 259 | + Status: Completed, |
| 260 | + Conclusion: Success, |
| 261 | + Number: 5, |
| 262 | + }, |
| 263 | + }, |
| 264 | + }, |
| 265 | + }, |
| 266 | + wantCompact: heredoc.Doc(` |
| 267 | + X foo in 1m0s (ID 999) |
| 268 | + X failed 1 |
| 269 | + X failed 2`), |
| 270 | + wantNormal: heredoc.Doc(` |
| 271 | + X foo in 1m0s (ID 999) |
| 272 | + ✓ passed 1 |
| 273 | + - skipped |
| 274 | + X failed 1 |
| 275 | + X failed 2 |
| 276 | + ✓ passed 2`), |
| 277 | + wantVerbose: heredoc.Doc(` |
| 278 | + X foo in 1m0s (ID 999) |
| 279 | + ✓ passed 1 |
| 280 | + - skipped |
| 281 | + X failed 1 |
| 282 | + X failed 2 |
| 283 | + ✓ passed 2`), |
| 284 | + }, |
| 285 | + { |
| 286 | + name: "multiple jobs", |
| 287 | + jobs: []Job{ |
| 288 | + { |
| 289 | + Name: "in-progress", |
| 290 | + ID: 999, |
| 291 | + StartedAt: startedAt, |
| 292 | + Status: InProgress, |
| 293 | + Steps: []Step{ |
| 294 | + { |
| 295 | + Name: "passed", |
| 296 | + StartedAt: startedAt, |
| 297 | + CompletedAt: completedAt, |
| 298 | + Status: Completed, |
| 299 | + Conclusion: Success, |
| 300 | + Number: 1, |
| 301 | + }, |
| 302 | + { |
| 303 | + Name: "in-progress", |
| 304 | + StartedAt: startedAt, |
| 305 | + Status: InProgress, |
| 306 | + Number: 2, |
| 307 | + }, |
| 308 | + }, |
| 309 | + }, |
| 310 | + { |
| 311 | + Name: "successful", |
| 312 | + ID: 999, |
| 313 | + StartedAt: startedAt, |
| 314 | + CompletedAt: completedAt, |
| 315 | + Status: Completed, |
| 316 | + Conclusion: Success, |
| 317 | + Steps: []Step{ |
| 318 | + { |
| 319 | + Name: "passed", |
| 320 | + StartedAt: startedAt, |
| 321 | + CompletedAt: completedAt, |
| 322 | + Status: Completed, |
| 323 | + Conclusion: Success, |
| 324 | + Number: 1, |
| 325 | + }, |
| 326 | + { |
| 327 | + Name: "skipped", |
| 328 | + Status: Completed, |
| 329 | + Conclusion: Skipped, |
| 330 | + Number: 2, |
| 331 | + }, |
| 332 | + }, |
| 333 | + }, |
| 334 | + { |
| 335 | + Name: "failed", |
| 336 | + ID: 999, |
| 337 | + StartedAt: startedAt, |
| 338 | + CompletedAt: completedAt, |
| 339 | + Status: Completed, |
| 340 | + Conclusion: Failure, |
| 341 | + Steps: []Step{ |
| 342 | + { |
| 343 | + Name: "passed", |
| 344 | + StartedAt: startedAt, |
| 345 | + CompletedAt: completedAt, |
| 346 | + Status: Completed, |
| 347 | + Conclusion: Success, |
| 348 | + Number: 1, |
| 349 | + }, |
| 350 | + { |
| 351 | + Name: "failed", |
| 352 | + StartedAt: startedAt, |
| 353 | + CompletedAt: completedAt, |
| 354 | + Status: Completed, |
| 355 | + Conclusion: Failure, |
| 356 | + Number: 2, |
| 357 | + }, |
| 358 | + }, |
| 359 | + }, |
| 360 | + }, |
| 361 | + wantCompact: heredoc.Doc(` |
| 362 | + * in-progress (ID 999) |
| 363 | + * in-progress |
| 364 | + ✓ successful in 1m0s (ID 999) |
| 365 | + X failed in 1m0s (ID 999) |
| 366 | + X failed`), |
| 367 | + wantNormal: heredoc.Doc(` |
| 368 | + * in-progress (ID 999) |
| 369 | + ✓ successful in 1m0s (ID 999) |
| 370 | + X failed in 1m0s (ID 999) |
| 371 | + ✓ passed |
| 372 | + X failed`), |
| 373 | + wantVerbose: heredoc.Doc(` |
| 374 | + * in-progress (ID 999) |
| 375 | + ✓ passed |
| 376 | + * in-progress |
| 377 | + ✓ successful in 1m0s (ID 999) |
| 378 | + ✓ passed |
| 379 | + - skipped |
| 380 | + X failed in 1m0s (ID 999) |
| 381 | + ✓ passed |
| 382 | + X failed`), |
| 383 | + }, |
| 384 | + } |
| 385 | + |
| 386 | + for _, tt := range tests { |
| 387 | + t.Run(tt.name, func(t *testing.T) { |
| 388 | + gotCompact := RenderJobsCompact(&iostreams.ColorScheme{}, tt.jobs) |
| 389 | + assert.Equal(t, tt.wantCompact, gotCompact, "unexpected compact mode output") |
| 390 | + |
| 391 | + gotNormal := RenderJobs(&iostreams.ColorScheme{}, tt.jobs, false) |
| 392 | + assert.Equal(t, tt.wantNormal, gotNormal, "unexpected normal mode output") |
| 393 | + |
| 394 | + gotVerbose := RenderJobs(&iostreams.ColorScheme{}, tt.jobs, true) |
| 395 | + assert.Equal(t, tt.wantVerbose, gotVerbose, "unexpected verbose mode output") |
| 396 | + }) |
| 397 | + } |
| 398 | +} |
0 commit comments