Fixed #424 - implement fs.promises.read tests (#429)

* implemented first two tests of fs.promises.read

* simplified the 2nd fs.promises.read test

* implemented test on file position update for fs.promises.read

* implemented directory read failure test for fs.promises.read

* implemented test to check the return type to be a Promise in fs.promises.read

* fixed indentation and also fixed a catch statement to allow all code to be reachable

* made tests more promise-friendly

* applied some style changes and test changes as per @rdittrich97 suggestions

* removed lines that are unnecessary for promise-based testing and syntactically simplified some function definitions

* removed more unnecessary check lines in directory check fail test
This commit is contained in:
y2s82 2018-10-09 12:51:59 -04:00 committed by David Humphrey
parent 86b6b2a907
commit 1156f420c4
1 changed files with 84 additions and 0 deletions

View File

@ -88,3 +88,87 @@ 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);
})
;
});
});