Skip to content

Commit bdc644f

Browse files
Trottrvagg
authored andcommitted
test: remove common.fileExists()
common.fileExists() can be replaced with fs.existsSync(). PR-URL: #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>
1 parent 3989869 commit bdc644f

25 files changed

+37
-52
lines changed

test/common/README.md

-6
Original file line numberDiff line numberDiff line change
@@ -121,12 +121,6 @@ Tests whether `name`, `expected`, and `code` are part of a raised warning. If
121121
an expected warning does not have a code then `common.noWarnCode` can be used
122122
to indicate this.
123123

124-
### fileExists(pathname)
125-
* pathname [&lt;string>]
126-
* return [&lt;boolean>]
127-
128-
Checks if `pathname` exists
129-
130124
### getArrayBufferViews(buf)
131125
* `buf` [&lt;Buffer>]
132126
* return [&lt;ArrayBufferView&#91;&#93;>]

test/common/index.js

+1-10
Original file line numberDiff line numberDiff line change
@@ -477,17 +477,8 @@ exports.hasMultiLocalhost = function hasMultiLocalhost() {
477477
return ret === 0;
478478
};
479479

480-
exports.fileExists = function(pathname) {
481-
try {
482-
fs.accessSync(pathname);
483-
return true;
484-
} catch (err) {
485-
return false;
486-
}
487-
};
488-
489480
exports.skipIfEslintMissing = function() {
490-
if (!exports.fileExists(
481+
if (!fs.existsSync(
491482
path.join(__dirname, '..', '..', 'tools', 'node_modules', 'eslint')
492483
)) {
493484
exports.skip('missing ESLint');

test/common/index.mjs

-2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ const {
3333
mustCallAtLeast,
3434
mustCallAsync,
3535
hasMultiLocalhost,
36-
fileExists,
3736
skipIfEslintMissing,
3837
canCreateSymLink,
3938
getCallSite,
@@ -92,7 +91,6 @@ export {
9291
mustCallAtLeast,
9392
mustCallAsync,
9493
hasMultiLocalhost,
95-
fileExists,
9694
skipIfEslintMissing,
9795
canCreateSymLink,
9896
getCallSite,

test/parallel/test-child-process-spawn-error.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,11 @@
2323
const common = require('../common');
2424
const spawn = require('child_process').spawn;
2525
const assert = require('assert');
26+
const fs = require('fs');
2627

2728
const enoentPath = 'foo123';
2829
const spawnargs = ['bar'];
29-
assert.strictEqual(common.fileExists(enoentPath), false);
30+
assert.strictEqual(fs.existsSync(enoentPath), false);
3031

3132
const enoentChild = spawn(enoentPath, spawnargs);
3233
enoentChild.on('error', common.mustCall(function(err) {

test/parallel/test-fs-existssync-false.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
'use strict';
2-
const common = require('../common');
2+
require('../common');
33
const tmpdir = require('../common/tmpdir');
44

55
// This test ensures that fs.existsSync doesn't incorrectly return false.
@@ -28,7 +28,7 @@ for (let i = 0; i < 50; i++) {
2828
}
2929

3030
// Test if file exists synchronously
31-
assert(common.fileExists(dir), 'Directory is not accessible');
31+
assert(fs.existsSync(dir), 'Directory is not accessible');
3232

3333
// Test if file exists asynchronously
3434
fs.access(dir, function(err) {

test/parallel/test-fs-mkdir-rmdir.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,19 @@ const d = path.join(tmpdir.path, 'dir');
1010
tmpdir.refresh();
1111

1212
// Make sure the directory does not exist
13-
assert(!common.fileExists(d));
13+
assert(!fs.existsSync(d));
1414
// Create the directory now
1515
fs.mkdirSync(d);
1616
// Make sure the directory exists
17-
assert(common.fileExists(d));
17+
assert(fs.existsSync(d));
1818
// Try creating again, it should fail with EEXIST
1919
assert.throws(function() {
2020
fs.mkdirSync(d);
2121
}, /EEXIST: file already exists, mkdir/);
2222
// Remove the directory now
2323
fs.rmdirSync(d);
2424
// Make sure the directory does not exist
25-
assert(!common.fileExists(d));
25+
assert(!fs.existsSync(d));
2626

2727
// Similarly test the Async version
2828
fs.mkdir(d, 0o666, common.mustCall(function(err) {

test/parallel/test-fs-mkdir.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ tmpdir.refresh();
3232

3333
fs.mkdir(pathname, common.mustCall(function(err) {
3434
assert.strictEqual(err, null);
35-
assert.strictEqual(common.fileExists(pathname), true);
35+
assert.strictEqual(fs.existsSync(pathname), true);
3636
}));
3737
}
3838

@@ -41,7 +41,7 @@ tmpdir.refresh();
4141

4242
fs.mkdir(pathname, 0o777, common.mustCall(function(err) {
4343
assert.strictEqual(err, null);
44-
assert.strictEqual(common.fileExists(pathname), true);
44+
assert.strictEqual(fs.existsSync(pathname), true);
4545
}));
4646
}
4747

@@ -50,7 +50,7 @@ tmpdir.refresh();
5050

5151
fs.mkdirSync(pathname);
5252

53-
const exists = common.fileExists(pathname);
53+
const exists = fs.existsSync(pathname);
5454
assert.strictEqual(exists, true);
5555
}
5656

test/parallel/test-fs-mkdtemp.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,16 @@ tmpdir.refresh();
1111
const tmpFolder = fs.mkdtempSync(path.join(tmpdir.path, 'foo.'));
1212

1313
assert.strictEqual(path.basename(tmpFolder).length, 'foo.XXXXXX'.length);
14-
assert(common.fileExists(tmpFolder));
14+
assert(fs.existsSync(tmpFolder));
1515

1616
const utf8 = fs.mkdtempSync(path.join(tmpdir.path, '\u0222abc.'));
1717
assert.strictEqual(Buffer.byteLength(path.basename(utf8)),
1818
Buffer.byteLength('\u0222abc.XXXXXX'));
19-
assert(common.fileExists(utf8));
19+
assert(fs.existsSync(utf8));
2020

2121
function handler(err, folder) {
2222
assert.ifError(err);
23-
assert(common.fileExists(folder));
23+
assert(fs.existsSync(folder));
2424
assert.strictEqual(this, undefined);
2525
}
2626

test/parallel/test-fs-symlink-dir-junction.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ fs.symlink(linkData, linkPath, 'junction', common.mustCall(function(err) {
4747

4848
fs.unlink(linkPath, common.mustCall(function(err) {
4949
assert.ifError(err);
50-
assert(!common.fileExists(linkPath));
51-
assert(common.fileExists(linkData));
50+
assert(!fs.existsSync(linkPath));
51+
assert(fs.existsSync(linkData));
5252
}));
5353
}));
5454
}));

test/parallel/test-require-exceptions.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@
2020
// USE OR OTHER DEALINGS IN THE SOFTWARE.
2121

2222
'use strict';
23-
const common = require('../common');
23+
require('../common');
2424
const assert = require('assert');
25+
const fs = require('fs');
2526
const fixtures = require('../common/fixtures');
2627

2728
// A module with an error in it should throw
@@ -52,5 +53,5 @@ function assertModuleNotFound(path) {
5253
}
5354

5455
function assertExists(fixture) {
55-
assert(common.fileExists(fixtures.path(fixture)));
56+
assert(fs.existsSync(fixtures.path(fixture)));
5657
}

test/parallel/test-trace-events-all.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const proc = cp.spawn(process.execPath,
1919
[ '--trace-events-enabled', '-e', CODE ]);
2020

2121
proc.once('exit', common.mustCall(() => {
22-
assert(common.fileExists(FILE_NAME));
22+
assert(fs.existsSync(FILE_NAME));
2323
fs.readFile(FILE_NAME, common.mustCall((err, data) => {
2424
const traces = JSON.parse(data.toString()).traceEvents;
2525
assert(traces.length > 0);

test/parallel/test-trace-events-api.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ if (isChild) {
130130
proc.once('exit', common.mustCall(() => {
131131
const file = path.join(tmpdir.path, 'node_trace.1.log');
132132

133-
assert(common.fileExists(file));
133+
assert(fs.existsSync(file));
134134
fs.readFile(file, common.mustCall((err, data) => {
135135
const traces = JSON.parse(data.toString()).traceEvents
136136
.filter((trace) => trace.cat !== '__metadata');

test/parallel/test-trace-events-async-hooks.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const proc = cp.spawn(process.execPath,
2121
'-e', CODE ]);
2222

2323
proc.once('exit', common.mustCall(() => {
24-
assert(common.fileExists(FILE_NAME));
24+
assert(fs.existsSync(FILE_NAME));
2525
fs.readFile(FILE_NAME, common.mustCall((err, data) => {
2626
const traces = JSON.parse(data.toString()).traceEvents;
2727
assert(traces.length > 0);

test/parallel/test-trace-events-binding.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ const proc = cp.spawn(process.execPath,
3232
'-e', CODE ]);
3333

3434
proc.once('exit', common.mustCall(() => {
35-
assert(common.fileExists(FILE_NAME));
35+
assert(fs.existsSync(FILE_NAME));
3636
fs.readFile(FILE_NAME, common.mustCall((err, data) => {
3737
const traces = JSON.parse(data.toString()).traceEvents
3838
.filter((trace) => trace.cat !== '__metadata');

test/parallel/test-trace-events-bootstrap.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ if (process.argv[2] === 'child') {
4343
proc.once('exit', common.mustCall(() => {
4444
const file = path.join(tmpdir.path, 'node_trace.1.log');
4545

46-
assert(common.fileExists(file));
46+
assert(fs.existsSync(file));
4747
fs.readFile(file, common.mustCall((err, data) => {
4848
const traces = JSON.parse(data.toString()).traceEvents
4949
.filter((trace) => trace.cat !== '__metadata');

test/parallel/test-trace-events-file-pattern.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ const proc = cp.spawn(process.execPath, [
2525
proc.once('exit', common.mustCall(() => {
2626
const expectedFilename = `${proc.pid}-1-${proc.pid}-1.tracing.log`;
2727

28-
assert(common.fileExists(expectedFilename));
28+
assert(fs.existsSync(expectedFilename));
2929
fs.readFile(expectedFilename, common.mustCall((err, data) => {
3030
const traces = JSON.parse(data.toString()).traceEvents;
3131
assert(traces.length > 0);

test/parallel/test-trace-events-fs-sync.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ for (const tr in tests) {
139139
assert.strictEqual(proc.status, 0, `${tr}:\n${util.inspect(proc)}`);
140140

141141
// Confirm that trace log file is created.
142-
assert(common.fileExists(traceFile));
142+
assert(fs.existsSync(traceFile));
143143
const data = fs.readFileSync(traceFile);
144144
const traces = JSON.parse(data.toString()).traceEvents;
145145
assert(traces.length > 0);

test/parallel/test-trace-events-metadata.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const proc = cp.spawn(process.execPath,
2121
'--title=bar',
2222
'-e', CODE ]);
2323
proc.once('exit', common.mustCall(() => {
24-
assert(common.fileExists(FILE_NAME));
24+
assert(fs.existsSync(FILE_NAME));
2525
fs.readFile(FILE_NAME, common.mustCall((err, data) => {
2626
const traces = JSON.parse(data.toString()).traceEvents
2727
.filter((trace) => trace.cat === '__metadata');

test/parallel/test-trace-events-none.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const proc_no_categories = cp.spawn(
2121
);
2222

2323
proc_no_categories.once('exit', common.mustCall(() => {
24-
assert(common.fileExists(FILE_NAME));
24+
assert(fs.existsSync(FILE_NAME));
2525
// Only __metadata categories should have been emitted.
2626
fs.readFile(FILE_NAME, common.mustCall((err, data) => {
2727
assert.ok(JSON.parse(data.toString()).traceEvents.every(

test/parallel/test-trace-events-perf.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ if (process.argv[2] === 'child') {
5050
proc.once('exit', common.mustCall(() => {
5151
const file = path.join(tmpdir.path, 'node_trace.1.log');
5252

53-
assert(common.fileExists(file));
53+
assert(fs.existsSync(file));
5454
fs.readFile(file, common.mustCall((err, data) => {
5555
const traces = JSON.parse(data.toString()).traceEvents
5656
.filter((trace) => trace.cat !== '__metadata');

test/parallel/test-trace-events-process-exit.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const proc = cp.spawn(process.execPath,
1919
'-e', 'process.exit()' ]);
2020

2121
proc.once('exit', common.mustCall(() => {
22-
assert(common.fileExists(FILE_NAME));
22+
assert(fs.existsSync(FILE_NAME));
2323
fs.readFile(FILE_NAME, common.mustCall((err, data) => {
2424
const traces = JSON.parse(data.toString()).traceEvents;
2525
assert(traces.length > 0);

test/parallel/test-trace-events-v8.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const proc = cp.spawn(process.execPath,
2121
'-e', CODE ]);
2222

2323
proc.once('exit', common.mustCall(() => {
24-
assert(common.fileExists(FILE_NAME));
24+
assert(fs.existsSync(FILE_NAME));
2525
fs.readFile(FILE_NAME, common.mustCall((err, data) => {
2626
const traces = JSON.parse(data.toString()).traceEvents;
2727
assert(traces.length > 0);

test/parallel/test-trace-events-vm.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ if (process.argv[2] === 'child') {
3333
proc.once('exit', common.mustCall(() => {
3434
const file = path.join(tmpdir.path, 'node_trace.1.log');
3535

36-
assert(common.fileExists(file));
36+
assert(fs.existsSync(file));
3737
fs.readFile(file, common.mustCall((err, data) => {
3838
const traces = JSON.parse(data.toString()).traceEvents
3939
.filter((trace) => trace.cat !== '__metadata');

test/parallel/test-trace-events-worker-metadata.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ if (isMainThread) {
1919
'--trace-event-categories', 'node',
2020
'-e', CODE ]);
2121
proc.once('exit', common.mustCall(() => {
22-
assert(common.fileExists(FILE_NAME));
22+
assert(fs.existsSync(FILE_NAME));
2323
fs.readFile(FILE_NAME, common.mustCall((err, data) => {
2424
const traces = JSON.parse(data.toString()).traceEvents;
2525
assert(traces.length > 0);

test/pummel/test-fs-watch-file-slow.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
// USE OR OTHER DEALINGS IN THE SOFTWARE.
2121

2222
'use strict';
23-
const common = require('../common');
23+
require('../common');
2424
const assert = require('assert');
2525
const path = require('path');
2626
const fs = require('fs');
@@ -42,14 +42,14 @@ fs.watchFile(FILENAME, { interval: TIMEOUT - 250 }, function(curr, prev) {
4242
console.log([curr, prev]);
4343
switch (++nevents) {
4444
case 1:
45-
assert.strictEqual(common.fileExists(FILENAME), false);
45+
assert.strictEqual(fs.existsSync(FILENAME), false);
4646
break;
4747
case 2:
4848
case 3:
49-
assert.strictEqual(common.fileExists(FILENAME), true);
49+
assert.strictEqual(fs.existsSync(FILENAME), true);
5050
break;
5151
case 4:
52-
assert.strictEqual(common.fileExists(FILENAME), false);
52+
assert.strictEqual(fs.existsSync(FILENAME), false);
5353
fs.unwatchFile(FILENAME);
5454
break;
5555
default:

0 commit comments

Comments
 (0)