summaryrefslogtreecommitdiff
path: root/vendor/github.com/onsi/ginkgo/reporters/stenographer/stenographer.go
blob: 601c74d66eb3fc808082f4704ba27403411f3e4e (plain)
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
/*
The stenographer is used by Ginkgo's reporters to generate output.

Move along, nothing to see here.
*/

package stenographer

import (
	"fmt"
	"io"
	"runtime"
	"strings"

	"github.com/onsi/ginkgo/types"
)

const defaultStyle = "\x1b[0m"
const boldStyle = "\x1b[1m"
const redColor = "\x1b[91m"
const greenColor = "\x1b[32m"
const yellowColor = "\x1b[33m"
const cyanColor = "\x1b[36m"
const grayColor = "\x1b[90m"
const lightGrayColor = "\x1b[37m"

type cursorStateType int

const (
	cursorStateTop cursorStateType = iota
	cursorStateStreaming
	cursorStateMidBlock
	cursorStateEndBlock
)

type Stenographer interface {
	AnnounceSuite(description string, randomSeed int64, randomizingAll bool, succinct bool)
	AnnounceAggregatedParallelRun(nodes int, succinct bool)
	AnnounceParallelRun(node int, nodes int, succinct bool)
	AnnounceTotalNumberOfSpecs(total int, succinct bool)
	AnnounceNumberOfSpecs(specsToRun int, total int, succinct bool)
	AnnounceSpecRunCompletion(summary *types.SuiteSummary, succinct bool)

	AnnounceSpecWillRun(spec *types.SpecSummary)
	AnnounceBeforeSuiteFailure(summary *types.SetupSummary, succinct bool, fullTrace bool)
	AnnounceAfterSuiteFailure(summary *types.SetupSummary, succinct bool, fullTrace bool)

	AnnounceCapturedOutput(output string)

	AnnounceSuccesfulSpec(spec *types.SpecSummary)
	AnnounceSuccesfulSlowSpec(spec *types.SpecSummary, succinct bool)
	AnnounceSuccesfulMeasurement(spec *types.SpecSummary, succinct bool)

	AnnouncePendingSpec(spec *types.SpecSummary, noisy bool)
	AnnounceSkippedSpec(spec *types.SpecSummary, succinct bool, fullTrace bool)

	AnnounceSpecTimedOut(spec *types.SpecSummary, succinct bool, fullTrace bool)
	AnnounceSpecPanicked(spec *types.SpecSummary, succinct bool, fullTrace bool)
	AnnounceSpecFailed(spec *types.SpecSummary, succinct bool, fullTrace bool)

	SummarizeFailures(summaries []*types.SpecSummary)
}

func New(color bool, enableFlakes bool, writer io.Writer) Stenographer {
	denoter := "•"
	if runtime.GOOS == "windows" {
		denoter = "+"
	}
	return &consoleStenographer{
		color:        color,
		denoter:      denoter,
		cursorState:  cursorStateTop,
		enableFlakes: enableFlakes,
		w:            writer,
	}
}

type consoleStenographer struct {
	color        bool
	denoter      string
	cursorState  cursorStateType
	enableFlakes bool
	w            io.Writer
}

var alternatingColors = []string{defaultStyle, grayColor}

func (s *consoleStenographer) AnnounceSuite(description string, randomSeed int64, randomizingAll bool, succinct bool) {
	if succinct {
		s.print(0, "[%d] %s ", randomSeed, s.colorize(boldStyle, description))
		return
	}
	s.printBanner(fmt.Sprintf("Running Suite: %s", description), "=")
	s.print(0, "Random Seed: %s", s.colorize(boldStyle, "%d", randomSeed))
	if randomizingAll {
		s.print(0, " - Will randomize all specs")
	}
	s.printNewLine()
}

func (s *consoleStenographer) AnnounceParallelRun(node int, nodes int, succinct bool) {
	if succinct {
		s.print(0, "- node #%d ", node)
		return
	}
	s.println(0,
		"Parallel test node %s/%s.",
		s.colorize(boldStyle, "%d", node),
		s.colorize(boldStyle, "%d", nodes),
	)
	s.printNewLine()
}

func (s *consoleStenographer) AnnounceAggregatedParallelRun(nodes int, succinct bool) {
	if succinct {
		s.print(0, "- %d nodes ", nodes)
		return
	}
	s.println(0,
		"Running in parallel across %s nodes",
		s.colorize(boldStyle, "%d", nodes),
	)
	s.printNewLine()
}

