Skip to content

Commit

Permalink
add fs wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
Tighearnán Carroll committed Mar 27, 2023
1 parent 3e06956 commit 7b48c25
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 5 deletions.
2 changes: 1 addition & 1 deletion internal/flags/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ type Options struct {
FeatureContents []Feature

// FS allows passing in an `fs.FS` to read features from, such as an `embed.FS`
// or os.DirFS(string). Defaults to os.DirFS("./").
// or os.DirFS(string).
FS fs.FS

// ShowHelp enables suite to show CLI flags usage help and exit.
Expand Down
21 changes: 21 additions & 0 deletions internal/storage/fs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package storage

import (
"io/fs"
"os"
)

// FS is a wrapper that falls back to `os`.
type FS struct {
FS fs.FS
}

// Open a file in the provided `fs.FS`. If none provided,
// open via `os.Open`
func (f FS) Open(name string) (fs.File, error) {
if f.FS == nil {
return os.Open(name)
}

return f.FS.Open(name)
}
6 changes: 2 additions & 4 deletions run.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,9 +226,7 @@ func runWithOptions(suiteName string, runner runner, opt Options) int {
}

runner.fmt = multiFmt.FormatterFunc(suiteName, output)
if opt.FS == nil {
opt.FS = os.DirFS("./")
}
opt.FS = storage.FS{FS: opt.FS}

if len(opt.FeatureContents) > 0 {
features, err := parser.ParseFromBytes(opt.Tags, opt.FeatureContents)
Expand Down Expand Up @@ -372,7 +370,7 @@ func getDefaultOptions() (*Options, error) {
}

opt.Paths = flagSet.Args()
opt.FS = os.DirFS("./")
opt.FS = storage.FS{FS: os.DirFS("./")}

return opt, nil
}

0 comments on commit 7b48c25

Please # to comment.