Skip to content

Commit

Permalink
fix filerjs#556: added tests for fs.ftruncate method
Browse files Browse the repository at this point in the history
  • Loading branch information
Julia Yatsenko authored and Julia Yatsenko committed Oct 31, 2018
1 parent ea95bad commit f38f559
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 f38f559

Please sign in to comment.