Repository: streamich/memfs
Pull request #22 (2017-08-28 20:16:06)

fix: error throwing semantics


Patch:
diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md
index aae744a0..cedbd57d 100644
--- a/CONTRIBUTING.md
+++ b/CONTRIBUTING.md
@@ -3,6 +3,7 @@
 To get started, download the project to your machine:
 
     git clone https://github.com/streamich/memfs
+    cd memfs
 
 Start from the `develop` branch:
 
@@ -11,7 +12,6 @@ Start from the `develop` branch:
 
 Install dependencies:
 
-    cd memfs
     npm install
 
 Also, you probably want to use the latest Node.js version and `ts-node`
@@ -29,13 +29,10 @@ extension.
 
 Run tests using this command:
 
-    npm test
-
-Also make sure that your test cases have your new code well, run coverage report
-using this command:
-
     npm run test-coverage-ts
 
+Also make sure that your test cases cover your new code well.
+
 When done, build the project:
 
     npm run build
diff --git a/lib/volume.js b/lib/volume.js
index e536d5db..9055290d 100644
--- a/lib/volume.js
+++ b/lib/volume.js
@@ -1135,16 +1135,16 @@ var Volume = (function () {
         this.wrapAsync(this.renameBase, [oldPathFilename, newPathFilename], callback);
     };
     Volume.prototype.existsBase = function (filename) {
+        return !!this.statBase(filename);
+    };
+    Volume.prototype.existsSync = function (path) {
         try {
-            return !!this.statBase(filename);
+            return this.existsBase(pathToFilename(path));
         }
         catch (err) {
             return false;
         }
     };
-    Volume.prototype.existsSync = function (path) {
-        return this.existsBase(pathToFilename(path));
-    };
     Volume.prototype.exists = function (path, callback) {
         var _this = this;
         var filename = pathToFilename(path);
diff --git a/package.json b/package.json
index d41ded4c..ff86b531 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
 {
   "name": "memfs",
-  "version": "2.2.0",
+  "version": "2.3.0",
   "description": "In-memory file-system with Node's fs API.",
   "main": "lib/index.js",
   "keywords": [
diff --git a/src/volume.ts b/src/volume.ts
index dffab444..403085c8 100644
--- a/src/volume.ts
+++ b/src/volume.ts
@@ -1349,17 +1349,17 @@ export class Volume {
     }
 
     private existsBase(filename: string): boolean {
+        return !!this.statBase(filename);
+    }
+
+    existsSync(path: TFilePath): boolean {
         try {
-            return !!this.statBase(filename);
+            return this.existsBase(pathToFilename(path));
         } catch(err) {
             return false;
         }
     }
 
-    existsSync(path: TFilePath): boolean {
-        return this.existsBase(pathToFilename(path));
-    }
-
     exists(path: TFilePath, callback: (exists: boolean) => void) {
         const filename = pathToFilename(path);
 


Test patch:
diff --git a/lib/test/volume/exists.test.js b/lib/test/volume/exists.test.js
new file mode 100644
index 00000000..0dbe0124
--- /dev/null
+++ b/lib/test/volume/exists.test.js
@@ -0,0 +1,38 @@
+"use strict";
+Object.defineProperty(exports, "__esModule", { value: true });
+var chai_1 = require("chai");
+var util_1 = require("./util");
+describe('exists(path, callback)', function () {
+    var vol = util_1.create();
+    it('Returns true if file exists', function (done) {
+        vol.exists('/foo', function (exists) {
+            chai_1.expect(exists).to.be.true;
+            done();
+        });
+    });
+    it('Returns false if file does not exist', function (done) {
+        vol.exists('/foo2', function (exists) {
+            chai_1.expect(exists).to.be.false;
+            done();
+        });
+    });
+    it('Throws correct error if callback not provided', function (done) {
+        try {
+            vol.exists('/foo', undefined);
+            throw new Error('not_this');
+        }
+        catch (err) {
+            chai_1.expect(err.message).to.equal('callback must be a function');
+            done();
+        }
+    });
+    it('invalid path type should throw', function () {
+        try {
+            vol.exists(123, function () { });
+            throw new Error('not_this');
+        }
+        catch (err) {
+            chai_1.expect(err.message !== 'not_this').to.be.true;
+        }
+    });
+});
diff --git a/lib/test/volume/existsSync.test.js b/lib/test/volume/existsSync.test.js
new file mode 100644
index 00000000..d075b58e
--- /dev/null
+++ b/lib/test/volume/existsSync.test.js
@@ -0,0 +1,18 @@
+"use strict";
+Object.defineProperty(exports, "__esModule", { value: true });
+var chai_1 = require("chai");
+var util_1 = require("./util");
+describe('existsSync(path)', function () {
+    var vol = util_1.create();
+    it('Returns true if file exists', function () {
+        var result = vol.existsSync('/foo');
+        chai_1.expect(result).to.be.true;
+    });
+    it('Returns false if file does not exist', function () {
+        var result = vol.existsSync('/foo2');
+        chai_1.expect(result).to.be.false;
+    });
+    it('invalid path type should not throw', function () {
+        chai_1.expect(vol.existsSync(123)).to.be.false;
+    });
+});
diff --git a/lib/volume.test.js b/lib/volume.test.js
index 294b41df..dc2b7325 100644
--- a/lib/volume.test.js
+++ b/lib/volume.test.js
@@ -682,42 +682,6 @@ describe('volume', function () {
         describe('.fstat(fd, callback)', function () {
             xit('...');
         });
-        describe('.existsSync(path)', function () {
-            var vol = volume_1.Volume.fromJSON({ '/foo': 'bar' });
-            it('Returns true if file exists', function () {
-                var result = vol.existsSync('/foo');
-                chai_1.expect(result).to.be.true;
-            });
-            it('Returns false if file does not exist', function () {
-                var result = vol.existsSync('/foo2');
-                chai_1.expect(result).to.be.false;
-            });
-        });
-        describe('.exists(path, callback)', function () {
-            var vol = volume_1.Volume.fromJSON({ '/foo': 'bar' });
-            it('Returns true if file exists', function (done) {
-                vol.exists('/foo', function (exists) {
-                    chai_1.expect(exists).to.be.true;
-                    done();
-                });
-            });
-            it('Returns false if file does not exist', function (done) {
-                vol.exists('/foo2', function (exists) {
-                    chai_1.expect(exists).to.be.false;
-                    done();
-                });
-            });
-            it('Throws correct error if callback not provided', function (done) {
-                try {
-                    vol.exists('/foo', undefined);
-                    throw new Error('not_this');
-                }
-                catch (err) {
-                    chai_1.expect(err.message).to.equal('callback must be a function');
-                    done();
-                }
-            });
-        });
         describe('.linkSync(existingPath, newPath)', function () {
             var vol = new volume_1.Volume;
             it('Create a new link', function () {
diff --git a/src/test/volume/exists.test.ts b/src/test/volume/exists.test.ts
new file mode 100644
index 00000000..4134ea6f
--- /dev/null
+++ b/src/test/volume/exists.test.ts
@@ -0,0 +1,36 @@
+import {expect} from 'chai';
+import {create} from "./util";
+
+
+describe('exists(path, callback)', () => {
+    const vol = create();
+    it('Returns true if file exists', done => {
+        vol.exists('/foo', exists => {
+            expect(exists).to.be.true;
+            done();
+        });
+    });
+    it('Returns false if file does not exist', done => {
+        vol.exists('/foo2', exists => {
+            expect(exists).to.be.false;
+            done();
+        });
+    });
+    it('Throws correct error if callback not provided', done => {
+        try {
+            vol.exists('/foo', undefined);
+            throw new Error('not_this');
+        } catch(err) {
+            expect(err.message).to.equal('callback must be a function');
+            done();
+        }
+    });
+    it('invalid path type should throw', () => {
+        try {
+            vol.exists(123 as any, () => {});
+            throw new Error('not_this');
+        } catch(err) {
+            expect(err.message !== 'not_this').to.be.true;
+        }
+    });
+});
diff --git a/src/test/volume/existsSync.test.ts b/src/test/volume/existsSync.test.ts
new file mode 100644
index 00000000..005c4bb7
--- /dev/null
+++ b/src/test/volume/existsSync.test.ts
@@ -0,0 +1,18 @@
+import {expect} from 'chai';
+import {create} from "./util";
+
+
+describe('existsSync(path)', () => {
+    const vol = create();
+    it('Returns true if file exists', () => {
+        const result = vol.existsSync('/foo');
+        expect(result).to.be.true;
+    });
+    it('Returns false if file does not exist', () => {
+        const result = vol.existsSync('/foo2');
+        expect(result).to.be.false;
+    });
+    it('invalid path type should not throw', () => {
+        expect(vol.existsSync(123 as any)).to.be.false;
+    });
+});
\ No newline at end of file
diff --git a/src/volume.test.ts b/src/volume.test.ts
index d9fa4ce8..1d08f2f4 100644
--- a/src/volume.test.ts
+++ b/src/volume.test.ts
@@ -687,41 +687,6 @@ describe('volume', () => {
         describe('.fstat(fd, callback)', () => {
             xit('...');
         });
-        describe('.existsSync(path)', () => {
-            const vol = Volume.fromJSON({'/foo': 'bar'});
-            it('Returns true if file exists', () => {
-                const result = vol.existsSync('/foo');
-                expect(result).to.be.true;
-            });
-            it('Returns false if file does not exist', () => {
-                const result = vol.existsSync('/foo2');
-                expect(result).to.be.false;
-            });
-        });
-        describe('.exists(path, callback)', () => {
-            const vol = Volume.fromJSON({'/foo': 'bar'});
-            it('Returns true if file exists', done => {
-                vol.exists('/foo', exists => {
-                    expect(exists).to.be.true;
-                    done();
-                });
-            });
-            it('Returns false if file does not exist', done => {
-                vol.exists('/foo2', exists => {
-                    expect(exists).to.be.false;
-                    done();
-                });
-            });
-            it('Throws correct error if callback not provided', done => {
-                try {
-                    vol.exists('/foo', undefined);
-                    throw new Error('not_this');
-                } catch(err) {
-                    expect(err.message).to.equal('callback must be a function');
-                    done();
-                }
-            });
-        });
         describe('.linkSync(existingPath, newPath)', () => {
             const vol = new Volume;
             it('Create a new link', () => {