Skip to content

Commit

Permalink
make pickleStepIDs unique accross multiple paths (#366)
Browse files Browse the repository at this point in the history
* make pickleStepIDs unique accross multiple paths

* add test case for duplicate pickle ids

* go fmt

Co-authored-by: Rickard Englund <rickard.englund@skf.com>
  • Loading branch information
Rickard Englund and Rickard Englund committed Dec 16, 2020
1 parent adfc936 commit 2fc2995
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 4 deletions.
7 changes: 3 additions & 4 deletions internal/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func parseFeatureDir(dir string, newIDFunc func() string) ([]*models.Feature, er
})
}

func parsePath(path string) ([]*models.Feature, error) {
func parsePath(path string, newIDFunc func() string) ([]*models.Feature, error) {
var features []*models.Feature

path, line := ExtractFeaturePathLine(path)
Expand All @@ -88,8 +88,6 @@ func parsePath(path string) ([]*models.Feature, error) {
return features, err
}

newIDFunc := (&messages.Incrementing{}).NewId

if fi.IsDir() {
return parseFeatureDir(path, newIDFunc)
}
Expand Down Expand Up @@ -119,8 +117,9 @@ func ParseFeatures(filter string, paths []string) ([]*models.Feature, error) {

featureIdxs := make(map[string]int)
uniqueFeatureURI := make(map[string]*models.Feature)
newIDFunc := (&messages.Incrementing{}).NewId
for _, path := range paths {
feats, err := parsePath(path)
feats, err := parsePath(path, newIDFunc)

switch {
case os.IsNotExist(err):
Expand Down
45 changes: 45 additions & 0 deletions internal/parser/parser_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package parser_test

import (
"io/ioutil"
"os"
"path/filepath"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/cucumber/godog/internal/parser"
)
Expand Down Expand Up @@ -32,3 +36,44 @@ func Test_FeatureFilePathParser(t *testing.T) {
assert.Equal(t, ln, c.line)
}
}

func Test_ParseFeatures_FromMultiplePaths(t *testing.T) {
const featureFileName = "godogs.feature"
const featureFileContents = `Feature: eat godogs
In order to be happy
As a hungry gopher
I need to be able to eat godogs
Scenario: Eat 5 out of 12
Given there are 12 godogs
When I eat 5
Then there should be 7 remaining`

baseDir := filepath.Join(os.TempDir(), t.Name(), "godogs")
errA := os.MkdirAll(baseDir+"/a", 0755)
errB := os.MkdirAll(baseDir+"/b", 0755)
defer os.RemoveAll(baseDir)

require.Nil(t, errA)
require.Nil(t, errB)

err := ioutil.WriteFile(filepath.Join(baseDir+"/a", featureFileName), []byte(featureFileContents), 0644)
require.Nil(t, err)
err = ioutil.WriteFile(filepath.Join(baseDir+"/b", featureFileName), []byte(featureFileContents), 0644)
require.Nil(t, err)

features, err := parser.ParseFeatures("", []string{baseDir + "/a", baseDir + "/b"})
assert.Nil(t, err)
assert.Len(t, features, 2)

pickleIDs := map[string]bool{}
for _, f := range features {
for _, p := range f.Pickles {
if pickleIDs[p.Id] {
assert.Failf(t, "found duplicate pickle ID", "Pickle ID %s was already used", p.Id)
}

pickleIDs[p.Id] = true
}
}
}

0 comments on commit 2fc2995

Please sign in to comment.