-
-
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.
Add support for loading features from fs.FS (#146)
- Loading branch information
1 parent
359187c
commit 2911ec3
Showing
6 changed files
with
159 additions
and
15 deletions.
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
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,11 @@ | ||
//go:build go1.16 | ||
// +build go1.16 | ||
|
||
package gobdd | ||
|
||
import ( | ||
"embed" | ||
) | ||
|
||
//go:embed features/*.feature | ||
var featuresFS embed.FS |
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
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,57 @@ | ||
//go:build go1.16 | ||
// +build go1.16 | ||
|
||
package gobdd | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"io/fs" | ||
) | ||
|
||
// WithFeaturesFS configures a filesystem and a path (glob pattern) where features can be found. | ||
func WithFeaturesFS(fs fs.FS, path string) func(*SuiteOptions) { | ||
return func(options *SuiteOptions) { | ||
options.featureSource = fsFeatureSource{ | ||
fs: fs, | ||
path: path, | ||
} | ||
} | ||
} | ||
|
||
type fsFeatureSource struct { | ||
fs fs.FS | ||
path string | ||
} | ||
|
||
func (s fsFeatureSource) loadFeatures() ([]feature, error) { | ||
files, err := fs.Glob(s.fs, s.path) | ||
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(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,19 @@ | ||
//go:build go1.16 | ||
// +build go1.16 | ||
|
||
package gobdd | ||
|
||
import ( | ||
"regexp" | ||
"testing" | ||
) | ||
|
||
func TestWithFeaturesFS(t *testing.T) { | ||
suite := NewSuite(t, WithFeaturesFS(featuresFS, "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() | ||
} |