Skip to content

Commit

Permalink
Merge pull request #241 from pizzafroide/feat/promises-api
Browse files Browse the repository at this point in the history
feat: promises api
  • Loading branch information
streamich authored Nov 7, 2018
2 parents 81706bb + 3e8d2d4 commit cc57ecd
Show file tree
Hide file tree
Showing 10 changed files with 983 additions and 23 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ This package depends on the following Node modules: `buffer`, `events`,
It also uses `process` and `setImmediate` globals, but mocks them, if not
available.

It uses `Promise` when available and throws when `promises` property is
accessed in an environment that do not support this ES2015 feature.

[npm-url]: https://www.npmjs.com/package/memfs
[npm-badge]: https://img.shields.io/npm/v/memfs.svg
[travis-url]: https://travis-ci.org/streamich/memfs
Expand Down
2 changes: 1 addition & 1 deletion docs/api-status.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ All of the [Node's `fs` API](https://nodejs.org/api/fs.html) is implemented.
Some error messages may be inaccurate. File permissions are currently not
implemented (you have access to any file), basically `fs.access()` is a no-op.

- [ ] Promises
- [x] Promises
- [x] Constants
- [x] `FSWatcher`
- [x] `ReadStream`
Expand Down
3 changes: 3 additions & 0 deletions src/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,7 @@ describe('memfs', () => {
expect(typeof memfs[method]).toBe('function');
}
});
it('Exports promises API', () => {
expect(typeof memfs.promises).toBe('object');
});
});
629 changes: 629 additions & 0 deletions src/__tests__/promises.test.ts

Large diffs are not rendered by default.

26 changes: 16 additions & 10 deletions src/__tests__/volume.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ describe('volume', () => {
});
});
describe('.read(fd, buffer, offset, length, position, callback)', () => {
xit('...');
xit('...', () => {});
});
describe('.readFileSync(path[, options])', () => {
const vol = new Volume;
Expand Down Expand Up @@ -595,7 +595,7 @@ describe('volume', () => {
});
});
describe('.symlink(target, path[, type], callback)', () => {
xit('...');
xit('...', () => {});
});
describe('.realpathSync(path[, options])', () => {
const vol = new Volume;
Expand Down Expand Up @@ -662,7 +662,7 @@ describe('volume', () => {
});
});
describe('.lstat(path, callback)', () => {
xit('...');
xit('...', () => {});
});
describe('.statSync(path)', () => {
const vol = new Volume;
Expand Down Expand Up @@ -695,7 +695,7 @@ describe('volume', () => {
});
});
describe('.stat(path, callback)', () => {
xit('...');
xit('...', () => {});
});
describe('.fstatSync(fd)', () => {
const vol = new Volume;
Expand All @@ -713,7 +713,7 @@ describe('volume', () => {
});
});
describe('.fstat(fd, callback)', () => {
xit('...');
xit('...', () => {});
});
describe('.linkSync(existingPath, newPath)', () => {
const vol = new Volume;
Expand All @@ -733,7 +733,7 @@ describe('volume', () => {
});
});
describe('.link(existingPath, newPath, callback)', () => {
xit('...');
xit('...', () => {});
});
describe('.readdirSync(path)', () => {
it('Returns simple list', () => {
Expand All @@ -760,7 +760,7 @@ describe('volume', () => {
});
});
describe('.readdir(path, callback)', () => {
xit('...');
xit('...', () => {});
});
describe('.readlinkSync(path[, options])', () => {
it('Simple symbolic link to one file', () => {
Expand Down Expand Up @@ -808,7 +808,7 @@ describe('volume', () => {
});
});
describe('.ftruncate(fd[, len], callback)', () => {
xit('...');
xit('...', () => {});
});
describe('.truncateSync(path[, len])', () => {
const vol = new Volume;
Expand All @@ -828,7 +828,7 @@ describe('volume', () => {
});
});
describe('.truncate(path[, len], callback)', () => {
xit('...');
xit('...', () => {});
});
describe('.utimesSync(path, atime, mtime)', () => {
const vol = new Volume;
Expand Down Expand Up @@ -878,7 +878,7 @@ describe('volume', () => {
});
});
describe('.mkdir(path[, mode], callback)', () => {
xit('...');
xit('...', () => {});
xit('Create /dir1/dir2/dir3', () => { });
});
describe('.mkdtempSync(prefix[, options])', () => {
Expand Down Expand Up @@ -936,6 +936,12 @@ describe('volume', () => {
}, 1);
});
});
describe('.promises', () => {
it('Have a promises property', () => {
const vol = new Volume;
expect(typeof vol.promises).toBe('object');
});
});
});
describe('StatWatcher', () => {
it('.vol points to current volume', () => {
Expand Down
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {Stats, Dirent} from './node';
import {Volume as _Volume, StatWatcher, FSWatcher, toUnixTimestamp, IReadStream, IWriteStream} from './volume';
import * as volume from './volume';
import { IPromisesAPI } from './promises';
const {fsSyncMethods, fsAsyncMethods} = require('fs-monkey/lib/util/lists');
import {constants} from './constants';
const {F_OK, R_OK, W_OK, X_OK} = constants;
Expand All @@ -21,6 +22,7 @@ export interface IFs extends _Volume {
FSWatcher: new () => FSWatcher,
ReadStream: new (...args) => IReadStream,
WriteStream: new (...args) => IWriteStream,
promises: IPromisesAPI,
_toUnixTimestamp,
}

Expand All @@ -39,6 +41,7 @@ export function createFsFromVolume(vol: _Volume): IFs {
fs.FSWatcher = vol.FSWatcher;
fs.WriteStream = vol.WriteStream;
fs.ReadStream = vol.ReadStream;
fs.promises = vol.promises;

fs._toUnixTimestamp = toUnixTimestamp;

Expand Down
2 changes: 1 addition & 1 deletion src/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ export class Node extends EventEmitter {

chmod(perm: number) {
this.perm = perm;
this.mode |= perm;
this.mode = (this.mode & ~0o777) | perm;
this.touch();
}

Expand Down
Loading

0 comments on commit cc57ecd

Please sign in to comment.