From c147d67e236e0506ba4ca72803dd34378de3cccb Mon Sep 17 00:00:00 2001 From: Daniil Demidovich Date: Tue, 22 Sep 2020 13:44:58 +0300 Subject: [PATCH 1/3] lib: fix readFile flag option typo --- lib/fs.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/fs.js b/lib/fs.js index c570a8c9f4fc2e..8e26339e215c89 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -322,7 +322,7 @@ function readFile(path, options, callback) { return; } - const flagsNumber = stringToFlags(options.flags); + const flagsNumber = stringToFlags(options.flag); path = getValidatedPath(path); const req = new FSReqCallback(); From e5834a75e1078b51edd91765c2da1ca7c9c474a8 Mon Sep 17 00:00:00 2001 From: Daniil Demidovich Date: Wed, 23 Sep 2020 10:29:05 +0300 Subject: [PATCH 2/3] test: add tests for readFile flags --- test/parallel/test-fs-readfile-flags.js | 70 +++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 test/parallel/test-fs-readfile-flags.js diff --git a/test/parallel/test-fs-readfile-flags.js b/test/parallel/test-fs-readfile-flags.js new file mode 100644 index 00000000000000..eae790edc3ff5c --- /dev/null +++ b/test/parallel/test-fs-readfile-flags.js @@ -0,0 +1,70 @@ +// Copyright Joyent, Inc. and other Node contributors. +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to permit +// persons to whom the Software is furnished to do so, subject to the +// following conditions: +// +// The above copyright notice and this permission notice shall be included +// in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN +// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, +// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR +// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE +// USE OR OTHER DEALINGS IN THE SOFTWARE. + +'use strict'; + +// Test of fs.readFile with different flags. +const common = require('../common'); +const fs = require('fs'); +const assert = require('assert'); +const fixtures = require('../common/fixtures'); + +const fn = fixtures.path('empty.txt'); +const nonexistingFn = fixtures.path('nonexistingfile.txt'); +const nonexistingFn2 = fixtures.path('nonexistingfile2.txt'); + +fs.readFile( + fn, + // With `a+` the file is created if it does not exist + { encoding: 'utf8', flag: 'a+' }, + common.mustCall((err, data) => { + assert.strictEqual(data, ''); + }) +); + +fs.readFile( + fn, + // Like `a+` but fails if the path exists. + { encoding: 'utf8', flag: 'ax+' }, + common.mustCall((err, data) => { + assert.strictEqual(err.code, 'EEXIST'); + }) +); + +fs.readFile( + nonexistingFn, + // With `a+` the file is created if it does not exist + { encoding: 'utf8', flag: 'a+' }, + common.mustCall((err, data) => { + assert.strictEqual(data, ''); + + fs.unlinkSync(nonexistingFn); + }) +); + +fs.readFile( + nonexistingFn2, + // Default flag is `r`. An exception occurs if the file does not exist. + { encoding: 'utf8' }, + common.mustCall((err, data) => { + assert.strictEqual(err.code, 'ENOENT'); + }) +); From c1a52d37aedc43c2de37f8ce8aaebbccd427544c Mon Sep 17 00:00:00 2001 From: Danila Date: Thu, 24 Sep 2020 15:13:45 +0300 Subject: [PATCH 3/3] Update test/parallel/test-fs-readfile-flags.js Co-authored-by: Rich Trott --- test/parallel/test-fs-readfile-flags.js | 109 ++++++++++-------------- 1 file changed, 45 insertions(+), 64 deletions(-) diff --git a/test/parallel/test-fs-readfile-flags.js b/test/parallel/test-fs-readfile-flags.js index eae790edc3ff5c..2aceee25506419 100644 --- a/test/parallel/test-fs-readfile-flags.js +++ b/test/parallel/test-fs-readfile-flags.js @@ -1,70 +1,51 @@ -// Copyright Joyent, Inc. and other Node contributors. -// -// Permission is hereby granted, free of charge, to any person obtaining a -// copy of this software and associated documentation files (the -// "Software"), to deal in the Software without restriction, including -// without limitation the rights to use, copy, modify, merge, publish, -// distribute, sublicense, and/or sell copies of the Software, and to permit -// persons to whom the Software is furnished to do so, subject to the -// following conditions: -// -// The above copyright notice and this permission notice shall be included -// in all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS -// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN -// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, -// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR -// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE -// USE OR OTHER DEALINGS IN THE SOFTWARE. - 'use strict'; // Test of fs.readFile with different flags. const common = require('../common'); const fs = require('fs'); const assert = require('assert'); -const fixtures = require('../common/fixtures'); - -const fn = fixtures.path('empty.txt'); -const nonexistingFn = fixtures.path('nonexistingfile.txt'); -const nonexistingFn2 = fixtures.path('nonexistingfile2.txt'); - -fs.readFile( - fn, - // With `a+` the file is created if it does not exist - { encoding: 'utf8', flag: 'a+' }, - common.mustCall((err, data) => { - assert.strictEqual(data, ''); - }) -); - -fs.readFile( - fn, - // Like `a+` but fails if the path exists. - { encoding: 'utf8', flag: 'ax+' }, - common.mustCall((err, data) => { - assert.strictEqual(err.code, 'EEXIST'); - }) -); - -fs.readFile( - nonexistingFn, - // With `a+` the file is created if it does not exist - { encoding: 'utf8', flag: 'a+' }, - common.mustCall((err, data) => { - assert.strictEqual(data, ''); - - fs.unlinkSync(nonexistingFn); - }) -); - -fs.readFile( - nonexistingFn2, - // Default flag is `r`. An exception occurs if the file does not exist. - { encoding: 'utf8' }, - common.mustCall((err, data) => { - assert.strictEqual(err.code, 'ENOENT'); - }) -); +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'); }) + ); +}