Fix #576: remove unnecessary methods from fs.promises, adjust tests to match

This commit is contained in:
David Humphrey 2018-11-27 21:24:18 -05:00
parent 2135ee17e9
commit d1cf1286cb
5 changed files with 51 additions and 234 deletions

View File

@ -265,46 +265,52 @@ function FileSystem(options, callback) {
}); });
FileSystem.prototype.promises = {}; 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', { name: 'open', promises: true },
'access', { name: 'access', promises: true },
'chmod', { name: 'chmod', promises: true },
'fchmod', { name: 'fchmod' },
'chown', { name: 'chown', promises: true },
'fchown', { name: 'fchown' },
'close', { name: 'close' },
'mknod', { name: 'mknod', promises: true },
'mkdir', { name: 'mkdir', promises: true },
'rmdir', { name: 'rmdir', promises: true },
'stat', { name: 'stat', promises: true },
'fstat', { name: 'fstat' },
'link', { name: 'link', promises: true },
'unlink', { name: 'unlink', promises: true },
'read', { name: 'read' },
'readFile', { name: 'readFile', promises: true },
'write', { name: 'write' },
'writeFile', { name: 'writeFile', promises: true },
'appendFile', { name: 'appendFile', promises: true },
'exists', { name: 'exists' },
'lseek', { name: 'lseek' },
'readdir', { name: 'readdir', promises: true },
'rename', { name: 'rename', promises: true },
'readlink', { name: 'readlink', promises: true },
'symlink', { name: 'symlink', promises: true },
'lstat', { name: 'lstat', promises: true },
'truncate', { name: 'truncate', promises: true },
'ftruncate', { name: 'ftruncate' },
'utimes', { name: 'utimes', promises: true },
'futimes', { name: 'futimes' },
'setxattr', { name: 'setxattr', promises: true },
'getxattr', { name: 'getxattr', promises: true },
'fsetxattr', { name: 'fsetxattr' },
'fgetxattr', { name: 'fgetxattr' },
'removexattr', { name: 'removexattr', promises: true },
'fremovexattr' { name: 'fremovexattr' }
].forEach(function(methodName) { ].forEach(function(method) {
var methodName = method.name;
var shouldPromisify = method.promises === true;
FileSystem.prototype[methodName] = function() { FileSystem.prototype[methodName] = function() {
var fs = this; var fs = this;
var args = Array.prototype.slice.call(arguments, 0); 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));
}
}); });
} }

View File

@ -112,36 +112,3 @@ describe('fsPromise.chmod', function() {
.catch( err => { throw err; }); .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; });
});
});

View File

@ -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);
})
;
});
});

View File

@ -96,12 +96,11 @@ describe('fsPromises.readdir', function() {
it('should return an error if the path is a file', function() { it('should return an error if the path is a file', function() {
var fsPromises = util.fs().promises; var fsPromises = util.fs().promises;
return fsPromises.open('/myfile', 'w') return fsPromises.writeFile('/myfile', 'contents')
.then(fd => fsPromises.close(fd))
.then(() => fsPromises.readdir('/myfile')) .then(() => fsPromises.readdir('/myfile'))
.catch(error => { .catch(error => {
expect(error).to.exist; expect(error).to.exist;
expect(error.code).to.equal('ENOTDIR'); expect(error.code).to.equal('ENOTDIR');
}); });
}); });
}); });

View File

@ -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) { it('should change atime and mtime of a file path', function(done) {
var fs = util.fs(); var fs = util.fs();
var atime = Date.parse('1 Oct 2000 15:33:22'); 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) { it('should update atime and mtime of directory path', function(done) {
var fs = util.fs(); var fs = util.fs();
var atime = Date.parse('1 Oct 2000 15:33:22'); 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 () { it('should change atime and mtime of a file path', function () {
var fs = util.fs().promises; var fs = util.fs().promises;
var atime = Date.parse('1 Oct 2000 15:33:22'); 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 () { it('should update atime and mtime of directory path', function () {
var fs = util.fs().promises; var fs = util.fs().promises;
var atime = Date.parse('1 Oct 2000 15:33:22'); var atime = Date.parse('1 Oct 2000 15:33:22');