2019-02-02 01:53:12 +00:00
|
|
|
'use strict';
|
|
|
|
const util = require('../lib/test-utils.js');
|
|
|
|
const expect = require('chai').expect;
|
2013-11-27 17:18:09 +00:00
|
|
|
|
2014-05-23 20:53:50 +00:00
|
|
|
describe('fs.write', function() {
|
|
|
|
beforeEach(util.setup);
|
|
|
|
afterEach(util.cleanup);
|
2018-12-15 05:17:43 +00:00
|
|
|
|
|
|
|
it('should be a function', function() {
|
2019-02-02 01:53:12 +00:00
|
|
|
const fs = util.fs();
|
2018-12-15 05:17:43 +00:00
|
|
|
expect(fs.write).to.be.a('function');
|
|
|
|
});
|
|
|
|
|
2019-01-02 23:07:44 +00:00
|
|
|
it('should error if file path is undefined', function() {
|
2019-02-02 01:53:12 +00:00
|
|
|
const fs = util.fs();
|
|
|
|
const fn = () => fs.writeFile(undefined, 'data');
|
2019-01-02 23:07:44 +00:00
|
|
|
expect(fn).to.throw();
|
2018-09-24 20:46:47 +00:00
|
|
|
});
|
|
|
|
|
2014-05-23 20:53:50 +00:00
|
|
|
it('should write data to a file', function(done) {
|
2019-02-02 01:53:12 +00:00
|
|
|
const fs = util.fs();
|
|
|
|
const buffer = Buffer.from([1, 2, 3, 4, 5, 6, 7, 8]);
|
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, 0, function(error, result) {
|
|
|
|
expect(error).not.to.exist;
|
|
|
|
expect(result).to.equal(buffer.length);
|
2013-11-27 17:18:09 +00:00
|
|
|
|
2014-05-23 20:53:50 +00:00
|
|
|
fs.stat('/myfile', function(error, result) {
|
|
|
|
expect(error).not.to.exist;
|
2018-11-28 18:54:20 +00:00
|
|
|
expect(result.isFile()).to.be.true;
|
2014-05-23 20:53:50 +00:00
|
|
|
expect(result.size).to.equal(buffer.length);
|
2018-12-29 18:10:58 +00:00
|
|
|
fs.close(fd, 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 file position', function(done) {
|
2019-02-02 01:53:12 +00:00
|
|
|
const fs = util.fs();
|
|
|
|
const buffer = Buffer.from([1, 2, 3, 4, 5, 6, 7, 8]);
|
|
|
|
let _result = 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
|
|
|
_result += result;
|
2013-11-27 17:18:09 +00:00
|
|
|
|
2014-01-21 21:25:09 +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;
|
|
|
|
_result += result;
|
|
|
|
|
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(error).not.to.exist;
|
|
|
|
expect(_result).to.equal(2 * buffer.length);
|
|
|
|
expect(result.size).to.equal(_result);
|
2018-12-29 18:10:58 +00:00
|
|
|
fs.close(fd, done);
|
2013-11-27 17:18:09 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|