Fix #577: deal with promise exceptions and test console spam

This commit is contained in:
David Humphrey (:humph) david.humphrey@senecacollege.ca 2018-12-01 22:02:26 -05:00 committed by David Humphrey
parent 923b999e89
commit d7945d745d
6 changed files with 76 additions and 141 deletions

View File

@ -91,24 +91,12 @@ describe('fsPromise.chmod', function() {
expect(typeof fsPromise.chmod).to.equal('function'); expect(typeof fsPromise.chmod).to.equal('function');
}); });
it('should return a promise', function() {
var fsPromise = util.fs().promises;
expect(fsPromise.chmod()).to.be.a('Promise');
});
it('should allow for updating mode of a given file', function() { it('should allow for updating mode of a given file', function() {
var fsPromise = util.fs().promises; var fsPromise = util.fs().promises;
return fsPromise.open('/file', 'w') return fsPromise.open('/file', 'w')
.then( () => { .then(() => fsPromise.chmod('/file', 0o444))
return fsPromise.chmod('/file', 0o444); .then(() => fsPromise.stat('/file'))
}) .then(stats => expect(stats.mode & 0o444).to.equal(0o444));
.then( () => {
return fsPromise.stat('/file');
})
.then( stats => {
expect(stats.mode & 0o444).to.equal(0o444);
})
.catch( err => { throw err; });
}); });
}); });

View File

@ -131,12 +131,12 @@ describe('fs.promises.chown', function(){
it('should allow updating uid and gid for a file', function() { it('should allow updating uid and gid for a file', function() {
var fsPromises = util.fs().promises; var fsPromises = util.fs().promises;
return fsPromises.writeFile('/file', 'data')
.then(() => return fsPromises
fsPromises.chown('/file', 500, 500)) .writeFile('/file', 'data')
.then(() => .then(() => fsPromises.chown('/file', 500, 500))
fsPromises.stat('/file')) .then(() => fsPromises.stat('/file'))
.then((stats) =>{ .then((stats) => {
expect(stats.uid).to.equal(500); expect(stats.uid).to.equal(500);
expect(stats.gid).to.equal(500); expect(stats.gid).to.equal(500);
}); });

View File

@ -117,10 +117,20 @@ describe('fs.promises.link', function() {
beforeEach(util.setup); beforeEach(util.setup);
afterEach(util.cleanup); afterEach(util.cleanup);
it('should return a promise', function() { it('should be a function', function(){
var fsPromise = util.fs().promises; var fsPromises = util.fs().promises;
var returnValue = fsPromise.link('/myfile', '/myotherfile'); expect(fsPromises.link).to.be.a('function');
expect(returnValue).to.be.a('promise');
}); });
it('should return a promise', function() {
var fsPromise = util.fs().promises;
var p = fsPromise
.writeFile('/myfile', '')
.then(() => fsPromise.link('/myfile', '/myotherfile'));
expect(p).to.be.a('promise');
return p;
});
}); });

View File

@ -60,6 +60,17 @@ describe('fs.readdir', function() {
expect(fsPromises.readdir).to.be.a('function'); expect(fsPromises.readdir).to.be.a('function');
}); });
it('should return an error if the path is a file', function() {
var fsPromises = util.fs().promises;
return fsPromises.writeFile('/myfile', 'contents')
.then(() => fsPromises.readdir('/myfile'))
.catch(error => {
expect(error).to.exist;
expect(error.code).to.equal('ENOTDIR');
});
});
it('(promise) should return a list of files from an existing directory', function() { it('(promise) should return a list of files from an existing directory', function() {
var fsPromises = util.fs().promises; var fsPromises = util.fs().promises;
@ -78,29 +89,6 @@ describe('fs.readdir', function() {
expect(files).to.exist; expect(files).to.exist;
expect(files.length).to.equal(1); expect(files.length).to.equal(1);
expect(files[0]).to.equal('tmp'); expect(files[0]).to.equal('tmp');
})
.catch(error => {
expect(error).not.to.exist;
});
});
});
/**
* fsPromises tests
*/
describe('fsPromises.readdir', function() {
beforeEach(util.setup);
afterEach(util.cleanup);
it('should return an error if the path is a file', function() {
var fsPromises = util.fs().promises;
return fsPromises.writeFile('/myfile', 'contents')
.then(() => fsPromises.readdir('/myfile'))
.catch(error => {
expect(error).to.exist;
expect(error.code).to.equal('ENOTDIR');
}); });
}); });
}); });

View File

