github.com/maps90/godog@v0.7.5-0.20170923143419-0093943021d4/fmt_progress.go (about) 1 package godog 2 3 import ( 4 "fmt" 5 "io" 6 "math" 7 "strings" 8 "sync" 9 10 "github.com/DATA-DOG/godog/gherkin" 11 ) 12 13 func init() { 14 Format("progress", "Prints a character per step.", progressFunc) 15 } 16 17 func progressFunc(suite string, out io.Writer) Formatter { 18 return &progress{ 19 basefmt: basefmt{ 20 started: timeNowFunc(), 21 indent: 2, 22 out: out, 23 }, 24 stepsPerRow: 70, 25 } 26 } 27 28 type progress struct { 29 basefmt 30 sync.Mutex 31 stepsPerRow int 32 steps int 33 } 34 35 func (f *progress) Node(n interface{}) { 36 f.Lock() 37 defer f.Unlock() 38 f.basefmt.Node(n) 39 } 40 41 func (f *progress) Feature(ft *gherkin.Feature, p string, c []byte) { 42 f.Lock() 43 defer f.Unlock() 44 f.basefmt.Feature(ft, p, c) 45 } 46 47 func (f *progress) Summary() { 48 left := math.Mod(float64(f.steps), float64(f.stepsPerRow)) 49 if left != 0 { 50 if int(f.steps) > f.stepsPerRow { 51 fmt.Fprintf(f.out, s(f.stepsPerRow-int(left))+fmt.Sprintf(" %d\n", f.steps)) 52 } else { 53 fmt.Fprintf(f.out, " %d\n", f.steps) 54 } 55 } 56 fmt.Fprintln(f.out, "") 57 58 if len(f.failed) > 0 { 59 fmt.Fprintln(f.out, "\n--- "+red("Failed steps:")+"\n") 60 for _, fail := range f.failed { 61 fmt.Fprintln(f.out, s(4)+red(strings.TrimSpace(fail.step.Keyword)+" "+fail.step.Text)+black(" # "+fail.line())) 62 fmt.Fprintln(f.out, s(6)+red("Error: ")+redb(fmt.Sprintf("%+v", fail.err))+"\n") 63 } 64 } 65 f.basefmt.Summary() 66 } 67 68 func (f *progress) step(res *stepResult) { 69 switch res.typ { 70 case passed: 71 fmt.Fprint(f.out, green(".")) 72 case skipped: 73 fmt.Fprint(f.out, cyan("-")) 74 case failed: 75 fmt.Fprint(f.out, red("F")) 76 case undefined: 77 fmt.Fprint(f.out, yellow("U")) 78 case pending: 79 fmt.Fprint(f.out, yellow("P")) 80 } 81 f.steps++ 82 if math.Mod(float64(f.steps), float64(f.stepsPerRow)) == 0 { 83 fmt.Fprintf(f.out, " %d\n", f.steps) 84 } 85 } 86 87 func (f *progress) Passed(step *gherkin.Step, match *StepDef) { 88 f.Lock() 89 defer f.Unlock() 90 f.basefmt.Passed(step, match) 91 f.step(f.passed[len(f.passed)-1]) 92 } 93 94 func (f *progress) Skipped(step *gherkin.Step, match *StepDef) { 95 f.Lock() 96 defer f.Unlock() 97 f.basefmt.Skipped(step, match) 98 f.step(f.skipped[len(f.skipped)-1]) 99 } 100 101 func (f *progress) Undefined(step *gherkin.Step, match *StepDef) { 102 f.Lock() 103 defer f.Unlock() 104 f.basefmt.Undefined(step, match) 105 f.step(f.undefined[len(f.undefined)-1]) 106 } 107 108 func (f *progress) Failed(step *gherkin.Step, match *StepDef, err error) { 109 f.Lock() 110 defer f.Unlock() 111 f.basefmt.Failed(step, match, err) 112 f.step(f.failed[len(f.failed)-1]) 113 } 114 115 func (f *progress) Pending(step *gherkin.Step, match *StepDef) { 116 f.Lock() 117 defer f.Unlock() 118 f.basefmt.Pending(step, match) 119 f.step(f.pending[len(f.pending)-1]) 120 }