Skip to content

Commit

Permalink
test: remove common.fileExists()
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Trott authored and targos committed Aug 11, 2018
1 parent c96e552 commit 0385e09
Show file tree
Hide file tree
Showing 25 changed files with 37 additions and 52 deletions.
6 changes: 0 additions & 6 deletions test/common/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 [&lt;string>]
* return [&lt;boolean>]

Checks if `pathname` exists

### getArrayBufferViews(buf)
* `buf` [&lt;Buffer>]
* return [&lt;ArrayBufferView&#91;&#93;>]
Expand Down
11 changes: 1 addition & 10 deletions test/common/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -477,17 +477,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');
Expand Down
2 changes: 0 additions & 2 deletions test/common/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ const {
mustCallAtLeast,
mustCallAsync,
hasMultiLocalhost,
fileExists,
skipIfEslintMissing,
canCreateSymLink,
getCallSite,
Expand Down Expand Up @@ -92,7 +91,6 @@ export {
mustCallAtLeast,
mustCallAsync,
hasMultiLocalhost,
fileExists,
skipIfEslintMissing,
canCreateSymLink,
getCallSite,
Expand Down
3 changes: 2 additions & 1 deletion test/parallel/test-child-process-spawn-error.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions test/parallel/test-fs-existssync-false.js
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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) {
Expand Down
6 changes: 3 additions & 3 deletions test/parallel/test-fs-mkdir-rmdir.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,19 @@ 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);
}, /EEXIST: file already exists, mkdir/);
// 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) {
Expand Down
6 changes: 3 additions & 3 deletions test/parallel/test-fs-mkdir.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}));
}

Expand All @@ -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);
}));
}

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

fs.mkdirSync(pathname);

const exists = common.fileExists(pathname);
const exists = fs.existsSync(pathname);
assert.strictEqual(exists, true);
}

Expand Down
6 changes: 3 additions & 3 deletions test/parallel/test-fs-mkdtemp.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
4 changes: 2 additions & 2 deletions test/parallel/test-fs-symlink-dir-junction.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}));
}));
}));
Expand Down
5 changes: 3 additions & 2 deletions test/parallel/test-require-exceptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -52,5 +53,5 @@ function assertModuleNotFound(path) {
}

function assertExists(fixture) {
assert(common.fileExists(fixtures.path(fixture)));
assert(fs.existsSync(fixtures.path(fixture)));
}
2 changes: 1 addition & 1 deletion test/parallel/test-trace-events-all.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-trace-events-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-trace-events-async-hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-trace-events-binding.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-trace-events-bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-trace-events-file-pattern.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-trace-events-fs-sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-trace-events-metadata.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-trace-events-none.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-trace-events-perf.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-trace-events-process-exit.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-trace-events-v8.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-trace-events-vm.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-trace-events-worker-metadata.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
8 changes: 4 additions & 4 deletions test/pummel/test-fs-watch-file-slow.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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:
Expand Down

0 comments on commit 0385e09

Please sign in to comment.