github.com/getgauge/gauge@v1.6.9/execution/result/suiteResult.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 result
     8  
     9  import (
    10  	"path/filepath"
    11  	"time"
    12  
    13  	"github.com/getgauge/gauge-proto/go/gauge_messages"
    14  	"github.com/getgauge/gauge/config"
    15  	"github.com/getgauge/gauge/env"
    16  )
    17  
    18  // SuitResult represents the result of suit execution
    19  type SuiteResult struct {
    20  	SpecResults             []*SpecResult
    21  	PreSuite                *(gauge_messages.ProtoHookFailure)
    22  	PostSuite               *(gauge_messages.ProtoHookFailure)
    23  	IsFailed                bool
    24  	SpecsFailedCount        int
    25  	ExecutionTime           int64 //in milliseconds
    26  	UnhandledErrors         []error
    27  	Environment             string
    28  	Tags                    string
    29  	ProjectName             string
    30  	Timestamp               string
    31  	SpecsSkippedCount       int
    32  	PreHookMessages         []string
    33  	PostHookMessages        []string
    34  	PreHookScreenshotFiles  []string
    35  	PostHookScreenshotFiles []string
    36  	PreHookScreenshots      [][]byte
    37  	PostHookScreenshots     [][]byte
    38  }
    39  
    40  // NewSuiteResult is a constructor for SuitResult
    41  func NewSuiteResult(tags string, startTime time.Time) *SuiteResult {
    42  	result := new(SuiteResult)
    43  	result.SpecResults = make([]*SpecResult, 0)
    44  	result.Timestamp = startTime.Format(config.LayoutForTimeStamp)
    45  	result.ProjectName = filepath.Base(config.ProjectRoot)
    46  	result.Environment = env.CurrentEnvironments()
    47  	result.Tags = tags
    48  	return result
    49  }
    50  
    51  // SetFailure sets the result to failed
    52  func (sr *SuiteResult) SetFailure() {
    53  	sr.IsFailed = true
    54  }
    55  
    56  // SetSpecsSkippedCount sets the count of specs skipped.
    57  func (sr *SuiteResult) SetSpecsSkippedCount() {
    58  	sr.SpecsSkippedCount = 0
    59  	for _, specRes := range sr.SpecResults {
    60  		if specRes.Skipped {
    61  			sr.SpecsSkippedCount++
    62  		}
    63  	}
    64  }
    65  
    66  // AddUnhandledError adds the unhandled error to suit result.
    67  func (sr *SuiteResult) AddUnhandledError(err error) {
    68  	sr.UnhandledErrors = append(sr.UnhandledErrors, err)
    69  }
    70  
    71  func (sr *SuiteResult) UpdateExecTime(startTime time.Time) {
    72  	sr.ExecutionTime = int64(time.Since(startTime) / 1e6)
    73  }
    74  
    75  // AddSpecResult adds a specs result to suit result.
    76  func (sr *SuiteResult) AddSpecResult(specResult *SpecResult) {
    77  	if specResult.IsFailed {
    78  		sr.IsFailed = true
    79  		sr.SpecsFailedCount++
    80  	}
    81  	sr.ExecutionTime += specResult.ExecutionTime
    82  	sr.SpecResults = append(sr.SpecResults, specResult)
    83  }
    84  
    85  // AddSpecResults adds multiple spec results to suit result.
    86  func (sr *SuiteResult) AddSpecResults(specResults []*SpecResult) {
    87  	for _, result := range specResults {
    88  		sr.AddSpecResult(result)
    89  	}
    90  }
    91  
    92  func (sr *SuiteResult) GetPreHook() []*gauge_messages.ProtoHookFailure {
    93  	if sr.PreSuite == nil {
    94  		return []*gauge_messages.ProtoHookFailure{}
    95  	}
    96  	return []*gauge_messages.ProtoHookFailure{sr.PreSuite}
    97  }
    98  
    99  func (sr *SuiteResult) GetPostHook() []*gauge_messages.ProtoHookFailure {
   100  	if sr.PostSuite == nil {
   101  		return []*gauge_messages.ProtoHookFailure{}
   102  	}
   103  	return []*gauge_messages.ProtoHookFailure{sr.PostSuite}
   104  }
   105  
   106  func (sr *SuiteResult) AddPreHook(f ...*gauge_messages.ProtoHookFailure) {
   107  	sr.PreSuite = f[0]
   108  }
   109  
   110  func (sr *SuiteResult) AddPostHook(f ...*gauge_messages.ProtoHookFailure) {
   111  	sr.PostSuite = f[0]
   112  }
   113  
   114  // ExecTime returns the time taken to execute the suit
   115  func (sr *SuiteResult) ExecTime() int64 {
   116  	return sr.ExecutionTime
   117  }
   118  
   119  // GetFailed returns the state of the result
   120  func (sr *SuiteResult) GetFailed() bool {
   121  	return sr.IsFailed
   122  }
   123  
   124  func (sr *SuiteResult) Item() interface{} {
   125  	return nil
   126  }