func (s *consoleStenographer) AnnounceNumberOfSpecs(specsToRun int, total int, succinct bool) {
	if succinct {
		s.print(0, "- %d/%d specs ", specsToRun, total)
		s.stream()
		return
	}
	s.println(0,
		"Will run %s of %s specs",
		s.colorize(boldStyle, "%d", specsToRun),
		s.colorize(boldStyle, "%d", total),
	)

	s.printNewLine()
}

func (s *consoleStenographer) AnnounceTotalNumberOfSpecs(total int, succinct bool) {
	if succinct {
		s.print(0, "- %d specs ", total)
		s.stream()
		return
	}
	s.println(0,
		"Will run %s specs",
		s.colorize(boldStyle, "%d", total),
	)

	s.printNewLine()
}

func (s *consoleStenographer) AnnounceSpecRunCompletion(summary *types.SuiteSummary, succinct bool) {
	if succinct && summary.SuiteSucceeded {
		s.print(0, " %s %s ", s.colorize(greenColor, "SUCCESS!"), summary.RunTime)
		return
	}
	s.printNewLine()
	color := greenColor
	if !summary.SuiteSucceeded {
		color = redColor
	}
	s.println(0, s.colorize(boldStyle+color, "Ran %d of %d Specs in %.3f seconds", summary.NumberOfSpecsThatWillBeRun, summary.NumberOfTotalSpecs, summary.RunTime.Seconds()))

	status := ""
	if summary.SuiteSucceeded {
		status = s.colorize(boldStyle+greenColor, "SUCCESS!")
	} else {
		status = s.colorize(boldStyle+redColor, "FAIL!")
	}

	flakes := ""
	if s.enableFlakes {
		flakes = " | " + s.colorize(yellowColor+boldStyle, "%d Flaked", summary.NumberOfFlakedSpecs)
	}

	s.print(0,
		"%s -- %s | %s | %s | %s\n",
		status,
		s.colorize(greenColor+boldStyle, "%d Passed", summary.NumberOfPassedSpecs),
		s.colorize(redColor+boldStyle, "%d Failed", summary.NumberOfFailedSpecs)+flakes,
		s.colorize(yellowColor+boldStyle, "%d Pending", summary.NumberOfPendingSpecs),
		s.colorize(cyanColor+boldStyle, "%d Skipped", summary.NumberOfSkippedSpecs),
	)
}

func (s *consoleStenographer) AnnounceSpecWillRun(spec *types.SpecSummary) {
	s.startBlock()
	for i, text := range spec.ComponentTexts[1 : len(spec.ComponentTexts)-1] {
		s.print(0, s.colorize(alternatingColors[i%2], text)+" ")
	}

	indentation := 0
	if len(spec.ComponentTexts) > 2 {
		indentation = 1
		s.printNewLine()
	}
	index := len(spec.ComponentTexts) - 1
	s.print(indentation, s.colorize(boldStyle, spec.ComponentTexts[index]))
	s.printNewLine()
	s.print(indentation, s.colorize(lightGrayColor, spec.ComponentCodeLocations[index].String()))
	s.printNewLine()
	s.midBlock()
}

func (s *consoleStenographer) AnnounceBeforeSuiteFailure(summary *types.SetupSummary, succinct bool, fullTrace bool) {
	s.announceSetupFailure("BeforeSuite", summary, succinct, fullTrace)
}

func (s *consoleStenographer) AnnounceAfterSuiteFailure(summary *types.SetupSummary, succinct bool, fullTrace bool) {
	s.announceSetupFailure("AfterSuite", summary, succinct, fullTrace)
}

func (s *consoleStenographer) announceSetupFailure(name string, summary *types.SetupSummary, succinct bool, fullTrace bool) {
	s.startBlock()
	var message string
	switch summary.State {
	case types.SpecStateFailed:
		message = "Failure"
	case types.SpecStatePanicked:
		message = "Panic"
	case types.SpecStateTimedOut:
		message = "Timeout"
	}

	s.println(0, s.colorize(redColor+boldStyle, "%s [%.3f seconds]", message, summary.RunTime.Seconds()))

	indentation := s.printCodeLocationBlock([]string{name}, []types.CodeLocation{summary.CodeLocation}, summary.ComponentType, 0, summary.State, true)

	s.printNewLine()
	s.printFailure(indentation, summary.State, summary.Failure, fullTrace)

	s.endBlock()
}

