2014-01-21 21:25:09 +00:00
|
|
|
define(["Filer", "util"], function(Filer, util) {
|
2013-12-10 16:59:44 +00:00
|
|
|
|
|
|
|
describe('fs.truncate', function() {
|
2014-01-21 21:25:09 +00:00
|
|
|
beforeEach(util.setup);
|
|
|
|
afterEach(util.cleanup);
|
2013-12-10 16:59:44 +00:00
|
|
|
|
|
|
|
it('should be a function', function() {
|
2014-01-21 21:25:09 +00:00
|
|
|
var fs = util.fs();
|
|
|
|
expect(fs.truncate).to.be.a('function');
|
2013-12-10 16:59:44 +00:00
|
|
|
});
|
|
|
|
|
2014-01-21 21:25:09 +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.";
|
|
|
|
|
2014-01-21 21:25:09 +00:00
|
|
|
fs.writeFile('/myfile', contents, function(error) {
|
2013-12-10 16:59:44 +00:00
|
|
|
if(error) throw error;
|
|
|
|
|
2014-01-21 21:25:09 +00:00
|
|
|
fs.truncate('/myfile', -1, function(error) {
|
|
|
|
expect(error).to.exist;
|
2014-04-17 15:53:55 +00:00
|
|
|
expect(error.code).to.equal("EINVAL");
|
2014-01-21 21:25:09 +00:00
|
|
|
done();
|
2013-12-10 16:59:44 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-01-21 21:25:09 +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-01-21 21:25:09 +00:00
|
|
|
fs.truncate('/', 0, function(error) {
|
|
|
|
expect(error).to.exist;
|
2014-04-17 15:53:55 +00:00
|
|
|
expect(error.code).to.equal("EISDIR");
|
2014-01-21 21:25:09 +00:00
|
|
|
done();
|
2013-12-10 16:59:44 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-01-21 21:25:09 +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]);
|
|
|
|
|
2014-01-21 21:25:09 +00:00
|
|
|
fs.open('/myfile', 'w', function(error, result) {
|
2013-12-10 16:59:44 +00:00
|
|
|
if(error) throw error;
|
|
|
|
|
|
|
|
var fd = result;
|
2014-01-21 21:25:09 +00:00
|
|
|
fs.write(fd, buffer, 0, buffer.length, 0, function(error, result) {
|
2013-12-10 16:59:44 +00:00
|
|
|
if(error) throw error;
|
|
|
|
|
2014-01-21 21:25:09 +00:00
|
|
|
fs.close(fd, function(error) {
|
2013-12-10 16:59:44 +00:00
|
|
|
if(error) throw error;
|
|
|
|
|
2014-01-21 21:25:09 +00:00
|
|
|
fs.truncate('/myfile', 1, function(error) {
|
|
|
|
expect(error).not.to.exist;
|
2013-12-10 16:59:44 +00:00
|
|
|
|
2014-01-21 21:25:09 +00:00
|
|
|
fs.readFile('/myfile', function(error, result) {
|
2013-12-10 16:59:44 +00:00
|
|
|
if(error) throw error;
|
|
|
|
|
2014-01-21 21:25:09 +00:00
|
|
|
expect(result).to.deep.equal(truncated);
|
|
|
|
done();
|
2013-12-10 16:59:44 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-01-21 21:25:09 +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]);
|
|
|
|
|
2014-01-21 21:25:09 +00:00
|
|
|
fs.open('/myfile', 'w', function(error, result) {
|
2013-12-10 16:59:44 +00:00
|
|
|
if(error) throw error;
|
|
|
|
|
|
|
|
var fd = result;
|
2014-01-21 21:25:09 +00:00
|
|
|
fs.write(fd, buffer, 0, buffer.length, 0, function(error, result) {
|
2013-12-10 16:59:44 +00:00
|
|
|
if(error) throw error;
|
|
|
|
|
2014-01-21 21:25:09 +00:00
|
|
|
fs.close(fd, function(error) {
|
2013-12-10 16:59:44 +00:00
|
|
|
if(error) throw error;
|
|
|
|
|
2014-01-21 21:25:09 +00:00
|
|
|
fs.truncate('/myfile', 9, function(error) {
|
|
|
|
expect(error).not.to.exist;
|
2013-12-10 16:59:44 +00:00
|
|
|
|
2014-01-21 21:25:09 +00:00
|
|
|
fs.readFile('/myfile', function(error, result) {
|
2013-12-10 16:59:44 +00:00
|
|
|
if(error) throw error;
|
|
|
|
|
2014-01-21 21:25:09 +00:00
|
|
|
expect(result).to.deep.equal(truncated);
|
|
|
|
done();
|
2013-12-10 16:59:44 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-01-21 21:25:09 +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]);
|
|
|
|
|
2014-01-21 21:25:09 +00:00
|
|
|
fs.open('/myfile', 'w', function(error, result) {
|
2013-12-10 16:59:44 +00:00
|
|
|
if(error) throw error;
|
|
|
|
|
|
|
|
var fd = result;
|
2014-01-21 21:25:09 +00:00
|
|
|
fs.write(fd, buffer, 0, buffer.length, 0, function(error, result) {
|
2013-12-10 16:59:44 +00:00
|
|
|
if(error) throw error;
|
|
|
|
|
2014-01-21 21:25:09 +00:00
|
|
|
fs.close(fd, function(error) {
|
2013-12-10 16:59:44 +00:00
|
|
|
if(error) throw error;
|
|
|
|
|
2014-01-21 21:25:09 +00:00
|
|
|
fs.truncate('/myfile', 0, function(error) {
|
|
|
|
expect(error).not.to.exist;
|
2013-12-10 16:59:44 +00:00
|
|
|
|
2014-01-21 21:25:09 +00:00
|
|
|
fs.stat('/myfile', function(error, result) {
|
2013-12-10 16:59:44 +00:00
|
|
|
if(error) throw error;
|
|
|
|
|
2014-01-21 21:25:09 +00:00
|
|
|
expect(result.size).to.equal(0);
|
|
|
|
done();
|
2013-12-10 16:59:44 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-01-21 21:25:09 +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]);
|
|
|
|
|
2014-01-21 21:25:09 +00:00
|
|
|
fs.open('/myfile', 'w', function(error, result) {
|
2013-12-10 16:59:44 +00:00
|
|
|
if(error) throw error;
|
|
|
|
|
|
|
|
var fd = result;
|
2014-01-21 21:25:09 +00:00
|
|
|
fs.write(fd, buffer, 0, buffer.length, 0, function(error, result) {
|
2013-12-10 16:59:44 +00:00
|
|
|
if(error) throw error;
|
|
|
|
|
2014-01-21 21:25:09 +00:00
|
|
|
fs.ftruncate(fd, 0, function(error) {
|
|
|
|
expect(error).not.to.exist;
|
2013-12-10 16:59:44 +00:00
|
|
|
|
2014-01-21 21:25:09 +00:00
|
|
|
fs.fstat(fd, function(error, result) {
|
2013-12-10 16:59:44 +00:00
|
|
|
if(error) throw error;
|
|
|
|
|
2014-01-21 21:25:09 +00:00
|
|
|
expect(result.size).to.equal(0);
|
|
|
|
done();
|
2013-12-10 16:59:44 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-01-21 21:25:09 +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]);
|
|
|
|
|
2014-01-21 21:25:09 +00:00
|
|
|
fs.open('/myfile', 'w', function(error, result) {
|
2013-12-10 16:59:44 +00:00
|
|
|
if(error) throw error;
|
|
|
|
|
|
|
|
var fd = result;
|
2014-01-21 21:25:09 +00:00
|
|
|
fs.write(fd, buffer, 0, buffer.length, 0, function(error, result) {
|
2013-12-10 16:59:44 +00:00
|
|
|
if(error) throw error;
|
|
|
|
|
2014-01-21 21:25:09 +00:00
|
|
|
fs.close(fd, function(error) {
|
2013-12-10 16:59:44 +00:00
|
|
|
if(error) throw error;
|
|
|
|
|
2014-01-21 21:25:09 +00:00
|
|
|
fs.symlink('/myfile', '/mylink', function(error) {
|
2013-12-10 16:59:44 +00:00
|
|
|
if(error) throw error;
|
|
|
|
|
2014-01-21 21:25:09 +00:00
|
|
|
fs.truncate('/mylink', 0, function(error) {
|
|
|
|
expect(error).not.to.exist;
|
2013-12-10 16:59:44 +00:00
|
|
|
|
2014-01-21 21:25:09 +00:00
|
|
|
fs.stat('/myfile', function(error, result) {
|
2013-12-10 16:59:44 +00:00
|
|
|
if(error) throw error;
|
|
|
|
|
2014-01-21 21:25:09 +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-01-21 21:25:09 +00:00
|
|
|
expect(result.size).not.to.equal(0);
|
|
|
|
done();
|
2013-12-10 16:59:44 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|