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

187 lines
5.1 KiB
JavaScript
Raw Normal View History

define(["Filer", "util"], function(Filer, util) {
2013-12-10 16:59:44 +00:00
describe('fs.truncate', function() {
beforeEach(util.setup);
afterEach(util.cleanup);
2013-12-10 16:59:44 +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 negative', function(done) {
var fs = util.fs();
2013-12-10 16:59:44 +00:00
var contents = "This is a file.";
fs.writeFile('/myfile', contents, function(error) {
2013-12-10 16:59:44 +00:00
if(error) throw error;
fs.truncate('/myfile', -1, function(error) {
expect(error).to.exist;
done();
2013-12-10 16:59:44 +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
fs.truncate('/', 0, function(error) {
expect(error).to.exist;
done();
2013-12-10 16:59:44 +00:00
});
});
it('should truncate a file', function(done) {
var fs = util.fs();
2013-12-10 16:59:44 +00:00
var buffer = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8]);
var truncated = new Uint8Array([1]);
fs.open('/myfile', 'w', function(error, result) {
2013-12-10 16:59:44 +00:00
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;
fs.close(fd, function(error) {
2013-12-10 16:59:44 +00:00
if(error) throw error;
fs.truncate('/myfile', 1, function(error) {
expect(error).not.to.exist;
2013-12-10 16:59:44 +00:00
fs.readFile('/myfile', function(error, result) {
2013-12-10 16:59:44 +00:00
if(error) throw error;
expect(result).to.deep.equal(truncated);
done();
2013-12-10 16:59:44 +00:00
});
});
});
});
});
});
it('should pad a file with zeros when the length is greater than the file size', function(done) {
var fs = util.fs();
2013-12-10 16:59:44 +00:00
var buffer = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8]);
var truncated = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8, 0]);
fs.open('/myfile', 'w', function(error, result) {
2013-12-10 16:59:44 +00:00
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;
fs.close(fd, function(error) {
2013-12-10 16:59:44 +00:00
if(error) throw error;
fs.truncate('/myfile', 9, function(error) {
expect(error).not.to.exist;
2013-12-10 16:59:44 +00:00
fs.readFile('/myfile', function(error, result) {
2013-12-10 16:59:44 +00:00
if(error) throw error;
expect(result).to.deep.equal(truncated);
done();
2013-12-10 16:59:44 +00:00
});
});
});
});
});
});
it('should update the file size', function(done) {
var fs = util.fs();
2013-12-10 16:59:44 +00:00
var buffer = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8]);
fs.open('/myfile', 'w', function(error, result) {
2013-12-10 16:59:44 +00:00
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;
fs.close(fd, function(error) {
2013-12-10 16:59:44 +00:00
if(error) throw error;
fs.truncate('/myfile', 0, function(error) {
expect(error).not.to.exist;
2013-12-10 16:59:44 +00:00
fs.stat('/myfile', function(error, result) {
2013-12-10 16:59:44 +00:00
if(error) throw error;
expect(result.size).to.equal(0);
done();
2013-12-10 16:59:44 +00:00
});
});
});
});
});
});
it('should truncate a valid descriptor', function(done) {
var fs = util.fs();
2013-12-10 16:59:44 +00:00
var buffer = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8]);
fs.open('/myfile', 'w', function(error, result) {
2013-12-10 16:59:44 +00:00
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;
fs.ftruncate(fd, 0, function(error) {
expect(error).not.to.exist;
2013-12-10 16:59:44 +00:00
fs.fstat(fd, function(error, result) {
2013-12-10 16:59:44 +00:00
if(error) throw error;
expect(result.size).to.equal(0);
done();
2013-12-10 16:59:44 +00:00
});
});
});
});
});
it('should follow symbolic links', function(done) {
var fs = util.fs();
2013-12-10 16:59:44 +00:00
var buffer = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8]);
fs.open('/myfile', 'w', function(error, result) {
2013-12-10 16:59:44 +00:00
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;
fs.close(fd, function(error) {
2013-12-10 16:59:44 +00:00
if(error) throw error;
fs.symlink('/myfile', '/mylink', function(error) {
2013-12-10 16:59:44 +00:00
if(error) throw error;
fs.truncate('/mylink', 0, function(error) {
expect(error).not.to.exist;
2013-12-10 16:59:44 +00:00
fs.stat('/myfile', function(error, result) {
2013-12-10 16:59:44 +00:00
if(error) throw error;
expect(result.size).to.equal(0);
fs.lstat('/mylink', function(error, result) {
2013-12-10 16:59:44 +00:00
if(error) throw error;
expect(result.size).not.to.equal(0);
done();
2013-12-10 16:59:44 +00:00
});
});
});
});
});
});
});
});
});
});