forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-fs-promises-file-handle-writeFile-after-truncate.js
More file actions
48 lines (42 loc) · 1.77 KB
/
Copy pathtest-fs-promises-file-handle-writeFile-after-truncate.js
File metadata and controls
48 lines (42 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
'use strict';
// This tests if the writeFile automatically adjusts the file offset even if
// the file is truncated.
const common = require('../common');
const fsPromises = require('fs').promises;
const path = require('path');
const assert = require('assert');
const tmpdir = require('../common/tmpdir');
const tmpDir = tmpdir.path;
const Data = 'FS Promises';
tmpdir.refresh();
(async function() {
assert(!/A/.test(Data));
const fileName = path.resolve(tmpDir, 'temp.txt');
const fileHandle = await fsPromises.open(fileName, 'w');
await fileHandle.writeFile('A'.repeat(Data.length * 2));
await fileHandle.truncate();
await fileHandle.writeFile(Data);
await fileHandle.close();
assert.deepStrictEqual(await fsPromises.readFile(fileName, 'UTF-8'), Data);
})().then(common.mustCall());
(async function() {
// In this case there is no truncate call, but still the contents of the file
// should be truncated before the new string is written.
assert(!/A/.test(Data));
const fileName = path.resolve(tmpDir, 'temp.txt');
const fileHandle = await fsPromises.open(fileName, 'w');
await fileHandle.writeFile('A'.repeat(Data.length * 2));
await fileHandle.writeFile(Data);
await fileHandle.close();
assert.deepStrictEqual(await fsPromises.readFile(fileName, 'UTF-8'), Data);
})().then(common.mustCall());
(async function() {
// This tests appendFile as well, as appendFile internally uses writeFile
const fileName = path.resolve(tmpDir, 'temp-1.txt');
const fileHandle = await fsPromises.open(fileName, 'w');
await fileHandle.writeFile('A'.repeat(Data.length * 2));
await fileHandle.truncate();
await fileHandle.appendFile(Data);
await fileHandle.close();
assert.deepStrictEqual(await fsPromises.readFile(fileName, 'UTF-8'), Data);
})().then(common.mustCall());