gitee.com/mirrors/gauge@v1.0.6/reporter/simpleConsole.go (about) 1 // Copyright 2015 ThoughtWorks, Inc. 2 3 // This file is part of Gauge. 4 5 // Gauge is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 10 // Gauge is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU General Public License for more details. 14 15 // You should have received a copy of the GNU General Public License 16 // along with Gauge. If not, see <http://www.gnu.org/licenses/>. 17 18 package reporter 19 20 import ( 21 "fmt" 22 "io" 23 "strings" 24 "sync" 25 26 "github.com/getgauge/gauge/execution/result" 27 "github.com/getgauge/gauge/gauge" 28 "github.com/getgauge/gauge/gauge_messages" 29 "github.com/getgauge/gauge/logger" 30 ) 31 32 type simpleConsole struct { 33 mu *sync.Mutex 34 indentation int 35 writer io.Writer 36 } 37 38 func newSimpleConsole(out io.Writer) *simpleConsole { 39 return &simpleConsole{mu: &sync.Mutex{}, writer: out} 40 } 41 42 func (sc *simpleConsole) SuiteStart() { 43 } 44 45 func (sc *simpleConsole) SpecStart(spec *gauge.Specification, res result.Result) { 46 if res.(*result.SpecResult).Skipped { 47 return 48 } 49 sc.mu.Lock() 50 defer sc.mu.Unlock() 51 formattedHeading := formatSpec(spec.Heading.Value) 52 logger.Info(false, formattedHeading) 53 fmt.Fprint(sc.writer, fmt.Sprintf("%s%s", formattedHeading, newline)) 54 } 55 56 func (sc *simpleConsole) SpecEnd(spec *gauge.Specification, res result.Result) { 57 if res.(*result.SpecResult).Skipped { 58 return 59 } 60 sc.mu.Lock() 61 defer sc.mu.Unlock() 62 printHookFailureSC(sc, res, res.GetPreHook) 63 printHookFailureSC(sc, res, res.GetPostHook) 64 fmt.Fprintln(sc.writer) 65 } 66 67 func (sc *simpleConsole) ScenarioStart(scenario *gauge.Scenario, i gauge_messages.ExecutionInfo, res result.Result) { 68 if res.(*result.ScenarioResult).ProtoScenario.ExecutionStatus == gauge_messages.ExecutionStatus_SKIPPED { 69 return 70 } 71 sc.mu.Lock() 72 defer sc.mu.Unlock() 73 sc.indentation += scenarioIndentation 74 formattedHeading := formatScenario(scenario.Heading.Value) 75 logger.Info(false, formattedHeading) 76 fmt.Fprint(sc.writer, fmt.Sprintf("%s%s", indent(formattedHeading, sc.indentation), newline)) 77 } 78 79 func (sc *simpleConsole) ScenarioEnd(scenario *gauge.Scenario, res result.Result, i gauge_messages.ExecutionInfo) { 80 if res.(*result.ScenarioResult).ProtoScenario.ExecutionStatus == gauge_messages.ExecutionStatus_SKIPPED { 81 return 82 } 83 sc.mu.Lock() 84 defer sc.mu.Unlock() 85 printHookFailureSC(sc, res, res.GetPreHook) 86 printHookFailureSC(sc, res, res.GetPostHook) 87 sc.indentation -= scenarioIndentation 88 } 89 90 func (sc *simpleConsole) StepStart(stepText string) { 91 sc.mu.Lock() 92 defer sc.mu.Unlock() 93 sc.indentation += stepIndentation 94 logger.Debug(false, stepText) 95 if Verbose { 96 fmt.Fprint(sc.writer, fmt.Sprintf("%s%s", indent(strings.TrimSpace(stepText), sc.indentation), newline)) 97 } 98 } 99 100 func (sc *simpleConsole) StepEnd(step gauge.Step, res result.Result, execInfo gauge_messages.ExecutionInfo) { 101 sc.mu.Lock() 102 defer sc.mu.Unlock() 103 printHookFailureSC(sc, res, res.GetPreHook) 104 stepRes := res.(*result.StepResult) 105 if stepRes.GetStepFailed() { 106 stepText := prepStepMsg(step.LineText) 107 logger.Error(false, stepText) 108 109 specInfo := prepSpecInfo(execInfo.GetCurrentSpec().GetFileName(), step.LineNo, step.InConcept()) 110 logger.Error(false, specInfo) 111 112 errMsg := prepErrorMessage(stepRes.ProtoStepExecResult().GetExecutionResult().GetErrorMessage()) 113 logger.Error(false, errMsg) 114 stacktrace := prepStacktrace(stepRes.ProtoStepExecResult().GetExecutionResult().GetStackTrace()) 115 logger.Error(false, stacktrace) 116 117 msg := formatErrorFragment(stepText, sc.indentation) + formatErrorFragment(specInfo, sc.indentation) + formatErrorFragment(errMsg, sc.indentation) + formatErrorFragment(stacktrace, sc.indentation) 118 fmt.Fprint(sc.writer, msg) 119 } 120 printHookFailureSC(sc, res, res.GetPostHook) 121 sc.indentation -= stepIndentation 122 } 123 124 func (sc *simpleConsole) ConceptStart(conceptHeading string) { 125 sc.mu.Lock() 126 defer sc.mu.Unlock() 127 sc.indentation += stepIndentation 128 logger.Debug(false, conceptHeading) 129 if Verbose { 130 fmt.Fprint(sc.writer, fmt.Sprintf("%s%s", indent(strings.TrimSpace(conceptHeading), sc.indentation), newline)) 131 } 132 } 133 134 func (sc *simpleConsole) ConceptEnd(res result.Result) { 135 sc.mu.Lock() 136 defer sc.mu.Unlock() 137 sc.indentation -= stepIndentation 138 } 139 140 func (sc *simpleConsole) SuiteEnd(res result.Result) { 141 sc.mu.Lock() 142 defer sc.mu.Unlock() 143 printHookFailureSC(sc, res, res.GetPreHook) 144 printHookFailureSC(sc, res, res.GetPostHook) 145 suiteRes := res.(*result.SuiteResult) 146 for _, e := range suiteRes.UnhandledErrors { 147 logger.Error(false, e.Error()) 148 fmt.Fprint(sc.writer, indent(e.Error(), sc.indentation+errorIndentation)+newline) 149 } 150 } 151 152 func (sc *simpleConsole) DataTable(table string) { 153 sc.mu.Lock() 154 defer sc.mu.Unlock() 155 logger.Debug(false, table) 156 fmt.Fprint(sc.writer, fmt.Sprintf("%s", table)) 157 } 158 159 func (sc *simpleConsole) Errorf(err string, args ...interface{}) { 160 sc.mu.Lock() 161 defer sc.mu.Unlock() 162 errorMessage := fmt.Sprintf(err, args...) 163 logger.Error(false, errorMessage) 164 errorString := indent(errorMessage, sc.indentation+errorIndentation) 165 fmt.Fprint(sc.writer, fmt.Sprintf("%s%s", errorString, newline)) 166 } 167 168 func (sc *simpleConsole) Write(b []byte) (int, error) { 169 sc.mu.Lock() 170 defer sc.mu.Unlock() 171 fmt.Fprint(sc.writer, string(b)) 172 return len(b), nil 173 } 174 175 func printHookFailureSC(sc *simpleConsole, res result.Result, hookFailure func() []*gauge_messages.ProtoHookFailure) { 176 if len(hookFailure()) > 0 { 177 errMsg := prepErrorMessage(hookFailure()[0].GetErrorMessage()) 178 logger.Error(false, errMsg) 179 stacktrace := prepStacktrace(hookFailure()[0].GetStackTrace()) 180 logger.Error(false, stacktrace) 181 fmt.Fprint(sc.writer, formatErrorFragment(errMsg, sc.indentation), formatErrorFragment(stacktrace, sc.indentation)) 182 } 183 }