jvilk / BrowserFS

BrowserFS is an in-browser filesystem that emulates the Node JS filesystem API and supports storing and retrieving files from various backends.
Other
3.06k stars 215 forks source link

Share test harness with `isomorphic-git`? #207

Closed billiegoose closed 6 years ago

billiegoose commented 6 years ago

@jvilk I've got a problem in isomorphic-git that you've solved for BrowserFS, which is how to load filesystem fixtures and run unit tests in the browser.

Believe it or not, despite making the backend test thing for a previous PR, I don't understand how the test harness in BrowserFS works. It looks like it involves browserify and rollup and webpack and TypeScript? I know this is a bit of a stretch, but I wondered if you'd be interested in explaining to me a bit more how it works, and in exchange I could help document/refactor the test harness into a shared npm package that both BrowserFS and isomorphic-git could use. Thoughts?

jvilk commented 6 years ago

Sure, I can explain things a bit, although I don't know if any of it is general purpose or reusable. It's convoluted. The unit tests do not run against a bundled version of BrowserFS; instead, it directly bundles the test and source modules into a single JavaScript file. I did this for two reasons: 1) convenience, and 2) because folks will consume BFS in the same way from NPM.

Here's how the build process proceeds:

Like with our regular bundles, Webpack is needed to support global Buffer references and some Node polyfills. I have not revisited this decision since to see if Rollup works sufficiently well to no longer use Webpack (or visa-versa). (I use Rollup + Webpack because Rollup significantly reduced bundle sizes. I have no idea how well Webpack 2+ works in this respect.)

Hope that helps, and hope I'm not too late with this information!

billiegoose commented 6 years ago

You may be right... it may not be as general purpose/reusable as I had hoped. I like the Base64 encoding idea though. I might be able to use that somehow....

What I ended up creating for isomorphic-git's tests is a, well, isomorphic fixture factory. The crux of it is:

async function makeFixture (name) {
  return process.browser ? makeBrowserFixture(name) : makeNodeFixture(name)
}

where makeFixture returns a { fs, dir } each time it's called. makeNodeFixture creates a temporary directory, copies the fixture files in name into the temp dir, and returns { fs, dir = tempdir }. makeBrowserFixture returns an OverlayFS of a readable HTTPRequestFS that hosts all the fixture directories and a writeable MemoryFS, and each time makeBrowserFixture is called it empties the MemoryFS to reset any writes made by an earlier test.

It ends up being two completely different mechanisms but manages to provide the same API for setting up fixtures. But since it uses BrowserFS to provide the fixture, that strategy probably can't be used in BrowserFS's own unit tests. ¯\_(ツ)_/¯