github.com/getgauge/gauge@v1.6.9/reporter/verboseColoredConsole_test.go (about)

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