2014-05-23 20:53:50 +00:00
|
|
|
var util = require('../lib/test-utils.js');
|
|
|
|
var expect = require('chai').expect;
|
2013-12-10 16:59:44 +00:00
|
|
|
|
2014-05-23 20:53:50 +00:00
|
|
|
describe('fs.truncate', function() {
|
|
|
|
beforeEach(util.setup);
|
|
|
|
afterEach(util.cleanup);
|
2013-12-10 16:59:44 +00:00
|
|
|
|
2014-05-23 20:53:50 +00:00
|
|
|
it('should be a function', function() {
|
|
|
|
var fs = util.fs();
|
|
|
|
expect(fs.truncate).to.be.a('function');
|
|
|
|
});
|
2013-12-10 16:59:44 +00:00
|
|
|
|
2018-10-09 17:53:26 +00:00
|
|
|
it('should error when length is not an integer', function(done) {
|
|
|
|
var fs = util.fs();
|
|
|
|
var contents = 'This is a file.';
|
|
|
|
|
|
|
|
fs.writeFile('/myfile', contents, function(error) {
|
|
|
|
if(error) throw error;
|
|
|
|
|
|
|
|
fs.truncate('/myfile', 'notAnInteger', function(error) {
|
|
|
|
expect(error).to.exist;
|
|
|
|
expect(error.code).to.equal('EINVAL');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-05-23 20:53:50 +00:00
|
|
|
it('should error when length is negative', function(done) {
|
|
|
|
var fs = util.fs();
|
2018-07-15 17:25:35 +00:00
|
|
|
var contents = 'This is a file.';
|
2013-12-10 16:59:44 +00:00
|
|
|
|
2014-05-23 20:53:50 +00:00
|
|
|
fs.writeFile('/myfile', contents, function(error) {
|
|
|
|
if(error) throw error;
|
2013-12-10 16:59:44 +00:00
|
|
|
|
2014-05-23 20:53:50 +00:00
|
|
|
fs.truncate('/myfile', -1, function(error) {
|
|
|
|
expect(error).to.exist;
|
2018-07-15 17:25:35 +00:00
|
|
|
expect(error.code).to.equal('EINVAL');
|
2014-05-23 20:53:50 +00:00
|
|
|
done();
|
2013-12-10 16:59:44 +00:00
|
|
|
});
|
|
|
|
});
|
2014-05-23 20:53:50 +00:00
|
|
|
});
|
2013-12-10 16:59:44 +00:00
|
|
|
|
2014-05-23 20:53:50 +00:00
|
|
|
it('should error when path is not a file', function(done) {
|
|
|
|
var fs = util.fs();
|
2013-12-10 16:59:44 +00:00
|
|
|
|
2014-05-23 20:53:50 +00:00
|
|
|
fs.truncate('/', 0, function(error) {
|
|
|
|
expect(error).to.exist;
|
2018-07-15 17:25:35 +00:00
|
|
|
expect(error.code).to.equal('EISDIR');
|
2014-05-23 20:53:50 +00:00
|
|
|
done();
|
2013-12-10 16:59:44 +00:00
|
|
|
});
|
2014-05-23 20:53:50 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should truncate a file', function(done) {
|
|
|
|
var fs = util.fs();
|
2014-06-04 19:52:08 +00:00
|
|
|
var buffer = new Buffer([1, 2, 3, 4, 5, 6, 7, 8]);
|
|
|
|
var truncated = new Buffer([1]);
|
2013-12-10 16:59:44 +00:00
|
|
|
|
2014-05-23 20:53:50 +00:00
|
|
|
fs.open('/myfile', 'w', function(error, result) {
|
|
|
|
if(error) throw error;
|
2013-12-10 16:59:44 +00:00
|
|
|
|
2014-05-23 20:53:50 +00:00
|
|
|
var fd = result;
|
|
|
|
fs.write(fd, buffer, 0, buffer.length, 0, function(error, result) {
|
2013-12-10 16:59:44 +00:00
|
|
|
if(error) throw error;
|
2018-07-26 22:07:50 +00:00
|
|
|
expect(result).to.equal(buffer.length);
|
2013-12-10 16:59:44 +00:00
|
|
|
|
2014-05-23 20:53:50 +00:00
|
|
|
fs.close(fd, function(error) {
|
2013-12-10 16:59:44 +00:00
|
|
|
if(error) throw error;
|
|
|
|
|
2014-05-23 20:53:50 +00:00
|
|
|
fs.truncate('/myfile', 1, function(error) {
|
|
|
|
expect(error).not.to.exist;
|
2013-12-10 16:59:44 +00:00
|
|
|
|
2014-05-23 20:53:50 +00:00
|
|
|
fs.readFile('/myfile', function(error, result) {
|
|
|
|
if(error) throw error;
|
2013-12-10 16:59:44 +00:00
|
|
|
|
2014-05-23 20:53:50 +00:00
|
|
|
expect(result).to.deep.equal(truncated);
|
|
|
|
done();
|
2013-12-10 16:59:44 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2014-05-23 20:53:50 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should pad a file with zeros when the length is greater than the file size', function(done) {
|
|
|
|
var fs = util.fs();
|
2014-06-04 19:52:08 +00:00
|
|
|
var buffer = new Buffer([1, 2, 3, 4, 5, 6, 7, 8]);
|
|
|
|
var truncated = new Buffer([1, 2, 3, 4, 5, 6, 7, 8, 0]);
|
2013-12-10 16:59:44 +00:00
|
|
|
|
2014-05-23 20:53:50 +00:00
|
|
|
fs.open('/myfile', 'w', function(error, result) {
|
|
|
|
if(error) throw error;
|
2013-12-10 16:59:44 +00:00
|
|
|
|
2014-05-23 20:53:50 +00:00
|
|
|
var fd = result;
|
|
|
|
fs.write(fd, buffer, 0, buffer.length, 0, function(error, result) {
|
2013-12-10 16:59:44 +00:00
|
|
|
if(error) throw error;
|
2018-07-26 22:07:50 +00:00
|
|
|
expect(result).to.equal(buffer.length);
|
2013-12-10 16:59:44 +00:00
|
|
|
|
2014-05-23 20:53:50 +00:00
|
|
|
fs.close(fd, function(error) {
|
2013-12-10 16:59:44 +00:00
|
|
|
if(error) throw error;
|
|
|
|
|
2014-05-23 20:53:50 +00:00
|
|
|
fs.truncate('/myfile', 9, function(error) {
|
|
|
|
expect(error).not.to.exist;
|
2013-12-10 16:59:44 +00:00
|
|
|
|
2014-05-23 20:53:50 +00:00
|
|
|
fs.readFile('/myfile', function(error, result) {
|
|
|
|
if(error) throw error;
|
2013-12-10 16:59:44 +00:00
|
|
|
|
2014-05-23 20:53:50 +00:00
|
|
|
expect(result).to.deep.equal(truncated);
|
|
|
|
done();
|
2013-12-10 16:59:44 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2014-05-23 20:53:50 +00:00
|
|
|
});
|
2013-12-10 16:59:44 +00:00
|
|
|
|
2014-05-23 20:53:50 +00:00
|
|
|
it('should update the file size', function(done) {
|
|
|
|
var fs = util.fs();
|
2014-06-04 19:52:08 +00:00
|
|
|
var buffer = new Buffer([1, 2, 3, 4, 5, 6, 7, 8]);
|
2013-12-10 16:59:44 +00:00
|
|
|
|
2014-05-23 20:53:50 +00:00
|
|
|
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) {
|
2013-12-10 16:59:44 +00:00
|
|
|
if(error) throw error;
|
2018-07-26 22:07:50 +00:00
|
|
|
expect(result).to.equal(buffer.length);
|
2013-12-10 16:59:44 +00:00
|
|
|
|
2014-05-23 20:53:50 +00:00
|
|
|
fs.close(fd, function(error) {
|
2013-12-10 16:59:44 +00:00
|
|
|
if(error) throw error;
|
|
|
|
|
2014-05-23 20:53:50 +00:00
|
|
|
fs.truncate('/myfile', 0, function(error) {
|
|
|
|
expect(error).not.to.exist;
|
2018-09-17 22:30:09 +00:00
|
|
|
|
|
|
|
fs.stat('/myfile', function(error, result) {
|
|
|
|
if(error) throw error;
|
|
|
|
|
|
|
|
expect(result.size).to.equal(0);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
fs.close(fd, function(error) {
|
|
|
|
if(error) throw error;
|
|
|
|
|
|
|
|
// We want to use undefined to see that it defaults to 0
|
|
|
|
fs.truncate('/myfile', undefined, function(error) {
|
|
|
|
expect(error).not.to.exist;
|
2013-12-10 16:59:44 +00:00
|
|
|
|
2014-05-23 20:53:50 +00:00
|
|
|
fs.stat('/myfile', function(error, result) {
|
|
|
|
if(error) throw error;
|
2013-12-10 16:59:44 +00:00
|
|
|
|
2014-05-23 20:53:50 +00:00
|
|
|
expect(result.size).to.equal(0);
|
|
|
|
done();
|
2013-12-10 16:59:44 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2014-05-23 20:53:50 +00:00
|
|
|
});
|
2013-12-10 16:59:44 +00:00
|
|
|
|
2014-05-23 20:53:50 +00:00
|
|
|
it('should follow symbolic links', function(done) {
|
|
|
|
var fs = util.fs();
|
2014-06-04 19:52:08 +00:00
|
|
|
var buffer = new Buffer([1, 2, 3, 4, 5, 6, 7, 8]);
|
2013-12-10 16:59:44 +00:00
|
|
|
|
2014-05-23 20:53:50 +00:00
|
|
|
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) {
|
2013-12-10 16:59:44 +00:00
|
|
|
if(error) throw error;
|
2018-07-26 22:07:50 +00:00
|
|
|
expect(result).to.equal(buffer.length);
|
2013-12-10 16:59:44 +00:00
|
|
|
|
2014-05-23 20:53:50 +00:00
|
|
|
fs.close(fd, function(error) {
|
2013-12-10 16:59:44 +00:00
|
|
|
if(error) throw error;
|
|
|
|
|
2014-05-23 20:53:50 +00:00
|
|
|
fs.symlink('/myfile', '/mylink', function(error) {
|
2013-12-10 16:59:44 +00:00
|
|
|
if(error) throw error;
|
|
|
|
|
2014-05-23 20:53:50 +00:00
|
|
|
fs.truncate('/mylink', 0, function(error) {
|
|
|
|
expect(error).not.to.exist;
|
2013-12-10 16:59:44 +00:00
|
|
|
|
2014-05-23 20:53:50 +00:00
|
|
|
fs.stat('/myfile', function(error, result) {
|
|
|
|
if(error) throw error;
|
2013-12-10 16:59:44 +00:00
|
|
|
|
2014-05-23 20:53:50 +00:00
|
|
|
expect(result.size).to.equal(0);
|
|
|
|
fs.lstat('/mylink', function(error, result) {
|
2013-12-10 16:59:44 +00:00
|
|
|
if(error) throw error;
|
|
|
|
|
2014-05-23 20:53:50 +00:00
|
|
|
expect(result.size).not.to.equal(0);
|
|
|
|
done();
|
2013-12-10 16:59:44 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2014-05-23 20:53:50 +00:00
|
|
|
});
|
2018-10-09 16:56:20 +00:00
|
|
|
|
|
|
|
|
|
|
|
describe('fsPromises.truncate', function () {
|
|
|
|
beforeEach(util.setup);
|
|
|
|
afterEach(util.cleanup);
|
|
|
|
it('should error when path does not exist (with promises)', () => {
|
|
|
|
var fsPromises = util.fs().promises;
|
|
|
|
|
|
|
|
return fsPromises.truncate('/NonExistingPath', 0)
|
|
|
|
.catch(error => {
|
|
|
|
expect(error).to.exist;
|
|
|
|
expect(error.code).to.equal('ENOENT');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|