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

191 lines
4.9 KiB
JavaScript
Raw Normal View History

2014-05-23 20:53:50 +00:00
var Filer = require('../..');
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
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");
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");
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 = 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;
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 = 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;
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 = 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;
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;
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 truncate a valid descriptor', function(done) {
var fs = util.fs();
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;
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) {
if(error) throw error;
2013-12-10 16:59:44 +00:00
2014-05-23 20:53:50 +00:00
fs.ftruncate(fd, 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.fstat(fd, 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 = 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;
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
});