2019-02-08 20:00:45 +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.writeFile, fs.readFile', 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() {
|
2019-02-08 20:00:45 +00:00
|
|
|
const fs = util.fs();
|
2014-05-23 20:53:50 +00:00
|
|
|
expect(fs.writeFile).to.be.a('function');
|
|
|
|
expect(fs.readFile).to.be.a('function');
|
|
|
|
});
|
2013-11-27 17:18:09 +00:00
|
|
|
|
2014-05-23 20:53:50 +00:00
|
|
|
it('should error when path is wrong to readFile', function(done) {
|
2019-02-08 20:00:45 +00:00
|
|
|
const fs = util.fs();
|
2013-11-27 17:18:09 +00:00
|
|
|
|
2014-05-23 20:53:50 +00:00
|
|
|
fs.readFile('/no-such-file', 'utf8', function(error, data) {
|
|
|
|
expect(error).to.exist;
|
2018-07-15 17:25:35 +00:00
|
|
|
expect(error.code).to.equal('ENOENT');
|
2014-05-23 20:53:50 +00:00
|
|
|
expect(data).not.to.exist;
|
|
|
|
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
|
|
|
|
2018-10-09 17:48:51 +00:00
|
|
|
|
|
|
|
it('should error when path is wrong to writeFile',function(done){
|
2019-02-08 20:00:45 +00:00
|
|
|
const fs = util.fs();
|
2018-10-09 17:48:51 +00:00
|
|
|
|
|
|
|
fs.writeFile('/tmp/myfile', '','utf8', function(error, result) {
|
|
|
|
expect(error).to.exist;
|
|
|
|
expect(error.code).to.equal('ENOENT');
|
|
|
|
expect(result).not.to.exist;
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2014-05-23 20:53:50 +00:00
|
|
|
it('should write, read a utf8 file without specifying utf8 in writeFile', function(done) {
|
2019-02-08 20:00:45 +00:00
|
|
|
const fs = util.fs();
|
|
|
|
const contents = 'This is a file.';
|
2013-11-27 17:18:09 +00:00
|
|
|
|
2014-05-23 20:53:50 +00:00
|
|
|
fs.writeFile('/myfile', contents, function(error) {
|
|
|
|
if(error) throw error;
|
|
|
|
fs.readFile('/myfile', 'utf8', function(error, data) {
|
|
|
|
expect(error).not.to.exist;
|
|
|
|
expect(data).to.equal(contents);
|
|
|
|
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
|
|
|
|
2019-01-25 19:52:22 +00:00
|
|
|
it('should write a string when given a number for the data', function(done) {
|
|
|
|
var fs = util.fs();
|
|
|
|
var contents = 7;
|
|
|
|
var contentsAsString = '7';
|
|
|
|
|
|
|
|
fs.writeFile('/myfile', contents, function(error) {
|
|
|
|
if(error) throw error;
|
|
|
|
|
|
|
|
fs.readFile('/myfile', 'utf8', function(error, data) {
|
|
|
|
expect(error).not.to.exist;
|
|
|
|
expect(data).to.equal(contentsAsString);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-05-23 20:53:50 +00:00
|
|
|
it('should write, read a utf8 file with "utf8" option to writeFile', function(done) {
|
2019-02-08 20:00:45 +00:00
|
|
|
const fs = util.fs();
|
|
|
|
const contents = 'This is a file.';
|
2013-11-27 17:18:09 +00:00
|
|
|
|
2014-05-23 20:53:50 +00:00
|
|
|
fs.writeFile('/myfile', contents, 'utf8', function(error) {
|
|
|
|
if(error) throw error;
|
|
|
|
fs.readFile('/myfile', 'utf8', function(error, data) {
|
|
|
|
expect(error).not.to.exist;
|
|
|
|
expect(data).to.equal(contents);
|
|
|
|
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 write, read a utf8 file with {encoding: "utf8"} option to writeFile', function(done) {
|
2019-02-08 20:00:45 +00:00
|
|
|
const fs = util.fs();
|
|
|
|
const contents = 'This is a file.';
|
2013-11-27 17:18:09 +00:00
|
|
|
|
2014-05-23 20:53:50 +00:00
|
|
|
fs.writeFile('/myfile', contents, { encoding: 'utf8' }, function(error) {
|
|
|
|
if(error) throw error;
|
|
|
|
fs.readFile('/myfile', 'utf8', function(error, data) {
|
|
|
|
expect(error).not.to.exist;
|
|
|
|
expect(data).to.equal(contents);
|
|
|
|
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 write, read a binary file', function(done) {
|
2019-02-08 20:00:45 +00:00
|
|
|
const fs = util.fs();
|
2018-07-26 22:01:07 +00:00
|
|
|
// String and utf8 binary encoded versions of the same thing: 'This is a file.'
|
2019-02-08 20:00:45 +00:00
|
|
|
const binary = Buffer.from([84, 104, 105, 115, 32, 105, 115, 32, 97, 32, 102, 105, 108, 101, 46]);
|
2013-11-27 17:18:09 +00:00
|
|
|
|
2014-05-23 20:53:50 +00:00
|
|
|
fs.writeFile('/myfile', binary, function(error) {
|
|
|
|
if(error) throw error;
|
|
|
|
fs.readFile('/myfile', function(error, data) {
|
|
|
|
expect(error).not.to.exist;
|
|
|
|
expect(data).to.deep.equal(binary);
|
|
|
|
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 follow symbolic links', function(done) {
|
2019-02-08 20:00:45 +00:00
|
|
|
const fs = util.fs();
|
|
|
|
const contents = 'This is a file.';
|
2013-11-27 17:18:09 +00:00
|
|
|
|
2014-05-23 20:53:50 +00:00
|
|
|
fs.writeFile('/myfile', '', { encoding: 'utf8' }, function(error) {
|
|
|
|
if(error) throw error;
|
|
|
|
fs.symlink('/myfile', '/myFileLink', function (error) {
|
|
|
|
if (error) throw error;
|
|
|
|
fs.writeFile('/myFileLink', contents, 'utf8', function (error) {
|
2013-11-27 17:18:09 +00:00
|
|
|
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);
|
|
|
|
done();
|
2013-11-27 17:18:09 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2014-05-23 20:53:50 +00:00
|
|
|
});
|
2018-10-20 20:59:26 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* fsPromises tests
|
|
|
|
*/
|
|
|
|
|
|
|
|
describe('fsPromises.writeFile, fsPromises.readFile', function() {
|
|
|
|
beforeEach(util.setup);
|
|
|
|
afterEach(util.cleanup);
|
|
|
|
|
|
|
|
it('should be a function', function() {
|
2019-02-08 20:00:45 +00:00
|
|
|
const fsPromises = util.fs().promises;
|
2018-10-20 20:59:26 +00:00
|
|
|
expect(fsPromises.writeFile).to.be.a('function');
|
|
|
|
expect(fsPromises.readFile).to.be.a('function');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return a promise', function() {
|
2019-02-08 20:00:45 +00:00
|
|
|
const fsPromises = util.fs().promises;
|
|
|
|
const contents = 'This is a file.';
|
2018-10-20 20:59:26 +00:00
|
|
|
|
2019-02-08 20:00:45 +00:00
|
|
|
const p = fsPromises.writeFile('/myfile', contents);
|
2018-10-20 20:59:26 +00:00
|
|
|
expect(p).to.be.a('Promise');
|
|
|
|
|
|
|
|
p.then(() => {
|
|
|
|
expect(fsPromises.readFile('/myfile', 'utf8')).to.be.a('Promise');
|
|
|
|
});
|
|
|
|
return p;
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should error when path is wrong to readFile', function() {
|
2019-02-08 20:00:45 +00:00
|
|
|
const fsPromises = util.fs().promises;
|
2018-10-20 20:59:26 +00:00
|
|
|
|
|
|
|
return fsPromises.readFile('/no-such-file', 'utf8')
|
|
|
|
.catch(error => {
|
|
|
|
expect(error).to.exist;
|
|
|
|
expect(error.code).to.equal('ENOENT');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should write, read a utf8 file without specifying utf8 in writeFile', function() {
|
2019-02-08 20:00:45 +00:00
|
|
|
const fsPromises = util.fs().promises;
|
|
|
|
const contents = 'This is a file.';
|
2018-10-20 20:59:26 +00:00
|
|
|
|
|
|
|
return fsPromises.writeFile('/myfile', contents)
|
|
|
|
.then( () => fsPromises.readFile('/myfile', 'utf8'))
|
|
|
|
.then(data => { expect(data).to.equal(contents); });
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should write, read a utf8 file with "utf8" option to writeFile', function() {
|
2019-02-08 20:00:45 +00:00
|
|
|
const fsPromises = util.fs().promises;
|
|
|
|
const contents = 'This is a file.';
|
2018-10-20 20:59:26 +00:00
|
|
|
|
|
|
|
return fsPromises.writeFile('/myfile', contents, 'utf8')
|
|
|
|
.then( () => fsPromises.readFile('/myfile', 'utf8'))
|
|
|
|
.then(data => { expect(data).to.equal(contents); });
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should write, read a utf8 file with {encoding: "utf8"} option to writeFile', function() {
|
2019-02-08 20:00:45 +00:00
|
|
|
const fsPromises = util.fs().promises;
|
|
|
|
const contents = 'This is a file.';
|
2018-10-20 20:59:26 +00:00
|
|
|
|
|
|
|
return fsPromises.writeFile('/myfile', contents, { encoding: 'utf8' })
|
|
|
|
.then( () => fsPromises.readFile('/myfile', 'utf8'))
|
|
|
|
.then(data => { expect(data).to.equal(contents); });
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should write, read a binary file', function() {
|
2019-02-08 20:00:45 +00:00
|
|
|
const fsPromises = util.fs().promises;
|
2018-10-20 20:59:26 +00:00
|
|
|
// String and utf8 binary encoded versions of the same thing: 'This is a file.'
|
2019-02-08 20:00:45 +00:00
|
|
|
const binary = Buffer.from([84, 104, 105, 115, 32, 105, 115, 32, 97, 32, 102, 105, 108, 101, 46]);
|
2018-10-20 20:59:26 +00:00
|
|
|
|
|
|
|
return fsPromises.writeFile('/myfile', binary)
|
|
|
|
.then( () => fsPromises.readFile('/myfile'))
|
|
|
|
.then(data => { expect(data).to.deep.equal(binary); });
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should follow symbolic links', function() {
|
2019-02-08 20:00:45 +00:00
|
|
|
const fsPromises = util.fs().promises;
|
|
|
|
const contents = 'This is a file.';
|
2018-10-20 20:59:26 +00:00
|
|
|
|
|
|
|
return fsPromises.writeFile('/myfile', '', { encoding: 'utf8' })
|
|
|
|
.then( () => fsPromises.symlink('/myfile', '/myFileLink'))
|
|
|
|
.then( () => fsPromises.writeFile('/myFileLink', contents, 'utf8'))
|
|
|
|
.then( () => fsPromises.readFile('/myFileLink', 'utf8'))
|
|
|
|
.then(data => { expect(data).to.equal(contents); });
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|