• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 n = 220;
33const s = '南越国是前203年至前111年存在于岭南地区的一个国家,国都位于番禺,疆域包括今天中国的广东、' +
34          '广西两省区的大部份地区,福建省、湖南、贵州、云南的一小部份地区和越南的北部。' +
35          '南越国是秦朝灭亡后,由南海郡尉赵佗于前203年起兵兼并桂林郡和象郡后建立。' +
36          '前196年和前179年,南越国曾先后两次名义上臣属于西汉,成为西汉的“外臣”。前112年,' +
37          '南越国末代君主赵建德与西汉发生战争,被汉武帝于前111年所灭。南越国共存在93年,' +
38          '历经五代君主。南越国是岭南地区的第一个有记载的政权国家,采用封建制和郡县制并存的制度,' +
39          '它的建立保证了秦末乱世岭南地区社会秩序的稳定,有效的改善了岭南地区落后的政治、##济现状。\n';
40
41tmpdir.refresh();
42
43const throwNextTick = (e) => { process.nextTick(() => { throw e; }); };
44
45// Test that empty file will be created and have content added (callback API).
46{
47  const filename = join(tmpdir.path, 'append.txt');
48
49  fs.appendFile(filename, s, common.mustCall(function(e) {
50    assert.ifError(e);
51
52    fs.readFile(filename, common.mustCall(function(e, buffer) {
53      assert.ifError(e);
54      assert.strictEqual(Buffer.byteLength(s), buffer.length);
55    }));
56  }));
57}
58
59// Test that empty file will be created and have content added (promise API).
60{
61  const filename = join(tmpdir.path, 'append-promise.txt');
62
63  fs.promises.appendFile(filename, s)
64    .then(common.mustCall(() => fs.promises.readFile(filename)))
65    .then((buffer) => {
66      assert.strictEqual(Buffer.byteLength(s), buffer.length);
67    })
68    .catch(throwNextTick);
69}
70
71// Test that appends data to a non-empty file (callback API).
72{
73  const filename = join(tmpdir.path, 'append-non-empty.txt');
74  fs.writeFileSync(filename, currentFileData);
75
76  fs.appendFile(filename, s, common.mustCall(function(e) {
77    assert.ifError(e);
78
79    fs.readFile(filename, common.mustCall(function(e, buffer) {
80      assert.ifError(e);
81      assert.strictEqual(Buffer.byteLength(s) + currentFileData.length,
82                         buffer.length);
83    }));
84  }));
85}
86
87// Test that appends data to a non-empty file (promise API).
88{
89  const filename = join(tmpdir.path, 'append-non-empty-promise.txt');
90  fs.writeFileSync(filename, currentFileData);
91
92  fs.promises.appendFile(filename, s)
93    .then(common.mustCall(() => fs.promises.readFile(filename)))
94    .then((buffer) => {
95      assert.strictEqual(Buffer.byteLength(s) + currentFileData.length,
96                         buffer.length);
97    })
98    .catch(throwNextTick);
99}
100
101// Test that appendFile accepts buffers (callback API).
102{
103  const filename = join(tmpdir.path, 'append-buffer.txt');
104  fs.writeFileSync(filename, currentFileData);
105
106  const buf = Buffer.from(s, 'utf8');
107
108  fs.appendFile(filename, buf, common.mustCall((e) => {
109    assert.ifError(e);
110
111    fs.readFile(filename, common.mustCall((e, buffer) => {
112      assert.ifError(e);
113      assert.strictEqual(buf.length + currentFileData.length, buffer.length);
114    }));
115  }));
116}
117
118// Test that appendFile accepts buffers (promises API).
119{
120  const filename = join(tmpdir.path, 'append-buffer-promises.txt');
121  fs.writeFileSync(filename, currentFileData);
122
123  const buf = Buffer.from(s, 'utf8');
124
125  fs.promises.appendFile(filename, buf)
126    .then(common.mustCall(() => fs.promises.readFile(filename)))
127    .then((buffer) => {
128      assert.strictEqual(buf.length + currentFileData.length, buffer.length);
129    })
130    .catch(throwNextTick);
131}
132
133// Test that appendFile accepts numbers (callback API).
134{
135  const filename = join(tmpdir.path, 'append-numbers.txt');
136  fs.writeFileSync(filename, currentFileData);
137
138  const m = 0o600;
139  fs.appendFile(filename, n, { mode: m }, common.mustCall((e) => {
140    assert.ifError(e);
141
142    // Windows permissions aren't Unix.
143    if (!common.isWindows) {
144      const st = fs.statSync(filename);
145      assert.strictEqual(st.mode & 0o700, m);
146    }
147
148    fs.readFile(filename, common.mustCall((e, buffer) => {
149      assert.ifError(e);
150      assert.strictEqual(Buffer.byteLength(String(n)) + currentFileData.length,
151                         buffer.length);
152    }));
153  }));
154}
155
156// Test that appendFile accepts numbers (promises API).
157{
158  const filename = join(tmpdir.path, 'append-numbers-promises.txt');
159  fs.writeFileSync(filename, currentFileData);
160
161  const m = 0o600;
162  fs.promises.appendFile(filename, n, { mode: m })
163    .then(common.mustCall(() => {
164      // Windows permissions aren't Unix.
165      if (!common.isWindows) {
166        const st = fs.statSync(filename);
167        assert.strictEqual(st.mode & 0o700, m);
168      }
169
170      return fs.promises.readFile(filename);
171    }))
172    .then((buffer) => {
173      assert.strictEqual(Buffer.byteLength(String(n)) + currentFileData.length,
174                         buffer.length);
175    })
176    .catch(throwNextTick);
177}
178
179// Test that appendFile accepts file descriptors (callback API).
180{
181  const filename = join(tmpdir.path, 'append-descriptors.txt');
182  fs.writeFileSync(filename, currentFileData);
183
184  fs.open(filename, 'a+', common.mustCall((e, fd) => {
185    assert.ifError(e);
186
187    fs.appendFile(fd, s, common.mustCall((e) => {
188      assert.ifError(e);
189
190      fs.close(fd, common.mustCall((e) => {
191        assert.ifError(e);
192
193        fs.readFile(filename, common.mustCall((e, buffer) => {
194          assert.ifError(e);
195          assert.strictEqual(Buffer.byteLength(s) + currentFileData.length,
196                             buffer.length);
197        }));
198      }));
199    }));
200  }));
201}
202
203// Test that appendFile accepts file descriptors (promises API).
204{
205  const filename = join(tmpdir.path, 'append-descriptors-promises.txt');
206  fs.writeFileSync(filename, currentFileData);
207
208  let fd;
209  fs.promises.open(filename, 'a+')
210    .then(common.mustCall((fileDescriptor) => {
211      fd = fileDescriptor;
212      return fs.promises.appendFile(fd, s);
213    }))
214    .then(common.mustCall(() => fd.close()))
215    .then(common.mustCall(() => fs.promises.readFile(filename)))
216    .then(common.mustCall((buffer) => {
217      assert.strictEqual(Buffer.byteLength(s) + currentFileData.length,
218                         buffer.length);
219    }))
220    .catch(throwNextTick);
221}
222
223assert.throws(
224  () => fs.appendFile(join(tmpdir.path, 'append6.txt'), console.log),
225  { code: 'ERR_INVALID_CALLBACK' });
226