-
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support for loading features from a filesystem
Signed-off-by: Mark Sagi-Kazar <mark.sagikazar@gmail.com>
- Loading branch information
1 parent
0c9d1c4
commit 656c830
Showing
4 changed files
with
102 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |