Skip to content

Commit fb95d4c

Browse files
committed
fix: error throwing semantics
1 parent 0a7d55a commit fb95d4c

10 files changed

+123
-87
lines changed

CONTRIBUTING.md

+3-6
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
To get started, download the project to your machine:
44

55
git clone https://github.com/streamich/memfs
6+
cd memfs
67

78
Start from the `develop` branch:
89

@@ -11,7 +12,6 @@ Start from the `develop` branch:
1112

1213
Install dependencies:
1314

14-
cd memfs
1515
npm install
1616

1717
Also, you probably want to use the latest Node.js version and `ts-node`
@@ -29,13 +29,10 @@ extension.
2929

3030
Run tests using this command:
3131

32-
npm test
33-
34-
Also make sure that your test cases have your new code well, run coverage report
35-
using this command:
36-
3732
npm run test-coverage-ts
3833

34+
Also make sure that your test cases cover your new code well.
35+
3936
When done, build the project:
4037

4138
npm run build

lib/test/volume/exists.test.js

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
"use strict";
2+
Object.defineProperty(exports, "__esModule", { value: true });
3+
var chai_1 = require("chai");
4+
var util_1 = require("./util");
5+
describe('exists(path, callback)', function () {
6+
var vol = util_1.create();
7+
it('Returns true if file exists', function (done) {
8+
vol.exists('/foo', function (exists) {
9+
chai_1.expect(exists).to.be.true;
10+
done();
11+
});
12+
});
13+
it('Returns false if file does not exist', function (done) {
14+
vol.exists('/foo2', function (exists) {
15+
chai_1.expect(exists).to.be.false;
16+
done();
17+
});
18+
});
19+
it('Throws correct error if callback not provided', function (done) {
20+
try {
21+
vol.exists('/foo', undefined);
22+
throw new Error('not_this');
23+
}
24+
catch (err) {
25+
chai_1.expect(err.message).to.equal('callback must be a function');
26+
done();
27+
}
28+
});
29+
it('invalid path type should throw', function () {
30+
try {
31+
vol.exists(123, function () { });
32+
throw new Error('not_this');
33+
}
34+
catch (err) {
35+
chai_1.expect(err.message !== 'not_this').to.be.true;
36+
}
37+
});
38+
});

lib/test/volume/existsSync.test.js

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
"use strict";
2+
Object.defineProperty(exports, "__esModule", { value: true });
3+
var chai_1 = require("chai");
4+
var util_1 = require("./util");
5+
describe('existsSync(path)', function () {
6+
var vol = util_1.create();
7+
it('Returns true if file exists', function () {
8+
var result = vol.existsSync('/foo');
9+
chai_1.expect(result).to.be.true;
10+
});
11+
it('Returns false if file does not exist', function () {
12+
var result = vol.existsSync('/foo2');
13+
chai_1.expect(result).to.be.false;
14+
});
15+
it('invalid path type should not throw', function () {
16+
chai_1.expect(vol.existsSync(123)).to.be.false;
17+
});
18+
});

