filer/tests/spec/fs.appendFile.spec.js

225 lines
7.0 KiB
JavaScript
Raw Normal View History

2019-01-30 03:43:56 +00:00
'use strict';
const util = require('../lib/test-utils.js');
const expect = require('chai').expect;
2014-05-23 20:53:50 +00:00
describe('fs.appendFile', function() {
const contents = 'This is a file.';
2014-05-23 20:53:50 +00:00
beforeEach(function(done) {
util.setup(function() {
2019-01-30 03:43:56 +00:00
let fs = util.fs();
fs.writeFile('/myfile', contents, function(error) {
2014-05-23 20:53:50 +00:00
if(error) throw error;
done();
});
});
2014-05-23 20:53:50 +00:00
});
afterEach(util.cleanup);
2014-05-23 20:53:50 +00:00
it('should be a function', function() {
2019-01-30 03:43:56 +00:00
let fs = util.fs();
2014-05-23 20:53:50 +00:00
expect(fs.appendFile).to.be.a('function');
});
2014-05-23 20:53:50 +00:00
it('should append a utf8 file without specifying utf8 in appendFile', function(done) {
2019-01-30 03:43:56 +00:00
let fs = util.fs();
const more = ' Appended.';
2014-05-23 20:53:50 +00:00
fs.appendFile('/myfile', more, function(error) {
if(error) throw error;
fs.readFile('/myfile', 'utf8', function(error, data) {
expect(error).not.to.exist;
expect(data).to.equal(contents + more);
done();
});
});
2014-05-23 20:53:50 +00:00
});
2014-05-23 20:53:50 +00:00
it('should append a utf8 file with "utf8" option to appendFile', function(done) {
2019-01-30 03:43:56 +00:00
let fs = util.fs();
const more = ' Appended.';
2014-05-23 20:53:50 +00:00
fs.appendFile('/myfile', more, 'utf8', function(error) {
if(error) throw error;
2014-05-23 20:53:50 +00:00
fs.readFile('/myfile', 'utf8', function(error, data) {
expect(error).not.to.exist;
expect(data).to.equal(contents + more);
done();
});
});
2014-05-23 20:53:50 +00:00
});
2014-05-23 20:53:50 +00:00
it('should append a utf8 file with {encoding: "utf8"} option to appendFile', function(done) {
2019-01-30 03:43:56 +00:00
let fs = util.fs();
const more = ' Appended.';
2014-05-23 20:53:50 +00:00
fs.appendFile('/myfile', more, { encoding: 'utf8' }, function(error) {
if(error) throw error;
2014-05-23 20:53:50 +00:00
fs.readFile('/myfile', { encoding: 'utf8' }, function(error, data) {
expect(error).not.to.exist;
expect(data).to.equal(contents + more);
done();
});
});
2014-05-23 20:53:50 +00:00
});
2014-05-23 20:53:50 +00:00
it('should append a binary file', function(done) {
2019-01-30 03:43:56 +00:00
let fs = util.fs();
// String and utf8 binary encoded versions of the same thing: 'This is a file.'
2019-01-30 03:43:56 +00:00
const binary = Buffer.from([84, 104, 105, 115, 32, 105, 115, 32, 97, 32, 102, 105, 108, 101, 46]);
const binary2 = Buffer.from([32, 65, 112, 112, 101, 110, 100, 101, 100, 46]);
const binary3 = Buffer.from([84, 104, 105, 115, 32, 105, 115, 32, 97, 32, 102, 105, 108, 101, 46,
32, 65, 112, 112, 101, 110, 100, 101, 100, 46]);
2014-05-23 20:53:50 +00:00
fs.writeFile('/mybinaryfile', binary, function(error) {
if(error) throw error;
2014-05-23 20:53:50 +00:00
fs.appendFile('/mybinaryfile', binary2, function(error) {
if(error) throw error;
2014-05-23 20:53:50 +00:00
fs.readFile('/mybinaryfile', 'ascii', function(error, data) {
expect(error).not.to.exist;
expect(data).to.deep.equal(binary3);
done();
});
});
});
2014-05-23 20:53:50 +00:00
});
2014-05-23 20:53:50 +00:00
it('should follow symbolic links', function(done) {
2019-01-30 03:43:56 +00:00
let fs = util.fs();
const contents = 'This is a file.';
const more = ' Appended.';
2014-05-23 20:53:50 +00:00
fs.symlink('/myfile', '/myFileLink', function (error) {
if (error) throw error;
2014-05-23 20:53:50 +00:00
fs.appendFile('/myFileLink', more, 'utf8', function (error) {
if (error) throw error;
2014-05-23 20:53:50 +00:00
fs.readFile('/myFileLink', 'utf8', function(error, data) {
expect(error).not.to.exist;
expect(data).to.equal(contents + more);
done();
});
});
});
2014-05-23 20:53:50 +00:00
});
2014-05-23 20:53:50 +00:00
it('should work when file does not exist, and create the file', function(done) {
2019-01-30 03:43:56 +00:00
let fs = util.fs();
2014-05-23 20:53:50 +00:00
fs.appendFile('/newfile', contents, { encoding: 'utf8' }, function(error) {
expect(error).not.to.exist;
2014-05-23 20:53:50 +00:00
fs.readFile('/newfile', 'utf8', function(err, data) {
if(err) throw err;
expect(data).to.equal(contents);
done();
});
});
});
it('should accept numbers and append them to the file', function(done) {
2019-01-30 03:43:56 +00:00
let fs = util.fs();
const more = 10000;
fs.appendFile('/myfile', more, 'utf8', function(error) {
if(error) throw error;
fs.readFile('/myfile', 'utf8', function(error, data) {
expect(error).not.to.exist;
expect(data).to.equal(contents + more);
done();
});
});
});
});
describe('fs.promises.appendFile', function() {
beforeEach(function(done) {
util.setup(function() {
2019-01-30 03:43:56 +00:00
let fs = util.fs();
2018-12-11 04:57:04 +00:00
return fs.promises.writeFile('/myfile', 'This is a file.', { encoding: 'utf8' })
.then(done)
.catch(done);
});
});
afterEach(util.cleanup);
it('should be a function', function() {
2019-01-30 03:43:56 +00:00
let fs = util.fs();
expect(fs.promises.appendFile).to.be.a('function');
});
2018-12-11 04:57:04 +00:00
it('should append a utf8 file without specifying utf8 in appendFile', function() {
2019-01-30 03:43:56 +00:00
let fs = util.fs();
const contents = 'This is a file.';
const more = ' Appended.';
2018-12-11 04:57:04 +00:00
return fs.promises.appendFile('/myfile', more)
.then(() => fs.promises.readFile('/myfile', 'utf8'))
.then(data => expect(data).to.equal(contents + more));
});
2018-12-11 04:57:04 +00:00
it('should append a utf8 file with "utf8" option to appendFile', function() {
2019-01-30 03:43:56 +00:00
let fs = util.fs();
const contents = 'This is a file.';
const more = ' Appended.';
2018-12-11 04:57:04 +00:00
return fs.promises.appendFile('/myfile', more, 'utf8')
.then(() => fs.promises.readFile('/myfile', 'utf8'))
.then(data => expect(data).to.equal(contents + more));
});
2018-12-11 04:57:04 +00:00
it('should append a utf8 file with {encoding: "utf8"} option to appendFile', function() {
2019-01-30 03:43:56 +00:00
let fs = util.fs();
const contents = 'This is a file.';
const more = ' Appended.';
2018-12-11 04:57:04 +00:00
return fs.promises.appendFile('/myfile', more, { encoding: 'utf8' })
.then(() => fs.promises.readFile('/myfile', { encoding: 'utf8' }))
.then(data => expect(data).to.equal(contents + more));
});
2018-12-11 04:57:04 +00:00
it('should append a binary file', function() {
2019-01-30 03:43:56 +00:00
let fs = util.fs();
// String and utf8 binary encoded versions of the same thing: 'This is a file.'
2019-01-30 03:43:56 +00:00
const binary = new Buffer([84, 104, 105, 115, 32, 105, 115, 32, 97, 32, 102, 105, 108, 101, 46]);
const binary2 = new Buffer([32, 65, 112, 112, 101, 110, 100, 101, 100, 46]);
const binary3 = new Buffer([84, 104, 105, 115, 32, 105, 115, 32, 97, 32, 102, 105, 108, 101, 46,
32, 65, 112, 112, 101, 110, 100, 101, 100, 46]);
2018-12-11 04:57:04 +00:00
return fs.promises.writeFile('/mybinaryfile', binary)
.then(() => fs.promises.appendFile('/mybinaryfile', binary2))
.then(() => fs.promises.readFile('/mybinaryfile', 'ascii'))
.then(data => expect(data).to.deep.equal(binary3));
});
2018-12-11 04:57:04 +00:00
it('should follow symbolic links', function() {
2019-01-30 03:43:56 +00:00
let fs = util.fs();
const contents = 'This is a file.';
const more = ' Appended.';
2018-12-11 04:57:04 +00:00
return fs.promises.symlink('/myfile', '/myFileLink')
.then(() => fs.promises.appendFile('/myFileLink', more, 'utf8'))
.then(() => fs.promises.readFile('/myFileLink', 'utf8'))
.then(data => expect(data).to.equal(contents + more));
});
2018-12-11 04:57:04 +00:00
it('should work when file does not exist, and create the file', function() {
2019-01-30 03:43:56 +00:00
let fs = util.fs();
const contents = 'This is a file.';
2018-12-11 04:57:04 +00:00
return fs.promises.appendFile('/newfile', contents, { encoding: 'utf8' })
.then(() => fs.promises.readFile('/newfile', 'utf8'))
.then(data => expect(data).to.equal(contents));
});
2018-12-11 04:57:04 +00:00
});