Skip to content

Commit

Permalink
vfs: Test additional VFS behaviors
Browse files Browse the repository at this point in the history
  • Loading branch information
meschbach committed Apr 16, 2019
1 parent 55ca8ca commit 1e6b426
Showing 1 changed file with 57 additions and 16 deletions.
73 changes: 57 additions & 16 deletions tests/vfs-tests.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
const {expect} = require("chai");
const {InMemoryVFS} = require("../vfs");
const {MemoryWritable, promisePiped} = require("../streams");
const {promiseEvent} = require("../future");

async function promiseFinished( from, writeChunk ){
const done = promiseEvent( from, "finish");
const result = await writeChunk();
await done;
return result;
}

function abstractVFSBehavior( VFS, name ) {
describe(name + " acts like a VFS", function () {
Expand All @@ -19,26 +27,59 @@ function abstractVFSBehavior( VFS, name ) {
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);
});
abstractReadBackBehaviors();
});

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);
describe("When creating a file as a stream", function(){
beforeEach(async function(){
this.sut = new VFS();
this.fileName = "mono-inc";
this.fileContents = "Is there somebody out here? Is somebody listening?";
const sink = await this.sut.createWritableStream(this.fileName);
await promiseFinished(sink, async () => {
sink.end(this.fileContents);
});
});

abstractReadBackBehaviors();
});
});
}

abstractVFSBehavior(InMemoryVFS, "InMemoryVFS");

function abstractReadBackBehaviors(){
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(42);
const bytes2 = await this.sut.asBytes(this.fileName);
expect(bytes2.toString("utf-8")).to.deep.eq(this.fileContents);
});

it("exists", async function(){
expect( await this.sut.exists(this.fileName)).to.eq(true);
});

describe("And is unlinked", function(){
beforeEach(async function(){
await this.sut.unlink(this.fileName);
});

it("does not exist", async function(){
expect(await this.sut.exists(this.fileName)).to.eq(false);
});
});
}

0 comments on commit 1e6b426

Please sign in to comment.