lib/volume.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -1135,16 +1135,16 @@ var Volume = (function () {
11351135
this.wrapAsync(this.renameBase, [oldPathFilename, newPathFilename], callback);
11361136
};
11371137
Volume.prototype.existsBase = function (filename) {
1138+
return !!this.statBase(filename);
1139+
};
1140+
Volume.prototype.existsSync = function (path) {
11381141
try {
1139-
return !!this.statBase(filename);
1142+
return this.existsBase(pathToFilename(path));
11401143
}
11411144
catch (err) {
11421145
return false;
11431146
}
11441147
};
1145-
Volume.prototype.existsSync = function (path) {
1146-
return this.existsBase(pathToFilename(path));
1147-
};
11481148
Volume.prototype.exists = function (path, callback) {
11491149
var _this = this;
11501150
var filename = pathToFilename(path);

lib/volume.test.js

-36
Original file line numberDiff line numberDiff line change
@@ -682,42 +682,6 @@ describe('volume', function () {
682682
describe('.fstat(fd, callback)', function () {
683683
xit('...');
684684
});
685-
describe('.existsSync(path)', function () {
686-
var vol = volume_1.Volume.fromJSON({ '/foo': 'bar' });
687-
it('Returns true if file exists', function () {
688-
var result = vol.existsSync('/foo');
689-
chai_1.expect(result).to.be.true;
690-
});
691-
it('Returns false if file does not exist', function () {
692-
var result = vol.existsSync('/foo2');
693-
chai_1.expect(result).to.be.false;
694-
});
695-
});
696-
describe('.exists(path, callback)', function () {
697-
var vol = volume_1.Volume.fromJSON({ '/foo': 'bar' });
698-
it('Returns true if file exists', function (done) {
699-
vol.exists('/foo', function (exists) {
700-
chai_1.expect(exists).to.be.true;
701-
done();
702-
});
703-
});
704-
it('Returns false if file does not exist', function (done) {
705-
vol.exists('/foo2', function (exists) {
706-
chai_1.expect(exists).to.be.false;
707-
done();
708-
});
709-
});
710-
it('Throws correct error if callback not provided', function (done) {
711-
try {
712-
vol.exists('/foo', undefined);
713-
throw new Error('not_this');
714-
}
715-
catch (err) {
716-
chai_1.expect(err.message).to.equal('callback must be a function');
717-
done();
718-
}
719-
});
720-
});
721685
describe('.linkSync(existingPath, newPath)', function () {
722686
var vol = new volume_1.Volume;
723687
it('Create a new link', function () {

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "memfs",
3-
"version": "2.2.0",
3+
"version": "2.3.0",
44
"description": "In-memory file-system with Node's fs API.",
55
"main": "lib/index.js",
66
"keywords": [

src/test/volume/exists.test.ts

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import {expect} from 'chai';
2+
import {create} from "./util";
3+
4+
5+
describe('exists(path, callback)', () => {
6+
const vol = create();
7+
it('Returns true if file exists', done => {
8+
vol.exists('/foo', exists => {
9+
expect(exists).to.be.true;
10+
done();
11+
});
12+
});
13+
it('Returns false if file does not exist', done => {
14+
vol.exists('/foo2', exists => {
15+
expect(exists).to.be.false;
16+
done();
17+
});
18+
});
19+
it('Throws correct error if callback not provided', done => {
20+
try {
21+
vol.exists('/foo', undefined);
22+
throw new Error('not_this');
23+
} catch(err) {
24+
expect(err.message).to.equal('callback must be a function');
25+
done();
26+
}
27+
});
28+
it('invalid path type should throw', () => {
29+
try {
30+
vol.exists(123 as any, () => {});
31+
throw new Error('not_this');
32+
} catch(err) {
33+
expect(err.message !== 'not_this').to.be.true;
34+
}
35+
});
36+
});

src/test/volume/existsSync.test.ts

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import {expect} from 'chai';
2+
import {create} from "./util";
3+
4+
5+
describe('existsSync(path)', () => {
6+
const vol = create();
7+
it('Returns true if file exists', () => {
8+
const result = vol.existsSync('/foo');
9+
expect(result).to.be.true;
10+
});
11+
it('Returns false if file does not exist', () => {
12+
const result = vol.existsSync('/foo2');
13+
expect(result).to.be.false;
14+
});
15+
it('invalid path type should not throw', () => {
16+
expect(vol.existsSync(123 as any)).to.be.false;
17+
});
18+
});

src/volume.test.ts

-35
Original file line numberDiff line numberDiff line change
@@ -687,41 +687,6 @@ describe('volume', () => {
687687
describe('.fstat(fd, callback)', () => {
688688
xit('...');
689689
});
690-
describe('.existsSync(path)', () => {
691-
const vol = Volume.fromJSON({'/foo': 'bar'});
692-
it('Returns true if file exists', () => {
693-
const result = vol.existsSync('/foo');
694-
expect(result).to.be.true;
695-
});
696-
it('Returns false if file does not exist', () => {
697-
const result = vol.existsSync('/foo2');
698-
expect(result).to.be.false;
699-
});
700-
});
701-
describe('.exists(path, callback)', () => {
702-
const vol = Volume.fromJSON({'/foo': 'bar'});
703-
it('Returns true if file exists', done => {
704-
vol.exists('/foo', exists => {
705-
expect(exists).to.be.true;
706-
done();
707-
});
708-
});
709-
it('Returns false if file does not exist', done => {
710-
vol.exists('/foo2', exists => {
711-
expect(exists).to.be.false;
712-
done();
713-
});
714-
});
715-
it('Throws correct error if callback not provided', done => {
716-
try {
717-
vol.exists('/foo', undefined);
718-
throw new Error('not_this');
719-
} catch(err) {
720-
expect(err.message).to.equal('callback must be a function');
721-
done();
722-
}
723-
});
724-
});
725690
describe('.linkSync(existingPath, newPath)', () => {
726691
const vol = new Volume;
727692
it('Create a new link', () => {

src/volume.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -1349,17 +1349,17 @@ export class Volume {
13491349
}
13501350

13511351
private existsBase(filename: string): boolean {
1352+
return !!this.statBase(filename);
1353+
}
1354+
1355+
existsSync(path: TFilePath): boolean {
13521356
try {
1353-
return !!this.statBase(filename);
1357+
return this.existsBase(pathToFilename(path));
13541358
} catch(err) {
13551359
return false;
13561360
}
13571361
}
13581362

1359-
existsSync(path: TFilePath): boolean {
1360-
return this.existsBase(pathToFilename(path));
1361-
}
1362-
13631363
exists(path: TFilePath, callback: (exists: boolean) => void) {
13641364
const filename = pathToFilename(path);
13651365

0 commit comments

Comments
 (0)