Skip to content

Commit

Permalink
fix #556: added tests for fs.ftruncate method (#557)
Browse files Browse the repository at this point in the history
* fix #556: added tests for fs.ftruncate method

* Update fs.ftruncate.spec.js
  • Loading branch information
yatsenko-julia authored and humphd committed Nov 29, 2018
1 parent 89cfef0 commit f57c733
Showing 1 changed file with 59 additions and 0 deletions.
59 changes: 59 additions & 0 deletions tests/spec/fs.ftruncate.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,63 @@ describe('fs.ftruncate', function() {
});
});
});

it('should assume a length of 0 if passed undefined', function(done) {
var fs = util.fs();
var buffer = new Buffer([1, 2, 3, 4, 5, 6, 7, 8]);

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

var fd = result;
fs.write(fd, buffer, 0, buffer.length, 0, function(error, result) {
if(error) throw error;
expect(result).to.equal(buffer.length);

// Pass undefined to see that it defaults to 0
fs.ftruncate(fd, undefined, function(error) {
expect(error).not.to.exist;

fs.stat('/myfile', function(error, result) {
if(error) throw error;

expect(result.size).to.equal(0);
done();
});
});

// Close file descriptor
fs.close(fd);
});
});
});

it('should update the file size', function(done) {
var fs = util.fs();
var buffer = new Buffer([1, 2, 3, 4, 5, 6, 7, 8]);

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

var fd = result;
fs.write(fd, buffer, 0, buffer.length, 0, function(error, result) {
if(error) throw error;
expect(result).to.equal(buffer.length);

fs.ftruncate(fd, 0, function(error) {
expect(error).not.to.exist;

fs.stat('/myfile', function(error, result) {
if(error) throw error;

expect(result.size).to.equal(0);
done();
});
});

// Close file descriptor
fs.close(fd);
});
});
});
});

0 comments on commit f57c733

Please sign in to comment.