github.com/getgauge/gauge@v1.6.9/execution/rerun/rerun_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 rerun
     8  
     9  import (
    10  	"os"
    11  	"path/filepath"
    12  	"testing"
    13  
    14  	"github.com/getgauge/common"
    15  	"github.com/getgauge/gauge-proto/go/gauge_messages"
    16  	"github.com/getgauge/gauge/config"
    17  	"github.com/getgauge/gauge/execution/result"
    18  	"github.com/getgauge/gauge/gauge"
    19  	"github.com/getgauge/gauge/util"
    20  
    21  	"sort"
    22  
    23  	. "gopkg.in/check.v1"
    24  )
    25  
    26  func Test(t *testing.T) { TestingT(t) }
    27  
    28  type MySuite struct{}
    29  
    30  var _ = Suite(&MySuite{})
    31  
    32  func (s *MySuite) SetUpTest(c *C) {
    33  	p, _ := filepath.Abs("_testdata")
    34  	config.ProjectRoot = p
    35  	failedMeta = newFailedMetaData()
    36  }
    37  
    38  func (s *MySuite) TestIfFailedFileIsCreated(c *C) {
    39  	msg := "hello world"
    40  
    41  	writeFailedMeta(msg)
    42  
    43  	file := filepath.Join(config.ProjectRoot, common.DotGauge, failedFile)
    44  	c.Assert(common.FileExists(file), Equals, true)
    45  	expected := msg
    46  
    47  	content, _ := os.ReadFile(file)
    48  
    49  	c.Assert(string(content), Equals, expected)
    50  	os.RemoveAll(filepath.Join(config.ProjectRoot, common.DotGauge))
    51  }
    52  
    53  func (s *MySuite) TestGetScenarioFailedMetadata(c *C) {
    54  	spec1Rel := filepath.Join("specs", "example1.spec")
    55  	spec1Abs := filepath.Join(config.ProjectRoot, spec1Rel)
    56  	sce := &gauge.Scenario{Span: &gauge.Span{Start: 2}}
    57  	sr1 := &result.ScenarioResult{ProtoScenario: &gauge_messages.ProtoScenario{ExecutionStatus: gauge_messages.ExecutionStatus_FAILED}}
    58  
    59  	prepareScenarioFailedMetadata(sr1, sce, &gauge_messages.ExecutionInfo{CurrentSpec: &gauge_messages.SpecInfo{FileName: spec1Abs}})
    60  
    61  	c.Assert(len(failedMeta.failedItemsMap[spec1Abs]), Equals, 1)
    62  	c.Assert(failedMeta.failedItemsMap[spec1Abs][spec1Rel+":2"], Equals, true)
    63  }
    64  
    65  func (s *MySuite) TestAddSpecPreHookFailedMetadata(c *C) {
    66  	spec1Rel := filepath.Join("specs", "example1.spec")
    67  	spec1Abs := filepath.Join(config.ProjectRoot, spec1Rel)
    68  	spec1 := &result.SpecResult{ProtoSpec: &gauge_messages.ProtoSpec{PreHookFailures: []*gauge_messages.ProtoHookFailure{{ErrorMessage: "error"}}, FileName: spec1Abs}}
    69  
    70  	addFailedMetadata(spec1, []string{}, addSpecFailedMetadata)
    71  
    72  	c.Assert(len(failedMeta.failedItemsMap[spec1Rel]), Equals, 1)
    73  	c.Assert(failedMeta.failedItemsMap[spec1Rel][spec1Rel], Equals, true)
    74  }
    75  
    76  func (s *MySuite) TestAddSpecPostHookFailedMetadata(c *C) {
    77  	spec1Rel := filepath.Join("specs", "example1.spec")
    78  	spec1Abs := filepath.Join(config.ProjectRoot, spec1Rel)
    79  	spec1 := &result.SpecResult{ProtoSpec: &gauge_messages.ProtoSpec{PostHookFailures: []*gauge_messages.ProtoHookFailure{{ErrorMessage: "error"}}, FileName: spec1Abs}}
    80  
    81  	addFailedMetadata(spec1, []string{}, addSpecFailedMetadata)
    82  
    83  	c.Assert(len(failedMeta.failedItemsMap[spec1Rel]), Equals, 1)
    84  	c.Assert(failedMeta.failedItemsMap[spec1Rel][spec1Rel], Equals, true)
    85  }
    86  
    87  func (s *MySuite) TestAddSpecFailedMetadataOverwritesPreviouslyAddedValues(c *C) {
    88  	spec1Rel := filepath.Join("specs", "example1.spec")
    89  	spec1Abs := filepath.Join(config.ProjectRoot, spec1Rel)
    90  	spec1 := &result.SpecResult{ProtoSpec: &gauge_messages.ProtoSpec{PreHookFailures: []*gauge_messages.ProtoHookFailure{{ErrorMessage: "error"}}, FileName: spec1Abs}}
    91  	failedMeta.failedItemsMap[spec1Rel] = make(map[string]bool)
    92  	failedMeta.failedItemsMap[spec1Rel]["scn1"] = true
    93  	failedMeta.failedItemsMap[spec1Rel]["scn2"] = true
    94  
    95  	addSpecFailedMetadata(spec1, []string{})
    96  
    97  	c.Assert(len(failedMeta.failedItemsMap[spec1Rel]), Equals, 1)
    98  	c.Assert(failedMeta.failedItemsMap[spec1Rel][spec1Rel], Equals, true)
    99  }
   100  
   101  func (s *MySuite) TestGetRelativePath(c *C) {
   102  	spec1Rel := filepath.Join("specs", "example1.spec")
   103  	spec1Abs := filepath.Join(config.ProjectRoot, spec1Rel)
   104  
   105  	path := util.RelPathToProjectRoot(spec1Abs)
   106  
   107  	c.Assert(path, Equals, spec1Rel)
   108  }
   109  
   110  func (s *MySuite) TestGetAllFailedItems(c *C) {
   111  	spec1Rel := filepath.Join("specs", "example1.spec")
   112  	spec2Rel := filepath.Join("specs", "example2.spec")
   113  	metaData := newFailedMetaData()
   114  	metaData.failedItemsMap[spec1Rel] = make(map[string]bool)
   115  	metaData.failedItemsMap[spec2Rel] = make(map[string]bool)
   116  	metaData.failedItemsMap[spec1Rel]["scn1"] = true
   117  	metaData.failedItemsMap[spec1Rel]["scn2"] = true
   118  	metaData.failedItemsMap[spec2Rel]["scn3"] = true
   119  
   120  	failedItems := metaData.getFailedItems()
   121  	sort.Strings(failedItems)
   122  
   123  	c.Assert(failedItems, DeepEquals, []string{"scn1", "scn2", "scn3"})
   124  }