-
Notifications
You must be signed in to change notification settings - Fork 30k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PR-URL: #35292 Reviewed-By: Andrey Pechkurov <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Masashi Hirano <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: Zeyu Yang <[email protected]> Reviewed-By: Gerhard Stöbich <[email protected]>
- Loading branch information
1 parent
9288f9d
commit cd0b136
Showing
2 changed files
with
52 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
'use strict'; | ||
|
||
// Test of fs.readFile with different flags. | ||
const common = require('../common'); | ||
const fs = require('fs'); | ||
const assert = require('assert'); | ||
const path = require('path'); | ||
const tmpdir = require('../common/tmpdir'); | ||
|
||
tmpdir.refresh(); | ||
|
||
{ | ||
const emptyFile = path.join(tmpdir.path, 'empty.txt'); | ||
fs.closeSync(fs.openSync(emptyFile, 'w')); | ||
|
||
fs.readFile( | ||
emptyFile, | ||
// With `a+` the file is created if it does not exist | ||
{ encoding: 'utf8', flag: 'a+' }, | ||
common.mustCall((err, data) => { assert.strictEqual(data, ''); }) | ||
); | ||
|
||
fs.readFile( | ||
emptyFile, | ||
// Like `a+` but fails if the path exists. | ||
{ encoding: 'utf8', flag: 'ax+' }, | ||
common.mustCall((err, data) => { assert.strictEqual(err.code, 'EEXIST'); }) | ||
); | ||
} | ||
|
||
{ | ||
const willBeCreated = path.join(tmpdir.path, 'will-be-created'); | ||
|
||
fs.readFile( | ||
willBeCreated, | ||
// With `a+` the file is created if it does not exist | ||
{ encoding: 'utf8', flag: 'a+' }, | ||
common.mustCall((err, data) => { assert.strictEqual(data, ''); }) | ||
); | ||
} | ||
|
||
{ | ||
const willNotBeCreated = path.join(tmpdir.path, 'will-not-be-created'); | ||
|
||
fs.readFile( | ||
willNotBeCreated, | ||
// Default flag is `r`. An exception occurs if the file does not exist. | ||
{ encoding: 'utf8' }, | ||
common.mustCall((err, data) => { assert.strictEqual(err.code, 'ENOENT'); }) | ||
); | ||
} |