Skip to content

Commit

Permalink
Specs reports location should be relative to reports dir when specs d…
Browse files Browse the repository at this point in the history
…ir is outside of gauge project dir. (#263)

* Specs reports location should be relative to reports dir when specs are outside of gauge project root. #249

Signed-off-by: Dharmendra Singh <[email protected]>

* Fixed platform dependent filepath in tests

Signed-off-by: Dharmendra Singh <[email protected]>
  • Loading branch information
negiDharmendra authored May 25, 2020
1 parent 83b3557 commit e196fae
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 60 deletions.
1 change: 1 addition & 0 deletions generator/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ type spec struct {
CommentsAfterDatatable string `json:"CommentsAfterDatatable"`
SpecHeading string `json:"SpecHeading"`
FileName string `json:"FileName"`
SpecFileName string `json:"SpecFileName"`
Tags []string `json:"Tags"`
ExecutionTime int64 `json:"ExecutionTime"`
ExecutionStatus status `json:"ExecutionStatus"`
Expand Down
12 changes: 8 additions & 4 deletions generator/transform.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package generator
import (
"encoding/base64"
"fmt"
"os"
"path/filepath"
"sort"
"strings"
Expand Down Expand Up @@ -63,7 +64,7 @@ func ToSuiteResult(pRoot string, psr *gm.ProtoSuiteResult) *SuiteResult {
}
suiteResult.SpecResults = make([]*spec, 0)
for _, protoSpecRes := range psr.GetSpecResults() {
suiteResult.SpecResults = append(suiteResult.SpecResults, toSpec(protoSpecRes))
suiteResult.SpecResults = append(suiteResult.SpecResults, toSpec(protoSpecRes, projectRoot))
suiteResult.PassedScenarioCount = suiteResult.PassedScenarioCount + int(protoSpecRes.GetScenarioCount()-protoSpecRes.GetScenarioFailedCount()-protoSpecRes.GetScenarioSkippedCount())
suiteResult.FailedScenarioCount = suiteResult.FailedScenarioCount + int(protoSpecRes.GetScenarioFailedCount())
suiteResult.SkippedScenarioCount = suiteResult.SkippedScenarioCount + int(protoSpecRes.GetScenarioSkippedCount())
Expand Down Expand Up @@ -280,19 +281,22 @@ func toSpecHeader(res *spec) *specHeader {
return &specHeader{
SpecName: res.SpecHeading,
ExecutionTime: formatTime(res.ExecutionTime),
FileName: res.FileName,
FileName: res.SpecFileName,
Tags: res.Tags,
Summary: toScenarioSummary(res),
}
}

func toSpec(res *gm.ProtoSpecResult) *spec {
func toSpec(res *gm.ProtoSpecResult, projectRoot string) *spec {
relSpecPath, _ := filepath.Rel(projectRoot, res.ProtoSpec.FileName)
normalizedSpecPath := strings.ReplaceAll(relSpecPath, fmt.Sprintf("..%c", os.PathSeparator), "")
spec := &spec{
Scenarios: make([]*scenario, 0),
BeforeSpecHookFailures: make([]*hookFailure, 0),
AfterSpecHookFailures: make([]*hookFailure, 0),
Errors: make([]buildError, 0),
FileName: res.GetProtoSpec().GetFileName(),
FileName: filepath.Join(projectRoot, normalizedSpecPath),
SpecFileName: res.GetProtoSpec().GetFileName(),
SpecHeading: res.GetProtoSpec().GetSpecHeading(),
IsTableDriven: res.GetProtoSpec().GetIsTableDriven(),
ExecutionTime: res.GetExecutionTime(),
Expand Down
90 changes: 56 additions & 34 deletions generator/transform_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package generator

import (
"os"
"path/filepath"
"reflect"
"testing"
Expand Down Expand Up @@ -142,7 +143,7 @@ var specRes1 = &gm.ProtoSpecResult{
ProtoSpec: &gm.ProtoSpec{
SpecHeading: "specRes1",
Tags: []string{"tag1", "tag2"},
FileName: "/tmp/gauge/specs/foobar.spec",
FileName: filepath.Join(string(os.PathSeparator), "tmp", "gauge", "specs", "foobar.spec"),
IsTableDriven: true,
Items: []*gm.ProtoItem{
newCommentItem("\n"),
Expand Down Expand Up @@ -190,6 +191,7 @@ var spec1 = &spec{
SpecHeading: "specRes1",
Tags: []string{"tag1", "tag2"},
FileName: "/tmp/gauge/specs/foobar.spec",
SpecFileName: "/tmp/gauge/specs/foobar.spec",
ExecutionTime: 211316,
IsTableDriven: true,
Datatable: &table{
Expand Down Expand Up @@ -268,6 +270,7 @@ var specResWithSpecHookFailure = &gm.ProtoSpecResult{
Skipped: true,
ExecutionTime: 211316,
ProtoSpec: &gm.ProtoSpec{
FileName: "specfile-1.spec",
SpecHeading: "specRes3",
Tags: []string{"tag1"},
PreHookFailures: []*gm.ProtoHookFailure{{
Expand Down Expand Up @@ -569,15 +572,16 @@ func TestToSpec(t *testing.T) {
Errors: make([]buildError, 0),
SpecHeading: "specRes1",
Tags: []string{"tag1", "tag2"},
FileName: "/tmp/gauge/specs/foobar.spec",
FileName: filepath.Join(string(os.PathSeparator), "tmp", "gauge", "specs", "foobar.spec"),
SpecFileName: filepath.Join(string(os.PathSeparator), "tmp", "gauge", "specs", "foobar.spec"),
IsTableDriven: true,
ExecutionStatus: pass,
ExecutionTime: 211316,
PreHookMessages: []string{"Before Spec Hook Message"},
PostHookMessages: []string{"After Spec Hook Message"},
}

got := toSpec(specRes1)
got := toSpec(specRes1, "/tmp/gauge/")
checkEqual(t, "", want, got)
}

Expand All @@ -599,7 +603,7 @@ func TestToSpecWithScenariosInOrder(t *testing.T) {
},
}

got := toSpec(specRes)
got := toSpec(specRes, "")
if len(got.Scenarios) != 5 {
t.Errorf("want:%q\ngot:%q\n", 5, len(got.Scenarios))
}
Expand All @@ -622,7 +626,8 @@ func TestToSpecWithScenariosInOrder(t *testing.T) {

func TestToSpecWithErrors(t *testing.T) {
specRes := &gm.ProtoSpecResult{
Failed: true,
Failed: true,
ProtoSpec: &gm.ProtoSpec{FileName: "spec-file-1.spec"},
Errors: []*gm.Error{
{
Filename: "fileName",
Expand All @@ -645,10 +650,12 @@ func TestToSpecWithErrors(t *testing.T) {
{FileName: "fileName1", LineNumber: 4, Message: "message1", ErrorType: validationErrorType},
},
Scenarios: make([]*scenario, 0),
FileName: "spec-file-1.spec",
SpecFileName: "spec-file-1.spec",
ExecutionStatus: fail,
}

got := toSpec(specRes)
got := toSpec(specRes, "")

checkEqual(t, "", want, got)
}
Expand All @@ -660,7 +667,8 @@ func TestToSpecForTableDrivenSpec(t *testing.T) {
Rows: []*row{{Cells: []string{"Gauge", "3"}, Result: fail}, {Cells: []string{"Mingle", "2"}, Result: pass}},
},
SpecHeading: "specRes1",
FileName: "/tmp/gauge/specs/foobar.spec",
FileName: filepath.Join(string(os.PathSeparator), "tmp", "gauge", "specs", "foobar.spec"),
SpecFileName: "/tmp/gauge/specs/foobar.spec",
IsTableDriven: true,
ExecutionStatus: pass,
ExecutionTime: 211316,
Expand Down Expand Up @@ -712,7 +720,7 @@ func TestToSpecForTableDrivenSpec(t *testing.T) {
SkippedScenarioCount: 0,
}

got := toSpec(datatableDrivenSpec)
got := toSpec(datatableDrivenSpec, "/tmp/gauge/")

checkEqual(t, "", want, got)
}
Expand All @@ -726,17 +734,19 @@ func TestToSpecWithHookFailure(t *testing.T) {
Errors: make([]buildError, 0),
Tags: []string{"tag1"},
SpecHeading: "specRes3",
SpecFileName: "specfile-1.spec",
FileName: "specfile-1.spec",
ExecutionStatus: skip,
ExecutionTime: 211316,
}

got := toSpec(specResWithSpecHookFailure)
got := toSpec(specResWithSpecHookFailure, "")
checkEqual(t, "", want, got)
}

func TestToSpecWithFileName(t *testing.T) {
want := specRes1.GetProtoSpec().GetFileName()
got := toSpec(specRes1).FileName
got := toSpec(specRes1, "/tmp/gauge").FileName

if got != want {
t.Errorf("Expecting spec.FileName=%s, got %s\n", want, got)
Expand All @@ -745,7 +755,7 @@ func TestToSpecWithFileName(t *testing.T) {

func TestToSpecWithSpecHeading(t *testing.T) {
want := specRes1.GetProtoSpec().GetSpecHeading()
got := toSpec(specRes1).SpecHeading
got := toSpec(specRes1, "").SpecHeading

if got != want {
t.Errorf("Expecting spec.SpecHeading=%s, got %s\n", want, got)
Expand All @@ -754,7 +764,7 @@ func TestToSpecWithSpecHeading(t *testing.T) {

func TestToSpecWithTags(t *testing.T) {
want := specRes1.GetProtoSpec().GetTags()
got := toSpec(specRes1).Tags
got := toSpec(specRes1, "").Tags

checkEqual(t, "", want, got)
}
Expand All @@ -770,7 +780,7 @@ To execute this specification, run
gauge specs
`
got := toSpec(specRes1).CommentsBeforeDatatable
got := toSpec(specRes1, "").CommentsBeforeDatatable

checkEqual(t, "", want, got)
}
Expand All @@ -780,13 +790,13 @@ func TestToSpecWithDataTableMapsCommentsAfterDatatable(t *testing.T) {
Comment 1
Comment 2
Comment 3`
got := toSpec(specRes1).CommentsAfterDatatable
got := toSpec(specRes1, "").CommentsAfterDatatable

checkEqual(t, "", want, got)
}

func TestToSpecWithDataTableIsTableDriven(t *testing.T) {
got := toSpec(specRes1).IsTableDriven
got := toSpec(specRes1, "").IsTableDriven

if !got {
t.Errorf("Expecting spec.IsTableDriven=true\n")
Expand All @@ -801,49 +811,49 @@ func TestToSpecWithDataTableHasDatatable(t *testing.T) {
&row{Cells: []string{"Mingle", "2"}, Result: skip},
},
}
got := toSpec(specRes1).Datatable
got := toSpec(specRes1, "").Datatable

checkEqual(t, "", want, got)
}

func TestToSpecWithDataTableExecutionTime(t *testing.T) {
want := 211316
got := toSpec(specRes1).ExecutionTime
got := toSpec(specRes1, "").ExecutionTime

checkEqual(t, "", want, got)
}

func TestToSpecWithDataTableExecutionStatusPass(t *testing.T) {
want := pass
got := toSpec(specRes1).ExecutionStatus
got := toSpec(specRes1, "").ExecutionStatus

checkEqual(t, "", want, got)
}

func TestToSpecWithDataTableExecutionStatusSkip(t *testing.T) {
want := skip
got := toSpec(&gm.ProtoSpecResult{Skipped: true, Failed: false}).ExecutionStatus
got := toSpec(&gm.ProtoSpecResult{Skipped: true, Failed: false, ProtoSpec: &gm.ProtoSpec{FileName: "spec-file.spec"}}, "").ExecutionStatus

checkEqual(t, "", want, got)
}

func TestToSpecWithDataTableExecutionStatusFail(t *testing.T) {
want := fail
got := toSpec(&gm.ProtoSpecResult{Skipped: false, Failed: true}).ExecutionStatus
got := toSpec(&gm.ProtoSpecResult{Skipped: false, Failed: true, ProtoSpec: &gm.ProtoSpec{FileName: "spec-file.spec"}}, "").ExecutionStatus

checkEqual(t, "", want, got)
}

func TestToSpecWithBeforeHookFailure(t *testing.T) {
want := []*hookFailure{{ErrMsg: "err", HookName: "Before Spec", FailureScreenshotFile: "Screenshot.png", StackTrace: "Stacktrace"}}
got := toSpec(specResWithSpecHookFailure).BeforeSpecHookFailures
got := toSpec(specResWithSpecHookFailure, "").BeforeSpecHookFailures

checkEqual(t, "", want, got)
}

func TestToSpecWithAfterHookFailure(t *testing.T) {
want := []*hookFailure{{ErrMsg: "err", HookName: "After Spec", FailureScreenshotFile: "Screenshot.png", StackTrace: "Stacktrace", TableRowIndex: 0}}
got := toSpec(specResWithSpecHookFailure).AfterSpecHookFailures
got := toSpec(specResWithSpecHookFailure, "").AfterSpecHookFailures

checkEqual(t, "", want, got)
}
Expand Down Expand Up @@ -876,15 +886,15 @@ func TestToSpecWithScenarios(t *testing.T) {
},
},
},
}).Scenarios
}, "").Scenarios

if len(got) != 2 {
t.Errorf("Expected 2 scenarios, got %d\n", len(got))
}
}

func TestToSpecWithScenariosTableDriven(t *testing.T) {
got := toSpec(datatableDrivenSpec).Scenarios
got := toSpec(datatableDrivenSpec, "").Scenarios

if len(got) != 2 {
t.Errorf("Expected 2 scenarios, got %d\n", len(got))
Expand Down Expand Up @@ -930,7 +940,7 @@ func TestToSpecWithScenarioStatusCounts(t *testing.T) {
},
},
},
})
}, "")

if got.PassedScenarioCount != 1 {
t.Errorf("Expected spec.PassedScenarioCount=1, got %d\n", got.PassedScenarioCount)
Expand Down Expand Up @@ -1464,12 +1474,12 @@ func TestMapExecutionTimeToSuiteResult(t *testing.T) {

func TestSpecsCountToSuiteResult(t *testing.T) {
psr := &gm.ProtoSuiteResult{SpecsFailedCount: 2, SpecsSkippedCount: 1, SpecResults: []*gm.ProtoSpecResult{
{Skipped: false, Failed: false},
{Skipped: false, Failed: true},
{Skipped: true, Failed: false},
{Skipped: false, Failed: true},
{Skipped: false, Failed: false},
{Skipped: false, Failed: false},
{Skipped: false, Failed: false, ProtoSpec: &gm.ProtoSpec{FileName: "spec-file-1.spec"}},
{Skipped: false, Failed: true, ProtoSpec: &gm.ProtoSpec{FileName: "spec-file-2.spec"}},
{Skipped: true, Failed: false, ProtoSpec: &gm.ProtoSpec{FileName: "spec-file-3.spec"}},
{Skipped: false, Failed: true, ProtoSpec: &gm.ProtoSpec{FileName: "spec-file-4.spec"}},
{Skipped: false, Failed: false, ProtoSpec: &gm.ProtoSpec{FileName: "spec-file-5.spec"}},
{Skipped: false, Failed: false, ProtoSpec: &gm.ProtoSpec{FileName: "spec-file-6.spec"}},
}}
res := ToSuiteResult("", psr)

Expand All @@ -1486,9 +1496,9 @@ func TestSpecsCountToSuiteResult(t *testing.T) {

func TestScenarioCountToSuiteResult(t *testing.T) {
psr := &gm.ProtoSuiteResult{SpecResults: []*gm.ProtoSpecResult{
{ScenarioCount: 3, ScenarioFailedCount: 2},
{ScenarioCount: 3, ScenarioSkippedCount: 1},
{ScenarioCount: 3, ScenarioSkippedCount: 1, ScenarioFailedCount: 2},
{ScenarioCount: 3, ScenarioFailedCount: 2, ProtoSpec: &gm.ProtoSpec{FileName: "spec-file-1.spec"}},
{ScenarioCount: 3, ScenarioSkippedCount: 1, ProtoSpec: &gm.ProtoSpec{FileName: "spec-file-2.spec"}},
{ScenarioCount: 3, ScenarioSkippedCount: 1, ScenarioFailedCount: 2, ProtoSpec: &gm.ProtoSpec{FileName: "spec-file-3.spec"}},
}}
res := ToSuiteResult("", psr)

Expand All @@ -1503,6 +1513,18 @@ func TestScenarioCountToSuiteResult(t *testing.T) {
}
}

func TestToSuiteResultShouldNormalizeSpecFilepath(t *testing.T) {
projectRoot := filepath.Join(string(os.PathSeparator), "user", "user-name", "work", "gauge-project")
psr := &gm.ProtoSuiteResult{SpecsFailedCount: 2, SpecsSkippedCount: 1, SpecResults: []*gm.ProtoSpecResult{
{ProtoSpec: &gm.ProtoSpec{FileName: "/user/user-name/work/common-specs/specs/spec-file-1.spec"}},
}}
res := ToSuiteResult(projectRoot, psr)
ecpectedFilePath := filepath.Join(projectRoot, "common-specs", "specs", "spec-file-1.spec")
if res.SpecResults[0].FileName != ecpectedFilePath {
t.Errorf("Expected normalized spec file path to be : %s; got %s\n", ecpectedFilePath, res.SpecResults[0].FileName)
}
}

func TestMapPreHookFailureToSuiteResult(t *testing.T) {
psr := &gm.ProtoSuiteResult{PreHookFailure: &gm.ProtoHookFailure{ErrorMessage: "foo failure"}}
res := ToSuiteResult("", psr)
Expand Down
2 changes: 1 addition & 1 deletion regenerate/_testdata/expectedE2E/simpleSuiteRes/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ <h3 class="title">Specifications</h3>
<div id="listOfSpecifications">
<ul id="scenarios" class="spec-list">

<a href="example.html">
<a href="specs/example.html">

<li class="passed spec-name">

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit e196fae

Please sign in to comment.