test: remove common.fileExists()
common.fileExists() can be replaced with fs.existsSync(). PR-URL: https://github.com/nodejs/node/pull/22151 Reviewed-By: Richard Lau <riclau@uk.ibm.com> Reviewed-By: Weijia Wang <starkwang@126.com> Reviewed-By: Jon Moss <me@jonathanmoss.me> Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Сковорода Никита Андреевич <chalkerx@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com> Reviewed-By: Tobias Nießen <tniessen@tnie.de> Reviewed-By: Franziska Hinkelmann <franziska.hinkelmann@gmail.com>
This commit is contained in:
parent
6ad12d47f5
commit
41ae423718
@ -121,12 +121,6 @@ Tests whether `name`, `expected`, and `code` are part of a raised warning. If
|
||||
an expected warning does not have a code then `common.noWarnCode` can be used
|
||||
to indicate this.
|
||||
|
||||
### fileExists(pathname)
|
||||
* pathname [<string>]
|
||||
* return [<boolean>]
|
||||
|
||||
Checks if `pathname` exists
|
||||
|
||||
### getArrayBufferViews(buf)
|
||||
* `buf` [<Buffer>]
|
||||
* return [<ArrayBufferView[]>]
|
||||
|
@ -476,17 +476,8 @@ exports.hasMultiLocalhost = function hasMultiLocalhost() {
|
||||
return ret === 0;
|
||||
};
|
||||
|
||||
exports.fileExists = function(pathname) {
|
||||
try {
|
||||
fs.accessSync(pathname);
|
||||
return true;
|
||||
} catch (err) {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
exports.skipIfEslintMissing = function() {
|
||||
if (!exports.fileExists(
|
||||
if (!fs.existsSync(
|
||||
path.join(__dirname, '..', '..', 'tools', 'node_modules', 'eslint')
|
||||
)) {
|
||||
exports.skip('missing ESLint');
|
||||
|
@ -33,7 +33,6 @@ const {
|
||||
mustCallAtLeast,
|
||||
mustCallAsync,
|
||||
hasMultiLocalhost,
|
||||
fileExists,
|
||||
skipIfEslintMissing,
|
||||
canCreateSymLink,
|
||||
getCallSite,
|
||||
@ -92,7 +91,6 @@ export {
|
||||
mustCallAtLeast,
|
||||
mustCallAsync,
|
||||
hasMultiLocalhost,
|
||||
fileExists,
|
||||
skipIfEslintMissing,
|
||||
canCreateSymLink,
|
||||
getCallSite,
|
||||
|
@ -23,10 +23,11 @@
|
||||
const common = require('../common');
|
||||
const spawn = require('child_process').spawn;
|
||||
const assert = require('assert');
|
||||
const fs = require('fs');
|
||||
|
||||
const enoentPath = 'foo123';
|
||||
const spawnargs = ['bar'];
|
||||
assert.strictEqual(common.fileExists(enoentPath), false);
|
||||
assert.strictEqual(fs.existsSync(enoentPath), false);
|
||||
|
||||
const enoentChild = spawn(enoentPath, spawnargs);
|
||||
enoentChild.on('error', common.mustCall(function(err) {
|
||||
|
@ -1,5 +1,5 @@
|
||||
'use strict';
|
||||
const common = require('../common');
|
||||
require('../common');
|
||||
const tmpdir = require('../common/tmpdir');
|
||||
|
||||
// This test ensures that fs.existsSync doesn't incorrectly return false.
|
||||
@ -28,7 +28,7 @@ for (let i = 0; i < 50; i++) {
|
||||
}
|
||||
|
||||
// Test if file exists synchronously
|
||||
assert(common.fileExists(dir), 'Directory is not accessible');
|
||||
assert(fs.existsSync(dir), 'Directory is not accessible');
|
||||
|
||||
// Test if file exists asynchronously
|
||||
fs.access(dir, function(err) {
|
||||
|
@ -10,11 +10,11 @@ const d = path.join(tmpdir.path, 'dir');
|
||||
tmpdir.refresh();
|
||||
|
||||
// Make sure the directory does not exist
|
||||
assert(!common.fileExists(d));
|
||||
assert(!fs.existsSync(d));
|
||||
// Create the directory now
|
||||
fs.mkdirSync(d);
|
||||
// Make sure the directory exists
|
||||
assert(common.fileExists(d));
|
||||
assert(fs.existsSync(d));
|
||||
// Try creating again, it should fail with EEXIST
|
||||
assert.throws(function() {
|
||||
fs.mkdirSync(d);
|
||||
@ -22,7 +22,7 @@ assert.throws(function() {
|
||||
// Remove the directory now
|
||||
fs.rmdirSync(d);
|
||||
// Make sure the directory does not exist
|
||||
assert(!common.fileExists(d));
|
||||
assert(!fs.existsSync(d));
|
||||
|
||||
// Similarly test the Async version
|
||||
fs.mkdir(d, 0o666, common.mustCall(function(err) {
|
||||
|
@ -32,7 +32,7 @@ tmpdir.refresh();
|
||||
|
||||
fs.mkdir(pathname, common.mustCall(function(err) {
|
||||
assert.strictEqual(err, null);
|
||||
assert.strictEqual(common.fileExists(pathname), true);
|
||||
assert.strictEqual(fs.existsSync(pathname), true);
|
||||
}));
|
||||
}
|
||||
|
||||
@ -41,7 +41,7 @@ tmpdir.refresh();
|
||||
|
||||
fs.mkdir(pathname, 0o777, common.mustCall(function(err) {
|
||||
assert.strictEqual(err, null);
|
||||
assert.strictEqual(common.fileExists(pathname), true);
|
||||
assert.strictEqual(fs.existsSync(pathname), true);
|
||||
}));
|
||||
}
|
||||
|
||||
@ -50,7 +50,7 @@ tmpdir.refresh();
|
||||
|
||||
fs.mkdirSync(pathname);
|
||||
|
||||
const exists = common.fileExists(pathname);
|
||||
const exists = fs.existsSync(pathname);
|
||||
assert.strictEqual(exists, true);
|
||||
}
|
||||
|
||||
|
@ -11,16 +11,16 @@ tmpdir.refresh();
|
||||
const tmpFolder = fs.mkdtempSync(path.join(tmpdir.path, 'foo.'));
|
||||
|
||||
assert.strictEqual(path.basename(tmpFolder).length, 'foo.XXXXXX'.length);
|
||||
assert(common.fileExists(tmpFolder));
|
||||
assert(fs.existsSync(tmpFolder));
|
||||
|
||||
const utf8 = fs.mkdtempSync(path.join(tmpdir.path, '\u0222abc.'));
|
||||
assert.strictEqual(Buffer.byteLength(path.basename(utf8)),
|
||||
Buffer.byteLength('\u0222abc.XXXXXX'));
|
||||
assert(common.fileExists(utf8));
|
||||
assert(fs.existsSync(utf8));
|
||||
|
||||
function handler(err, folder) {
|
||||
assert.ifError(err);
|
||||
assert(common.fileExists(folder));
|
||||
assert(fs.existsSync(folder));
|
||||
assert.strictEqual(this, undefined);
|
||||
}
|
||||
|
||||
|
@ -47,8 +47,8 @@ fs.symlink(linkData, linkPath, 'junction', common.mustCall(function(err) {
|
||||
|
||||
fs.unlink(linkPath, common.mustCall(function(err) {
|
||||
assert.ifError(err);
|
||||
assert(!common.fileExists(linkPath));
|
||||
assert(common.fileExists(linkData));
|
||||
assert(!fs.existsSync(linkPath));
|
||||
assert(fs.existsSync(linkData));
|
||||
}));
|
||||
}));
|
||||
}));
|
||||
|
@ -20,8 +20,9 @@
|
||||
// USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
'use strict';
|
||||
const common = require('../common');
|
||||
require('../common');
|
||||
const assert = require('assert');
|
||||
const fs = require('fs');
|
||||
const fixtures = require('../common/fixtures');
|
||||
|
||||
// A module with an error in it should throw
|
||||
@ -52,5 +53,5 @@ function assertModuleNotFound(path) {
|
||||
}
|
||||
|
||||
function assertExists(fixture) {
|
||||
assert(common.fileExists(fixtures.path(fixture)));
|
||||
assert(fs.existsSync(fixtures.path(fixture)));
|
||||
}
|
||||
|
@ -19,7 +19,7 @@ const proc = cp.spawn(process.execPath,
|
||||
[ '--trace-events-enabled', '-e', CODE ]);
|
||||
|
||||
proc.once('exit', common.mustCall(() => {
|
||||
assert(common.fileExists(FILE_NAME));
|
||||
assert(fs.existsSync(FILE_NAME));
|
||||
fs.readFile(FILE_NAME, common.mustCall((err, data) => {
|
||||
const traces = JSON.parse(data.toString()).traceEvents;
|
||||
assert(traces.length > 0);
|
||||
|
@ -130,7 +130,7 @@ if (isChild) {
|
||||
proc.once('exit', common.mustCall(() => {
|
||||
const file = path.join(tmpdir.path, 'node_trace.1.log');
|
||||
|
||||
assert(common.fileExists(file));
|
||||
assert(fs.existsSync(file));
|
||||
fs.readFile(file, common.mustCall((err, data) => {
|
||||
const traces = JSON.parse(data.toString()).traceEvents
|
||||
.filter((trace) => trace.cat !== '__metadata');
|
||||
|
@ -21,7 +21,7 @@ const proc = cp.spawn(process.execPath,
|
||||
'-e', CODE ]);
|
||||
|
||||
proc.once('exit', common.mustCall(() => {
|
||||
assert(common.fileExists(FILE_NAME));
|
||||
assert(fs.existsSync(FILE_NAME));
|
||||
fs.readFile(FILE_NAME, common.mustCall((err, data) => {
|
||||
const traces = JSON.parse(data.toString()).traceEvents;
|
||||
assert(traces.length > 0);
|
||||
|
@ -32,7 +32,7 @@ const proc = cp.spawn(process.execPath,
|
||||
'-e', CODE ]);
|
||||
|
||||
proc.once('exit', common.mustCall(() => {
|
||||
assert(common.fileExists(FILE_NAME));
|
||||
assert(fs.existsSync(FILE_NAME));
|
||||
fs.readFile(FILE_NAME, common.mustCall((err, data) => {
|
||||
const traces = JSON.parse(data.toString()).traceEvents
|
||||
.filter((trace) => trace.cat !== '__metadata');
|
||||
|
@ -43,7 +43,7 @@ if (process.argv[2] === 'child') {
|
||||
proc.once('exit', common.mustCall(() => {
|
||||
const file = path.join(tmpdir.path, 'node_trace.1.log');
|
||||
|
||||
assert(common.fileExists(file));
|
||||
assert(fs.existsSync(file));
|
||||
fs.readFile(file, common.mustCall((err, data) => {
|
||||
const traces = JSON.parse(data.toString()).traceEvents
|
||||
.filter((trace) => trace.cat !== '__metadata');
|
||||
|
@ -25,7 +25,7 @@ const proc = cp.spawn(process.execPath, [
|
||||
proc.once('exit', common.mustCall(() => {
|
||||
const expectedFilename = `${proc.pid}-1-${proc.pid}-1.tracing.log`;
|
||||
|
||||
assert(common.fileExists(expectedFilename));
|
||||
assert(fs.existsSync(expectedFilename));
|
||||
fs.readFile(expectedFilename, common.mustCall((err, data) => {
|
||||
const traces = JSON.parse(data.toString()).traceEvents;
|
||||
assert(traces.length > 0);
|
||||
|
@ -139,7 +139,7 @@ for (const tr in tests) {
|
||||
assert.strictEqual(proc.status, 0, `${tr}:\n${util.inspect(proc)}`);
|
||||
|
||||
// Confirm that trace log file is created.
|
||||
assert(common.fileExists(traceFile));
|
||||
assert(fs.existsSync(traceFile));
|
||||
const data = fs.readFileSync(traceFile);
|
||||
const traces = JSON.parse(data.toString()).traceEvents;
|
||||
assert(traces.length > 0);
|
||||
|
@ -21,7 +21,7 @@ const proc = cp.spawn(process.execPath,
|
||||
'--title=bar',
|
||||
'-e', CODE ]);
|
||||
proc.once('exit', common.mustCall(() => {
|
||||
assert(common.fileExists(FILE_NAME));
|
||||
assert(fs.existsSync(FILE_NAME));
|
||||
fs.readFile(FILE_NAME, common.mustCall((err, data) => {
|
||||
const traces = JSON.parse(data.toString()).traceEvents
|
||||
.filter((trace) => trace.cat === '__metadata');
|
||||
|
@ -21,7 +21,7 @@ const proc_no_categories = cp.spawn(
|
||||
);
|
||||
|
||||
proc_no_categories.once('exit', common.mustCall(() => {
|
||||
assert(common.fileExists(FILE_NAME));
|
||||
assert(fs.existsSync(FILE_NAME));
|
||||
// Only __metadata categories should have been emitted.
|
||||
fs.readFile(FILE_NAME, common.mustCall((err, data) => {
|
||||
assert.ok(JSON.parse(data.toString()).traceEvents.every(
|
||||
|
@ -50,7 +50,7 @@ if (process.argv[2] === 'child') {
|
||||
proc.once('exit', common.mustCall(() => {
|
||||
const file = path.join(tmpdir.path, 'node_trace.1.log');
|
||||
|
||||
assert(common.fileExists(file));
|
||||
assert(fs.existsSync(file));
|
||||
fs.readFile(file, common.mustCall((err, data) => {
|
||||
const traces = JSON.parse(data.toString()).traceEvents
|
||||
.filter((trace) => trace.cat !== '__metadata');
|
||||
|
@ -19,7 +19,7 @@ const proc = cp.spawn(process.execPath,
|
||||
'-e', 'process.exit()' ]);
|
||||
|
||||
proc.once('exit', common.mustCall(() => {
|
||||
assert(common.fileExists(FILE_NAME));
|
||||
assert(fs.existsSync(FILE_NAME));
|
||||
fs.readFile(FILE_NAME, common.mustCall((err, data) => {
|
||||
const traces = JSON.parse(data.toString()).traceEvents;
|
||||
assert(traces.length > 0);
|
||||
|
@ -21,7 +21,7 @@ const proc = cp.spawn(process.execPath,
|
||||
'-e', CODE ]);
|
||||
|
||||
proc.once('exit', common.mustCall(() => {
|
||||
assert(common.fileExists(FILE_NAME));
|
||||
assert(fs.existsSync(FILE_NAME));
|
||||
fs.readFile(FILE_NAME, common.mustCall((err, data) => {
|
||||
const traces = JSON.parse(data.toString()).traceEvents;
|
||||
assert(traces.length > 0);
|
||||
|
@ -33,7 +33,7 @@ if (process.argv[2] === 'child') {
|
||||
proc.once('exit', common.mustCall(() => {
|
||||
const file = path.join(tmpdir.path, 'node_trace.1.log');
|
||||
|
||||
assert(common.fileExists(file));
|
||||
assert(fs.existsSync(file));
|
||||
fs.readFile(file, common.mustCall((err, data) => {
|
||||
const traces = JSON.parse(data.toString()).traceEvents
|
||||
.filter((trace) => trace.cat !== '__metadata');
|
||||
|
@ -19,7 +19,7 @@ if (isMainThread) {
|
||||
'--trace-event-categories', 'node',
|
||||
'-e', CODE ]);
|
||||
proc.once('exit', common.mustCall(() => {
|
||||
assert(common.fileExists(FILE_NAME));
|
||||
assert(fs.existsSync(FILE_NAME));
|
||||
fs.readFile(FILE_NAME, common.mustCall((err, data) => {
|
||||
const traces = JSON.parse(data.toString()).traceEvents;
|
||||
assert(traces.length > 0);
|
||||
|
@ -20,7 +20,7 @@
|
||||
// USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
'use strict';
|
||||
const common = require('../common');
|
||||
require('../common');
|
||||
const assert = require('assert');
|
||||
const path = require('path');
|
||||
const fs = require('fs');
|
||||
@ -42,14 +42,14 @@ fs.watchFile(FILENAME, { interval: TIMEOUT - 250 }, function(curr, prev) {
|
||||
console.log([curr, prev]);
|
||||
switch (++nevents) {
|
||||
case 1:
|
||||
assert.strictEqual(common.fileExists(FILENAME), false);
|
||||
assert.strictEqual(fs.existsSync(FILENAME), false);
|
||||
break;
|
||||
case 2:
|
||||
case 3:
|
||||
assert.strictEqual(common.fileExists(FILENAME), true);
|
||||
assert.strictEqual(fs.existsSync(FILENAME), true);
|
||||
break;
|
||||
case 4:
|
||||
assert.strictEqual(common.fileExists(FILENAME), false);
|
||||
assert.strictEqual(fs.existsSync(FILENAME), false);
|
||||
fs.unwatchFile(FILENAME);
|
||||
break;
|
||||
default:
|
||||
|
Loading…
x
Reference in New Issue
Block a user