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