1// Copyright Joyent, Inc. and other Node contributors. 2// 3// Permission is hereby granted, free of charge, to any person obtaining a 4// copy of this software and associated documentation files (the 5// "Software"), to deal in the Software without restriction, including 6// without limitation the rights to use, copy, modify, merge, publish, 7// distribute, sublicense, and/or sell copies of the Software, and to permit 8// persons to whom the Software is furnished to do so, subject to the 9// following conditions: 10// 11// The above copyright notice and this permission notice shall be included 12// in all copies or substantial portions of the Software. 13// 14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 15// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN 17// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 18// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 19// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 20// USE OR OTHER DEALINGS IN THE SOFTWARE. 21 22'use strict'; 23const common = require('../common'); 24const assert = require('assert'); 25const fs = require('fs'); 26const join = require('path').join; 27 28const tmpdir = require('../common/tmpdir'); 29 30const currentFileData = 'ABCD'; 31 32const s = '南越国是前203年至前111年存在于岭南地区的一个国家,国都位于番禺,疆域包括今天中国的广东、' + 33 '广西两省区的大部份地区,福建省、湖南、贵州、云南的一小部份地区和越南的北部。' + 34 '南越国是秦朝灭亡后,由南海郡尉赵佗于前203年起兵兼并桂林郡和象郡后建立。' + 35 '前196年和前179年,南越国曾先后两次名义上臣属于西汉,成为西汉的“外臣”。前112年,' + 36 '南越国末代君主赵建德与西汉发生战争,被汉武帝于前111年所灭。南越国共存在93年,' + 37 '历经五代君主。南越国是岭南地区的第一个有记载的政权国家,采用封建制和郡县制并存的制度,' + 38 '它的建立保证了秦末乱世岭南地区社会秩序的稳定,有效的改善了岭南地区落后的政治、##济现状。\n'; 39 40tmpdir.refresh(); 41 42const throwNextTick = (e) => { process.nextTick(() => { throw e; }); }; 43 44// Test that empty file will be created and have content added (callback API). 45{ 46 const filename = join(tmpdir.path, 'append.txt'); 47 48 fs.appendFile(filename, s, common.mustSucceed(() => { 49 fs.readFile(filename, common.mustSucceed((buffer) => { 50 assert.strictEqual(Buffer.byteLength(s), buffer.length); 51 })); 52 })); 53} 54 55// Test that empty file will be created and have content added (promise API). 56{ 57 const filename = join(tmpdir.path, 'append-promise.txt'); 58 59 fs.promises.appendFile(filename, s) 60 .then(common.mustCall(() => fs.promises.readFile(filename))) 61 .then((buffer) => { 62 assert.strictEqual(Buffer.byteLength(s), buffer.length); 63 }) 64 .catch(throwNextTick); 65} 66 67// Test that appends data to a non-empty file (callback API). 68{ 69 const filename = join(tmpdir.path, 'append-non-empty.txt'); 70 fs.writeFileSync(filename, currentFileData); 71 72 fs.appendFile(filename, s, common.mustSucceed(() => { 73 fs.readFile(filename, common.mustSucceed((buffer) => { 74 assert.strictEqual(Buffer.byteLength(s) + currentFileData.length, 75 buffer.length); 76 })); 77 })); 78} 79 80// Test that appends data to a non-empty file (promise API). 81{ 82 const filename = join(tmpdir.path, 'append-non-empty-promise.txt'); 83 fs.writeFileSync(filename, currentFileData); 84 85 fs.promises.appendFile(filename, s) 86 .then(common.mustCall(() => fs.promises.readFile(filename))) 87 .then((buffer) => { 88 assert.strictEqual(Buffer.byteLength(s) + currentFileData.length, 89 buffer.length); 90 }) 91 .catch(throwNextTick); 92} 93 94// Test that appendFile accepts buffers (callback API). 95{ 96 const filename = join(tmpdir.path, 'append-buffer.txt'); 97 fs.writeFileSync(filename, currentFileData); 98 99 const buf = Buffer.from(s, 'utf8'); 100 101 fs.appendFile(filename, buf, common.mustSucceed(() => { 102 fs.readFile(filename, common.mustSucceed((buffer) => { 103 assert.strictEqual(buf.length + currentFileData.length, buffer.length); 104 })); 105 })); 106} 107 108// Test that appendFile accepts buffers (promises API). 109{ 110 const filename = join(tmpdir.path, 'append-buffer-promises.txt'); 111 fs.writeFileSync(filename, currentFileData); 112 113 const buf = Buffer.from(s, 'utf8'); 114 115 fs.promises.appendFile(filename, buf) 116 .then(common.mustCall(() => fs.promises.readFile(filename))) 117 .then((buffer) => { 118 assert.strictEqual(buf.length + currentFileData.length, buffer.length); 119 }) 120 .catch(throwNextTick); 121} 122 123// Test that appendFile does not accept invalid data type (callback API). 124[false, 5, {}, null, undefined].forEach(async (data) => { 125 const errObj = { 126 code: 'ERR_INVALID_ARG_TYPE', 127 message: /"data"|"buffer"/ 128 }; 129 const filename = join(tmpdir.path, 'append-invalid-data.txt'); 130 131 assert.throws( 132 () => fs.appendFile(filename, data, common.mustNotCall()), 133 errObj 134 ); 135 136 assert.throws( 137 () => fs.appendFileSync(filename, data), 138 errObj 139 ); 140 141 await assert.rejects( 142 fs.promises.appendFile(filename, data), 143 errObj 144 ); 145 // The filename shouldn't exist if throwing error. 146 assert.throws( 147 () => fs.statSync(filename), 148 { 149 code: 'ENOENT', 150 message: /no such file or directory/ 151 } 152 ); 153}); 154 155// Test that appendFile accepts file descriptors (callback API). 156{ 157 const filename = join(tmpdir.path, 'append-descriptors.txt'); 158 fs.writeFileSync(filename, currentFileData); 159 160 fs.open(filename, 'a+', common.mustSucceed((fd) => { 161 fs.appendFile(fd, s, common.mustSucceed(() => { 162 fs.close(fd, common.mustSucceed(() => { 163 fs.readFile(filename, common.mustSucceed((buffer) => { 164 assert.strictEqual(Buffer.byteLength(s) + currentFileData.length, 165 buffer.length); 166 })); 167 })); 168 })); 169 })); 170} 171 172// Test that appendFile accepts file descriptors (promises API). 173{ 174 const filename = join(tmpdir.path, 'append-descriptors-promises.txt'); 175 fs.writeFileSync(filename, currentFileData); 176 177 let fd; 178 fs.promises.open(filename, 'a+') 179 .then(common.mustCall((fileDescriptor) => { 180 fd = fileDescriptor; 181 return fs.promises.appendFile(fd, s); 182 })) 183 .then(common.mustCall(() => fd.close())) 184 .then(common.mustCall(() => fs.promises.readFile(filename))) 185 .then(common.mustCall((buffer) => { 186 assert.strictEqual(Buffer.byteLength(s) + currentFileData.length, 187 buffer.length); 188 })) 189 .catch(throwNextTick); 190} 191 192assert.throws( 193 () => fs.appendFile(join(tmpdir.path, 'append6.txt'), console.log), 194 { code: 'ERR_INVALID_ARG_TYPE' }); 195