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 <[email protected]> Reviewed-By: Weijia Wang <[email protected]> Reviewed-By: Jon Moss <[email protected]> Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Сковорода Никита Андреевич <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Trivikram Kamat <[email protected]> Reviewed-By: Tobias Nießen <[email protected]> Reviewed-By: Franziska Hinkelmann <[email protected]>
1 parent 3989869 commit bdc644f

25 files changed

+37
-52
lines changed

test/common/README.md

Lines changed: 0 additions & 6 deletions
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

Lines changed: 1 addition & 10 deletions
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

Lines changed: 0 additions & 2 deletions
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

Lines changed: 2 additions & 1 deletion
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 3 additions & 3 deletions
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

Lines changed: 3 additions & 3 deletions
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

Lines changed: 3 additions & 3 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 3 additions & 2 deletions
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
}

0 commit comments

Comments
 (0)