Skip to content

Commit

Permalink
vfs: Began testing InMemoryVFS
Browse files Browse the repository at this point in the history
  • Loading branch information
meschbach committed Apr 16, 2019
1 parent b77eb8a commit 55ca8ca
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 0 deletions.
44 changes: 44 additions & 0 deletions tests/vfs-tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
const {expect} = require("chai");
const {InMemoryVFS} = require("../vfs");
const {MemoryWritable, promisePiped} = require("../streams");

function abstractVFSBehavior( VFS, name ) {
describe(name + " acts like a VFS", function () {
describe("Given a file which does not exist", function(){
it("when queried it does not exist", async function(){
const sut = new VFS();
expect(await sut.exists("nonexistent-file")).to.eq(false);
});
});

describe("When creating a file as bytes", function(){
beforeEach(async function(){
this.sut = new VFS();
this.fileName = "devision";
this.fileContents = "where do we go from here, stop and shed a tear";
await this.sut.putBytes(this.fileName, this.fileContents, this.fileContents.encoding);
});

it("is readable as a stream", async function(){
const istream = await this.sut.createReadableStream(this.fileName);
//TODO: The following should really be abstracted into a "promise to read all bytes" thing
const sink = new MemoryWritable();
await promisePiped(istream,sink);
expect(sink.bytes.toString("utf-8")).to.deep.eq(this.fileContents);
});

it("is readable as bytes", async function(){
const bytes = await this.sut.asBytes(this.fileName);
expect(bytes.toString("utf-8")).to.deep.eq(this.fileContents);
});
it("can not mutate returned byte buffer", async function(){
const bytes1 = await this.sut.asBytes(this.fileName);
bytes1.writeUInt32LE(51);
const bytes2 = await this.sut.asBytes(this.fileName);
expect(bytes2.toString("utf-8")).to.deep.eq(this.fileContents);
});
});
});
}

abstractVFSBehavior(InMemoryVFS, "InMemoryVFS");
55 changes: 55 additions & 0 deletions vfs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/**
* @module vfs
*
* Virtual File System
*
* Provides a framework to abstract out operating against the local file system or a remote file system. This is useful
* in testing, distributed portability, or vendor independence.
*/
const {
MemoryReadable,
MemoryWritable
} = require("./streams");

class InMemoryVFS {
constructor() {
this.files = {};
}

async exists( file ){
return !!this.files[file];
}

async unlink( file ){
if( this.files[file] ){
delete this.files[file];
}
}

async createReadableStream( file ){
if( !(await this.exists(file)) ){
throw new Error("No such file "+ file);
}
return new MemoryReadable(this.files[file]);
}

async asBytes( file ){
return Buffer.from(this.files[file]);
}

async putBytes( file, bytes, encoding ){
this.files[file] = Buffer.from(bytes, encoding);
}

async createWritableStream( file ){
const writable = new MemoryWritable();
writable.on("finish", () => {
this.files[file] = writable.bytes;
});
return writable;
}
}

module.exports = {
InMemoryVFS
}

0 comments on commit 55ca8ca

Please sign in to comment.