This commit is contained in:
Muchtar Salimov 2022-02-14 17:38:40 +05:30 committed by GitHub
commit bd42d6d49c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 45 additions and 0 deletions

View File

@ -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);
}

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