Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

fs.writeFile behaviour not consistent with fs.write functions. #657

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ fs.writeFile = function(path, data, encoding_, callback) {
if (openErr) {
if (callback) callback(openErr);
} else {
var buffer = Buffer.isBuffer(data) ? data : new Buffer(data, encoding);
var buffer = Buffer.isBuffer(data) ? data : new Buffer('' + data, encoding);
writeAll(fd, buffer, 0, buffer.length, callback);
}
});
Expand All @@ -448,7 +448,7 @@ fs.writeFile = function(path, data, encoding_, callback) {
fs.writeFileSync = function(path, data, encoding) {
var fd = fs.openSync(path, 'w');
if (!Buffer.isBuffer(data)) {
data = new Buffer(data, encoding || 'utf8');
data = new Buffer('' + data, encoding || 'utf8');
}
var written = 0;
var length = data.length;
Expand Down
22 changes: 21 additions & 1 deletion test/simple/test-fs-write-file.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ var filename = join(common.fixturesDir, 'test.txt');

common.error('writing to ' + filename);

var n = 220;
var s = '南越国是前203年至前111年存在于岭南地区的一个国家,国都位于番禺,疆域包括今天中国的广东、' +
'广西两省区的大部份地区,福建省、湖南、贵州、云南的一小部份地区和越南的北部。' +
'南越国是秦朝灭亡后,由南海郡尉赵佗于前203年起兵兼并桂林郡和象郡后建立。' +
Expand Down Expand Up @@ -50,11 +51,30 @@ fs.writeFile(filename2, buf, function(e) {
});
});

// test that writeFile accepts numbers.
var filename3 = join(common.fixturesDir, 'test3.txt');
common.error('writing to ' + filename3);

fs.writeFile(filename3, n, function(e) {
if (e) throw e;

ncallbacks++;
common.error('file3 written');

fs.readFile(filename3, function(e, buffer) {
if (e) throw e;
common.error('file3 read');
ncallbacks++;
assert.equal(Buffer.byteLength('' + n), buffer.length);
});
});


process.addListener('exit', function() {
common.error('done');
assert.equal(4, ncallbacks);
assert.equal(6, ncallbacks);

fs.unlinkSync(filename);
fs.unlinkSync(filename2);
fs.unlinkSync(filename3);
});