filer/tests/spec/fs.truncate.spec.js

242 lines
6.4 KiB
JavaScript
Raw Normal View History

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
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();
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;
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;
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();
var buffer = Buffer.from([1, 2, 3, 4, 5, 6, 7, 8]);
var truncated = Buffer.from([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();
var buffer = Buffer.from([1, 2, 3, 4, 5, 6, 7, 8]);
var truncated = Buffer.from([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();
var buffer = Buffer.from([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;
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 = Buffer.from([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();
var buffer = Buffer.from([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
});
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');
});
});
it('should error when length is negative', () => {
var fsPromises = util.fs().promises;
var contents = 'This is a file.';
2018-09-24 23:04:41 +00:00
return fsPromises.writeFile('/myfile', contents)
.then(() => fsPromises.truncate('/myfile', -1))
.catch(error => {
expect(error).to.exist;
expect(error.code).to.equal('EINVAL');
});
});
});