From 656c830ff2102eb96ac713cac0abaec578da5385 Mon Sep 17 00:00:00 2001 From: Mark Sagi-Kazar Date: Sun, 31 Jul 2022 17:20:59 +0200 Subject: [PATCH] feat: add support for loading features from a filesystem Signed-off-by: Mark Sagi-Kazar --- features/features.go | 17 ++++++++++++ go.mod | 2 +- gobdd_go1_16.go | 63 ++++++++++++++++++++++++++++++++++++++++++++ gobdd_go1_16_test.go | 21 +++++++++++++++ 4 files changed, 102 insertions(+), 1 deletion(-) create mode 100644 features/features.go create mode 100644 gobdd_go1_16.go create mode 100644 gobdd_go1_16_test.go diff --git a/features/features.go b/features/features.go new file mode 100644 index 0000000..ecdf057 --- /dev/null +++ b/features/features.go @@ -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 +} diff --git a/go.mod b/go.mod index f423d32..d0971ae 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/gobdd_go1_16.go b/gobdd_go1_16.go new file mode 100644 index 0000000..b06a4f9 --- /dev/null +++ b/gobdd_go1_16.go @@ -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 +} diff --git a/gobdd_go1_16_test.go b/gobdd_go1_16_test.go new file mode 100644 index 0000000..4486e87 --- /dev/null +++ b/gobdd_go1_16_test.go @@ -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() +}