Skip to content

Commit

Permalink
fix: error throwing semantics
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich committed Aug 28, 2017
1 parent 0a7d55a commit fb95d4c
Show file tree
Hide file tree
Showing 10 changed files with 123 additions and 87 deletions.
9 changes: 3 additions & 6 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand All @@ -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`
Expand All @@ -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
Expand Down
38 changes: 38 additions & 0 deletions lib/test/volume/exists.test.js
Original file line number Diff line number Diff line change
@@ -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;
}
});
});
18 changes: 18 additions & 0 deletions lib/test/volume/existsSync.test.js
Original file line number Diff line number Diff line change
@@ -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;
});
});
8 changes: 4 additions & 4 deletions lib/volume.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
36 changes: 0 additions & 36 deletions lib/volume.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": [
Expand Down
36 changes: 36 additions & 0 deletions src/test/volume/exists.test.ts
Original file line number Diff line number Diff line change
@@ -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;
}
});
});
18 changes: 18 additions & 0 deletions src/test/volume/existsSync.test.ts
Original file line number Diff line number Diff line change
@@ -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;
});
});
35 changes: 0 additions & 35 deletions src/volume.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
10 changes: 5 additions & 5 deletions src/volume.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit fb95d4c

Please sign in to comment.