Skip to content

Commit

Permalink
vfs: Ensure file not found message is intelligable
Browse files Browse the repository at this point in the history
  • Loading branch information
meschbach committed Apr 16, 2019
1 parent 1e6b426 commit 85e77dc
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
14 changes: 14 additions & 0 deletions tests/vfs-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,20 @@ function abstractVFSBehavior( VFS, name ) {
const sut = new VFS();
expect(await sut.exists("nonexistent-file")).to.eq(false);
});

it("raises a reasonable error the file does not exist", async function () {
const exampleFileName = "non-existent-file";
let raised = false;
try {
const sut = new VFS();
await sut.asBytes(exampleFileName);
}catch(e){
raised = true;
expect(e.message).to.include(exampleFileName);
}finally {
expect(raised).to.eq(true);
}
})
});

describe("When creating a file as bytes", function(){
Expand Down
11 changes: 8 additions & 3 deletions vfs.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,25 @@ class InMemoryVFS {
return !!this.files[file];
}

async _expectFile( file ){
if( !this.files[file]){
throw new Error("No such file " + 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);
}
await this._expectFile(file);
return new MemoryReadable(this.files[file]);
}

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

Expand Down

0 comments on commit 85e77dc

Please sign in to comment.