forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest-fs-readfile-flags.js
More file actions
50 lines (41 loc) · 1.38 KB
/
Copy pathtest-fs-readfile-flags.js
File metadata and controls
50 lines (41 loc) · 1.38 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
49
50
'use strict';
// Test of fs.readFile with different flags.
const common = require('../common');
const fs = require('fs');
const assert = require('assert');
const tmpdir = require('../common/tmpdir');
tmpdir.refresh();
{
const emptyFile = tmpdir.resolve('empty.txt');
fs.closeSync(fs.openSync(emptyFile, 'w'));
fs.readFile(
emptyFile,
// With `a+` the file is created if it does not exist
common.mustNotMutateObjectDeep({ encoding: 'utf8', flag: 'a+' }),
common.mustCall((err, data) => { assert.strictEqual(data, ''); })
);
fs.readFile(
emptyFile,
// Like `a+` but fails if the path exists.
common.mustNotMutateObjectDeep({ encoding: 'utf8', flag: 'ax+' }),
common.mustCall((err, data) => { assert.strictEqual(err.code, 'EEXIST'); })
);
}
{
const willBeCreated = tmpdir.resolve('will-be-created');
fs.readFile(
willBeCreated,
// With `a+` the file is created if it does not exist
common.mustNotMutateObjectDeep({ encoding: 'utf8', flag: 'a+' }),
common.mustCall((err, data) => { assert.strictEqual(data, ''); })
);
}
{
const willNotBeCreated = tmpdir.resolve('will-not-be-created');
fs.readFile(
willNotBeCreated,
// Default flag is `r`. An exception occurs if the file does not exist.
common.mustNotMutateObjectDeep({ encoding: 'utf8' }),
common.mustCall((err, data) => { assert.strictEqual(err.code, 'ENOENT'); })
);
}