@ -151,56 +151,40 @@ describe('fs.promises.utimes', function () {
expect(fs.utimes).to.be.a('function'); expect(fs.utimes).to.be.a('function');
}); });
it('should return a promise', function() {
var fs = util.fs().promises;
expect(fs.utimes()).to.be.a('Promise');
});
it('should error when atime is negative', function () { it('should error when atime is negative', function () {
var fs = util.fs().promises; var fs = util.fs().promises;
return fs.writeFile('/testfile', '')
.then(function () { return fs
fs.utimes('/testfile', -1, Date.now()) .writeFile('/testfile', '')
.then(() => fs.utimes('/testfile', -1, Date.now()))
.catch(function (error) { .catch(function (error) {
expect(error).to.exist; expect(error).to.exist;
expect(error.code).to.equal('EINVAL'); expect(error.code).to.equal('EINVAL');
}); });
})
.catch(function (error) {
throw error;
});
}); });
it('should error when mtime is negative', function () { it('should error when mtime is negative', function () {
var fs = util.fs().promises; var fs = util.fs().promises;
return fs.writeFile('/testfile', '') return fs
.then(function () { .writeFile('/testfile', '')
fs.utimes('/testfile', Date.now(), -1) .then(() => fs.utimes('/testfile', Date.now(), -1))
.catch(function (error) { .catch(function (error) {
expect(error).to.exist; expect(error).to.exist;
expect(error.code).to.equal('EINVAL'); expect(error.code).to.equal('EINVAL');
}); });
})
.catch(function (error) {
throw error;
});
}); });
it('should error when mtime is an invalid number', function () { it('should error when mtime is an invalid number', function () {
var fs = util.fs().promises; var fs = util.fs().promises;
return fs.writeFile('/testfile', '') return fs
.then(function () { .writeFile('/testfile', '')
fs.utimes('/testfile', Date.now(), 'invalid datetime') .then(() => fs.utimes('/testfile', Date.now(), 'invalid datetime'))
.catch(function (error) { .catch(function (error) {
expect(error).to.exist; expect(error).to.exist;
expect(error.code).to.equal('EINVAL'); expect(error.code).to.equal('EINVAL');
}); });
})
.catch(function (error) {
throw error;
});
}); });
it('should change atime and mtime of a file path', function () { it('should change atime and mtime of a file path', function () {
@ -208,25 +192,11 @@ describe('fs.promises.utimes', function () {
var atime = Date.parse('1 Oct 2000 15:33:22'); var atime = Date.parse('1 Oct 2000 15:33:22');
var mtime = Date.parse('30 Sep 2000 06:43:54'); var mtime = Date.parse('30 Sep 2000 06:43:54');
return fs.writeFile('/testfile', '') return fs
.then(function () { .writeFile('/testfile', '')
fs.utimes('/testfile', atime, mtime) .then(() => fs.utimes('/testfile', atime, mtime))
.then(function () { .then(() => fs.stat('/testfile'))
fs.stat('/testfile') .then(stat => expect(stat.mtime).to.equal(mtime));
.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 () {
@ -234,53 +204,32 @@ describe('fs.promises.utimes', function () {
var atime = Date.parse('1 Oct 2000 15:33:22'); var atime = Date.parse('1 Oct 2000 15:33:22');
var mtime = Date.parse('30 Sep 2000 06:43:54'); var mtime = Date.parse('30 Sep 2000 06:43:54');
return fs.mkdir('/testdir') return fs
.then(function () { .mkdir('/testdir')
fs.utimes('/testdir', atime, mtime) .then(() => fs.utimes('/testdir', atime, mtime))
.then(function () { .then(() => fs.stat('/testdir'))
fs.stat('/testdir') .then(stat => {
.then(function (stat) {
expect(stat.mtime).to.equal(mtime); 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 using current time if arguments are null', function () { it('should update atime and mtime using current time if arguments are null', function () {
var fs = util.fs().promises; var fs = util.fs().promises;
var t1;
return fs.writeFile('/myfile', '') return fs
.then(function () { .writeFile('/myfile', '')
var then = Date.now(); .then(() => {
fs.utimes('/myfile', null, null) t1 = Date.now();
.then(function () { return fs.utimes('/myfile', null, null);
fs.stat('/myfile') })
.then(function (stat) { .then(() => fs.stat('/myfile'))
.then(stat => {
// Note: testing estimation as time may differ by a couple of milliseconds // Note: testing estimation as time may differ by a couple of milliseconds
// This number should be increased if tests are on slow systems // This number should be increased if tests are on slow systems
var delta = Date.now() - then; var delta = Date.now() - t1;
expect(then - stat.atime).to.be.at.most(delta); expect(t1 - stat.atime).to.be.at.most(delta);
expect(then - stat.mtime).to.be.at.most(delta); expect(t1 - stat.mtime).to.be.at.most(delta);
})
.catch(function (error) {
expect(error).not.to.exist;
});
})
.catch(function (error) {
expect(error).not.to.exist;
});
})
.catch(function (error) {
throw error;
}); });
}); });
}); });

View File

@ -5,7 +5,7 @@ describe('fs.watch', function() {
// Our watch infrastucture is dependent on document.localStorage // Our watch infrastucture is dependent on document.localStorage
// see lib/intercom.js. Bail if we don't have access to it. // see lib/intercom.js. Bail if we don't have access to it.
before(function() { before(function() {
if(!(global.document && global.document.localStorage)) { if(typeof global.localStorage === 'undefined') {
/* eslint no-console: 0 */ /* eslint no-console: 0 */
console.log('Skipping fs.watch() tests--not supported in current environment.'); console.log('Skipping fs.watch() tests--not supported in current environment.');
this.skip(); this.skip();