• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3const common = require('../common');
4
5// The following tests validate base functionality for the fs.promises
6// FileHandle.appendFile method.
7
8const fs = require('fs');
9const { open } = fs.promises;
10const path = require('path');
11const tmpdir = require('../common/tmpdir');
12const assert = require('assert');
13const tmpDir = tmpdir.path;
14
15tmpdir.refresh();
16
17async function validateAppendBuffer() {
18  const filePath = path.resolve(tmpDir, 'tmp-append-file-buffer.txt');
19  const fileHandle = await open(filePath, 'a');
20  const buffer = Buffer.from('a&Dp'.repeat(100), 'utf8');
21
22  await fileHandle.appendFile(buffer);
23  const appendedFileData = fs.readFileSync(filePath);
24  assert.deepStrictEqual(appendedFileData, buffer);
25
26  await fileHandle.close();
27}
28
29async function validateAppendString() {
30  const filePath = path.resolve(tmpDir, 'tmp-append-file-string.txt');
31  const fileHandle = await open(filePath, 'a');
32  const string = 'x~yz'.repeat(100);
33
34  await fileHandle.appendFile(string);
35  const stringAsBuffer = Buffer.from(string, 'utf8');
36  const appendedFileData = fs.readFileSync(filePath);
37  assert.deepStrictEqual(appendedFileData, stringAsBuffer);
38
39  await fileHandle.close();
40}
41
42validateAppendBuffer()
43  .then(validateAppendString)
44  .then(common.mustCall());
45