go-testfixtures / testfixtures

Ruby on Rails like test fixtures for Go. Write tests against a real database
https://pkg.go.dev/github.com/go-testfixtures/testfixtures/v3?tab=doc
MIT License
1.11k stars 80 forks source link

Load fixtures from http.FileSystem to support files bundled with packr #49

Open stoewer opened 5 years ago

stoewer commented 5 years ago

For some testing scenarios it can be handy to generate a binary that is able to populate it's own test database. A prerequisite for this is to bundle the fixture files with the binary. One possible way to do so is packr. But there is no straightforward way to make the bundled files available for the testfixtures package.

Luckily packr implements http.FileSystem in order to represent the bundled files in the binary. I therefore suggest to add the following functions to testfixtures:

func NewFileSystem(db *sql.DB, helper Helper, fs http.FileSystem) (*Context, error) 
func LoadFixtureFileSystem(db *sql.DB, helper Helper, fs http.FileSystem) error

This would allow testfixtures to work together with packr or other sources for fixture files that implement the http.FileSystem interface.

stoewer commented 5 years ago

If I could get some :+1: from the maintainers I would be willing to work on this feature.

andreynering commented 5 years ago

Hi @stoewer,

I used embedding tools like fileb0x a lot, but for production stuff like assets and HTML templates. I found it very curious that you want to use it for testing stuff.

I have no opposition to adding a function that accepts http.FileSystem, though. I'd be happy to review a pull request for it. 🙂

Something to keep in mind is that it may be necessary to add an additional directory parameter to that function, otherwise users won't be able to load fixtures if they're not in the root directory.

stoewer commented 5 years ago

I discovered a bug in packr while implementing this feature #211. I'll suspend the development until it get's fixed.

andreynering commented 4 years ago

Perhaps we should wait to do this now that the Go team is discussing embed assets support in Go itself?

Once that is released, it will become the default way of embedding assets in Go, and so we should follow its pattern.