filer/tests/spec/fs.lseek.spec.js

168 lines
5.3 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-11-27 17:18:09 +00:00
2014-05-23 20:53:50 +00:00
describe('fs.lseek', function() {
beforeEach(util.setup);
afterEach(util.cleanup);
2013-11-27 17:18:09 +00:00
2014-05-23 20:53:50 +00:00
it('should be a function', function() {
var fs = util.fs();
expect(fs.lseek).to.be.a('function');
});
it('should not follow symbolic links', function(done) {
var fs = util.fs();
2013-11-27 17:18:09 +00:00
2014-05-23 20:53:50 +00:00
fs.open('/myfile', 'w', function (error, fd) {
if (error) throw error;
2013-11-27 17:18:09 +00:00
2014-05-23 20:53:50 +00:00
fs.close(fd, function (error) {
2013-11-27 17:18:09 +00:00
if (error) throw error;
2014-05-23 20:53:50 +00:00
fs.symlink('/myfile', '/myFileLink', function (error) {
2013-11-27 17:18:09 +00:00
if (error) throw error;
2014-05-23 20:53:50 +00:00
fs.rename('/myFileLink', '/myOtherFileLink', function (error) {
2013-11-27 17:18:09 +00:00
if (error) throw error;
2014-05-23 20:53:50 +00:00
fs.stat('/myfile', function (error, result) {
expect(error).not.to.exist;
2013-11-27 17:18:09 +00:00
2014-05-23 20:53:50 +00:00
fs.lstat('/myFileLink', function (error, result) {
expect(error).to.exist;
2013-11-27 17:18:09 +00:00
2014-05-23 20:53:50 +00:00
fs.stat('/myOtherFileLink', function (error, result) {
if (error) throw error;
expect(result.nlinks).to.equal(1);
done();
2013-11-27 17:18:09 +00:00
});
});
});
});
});
});
});
2014-05-23 20:53:50 +00:00
});
it('should set the current position if whence is SET', function(done) {
var fs = util.fs();
var offset = 3;
var buffer = new Filer.Buffer([1, 2, 3, 4, 5, 6, 7, 8]);
var result_buffer = new Filer.Buffer(buffer.length + offset);
result_buffer.fill(0);
2013-11-27 17:18:09 +00:00
2014-05-23 20:53:50 +00:00
fs.open('/myfile', 'w+', function(error, fd) {
if(error) throw error;
2013-11-27 17:18:09 +00:00
2014-05-23 20:53:50 +00:00
fs.write(fd, buffer, 0, buffer.length, undefined, function(error, result) {
2013-11-27 17:18:09 +00:00
if(error) throw error;
2014-05-23 20:53:50 +00:00
fs.lseek(fd, offset, 'SET', function(error, result) {
expect(error).not.to.exist;
expect(result).to.equal(offset);
2013-11-27 17:18:09 +00:00
2014-05-23 20:53:50 +00:00
fs.write(fd, buffer, 0, buffer.length, undefined, function(error, result) {
if(error) throw error;
2013-11-27 17:18:09 +00:00
2014-05-23 20:53:50 +00:00
fs.read(fd, result_buffer, 0, result_buffer.length, 0, function(error, result) {
2013-11-27 17:18:09 +00:00
if(error) throw error;
2014-05-23 20:53:50 +00:00
fs.stat('/myfile', function(error, result) {
2013-11-27 17:18:09 +00:00
if(error) throw error;
2014-05-23 20:53:50 +00:00
expect(result.size).to.equal(offset + buffer.length);
var expected = new Filer.Buffer([1, 2, 3, 1, 2, 3, 4, 5, 6, 7, 8]);
2014-05-23 20:53:50 +00:00
expect(result_buffer).to.deep.equal(expected);
done();
2013-11-27 17:18:09 +00:00
});
});
});
});
});
});
2014-05-23 20:53:50 +00:00
});
2013-11-27 17:18:09 +00:00
2014-05-23 20:53:50 +00:00
it('should update the current position if whence is CUR', function(done) {
var fs = util.fs();
var offset = -2;
var buffer = new Filer.Buffer([1, 2, 3, 4, 5, 6, 7, 8]);
var result_buffer = new Filer.Buffer(2 * buffer.length + offset);
result_buffer.fill(0);
2013-11-27 17:18:09 +00:00
2014-05-23 20:53:50 +00:00
fs.open('/myfile', 'w+', function(error, fd) {
if(error) throw error;
fs.write(fd, buffer, 0, buffer.length, undefined, function(error, result) {
2013-11-27 17:18:09 +00:00
if(error) throw error;
2014-05-23 20:53:50 +00:00
fs.lseek(fd, offset, 'CUR', function(error, result) {
expect(error).not.to.exist;
expect(result).to.equal(offset + buffer.length);
2013-11-27 17:18:09 +00:00
2014-05-23 20:53:50 +00:00
fs.write(fd, buffer, 0, buffer.length, undefined, function(error, result) {
if(error) throw error;
2013-11-27 17:18:09 +00:00
2014-05-23 20:53:50 +00:00
fs.read(fd, result_buffer, 0, result_buffer.length, 0, function(error, result) {
2013-11-27 17:18:09 +00:00
if(error) throw error;
2014-05-23 20:53:50 +00:00
fs.stat('/myfile', function(error, result) {
2013-11-27 17:18:09 +00:00
if(error) throw error;
2014-05-23 20:53:50 +00:00
expect(result.size).to.equal(offset + 2 * buffer.length);
var expected = new Filer.Buffer([1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 7, 8]);
2014-05-23 20:53:50 +00:00
expect(result_buffer).to.deep.equal(expected);
done();
2013-11-27 17:18:09 +00:00
});
});
});
});
});
});
2014-05-23 20:53:50 +00:00
});
it('should update the current position if whence is END', function(done) {
var fs = util.fs();
var offset = 5;
var buffer = new Filer.Buffer([1, 2, 3, 4, 5, 6, 7, 8]);
2014-05-23 20:53:50 +00:00
var result_buffer;
2013-11-27 17:18:09 +00:00
2014-05-23 20:53:50 +00:00
fs.open('/myfile', 'w+', function(error, result) {
if(error) throw error;
2013-11-27 17:18:09 +00:00
2014-05-23 20:53:50 +00:00
var fd1 = result;
fs.write(fd1, buffer, 0, buffer.length, undefined, function(error, result) {
2013-11-27 17:18:09 +00:00
if(error) throw error;
2014-05-23 20:53:50 +00:00
fs.open('/myfile', 'w+', function(error, result) {
2013-11-27 17:18:09 +00:00
if(error) throw error;
2014-05-23 20:53:50 +00:00
var fd2 = result;
fs.lseek(fd2, offset, 'END', function(error, result) {
expect(error).not.to.exist;
expect(result).to.equal(offset + buffer.length);
2013-11-27 17:18:09 +00:00
2014-05-23 20:53:50 +00:00
fs.write(fd2, buffer, 0, buffer.length, undefined, function(error, result) {
if(error) throw error;
2013-11-27 17:18:09 +00:00
2014-05-23 20:53:50 +00:00
fs.stat('/myfile', function(error, result) {
2013-11-27 17:18:09 +00:00
if(error) throw error;
2014-05-23 20:53:50 +00:00
expect(result.size).to.equal(offset + 2 * buffer.length);
result_buffer = new Filer.Buffer(result.size);
result_buffer.fill(0);
2014-05-23 20:53:50 +00:00
fs.read(fd2, result_buffer, 0, result_buffer.length, 0, function(error, result) {
2013-11-27 17:18:09 +00:00
if(error) throw error;
var expected = new Filer.Buffer([1, 2, 3, 4, 5, 6, 7, 8, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8]);
2014-05-23 20:53:50 +00:00
expect(result_buffer).to.deep.equal(expected);
done();
2013-11-27 17:18:09 +00:00
});
});
});
});
});
});
});
});
});