Fix appendFile bug shown to fail in PR-494

This commit is contained in:
Muchtar Salimov 2018-10-08 21:12:48 -04:00
parent fd3de6be2c
commit f8fb920515
2 changed files with 33 additions and 1 deletions

View File

@ -1875,7 +1875,7 @@ function appendFile(fs, context, path, data, options, callback) {
if(typeof data === 'number') {
data = '' + data;
}
if(typeof data === 'string' && options.encoding === 'utf8') {
if(typeof data === 'string' && (options.encoding == null || options.encoding === 'utf8')) {
data = Encoding.encode(data);
}

View File

@ -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) {
var fs = util.fs();