diff --git a/src/filesystem/interface.js b/src/filesystem/interface.js index 98e6d8b..9b8f137 100644 --- a/src/filesystem/interface.js +++ b/src/filesystem/interface.js @@ -265,46 +265,52 @@ function FileSystem(options, callback) { }); FileSystem.prototype.promises = {}; /** - * Public API for FileSystem - */ + * Public API for FileSystem. All node.js methods that are + * exposed on fs.promises include `promise: true`. We also + * include our own extra methods, but skip the fd versions + * to match node.js, which puts these on a FileHandle object. + */ [ - 'open', - 'access', - 'chmod', - 'fchmod', - 'chown', - 'fchown', - 'close', - 'mknod', - 'mkdir', - 'rmdir', - 'stat', - 'fstat', - 'link', - 'unlink', - 'read', - 'readFile', - 'write', - 'writeFile', - 'appendFile', - 'exists', - 'lseek', - 'readdir', - 'rename', - 'readlink', - 'symlink', - 'lstat', - 'truncate', - 'ftruncate', - 'utimes', - 'futimes', - 'setxattr', - 'getxattr', - 'fsetxattr', - 'fgetxattr', - 'removexattr', - 'fremovexattr' - ].forEach(function(methodName) { + { name: 'open', promises: true }, + { name: 'access', promises: true }, + { name: 'chmod', promises: true }, + { name: 'fchmod' }, + { name: 'chown', promises: true }, + { name: 'fchown' }, + { name: 'close' }, + { name: 'mknod', promises: true }, + { name: 'mkdir', promises: true }, + { name: 'rmdir', promises: true }, + { name: 'stat', promises: true }, + { name: 'fstat' }, + { name: 'link', promises: true }, + { name: 'unlink', promises: true }, + { name: 'read' }, + { name: 'readFile', promises: true }, + { name: 'write' }, + { name: 'writeFile', promises: true }, + { name: 'appendFile', promises: true }, + { name: 'exists' }, + { name: 'lseek' }, + { name: 'readdir', promises: true }, + { name: 'rename', promises: true }, + { name: 'readlink', promises: true }, + { name: 'symlink', promises: true }, + { name: 'lstat', promises: true }, + { name: 'truncate', promises: true }, + { name: 'ftruncate' }, + { name: 'utimes', promises: true }, + { name: 'futimes' }, + { name: 'setxattr', promises: true }, + { name: 'getxattr', promises: true }, + { name: 'fsetxattr' }, + { name: 'fgetxattr' }, + { name: 'removexattr', promises: true }, + { name: 'fremovexattr' } + ].forEach(function(method) { + var methodName = method.name; + var shouldPromisify = method.promises === true; + FileSystem.prototype[methodName] = function() { var fs = this; var args = Array.prototype.slice.call(arguments, 0); @@ -349,7 +355,10 @@ function FileSystem(options, callback) { } }; - FileSystem.prototype.promises[methodName] = promisify(FileSystem.prototype[methodName].bind(fs)); + // Add to fs.promises if appropriate + if(shouldPromisify) { + FileSystem.prototype.promises[methodName] = promisify(FileSystem.prototype[methodName].bind(fs)); + } }); } diff --git a/tests/spec/fs.chmod.spec.js b/tests/spec/fs.chmod.spec.js index 40050ce..5744399 100644 --- a/tests/spec/fs.chmod.spec.js +++ b/tests/spec/fs.chmod.spec.js @@ -112,36 +112,3 @@ describe('fsPromise.chmod', function() { .catch( err => { throw err; }); }); }); - -describe('fsPromise.fchmod', function() { - beforeEach(util.setup); - afterEach(util.setup); - - it('should be a function', function() { - var fsPromise = util.fs().promises; - expect(typeof fsPromise.fchmod).to.equal('function'); - }); - - it('should be a promise', function() { - var fsPromise = util.fs().promises; - expect(fsPromise.fchmod()).to.be.a('Promise'); - }); - - it('should allow for updating mode of a given file', function() { - var fsPromise = util.fs().promises; - var fdesc; - - return fsPromise.open('/file', 'w') - .then( fd => { - fdesc = fd; - return fsPromise.fchmod(fd, 0o777); - }) - .then( () => { - return fsPromise.fstat(fdesc); - }) - .then( stats => { - expect(stats.mode & 0o777).to.equal(0o777); - }) - .catch( err => { throw err; }); - }); -}); \ No newline at end of file diff --git a/tests/spec/fs.read.spec.js b/tests/spec/fs.read.spec.js index 86739e2..991db44 100644 --- a/tests/spec/fs.read.spec.js +++ b/tests/spec/fs.read.spec.js @@ -103,87 +103,3 @@ describe('fs.read', function() { }); }); }); - -describe('fs.promises.read', function() { - beforeEach(util.setup); - afterEach(util.cleanup); - - it('should be a function', function() { - var fsPromises = util.fs().promises; - expect(fsPromises.read).to.be.a('function'); - }); - - it('should return a promise', function() { - var fsPromises = util.fs().promises; - expect(fsPromises.read()).to.be.a('Promise'); - }); - it('should read data from a file', function() { - var fsPromises = util.fs().promises; - var wbuffer = new Filer.Buffer([1, 2, 3, 4, 5, 6, 7, 8]); - var rbuffer = new Filer.Buffer(wbuffer.length); - var fdesc; - rbuffer.fill(0); - - return fsPromises.open('/myfile', 'w+') - .then((fd)=>{ - fdesc=fd; - return fsPromises.write(fd, wbuffer, 0, wbuffer.length, 0); - }) - .then((result)=>{ - expect(result).to.equal(wbuffer.length); - return fsPromises.read(fdesc, rbuffer,0,rbuffer.length,0); - }) - .then((result)=>{ - expect(result).to.equal(rbuffer.length); - expect(wbuffer).to.deep.equal(rbuffer); - }) - ; - }); - - it('should update the current file position', function() { - var fsPromises = util.fs().promises; - var wbuffer = new Filer.Buffer([1, 2, 3, 4, 5, 6, 7, 8]); - var rbuffer = new Filer.Buffer(wbuffer.length); - rbuffer.fill(0); - var _result = 0; - var fdesc; - - return fsPromises.open('/myfile', 'w+') - .then((fd)=>{ - fdesc=fd; - return fsPromises.write(fd, wbuffer, 0, wbuffer.length, 0 ); - }) - .then((result)=>{ - expect(result).to.equal(wbuffer.length); - return fsPromises.read(fdesc, rbuffer, 0, rbuffer.length / 2, undefined); - }) - .then((result)=>{ - _result += result; - return fsPromises.read(fdesc,rbuffer,rbuffer.length / 2, rbuffer.length, undefined); - }) - .then((result)=>{ - _result+=result; - expect(_result).to.equal(rbuffer.length); - expect(wbuffer).to.deep.equal(rbuffer); - }) - ; - }); - - it('should fail to read a directory', function() { - var fsPromises = util.fs().promises; - var buf = new Filer.Buffer(20); - var buf2 = new Filer.Buffer(20); - buf.fill(0); - buf2.fill(0); - - return fsPromises.mkdir('/mydir') - .then(()=>fsPromises.open('/mydir', 'r')) - .then(fd=>fsPromises.read(fd, buf, 0, buf.length, 0)) - .catch((error)=>{ - expect(error).to.exist; - expect(error.code).to.equal('EISDIR'); - expect(buf).to.deep.equal(buf2); - }) - ; - }); -}); diff --git a/tests/spec/fs.readdir.spec.js b/tests/spec/fs.readdir.spec.js index 21449b6..26b766c 100644 --- a/tests/spec/fs.readdir.spec.js +++ b/tests/spec/fs.readdir.spec.js @@ -96,12 +96,11 @@ describe('fsPromises.readdir', function() { it('should return an error if the path is a file', function() { var fsPromises = util.fs().promises; - return fsPromises.open('/myfile', 'w') - .then(fd => fsPromises.close(fd)) + return fsPromises.writeFile('/myfile', 'contents') .then(() => fsPromises.readdir('/myfile')) .catch(error => { expect(error).to.exist; expect(error.code).to.equal('ENOTDIR'); }); }); -}); \ No newline at end of file +}); diff --git a/tests/spec/fs.utimes.spec.js b/tests/spec/fs.utimes.spec.js index a799198..5b88d6f 100644 --- a/tests/spec/fs.utimes.spec.js +++ b/tests/spec/fs.utimes.spec.js @@ -78,18 +78,6 @@ describe('fs.utimes', function() { }); }); - it('should error when file descriptor is invalid', function(done) { - var fs = util.fs(); - var atime = Date.parse('1 Oct 2000 15:33:22'); - var mtime = Date.parse('30 Sep 2000 06:43:54'); - - fs.futimes(1, atime, mtime, function (error) { - expect(error).to.exist; - expect(error.code).to.equal('EBADF'); - done(); - }); - }); - it('should change atime and mtime of a file path', function(done) { var fs = util.fs(); var atime = Date.parse('1 Oct 2000 15:33:22'); @@ -110,28 +98,6 @@ describe('fs.utimes', function() { }); }); - it ('should change atime and mtime for a valid file descriptor', function(done) { - var fs = util.fs(); - var ofd; - var atime = Date.parse('1 Oct 2000 15:33:22'); - var mtime = Date.parse('30 Sep 2000 06:43:54'); - - fs.open('/testfile', 'w', function (error, result) { - if (error) throw error; - - ofd = result; - fs.futimes(ofd, atime, mtime, function (error) { - expect(error).not.to.exist; - - fs.fstat(ofd, function (error, stat) { - expect(error).not.to.exist; - expect(stat.mtime).to.equal(mtime); - done(); - }); - }); - }); - }); - it('should update atime and mtime of directory path', function(done) { var fs = util.fs(); var atime = Date.parse('1 Oct 2000 15:33:22'); @@ -237,18 +203,6 @@ describe('fs.promises.utimes', function () { }); }); - it('should error when file descriptor is invalid', function () { - var fs = util.fs().promises; - var atime = Date.parse('1 Oct 2000 15:33:22'); - var mtime = Date.parse('30 Sep 2000 06:43:54'); - - return fs.futimes(1, atime, mtime) - .catch(function (error) { - expect(error).to.exist; - expect(error.code).to.equal('EBADF'); - }); - }); - it('should change atime and mtime of a file path', function () { var fs = util.fs().promises; var atime = Date.parse('1 Oct 2000 15:33:22'); @@ -275,34 +229,6 @@ describe('fs.promises.utimes', function () { }); }); - it('should change atime and mtime for a valid file descriptor', function () { - var fs = util.fs().promises; - var ofd; - var atime = Date.parse('1 Oct 2000 15:33:22'); - var mtime = Date.parse('30 Sep 2000 06:43:54'); - - return fs.open('/testfile', 'w') - .then(function (result) { - ofd = result; - fs.futimes(ofd, atime, mtime) - .then(function () { - fs.fstat(ofd) - .then(function (stat) { - expect(stat.mtime).to.equal(mtime); - }) - .catch(function (error) { - expect(error).not.to.exist; - }); - }) - .catch(function (error) { - expect(error).not.to.exist; - }); - }) - .catch(function (error) { - throw error; - }); - }); - it('should update atime and mtime of directory path', function () { var fs = util.fs().promises; var atime = Date.parse('1 Oct 2000 15:33:22');