From f8fb92051540aeebaf118bda302272d323747b33 Mon Sep 17 00:00:00 2001 From: Muchtar Salimov Date: Mon, 8 Oct 2018 21:12:48 -0400 Subject: [PATCH] Fix appendFile bug shown to fail in PR-494 --- src/filesystem/implementation.js | 2 +- tests/spec/fs.appendFile.spec.js | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/src/filesystem/implementation.js b/src/filesystem/implementation.js index 6f15e2c..6f69631 100644 --- a/src/filesystem/implementation.js +++ b/src/filesystem/implementation.js @@ -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); } diff --git a/tests/spec/fs.appendFile.spec.js b/tests/spec/fs.appendFile.spec.js index 7fb3aa9..b57777a 100644 --- a/tests/spec/fs.appendFile.spec.js +++ b/tests/spec/fs.appendFile.spec.js @@ -65,6 +65,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();