Skip to content

Commit

Permalink
feat: add support for loading features from a filesystem
Browse files Browse the repository at this point in the history
Signed-off-by: Mark Sagi-Kazar <mark.sagikazar@gmail.com>
  • Loading branch information
sagikazarmark committed Jul 31, 2022
1 parent 0c9d1c4 commit 656c830
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 1 deletion.
17 changes: 17 additions & 0 deletions features/features.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//go:build go1.16
// +build go1.16

package features

import (
"embed"
"io/fs"
)

//go:embed *.feature
var features embed.FS

// Features returns a filesystem that contains feature files.
func Features() fs.FS {
return features
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/go-bdd/gobdd

go 1.12
go 1.16

require (
github.com/cucumber/gherkin-go/v13 v13.0.0
Expand Down
63 changes: 63 additions & 0 deletions gobdd_go1_16.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
//go:build go1.16
// +build go1.16

package gobdd

import (
"fmt"
"io"
"io/fs"
)

// WithFeaturesFS configures a filesystem and a pattern (regexp) where features can be found.
// An empty path defaults to "*.feature"
func WithFeaturesFS(fs fs.FS, pattern string) func(*SuiteOptions) {
return func(options *SuiteOptions) {
options.featureSource = fsFeatureSource{
fs: fs,
pattern: pattern,
}
}
}

type fsFeatureSource struct {
fs fs.FS
pattern string
}

func (s fsFeatureSource) loadFeatures() ([]feature, error) {
pattern := s.pattern
if pattern == "" {
pattern = "*.feature"
}

files, err := fs.Glob(s.fs, pattern)
if err != nil {
return nil, fmt.Errorf("loading features: %w", err)
}

features := make([]feature, 0, len(files))

for _, f := range files {
features = append(features, fsFeature{
fs: s.fs,
file: f,
})
}

return features, nil
}

type fsFeature struct {
fs fs.FS
file string
}

func (f fsFeature) Open() (io.Reader, error) {
file, err := f.fs.Open(string(f.file))
if err != nil {
return nil, fmt.Errorf("opening feature: %w", err)
}

return file, nil
}
21 changes: 21 additions & 0 deletions gobdd_go1_16_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//go:build go1.16
// +build go1.16

package gobdd

import (
"regexp"
"testing"

"github.com/go-bdd/gobdd/features"
)

func TestWithFeaturesFS(t *testing.T) {
suite := NewSuite(t, WithFeaturesFS(features.Features(), "example.feature"))
compiled := regexp.MustCompile(`I add (\d+) and (\d+)`)
suite.AddRegexStep(compiled, add)
compiled = regexp.MustCompile(`the result should equal (\d+)`)
suite.AddRegexStep(compiled, check)

suite.Run()
}

0 comments on commit 656c830

Please sign in to comment.