diff --git a/lib/fileOperations.js b/lib/fileOperations.js index 4c194d4d..630a3f22 100644 --- a/lib/fileOperations.js +++ b/lib/fileOperations.js @@ -12,6 +12,10 @@ var MASK_MODE = parseInt('0777', 8); var DEFAULT_FILE_MODE = parseInt('0666', 8); var APPEND_MODE_REGEXP = /a/; +function isString(value) { + return (typeof value === 'string'); +} + function closeFd(propagatedErr, fd, callback) { if (typeof fd !== 'number') { return callback(propagatedErr); @@ -169,8 +173,12 @@ function writeFile(path, data, options, callback) { } if (!isBuffer(data)) { - callback(new TypeError('Data must be a Buffer')); - return; + if (isString(data)) { + data = new Buffer(data); + } else { + callback(new TypeError('Data must be a Buffer')); + return; + } } if (!options) { diff --git a/test/fileOperations.js b/test/fileOperations.js index 454eaba9..b0c8cda7 100644 --- a/test/fileOperations.js +++ b/test/fileOperations.js @@ -556,8 +556,16 @@ describe('writeFile', function() { }); }); - it('passes an error if called without buffer for data', function(done) { + it('does not pass an error if called with string as data', function(done) { writeFile(filepath, 'test', function(err) { + expect(err).toNotExist(); + + done(); + }); + }); + + it('passes an error if called with non-buffer/non-string as data', function(done) { + writeFile(filepath, {}, function(err) { expect(err).toExist(); done();