Skip to content

Commit

Permalink
tentative: replaced remaining callbacks with promises. check error pr…
Browse files Browse the repository at this point in the history
…opagation correctness
  • Loading branch information
Michael-Overall committed Sep 26, 2018
1 parent a1e73fe commit 5abefb6
Showing 1 changed file with 66 additions and 74 deletions.
140 changes: 66 additions & 74 deletions tests/spec/fs.rename.spec.js
@@ -1,43 +1,43 @@
var util = require('../lib/test-utils.js');
var expect = require('chai').expect;

describe('fs.rename', function() {
describe('fs.rename', function () {
beforeEach(util.setup);
afterEach(util.cleanup);

it('should be a function', function() {
it('should be a function', function () {
var fs = util.fs();
expect(fs.rename).to.be.a('function');
});

it('should rename an existing file', function(done) {
it('should rename an existing file', function (done) {
var complete1 = false;
var complete2 = false;
var fs = util.fs();

function maybeDone() {
if(complete1 && complete2) {
if (complete1 && complete2) {
done();
}
}

fs.open('/myfile', 'w+', function(error, fd) {
if(error) throw error;
fs.open('/myfile', 'w+', function (error, fd) {
if (error) throw error;

fs.close(fd, function(error) {
if(error) throw error;
fs.close(fd, function (error) {
if (error) throw error;

fs.rename('/myfile', '/myotherfile', function(error) {
if(error) throw error;
fs.rename('/myfile', '/myotherfile', function (error) {
if (error) throw error;

fs.stat('/myfile', function(error, result) {
fs.stat('/myfile', function (error, result) {
expect(error).to.exist;
expect(result).not.to.exist;
complete1 = true;
maybeDone();
});

fs.stat('/myotherfile', function(error, result) {
fs.stat('/myotherfile', function (error, result) {
expect(error).not.to.exist;
expect(result.nlinks).to.equal(1);
complete2 = true;
Expand All @@ -48,19 +48,19 @@ describe('fs.rename', function() {
});
});

it('should rename an existing directory', function(done) {
it('should rename an existing directory', function (done) {
var fs = util.fs();

fs.mkdir('/mydir', function(error) {
if(error) throw error;
fs.mkdir('/mydir', function (error) {
if (error) throw error;

fs.rename('/mydir', '/myotherdir', function(error) {
fs.rename('/mydir', '/myotherdir', function (error) {
expect(error).not.to.exist;
fs.stat('/mydir', function(error) {
fs.stat('/mydir', function (error) {
expect(error).to.exist;
expect(error.code).to.equal('ENOENT');

fs.stat('/myotherdir', function(error, result) {
fs.stat('/myotherdir', function (error, result) {
expect(error).not.to.exist;
expect(result.nlinks).to.equal(1);
done();
Expand All @@ -70,22 +70,22 @@ describe('fs.rename', function() {
});
});

it('should rename an existing directory if the new path points to an existing directory', function(done) {
it('should rename an existing directory if the new path points to an existing directory', function (done) {
var fs = util.fs();

fs.mkdir('/mydir', function(error) {
if(error) throw error;
fs.mkdir('/mydir', function (error) {
if (error) throw error;

fs.mkdir('/myotherdir', function(error) {
if(error) throw error;
fs.mkdir('/myotherdir', function (error) {
if (error) throw error;

fs.rename('/mydir', '/myotherdir', function(error) {
fs.rename('/mydir', '/myotherdir', function (error) {
expect(error).not.to.exist;
fs.stat('/mydir', function(error) {
fs.stat('/mydir', function (error) {
expect(error).to.exist;
expect(error.code).to.equal('ENOENT');

fs.stat('/myotherdir', function(error, result) {
fs.stat('/myotherdir', function (error, result) {
expect(error).not.to.exist;
expect(result.nlinks).to.equal(1);
done();
Expand All @@ -96,26 +96,26 @@ describe('fs.rename', function() {
});
});

it('should fail to rename an existing directory if the new path points to an existing directory that is not empty', function(done) {
it('should fail to rename an existing directory if the new path points to an existing directory that is not empty', function (done) {
var fs = util.fs();

fs.mkdir('/mydir', function(error) {
if(error) throw error;
fs.mkdir('/mydir', function (error) {
if (error) throw error;

fs.mkdir('/myotherdir', function(error) {
if(error) throw error;
fs.mkdir('/myotherdir', function (error) {
if (error) throw error;

fs.writeFile('/myotherdir/myfile', 'This is a file', function(error) {
if(error) throw error;
fs.writeFile('/myotherdir/myfile', 'This is a file', function (error) {
if (error) throw error;

fs.rename('/mydir', '/myotherdir', function(error) {
fs.rename('/mydir', '/myotherdir', function (error) {
expect(error).to.exist;
expect(error.code).to.equal('ENOTEMPTY');

fs.stat('/mydir', function(error) {
fs.stat('/mydir', function (error) {
expect(error).not.to.exist;

fs.stat('/myotherdir', function(error) {
fs.stat('/myotherdir', function (error) {
expect(error).not.to.exist;
done();
});
Expand All @@ -126,23 +126,23 @@ describe('fs.rename', function() {
});
});

it('should fail to rename an existing directory if the new path points to an existing file', function(done) {
it('should fail to rename an existing directory if the new path points to an existing file', function (done) {
var fs = util.fs();

fs.mkdir('/mydir', function(error) {
if(error) throw error;
fs.mkdir('/mydir', function (error) {
if (error) throw error;

fs.writeFile('/myfile', 'This is a file', function(error) {
if(error) throw error;
fs.writeFile('/myfile', 'This is a file', function (error) {
if (error) throw error;

fs.rename('/mydir', '/myfile', function(error) {
fs.rename('/mydir', '/myfile', function (error) {
expect(error).to.exist;
expect(error.code).to.equal('ENOTDIR');

fs.stat('/mydir', function(error) {
fs.stat('/mydir', function (error) {
expect(error).not.to.exist;

fs.stat('/myfile', function(error) {
fs.stat('/myfile', function (error) {
expect(error).not.to.exist;
done();
});
Expand All @@ -152,43 +152,35 @@ describe('fs.rename', function() {
});
});

it('(promise version) should rename an existing file', function(done) {
it('(promise version) should rename an existing file', function (done) {
var complete1 = false;
var complete2 = false;
var fs = util.fs();

function maybeDone() {
if(complete1 && complete2) {
if (complete1 && complete2) {
done();
}
}

fs.open('/myfile', 'w+', function(error, fd) {
if(error) throw error;

fs.close(fd, function(error) {
if(error) throw error;

fs.promises.rename('/myfile', '/myotherfile').then(
function(){
fs.stat('/myfile', function(error, result) {
expect(error).to.exist;
expect(result).not.to.exist;
complete1 = true;
maybeDone();
});

fs.stat('/myotherfile', function(error, result) {
expect(error).not.to.exist;
expect(result.nlinks).to.equal(1);
complete2 = true;
maybeDone();
});
},
function(error){throw error;}
);

});
});
//TODO: CHECK PROMISE ERRORS ARE PROPAGATED CORRECTLY (re-throw?)
Promise.all(
fs.promises.open('/myfile', 'w+')
.then((fd)=>fs.promises.close(fd)),
fs.promises.rename('/myfile', '/myotherfile'),
//TODO: for both stat() check expect() vs assert()
fs.promises.stat('/myfile')
.then( (result)=> expect(result).not.to.exist, (error) => expect(error).to.exist)
.finally(()=>{
complete1 = true;
maybeDone();
}),
fs.promises.stat('/myotherfile')
.then( (result) => expect(result.nlinks).to.equal(1), (error) => expect(error).not.to.exist)
.finally(()=>{
complete2 = true;
maybeDone();
})
);
//TODO: .catch() probably not necessary--we just want errors to percolate up...
});
});

0 comments on commit 5abefb6

Please sign in to comment.