Merge 8c7d873c4c
into 4f4015a4f7
This commit is contained in:
commit
bd42d6d49c
|
@ -1980,11 +1980,24 @@ function appendFile(context, path, data, options, callback) {
|
|||
if(!flags) {
|
||||
return callback(new Errors.EINVAL('flags is not valid', path));
|
||||
}
|
||||
|
||||
if (typeof options === 'object') {
|
||||
if (!options.encoding) {
|
||||
options.encoding = 'utf8';
|
||||
}
|
||||
if (!options.mode) {
|
||||
options.mode = 0o666;
|
||||
}
|
||||
if (!options.flag) {
|
||||
options.flag = 'a';
|
||||
}
|
||||
}
|
||||
|
||||
data = data || '';
|
||||
if(typeof data === 'number') {
|
||||
data = '' + data;
|
||||
}
|
||||
|
||||
if(typeof data === 'string' && options.encoding === 'utf8') {
|
||||
data = Buffer.from(data);
|
||||
}
|
||||
|
|
|
@ -66,6 +66,38 @@ describe('fs.appendFile', function() {
|
|||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('should append without error when explcitly entering encoding and flag options (default values)' , function(done) {
|
||||
var fs = util.fs();
|
||||
var contents = 'This is a file.';
|
||||
var more = ' Appended.';
|
||||
|
||||
fs.appendFile('/myfile', more , {encoding: 'utf8', flag: 'a'}, function(error) {
|
||||
if(error) throw error;
|
||||
|
||||
fs.readFile('/myfile', { encoding: 'utf8' }, function(error, data) {
|
||||
expect(error).not.to.exist;
|
||||
expect(data).to.equal(contents + more);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('should append without error when specfifying flag option (default value)' , function(done) {
|
||||
var fs = util.fs();
|
||||
var contents = 'This is a file.';
|
||||
var more = ' Appended.';
|
||||
|
||||
fs.appendFile('/myfile', more , {flag: 'a'}, function(error) {
|
||||
if(error) throw error;
|
||||
|
||||
fs.readFile('/myfile', { encoding: 'utf8' }, function(error, data) {
|
||||
expect(error).not.to.exist;
|
||||
expect(data).to.equal(contents + more);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('should append a binary file', function(done) {
|
||||
const fs = util.fs();
|
||||
|
|
Loading…
Reference in New Issue