gitee.com/mirrors/gauge@v1.0.6/reporter/verboseColoredConsole_test.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 "time" 23 24 "github.com/getgauge/gauge/execution/result" 25 "github.com/getgauge/gauge/gauge" 26 "github.com/getgauge/gauge/gauge_messages" 27 . "gopkg.in/check.v1" 28 ) 29 30 var ( 31 eraseLine = "\x1b[2K\r" 32 cursorUp = "\x1b[0A" 33 ) 34 35 type DummyResult struct { 36 PreHookFailure []*gauge_messages.ProtoHookFailure 37 PostHookFailure []*gauge_messages.ProtoHookFailure 38 IsFailed bool 39 } 40 41 func (r *DummyResult) GetPreHook() []*gauge_messages.ProtoHookFailure { 42 return r.PreHookFailure 43 } 44 func (r *DummyResult) GetPostHook() []*gauge_messages.ProtoHookFailure { 45 return r.PostHookFailure 46 } 47 func (r *DummyResult) AddPreHook(f ...*gauge_messages.ProtoHookFailure) { 48 } 49 func (r *DummyResult) AddPostHook(f ...*gauge_messages.ProtoHookFailure) { 50 } 51 func (r *DummyResult) SetFailure() { 52 r.IsFailed = true 53 } 54 func (r *DummyResult) GetFailed() bool { 55 return r.IsFailed 56 } 57 func (r *DummyResult) Item() interface{} { 58 return nil 59 } 60 func (r *DummyResult) ExecTime() int64 { 61 return 0 62 } 63 64 func setupVerboseColoredConsole() (*dummyWriter, *verboseColoredConsole) { 65 dw := newDummyWriter() 66 cc := newVerboseColoredConsole(dw) 67 return dw, cc 68 } 69 70 func (s *MySuite) TestSpecStart_ColoredConsole(c *C) { 71 dw, cc := setupVerboseColoredConsole() 72 73 cc.SpecStart(&gauge.Specification{Heading: &gauge.Heading{Value: "Spec heading"}}, &result.SpecResult{Skipped: false}) 74 75 c.Assert(dw.output, Equals, "# Spec heading\n") 76 } 77 78 func (s *MySuite) TestSpecEnd_ColoredConsole(c *C) { 79 dw, cc := setupVerboseColoredConsole() 80 81 res := &result.SpecResult{Skipped: false, ProtoSpec: &gauge_messages.ProtoSpec{}, IsFailed: false} 82 cc.SpecEnd(&gauge.Specification{}, res) 83 c.Assert(dw.output, Equals, "\n") 84 } 85 86 func (s *MySuite) TestScenarioStartInVerbose_ColoredConsole(c *C) { 87 dw, cc := setupVerboseColoredConsole() 88 cc.indentation = 2 89 scnRes := result.NewScenarioResult(&gauge_messages.ProtoScenario{ExecutionStatus: gauge_messages.ExecutionStatus_PASSED}) 90 91 cc.ScenarioStart(&gauge.Scenario{Heading: &gauge.Heading{Value: "my first scenario"}}, gauge_messages.ExecutionInfo{}, scnRes) 92 93 c.Assert(dw.output, Equals, " ## my first scenario\t\n") 94 } 95 96 func (s *MySuite) TestScenarioStartAndScenarioEnd_ColoredConsole(c *C) { 97 dw, cc := setupVerboseColoredConsole() 98 sceHeading := "First Scenario" 99 stepText := "* Say hello to all" 100 specInfo := gauge_messages.ExecutionInfo{CurrentSpec: &gauge_messages.SpecInfo{FileName: "hello.spec"}} 101 stepRes := result.NewStepResult(&gauge_messages.ProtoStep{StepExecutionResult: &gauge_messages.ProtoStepExecutionResult{}}) 102 sceRes := result.NewScenarioResult(&gauge_messages.ProtoScenario{ScenarioHeading: sceHeading}) 103 scnRes := result.NewScenarioResult(&gauge_messages.ProtoScenario{ExecutionStatus: gauge_messages.ExecutionStatus_PASSED}) 104 105 cc.ScenarioStart(&gauge.Scenario{Heading: &gauge.Heading{Value: sceHeading}}, gauge_messages.ExecutionInfo{}, scnRes) 106 c.Assert(dw.output, Equals, spaces(scenarioIndentation)+"## First Scenario\t\n") 107 dw.output = "" 108 109 cc.StepStart(stepText) 110 111 twoLevelIndentation := spaces(scenarioIndentation + stepIndentation) 112 expectedStepStartOutput := twoLevelIndentation + stepText + newline 113 c.Assert(dw.output, Equals, expectedStepStartOutput) 114 dw.output = "" 115 116 cc.StepEnd(gauge.Step{LineText: stepText}, stepRes, specInfo) 117 c.Assert(dw.output, Equals, cursorUp+eraseLine+twoLevelIndentation+stepText+"\t ...[PASS]\n") 118 119 cc.ScenarioEnd(nil, sceRes, gauge_messages.ExecutionInfo{}) 120 c.Assert(cc.headingBuffer.String(), Equals, "") 121 c.Assert(cc.pluginMessagesBuffer.String(), Equals, "") 122 } 123 124 func (s *MySuite) TestStepStart_Verbose(c *C) { 125 dw, cc := setupVerboseColoredConsole() 126 cc.indentation = 2 127 128 cc.StepStart("* say hello") 129 130 c.Assert(dw.output, Equals, " * say hello\n") 131 } 132 133 func (s *MySuite) TestFailingStepEndInVerbose_ColoredConsole(c *C) { 134 dw, cc := setupVerboseColoredConsole() 135 cc.indentation = 2 136 stepText := "* say hello" 137 cc.StepStart(stepText) 138 dw.output = "" 139 errMsg := "pre hook failure message" 140 stackTrace := "my stacktrace" 141 specInfo := gauge_messages.ExecutionInfo{CurrentSpec: &gauge_messages.SpecInfo{FileName: "hello.spec"}} 142 stepExeRes := &gauge_messages.ProtoStepExecutionResult{ExecutionResult: &gauge_messages.ProtoExecutionResult{ErrorMessage: errMsg, StackTrace: stackTrace}} 143 stepRes := result.NewStepResult(&gauge_messages.ProtoStep{StepExecutionResult: stepExeRes}) 144 stepRes.SetStepFailure() 145 146 cc.StepEnd(gauge.Step{LineText: stepText}, stepRes, specInfo) 147 148 expectedErrMsg := ` ` + ` 149 Failed Step: * say hello 150 Specification: hello.spec:0 151 Error Message: pre hook failure message 152 Stacktrace:` + spaces(1) + ` 153 my stacktrace 154 ` 155 c.Assert(dw.output, Equals, cursorUp+eraseLine+" "+stepText+"\t ...[FAIL]\n"+expectedErrMsg) 156 } 157 158 func (s *MySuite) TestStepStartAndStepEnd_ColoredConsole(c *C) { 159 dw, cc := setupVerboseColoredConsole() 160 cc.indentation = 2 161 stepText := "* Say hello to all" 162 errMsg := "pre hook failure message" 163 stacktrace := "my stacktrace" 164 specName := "hello.spec" 165 specInfo := gauge_messages.ExecutionInfo{CurrentSpec: &gauge_messages.SpecInfo{FileName: specName}} 166 stepExeRes := &gauge_messages.ProtoStepExecutionResult{ExecutionResult: &gauge_messages.ProtoExecutionResult{ErrorMessage: errMsg, StackTrace: stacktrace}} 167 stepRes := result.NewStepResult(&gauge_messages.ProtoStep{StepExecutionResult: stepExeRes}) 168 stepRes.SetStepFailure() 169 170 cc.StepStart(stepText) 171 172 expectedStepStartOutput := spaces(cc.indentation) + stepText + newline 173 c.Assert(dw.output, Equals, expectedStepStartOutput) 174 dw.output = "" 175 176 cc.StepEnd(gauge.Step{LineText: stepText}, stepRes, specInfo) 177 178 expectedErrMsg := spaces(8) + ` 179 Failed Step: ` + stepText + ` 180 Specification: ` + specName + `:0 181 Error Message: ` + errMsg + ` 182 Stacktrace:` + spaces(1) + ` 183 ` + stacktrace + ` 184 ` 185 expectedStepEndOutput := cursorUp + eraseLine + spaces(6) + stepText + "\t ...[FAIL]\n" + expectedErrMsg 186 c.Assert(dw.output, Equals, expectedStepEndOutput) 187 } 188 189 func (s *MySuite) TestStepFailure_ColoredConsole(c *C) { 190 dw, cc := setupVerboseColoredConsole() 191 cc.indentation = 2 192 errMsg := "pre hook failure message" 193 stacktrace := "my stacktrace" 194 specName := "hello.spec" 195 specInfo := gauge_messages.ExecutionInfo{CurrentSpec: &gauge_messages.SpecInfo{FileName: specName}} 196 stepExeRes := &gauge_messages.ProtoStepExecutionResult{ExecutionResult: &gauge_messages.ProtoExecutionResult{ErrorMessage: errMsg, StackTrace: stacktrace}} 197 stepRes := result.NewStepResult(&gauge_messages.ProtoStep{StepExecutionResult: stepExeRes}) 198 stepRes.SetStepFailure() 199 stepText := "* Say hello to all" 200 cc.StepStart(stepText) 201 202 expectedStepStartOutput := spaces(cc.indentation) + stepText + newline 203 c.Assert(dw.output, Equals, expectedStepStartOutput) 204 dw.output = "" 205 206 cc.Errorf("Failed!") 207 c.Assert(dw.output, Equals, spaces(cc.indentation+errorIndentation)+"Failed!\n") 208 dw.output = "" 209 210 cc.StepEnd(gauge.Step{LineText: stepText}, stepRes, specInfo) 211 212 expectedErrMsg := spaces(8) + ` 213 Failed Step: ` + stepText + ` 214 Specification: ` + specName + `:0 215 Error Message: ` + errMsg + ` 216 Stacktrace:` + spaces(1) + ` 217 ` + stacktrace + ` 218 ` 219 expectedStepEndOutput := cursorUp + eraseLine + cursorUp + eraseLine + spaces(6) + "* Say hello to all\t ...[FAIL]\n" + spaces(8) + "Failed!\n" + expectedErrMsg 220 c.Assert(dw.output, Equals, expectedStepEndOutput) 221 } 222 223 func (s *MySuite) TestConceptStartAndEnd_ColoredConsole(c *C) { 224 dw, cc := setupVerboseColoredConsole() 225 cc.indentation = 4 226 cpt1 := "* my concept" 227 cpt2 := "* my concept1" 228 cptRes1 := &DummyResult{IsFailed: true} 229 cptRes2 := &DummyResult{IsFailed: true} 230 231 cc.ConceptStart(cpt1) 232 c.Assert(dw.output, Equals, spaces(8)+cpt1+newline) 233 c.Assert(cc.indentation, Equals, 8) 234 235 dw.output = "" 236 cc.ConceptStart(cpt2) 237 c.Assert(dw.output, Equals, spaces(12)+cpt2+newline) 238 c.Assert(cc.indentation, Equals, 12) 239 240 cc.ConceptEnd(cptRes1) 241 c.Assert(cc.indentation, Equals, 8) 242 243 cc.ConceptEnd(cptRes2) 244 c.Assert(cc.indentation, Equals, 4) 245 } 246 247 func (s *MySuite) TestDataTable_ColoredConsole(c *C) { 248 dw, cc := setupVerboseColoredConsole() 249 cc.indentation = 2 250 table := `|Product|Description | 251 |-------|-----------------------------| 252 |Gauge |Test automation with ease |` 253 254 want := `|Product|Description | 255 |-------|-----------------------------| 256 |Gauge |Test automation with ease |` 257 258 cc.DataTable(table) 259 260 c.Assert(dw.output, Equals, want) 261 } 262 263 func (s *MySuite) TestError_ColoredConsole(c *C) { 264 dw, cc := setupVerboseColoredConsole() 265 initialIndentation := 6 266 cc.indentation = initialIndentation 267 268 cc.Errorf("Failed %s", "network error") 269 270 c.Assert(dw.output, Equals, fmt.Sprintf("%sFailed network error\n", spaces(initialIndentation+errorIndentation))) 271 } 272 273 func (s *MySuite) TestWrite_VerboseColoredConsole(c *C) { 274 _, cc := setupVerboseColoredConsole() 275 cc.indentation = 6 276 input := "hello, gauge" 277 278 _, err := cc.Write([]byte(input)) 279 280 c.Assert(err, Equals, nil) 281 c.Assert(cc.pluginMessagesBuffer.String(), Equals, input) 282 } 283 284 func (s *MySuite) TestStepEndWithPreHookFailure_ColoredConsole(c *C) { 285 dw, cc := setupVerboseColoredConsole() 286 cc.indentation = scenarioIndentation 287 errMsg := "pre hook failure message" 288 stackTrace := "my stacktrace" 289 stepText := "* my step" 290 specName := "hello.spec" 291 specInfo := gauge_messages.ExecutionInfo{CurrentSpec: &gauge_messages.SpecInfo{FileName: specName}} 292 preHookFailure := &gauge_messages.ProtoHookFailure{ErrorMessage: errMsg, StackTrace: stackTrace} 293 stepRes := result.NewStepResult(&gauge_messages.ProtoStep{StepExecutionResult: &gauge_messages.ProtoStepExecutionResult{PreHookFailure: preHookFailure}}) 294 cc.StepStart(stepText) 295 dw.output = "" 296 297 cc.StepEnd(gauge.Step{LineText: stepText}, stepRes, specInfo) 298 299 c.Assert(cc.indentation, Equals, scenarioIndentation) 300 expectedErrMsg := spaces(8) + `Error Message: ` + errMsg + ` 301 Stacktrace:` + spaces(1) + ` 302 ` + stackTrace + ` 303 ` 304 c.Assert(dw.output, Equals, cursorUp+eraseLine+spaces(scenarioIndentation+stepIndentation)+stepText+newline+expectedErrMsg) 305 } 306 307 func (s *MySuite) TestStepEndWithPostHookFailure_ColoredConsole(c *C) { 308 dw, cc := setupVerboseColoredConsole() 309 cc.indentation = scenarioIndentation 310 errMsg := "post hook failure message" 311 stackTrace := "my stacktrace" 312 specName := "hello.spec" 313 stepText := "* my step" 314 specInfo := gauge_messages.ExecutionInfo{CurrentSpec: &gauge_messages.SpecInfo{FileName: specName}} 315 postHookFailure := &gauge_messages.ProtoHookFailure{ErrorMessage: errMsg, StackTrace: stackTrace} 316 stepRes := result.NewStepResult(&gauge_messages.ProtoStep{StepExecutionResult: &gauge_messages.ProtoStepExecutionResult{PostHookFailure: postHookFailure}}) 317 cc.StepStart(stepText) 318 dw.output = "" 319 320 cc.StepEnd(gauge.Step{LineText: stepText}, stepRes, specInfo) 321 322 c.Assert(cc.indentation, Equals, 2) 323 expectedErrMsg := spaces(8) + `Error Message: ` + errMsg + ` 324 Stacktrace:` + spaces(1) + ` 325 ` + stackTrace + ` 326 ` 327 c.Assert(dw.output, Equals, cursorUp+eraseLine+spaces(scenarioIndentation+stepIndentation)+stepText+newline+expectedErrMsg) 328 } 329 330 func (s *MySuite) TestStepEndWithPreAndPostHookFailure_ColoredConsole(c *C) { 331 dw, cc := setupVerboseColoredConsole() 332 cc.indentation = scenarioIndentation 333 preHookErrMsg := "pre hook failure message" 334 postHookErrMsg := "post hook failure message" 335 stackTrace := "my stacktrace" 336 specName := "hello.spec" 337 stepText := "* my step" 338 specInfo := gauge_messages.ExecutionInfo{CurrentSpec: &gauge_messages.SpecInfo{FileName: specName}} 339 preHookFailure := &gauge_messages.ProtoHookFailure{ErrorMessage: preHookErrMsg, StackTrace: stackTrace} 340 postHookFailure := &gauge_messages.ProtoHookFailure{ErrorMessage: postHookErrMsg, StackTrace: stackTrace} 341 stepExeRes := &gauge_messages.ProtoStepExecutionResult{PostHookFailure: postHookFailure, PreHookFailure: preHookFailure} 342 stepRes := result.NewStepResult(&gauge_messages.ProtoStep{StepExecutionResult: stepExeRes}) 343 cc.StepStart(stepText) 344 dw.output = "" 345 346 cc.StepEnd(gauge.Step{LineText: stepText}, stepRes, specInfo) 347 348 c.Assert(cc.indentation, Equals, scenarioIndentation) 349 err1 := fmt.Sprintf("%sError Message: %s\n%sStacktrace: \n%s%s\n", spaces(8), preHookErrMsg, spaces(8), spaces(8), stackTrace) 350 err2 := fmt.Sprintf("%sError Message: %s\n%sStacktrace: \n%s%s\n", spaces(8), postHookErrMsg, spaces(8), spaces(8), stackTrace) 351 c.Assert(dw.output, Equals, cursorUp+eraseLine+spaces(scenarioIndentation+stepIndentation)+stepText+newline+err1+err2) 352 } 353 354 func (s *MySuite) TestSubscribeScenarioEndPreHookFailure_ColoredConsole(c *C) { 355 dw, cc := setupVerboseColoredConsole() 356 cc.indentation = scenarioIndentation 357 currentReporter = cc 358 preHookErrMsg := "pre hook failure message" 359 stackTrace := "my stacktrace" 360 preHookFailure := &gauge_messages.ProtoHookFailure{ErrorMessage: preHookErrMsg, StackTrace: stackTrace} 361 sceRes := result.NewScenarioResult(&gauge_messages.ProtoScenario{ 362 ExecutionStatus: gauge_messages.ExecutionStatus_PASSED, 363 PreHookFailure: preHookFailure, 364 }) 365 366 cc.ScenarioEnd(nil, sceRes, gauge_messages.ExecutionInfo{}) 367 368 ind := spaces(scenarioIndentation + errorIndentation) 369 want := ind + "Error Message: " + preHookErrMsg + newline + ind + "Stacktrace: \n" + ind + stackTrace + newline 370 c.Assert(dw.output, Equals, want) 371 c.Assert(cc.indentation, Equals, 0) 372 } 373 374 func (s *MySuite) TestSpecEndWithPostHookFailure_ColoredConsole(c *C) { 375 dw, cc := setupVerboseColoredConsole() 376 cc.indentation = 0 377 errMsg := "post hook failure message" 378 stackTrace := "my stacktrace" 379 postHookFailure := &gauge_messages.ProtoHookFailure{ErrorMessage: errMsg, StackTrace: stackTrace} 380 res := &result.SpecResult{Skipped: false, ProtoSpec: &gauge_messages.ProtoSpec{PostHookFailures: []*gauge_messages.ProtoHookFailure{postHookFailure}}} 381 382 cc.SpecEnd(&gauge.Specification{}, res) 383 384 c.Assert(cc.indentation, Equals, 0) 385 ind := spaces(errorIndentation) 386 want := ind + "Error Message: " + errMsg + newline + ind + "Stacktrace: \n" + ind + stackTrace + newline + newline 387 c.Assert(dw.output, Equals, want) 388 } 389 390 func (s *MySuite) TestSuiteEndWithPostHookFailure_ColoredConsole(c *C) { 391 dw, cc := setupVerboseColoredConsole() 392 cc.indentation = 0 393 errMsg := "post hook failure message" 394 stackTrace := "my stacktrace" 395 res := result.NewSuiteResult("", time.Now()) 396 postHookFailure := &gauge_messages.ProtoHookFailure{ErrorMessage: errMsg, StackTrace: stackTrace} 397 res.PostSuite = postHookFailure 398 399 cc.SuiteEnd(res) 400 401 c.Assert(cc.indentation, Equals, 0) 402 ind := spaces(errorIndentation) 403 want := ind + "Error Message: " + errMsg + newline + ind + "Stacktrace: \n" + ind + stackTrace + newline 404 c.Assert(dw.output, Equals, want) 405 }