-
-
Notifications
You must be signed in to change notification settings - Fork 19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for loading features from fs.FS #146
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
21cda6f
refactor: introduce abstraction for loading features from alternative…
sagikazarmark 675200a
feat: add support for loading features from a filesystem
sagikazarmark f5bf4f5
refactor: remove features package
sagikazarmark 545d683
fix: lint
sagikazarmark fb14f48
feat: make pattern optional by introducing a separate function
sagikazarmark 6d6af1e
docs: document WithFeaturesFS
sagikazarmark 5f5ae72
Revert "feat: make pattern optional by introducing a separate function"
sagikazarmark b9bc45f
chore: make path parameter mandatory without fallback
sagikazarmark f7de812
docs: improve docs about WithFeaturesFS
sagikazarmark File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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() | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any ideas for better tests?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the test is OK. I have some thoughts about the API (see commend below).