func (s *consoleStenographer) AnnounceCapturedOutput(output string) {
	if output == "" {
		return
	}

	s.startBlock()
	s.println(0, output)
	s.midBlock()
}

func (s *consoleStenographer) AnnounceSuccesfulSpec(spec *types.SpecSummary) {
	s.print(0, s.colorize(greenColor, s.denoter))
	s.stream()
}

func (s *consoleStenographer) AnnounceSuccesfulSlowSpec(spec *types.SpecSummary, succinct bool) {
	s.printBlockWithMessage(
		s.colorize(greenColor, "%s [SLOW TEST:%.3f seconds]", s.denoter, spec.RunTime.Seconds()),
		"",
		spec,
		succinct,
	)
}

func (s *consoleStenographer) AnnounceSuccesfulMeasurement(spec *types.SpecSummary, succinct bool) {
	s.printBlockWithMessage(
		s.colorize(greenColor, "%s [MEASUREMENT]", s.denoter),
		s.measurementReport(spec, succinct),
		spec,
		succinct,
	)
}

func (s *consoleStenographer) AnnouncePendingSpec(spec *types.SpecSummary, noisy bool) {
	if noisy {
		s.printBlockWithMessage(
			s.colorize(yellowColor, "P [PENDING]"),
			"",
			spec,
			false,
		)
	} else {
		s.print(0, s.colorize(yellowColor, "P"))
		s.stream()
	}
}

func (s *consoleStenographer) AnnounceSkippedSpec(spec *types.SpecSummary, succinct bool, fullTrace bool) {
	// Skips at runtime will have a non-empty spec.Failure. All others should be succinct.
	if succinct || spec.Failure == (types.SpecFailure{}) {
		s.print(0, s.colorize(cyanColor, "S"))
		s.stream()
	} else {
		s.startBlock()
		s.println(0, s.colorize(cyanColor+boldStyle, "S [SKIPPING]%s [%.3f seconds]", s.failureContext(spec.Failure.ComponentType), spec.RunTime.Seconds()))

		indentation := s.printCodeLocationBlock(spec.ComponentTexts, spec.ComponentCodeLocations, spec.Failure.ComponentType, spec.Failure.ComponentIndex, spec.State, succinct)

		s.printNewLine()
		s.printSkip(indentation, spec.Failure)
		s.endBlock()
	}
}

func (s *consoleStenographer) AnnounceSpecTimedOut(spec *types.SpecSummary, succinct bool, fullTrace bool) {
	s.printSpecFailure(fmt.Sprintf("%s... Timeout", s.denoter), spec, succinct, fullTrace)
}

func (s *consoleStenographer) AnnounceSpecPanicked(spec *types.SpecSummary, succinct bool, fullTrace bool) {
	s.printSpecFailure(fmt.Sprintf("%s! Panic", s.denoter), spec, succinct, fullTrace)
}

func (s *consoleStenographer) AnnounceSpecFailed(spec *types.SpecSummary, succinct bool, fullTrace bool) {
	s.printSpecFailure(fmt.Sprintf("%s Failure", s.denoter), spec, succinct, fullTrace)
}

func (s *consoleStenographer) SummarizeFailures(summaries []*types.SpecSummary) {
	failingSpecs := []*types.SpecSummary{}

	for _, summary := range summaries {
		if summary.HasFailureState() {
			failingSpecs = append(failingSpecs, summary)
		}
	}

	if len(failingSpecs) == 0 {
		return
	}

	s.printNewLine()
	s.printNewLine()
	plural := "s"
	if len(failingSpecs) == 1 {
		plural = ""
	}
	s.println(0, s.colorize(redColor+boldStyle, "Summarizing %d Failure%s:", len(failingSpecs), plural))
	for _, summary := range failingSpecs {
		s.printNewLine()
		if summary.HasFailureState() {
			if summary.TimedOut() {
				s.print(0, s.colorize(redColor+boldStyle, "[Timeout...] "))
			} else if summary.Panicked() {
				s.print(0, s.colorize(redColor+boldStyle, "[Panic!] "))
			} else if summary.Failed() {
				s.print(0, s.colorize(redColor+boldStyle, "[Fail] "))
			}
			s.printSpecContext(summary.ComponentTexts, summary.ComponentCodeLocations, summary.Failure.ComponentType, summary.Failure.ComponentIndex, summary.State, true)
			s.printNewLine()
			s.println(0, s.colorize(lightGrayColor, summary.Failure.Location.String()))
		}
	}
}

