From 85e77dc3a78f524f6721d4f21e8924aee2ebd4ad Mon Sep 17 00:00:00 2001 From: Mark Eschbach Date: Tue, 16 Apr 2019 11:19:24 -0700 Subject: [PATCH] vfs: Ensure file not found message is intelligable --- tests/vfs-tests.js | 14 ++++++++++++++ vfs.js | 11 ++++++++--- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/tests/vfs-tests.js b/tests/vfs-tests.js index 16891fb..98b558d 100644 --- a/tests/vfs-tests.js +++ b/tests/vfs-tests.js @@ -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(){ diff --git a/vfs.js b/vfs.js index 3af453c..29dce9e 100644 --- a/vfs.js +++ b/vfs.js @@ -20,6 +20,12 @@ 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]; @@ -27,13 +33,12 @@ class InMemoryVFS { } 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]); }