mirror of
https://github.com/nodejs/node.git
synced 2024-11-21 10:59:27 +00:00
74e0ca3f49
PR-URL: https://github.com/nodejs/node/pull/49125 Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Chemi Atlow <chemi@atlow.co.il>
51 lines
1.4 KiB
JavaScript
51 lines
1.4 KiB
JavaScript
'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'); })
|
|
);
|
|
}
|