func (s *consoleStenographer) startBlock() {
	if s.cursorState == cursorStateStreaming {
		s.printNewLine()
		s.printDelimiter()
	} else if s.cursorState == cursorStateMidBlock {
		s.printNewLine()
	}
}

func (s *consoleStenographer) midBlock() {
	s.cursorState = cursorStateMidBlock
}

func (s *consoleStenographer) endBlock() {
	s.printDelimiter()
	s.cursorState = cursorStateEndBlock
}

func (s *consoleStenographer) stream() {
	s.cursorState = cursorStateStreaming
}

func (s *consoleStenographer) printBlockWithMessage(header string, message string, spec *types.SpecSummary, succinct bool) {
	s.startBlock()
	s.println(0, header)

	indentation := s.printCodeLocationBlock(spec.ComponentTexts, spec.ComponentCodeLocations, types.SpecComponentTypeInvalid, 0, spec.State, succinct)

	if message != "" {
		s.printNewLine()
		s.println(indentation, message)
	}

	s.endBlock()
}

func (s *consoleStenographer) printSpecFailure(message string, spec *types.SpecSummary, succinct bool, fullTrace bool) {
	s.startBlock()
	s.println(0, s.colorize(redColor+boldStyle, "%s%s [%.3f seconds]", message, s.failureContext(spec.Failure.ComponentType), spec.RunTime.Seconds()))

	indentation := s.printCodeLocationBlock(spec.ComponentTexts, spec.ComponentCodeLocations, spec.Failure.ComponentType, spec.Failure.ComponentIndex, spec.State, succinct)

	s.printNewLine()
	s.printFailure(indentation, spec.State, spec.Failure, fullTrace)
	s.endBlock()
}

func (s *consoleStenographer) failureContext(failedComponentType types.SpecComponentType) string {
	switch failedComponentType {
	case types.SpecComponentTypeBeforeSuite:
		return " in Suite Setup (BeforeSuite)"
	case types.SpecComponentTypeAfterSuite:
		return " in Suite Teardown (AfterSuite)"
	case types.SpecComponentTypeBeforeEach:
		return " in Spec Setup (BeforeEach)"
	case types.SpecComponentTypeJustBeforeEach:
		return " in Spec Setup (JustBeforeEach)"
	case types.SpecComponentTypeAfterEach:
		return " in Spec Teardown (AfterEach)"
	}

	return ""
}

func (s *consoleStenographer) printSkip(indentation int, spec types.SpecFailure) {
	s.println(indentation, s.colorize(cyanColor, spec.Message))
	s.printNewLine()
	s.println(indentation, spec.Location.String())
}

func (s *consoleStenographer) printFailure(indentation int, state types.SpecState, failure types.SpecFailure, fullTrace bool) {
	if state == types.SpecStatePanicked {
		s.println(indentation, s.colorize(redColor+boldStyle, failure.Message))
		s.println(indentation, s.colorize(redColor, failure.ForwardedPanic))
		s.println(indentation, failure.Location.String())
		s.printNewLine()
		s.println(indentation, s.colorize(redColor, "Full Stack Trace"))
		s.println(indentation, failure.Location.FullStackTrace)
	} else {
		s.println(indentation, s.colorize(redColor, failure.Message))
		s.printNewLine()
		s.println(indentation, failure.Location.String())
		if fullTrace {
			s.printNewLine()
			s.println(indentation, s.colorize(redColor, "Full Stack Trace"))
			s.println(indentation, failure.Location.FullStackTrace)
		}
	}
}

func (s *consoleStenographer) printSpecContext(componentTexts []string, componentCodeLocations []types.CodeLocation, failedComponentType types.SpecComponentType, failedComponentIndex int, state types.SpecState, succinct bool) int {
	startIndex := 1
	indentation := 0

	if len(componentTexts) == 1 {
		startIndex = 0
	}

	for i := startIndex; i < len(componentTexts); i++ {
		if (state.IsFailure() || state == types.SpecStateSkipped) && i == failedComponentIndex {
			color := redColor
			if state == types.SpecStateSkipped {
				color = cyanColor
			}
			blockType := ""
			switch failedComponentType {
			case types.SpecComponentTypeBeforeSuite:
				blockType = "BeforeSuite"
			case types.SpecComponentTypeAfterSuite:
				blockType = "AfterSuite"
			case types.SpecComponentTypeBeforeEach:
				blockType = "BeforeEach"
			case types.SpecComponentTypeJustBeforeEach:
				blockType = "JustBeforeEach"
			case types.SpecComponentTypeAfterEach:
				blockType = "AfterEach"
			case types.SpecComponentTypeIt:
				blockType = "It"
			case types.SpecComponentTypeMeasure:
				blockType = "Measurement"
			}
			if succinct {
				s.print(0, s.colorize(color+boldStyle, "[%s] %s ", blockType, componentTexts[i]))
			} else {
				s.println(indentation, s.colorize(color+boldStyle, "%s [%s]", componentTexts[i], blockType))
				s.println(indentation, s.colorize(grayColor, "%s", componentCodeLocations[i]))
			}
		} else {
			if succinct {
				s.print(0, s.colorize(alternatingColors[i%2], "%s ", componentTexts[i]))
			} else {
				s.println(indentation, componentTexts[i])
				s.println(indentation, s.colorize(grayColor, "%s", componentCodeLocations[i]))
			}
		}
		indentation++
	}

	return indentation
}

func (s *consoleStenographer) printCodeLocationBlock(componentTexts []string, componentCodeLocations []types.CodeLocation, failedComponentType types.SpecComponentType, failedComponentIndex int, state types.SpecState, succinct bool) int {
	indentation := s.printSpecContext(componentTexts, componentCodeLocations, failedComponentType, failedComponentIndex, state, succinct)

	if succinct {
		if len(componentTexts) > 0 {
			s.printNewLine()
			s.print(0, s.colorize(lightGrayColor, "%s", componentCodeLocations[len(componentCodeLocations)-1]))
		}
		s.printNewLine()
		indentation = 1
	} else {
		indentation--
	}

	return indentation
}

func (s *consoleStenographer) orderedMeasurementKeys(measurements map[string]*types.SpecMeasurement) []string {
	orderedKeys := make([]string, len(measurements))
	for key, measurement := range measurements {
		orderedKeys[measurement.Order] = key
	}
	return orderedKeys
}

func (s *consoleStenographer) measurementReport(spec *types.SpecSummary, succinct bool) string {
	if len(spec.Measurements) == 0 {
		return "Found no measurements"
	}

	message := []string{}
	orderedKeys := s.orderedMeasurementKeys(spec.Measurements)

	if succinct {
		message = append(message, fmt.Sprintf("%s samples:", s.colorize(boldStyle, "%d", spec.NumberOfSamples)))
		for _, key := range orderedKeys {
			measurement := spec.Measurements[key]
			message = append(message, fmt.Sprintf("  %s - %s: %s%s, %s: %s%s ± %s%s, %s: %s%s",
				s.colorize(boldStyle, "%s", measurement.Name),
				measurement.SmallestLabel,
				s.colorize(greenColor, measurement.PrecisionFmt(), measurement.Smallest),
				measurement.Units,
				measurement.AverageLabel,
				s.colorize(cyanColor, measurement.PrecisionFmt(), measurement.Average),
				measurement.Units,
				s.colorize(cyanColor, measurement.PrecisionFmt(), measurement.StdDeviation),
				measurement.Units,
				measurement.LargestLabel,
				s.colorize(redColor, measurement.PrecisionFmt(), measurement.Largest),
				measurement.Units,
			))
		}
	} else {
		message = append(message, fmt.Sprintf("Ran %s samples:", s.colorize(boldStyle, "%d", spec.NumberOfSamples)))
		for _, key := range orderedKeys {
			measurement := spec.Measurements[key]
			info := ""
			if measurement.Info != nil {
				message = append(message, fmt.Sprintf("%v", measurement.Info))
			}

			message = append(message, fmt.Sprintf("%s:\n%s  %s: %s%s\n  %s: %s%s\n  %s: %s%s ± %s%s",
				s.colorize(boldStyle, "%s", measurement.Name),
				info,
				measurement.SmallestLabel,
				s.colorize(greenColor, measurement.PrecisionFmt(), measurement.Smallest),
				measurement.Units,
				measurement.LargestLabel,
				s.colorize(redColor, measurement.PrecisionFmt(), measurement.Largest),
				measurement.Units,
				measurement.AverageLabel,
				s.colorize(cyanColor, measurement.PrecisionFmt(), measurement.Average),
				measurement.Units,
				s.colorize(cyanColor, measurement.PrecisionFmt(), measurement.StdDeviation),
				measurement.Units,
			))
		}
	}

	return strings.Join(message, "\n")
}