From 0f2140b01bfb0c69db11d3d4cfe8980dbe7cc677 Mon Sep 17 00:00:00 2001 From: Rich Trott Date: Mon, 6 Aug 2018 06:47:11 -0700 Subject: [PATCH 1/2] test: remove common.fileExists() common.fileExists() can be replaced with fs.existsSync(). --- test/common/README.md | 6 ------ test/common/index.js | 11 +---------- test/parallel/test-child-process-spawn-error.js | 3 ++- test/parallel/test-fs-existssync-false.js | 4 ++-- test/parallel/test-fs-mkdir-rmdir.js | 6 +++--- test/parallel/test-fs-mkdir.js | 6 +++--- test/parallel/test-fs-mkdtemp.js | 6 +++--- test/parallel/test-fs-symlink-dir-junction.js | 4 ++-- test/parallel/test-require-exceptions.js | 5 +++-- test/parallel/test-trace-events-all.js | 2 +- test/parallel/test-trace-events-api.js | 2 +- test/parallel/test-trace-events-async-hooks.js | 2 +- test/parallel/test-trace-events-binding.js | 2 +- test/parallel/test-trace-events-bootstrap.js | 2 +- test/parallel/test-trace-events-file-pattern.js | 2 +- test/parallel/test-trace-events-fs-sync.js | 2 +- test/parallel/test-trace-events-metadata.js | 2 +- test/parallel/test-trace-events-none.js | 2 +- test/parallel/test-trace-events-perf.js | 2 +- test/parallel/test-trace-events-process-exit.js | 2 +- test/parallel/test-trace-events-v8.js | 2 +- test/parallel/test-trace-events-vm.js | 2 +- test/parallel/test-trace-events-worker-metadata.js | 2 +- test/pummel/test-fs-watch-file-slow.js | 8 ++++---- 24 files changed, 37 insertions(+), 50 deletions(-) diff --git a/test/common/README.md b/test/common/README.md index 3dee2f19704934..af66d898c1ba75 100644 --- a/test/common/README.md +++ b/test/common/README.md @@ -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[]>] diff --git a/test/common/index.js b/test/common/index.js index bcd28cc28710e7..084078cd9d0097 100644 --- a/test/common/index.js +++ b/test/common/index.js @@ -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'); diff --git a/test/parallel/test-child-process-spawn-error.js b/test/parallel/test-child-process-spawn-error.js index 109effa5c8338e..28591f88fa0e39 100644 --- a/test/parallel/test-child-process-spawn-error.js +++ b/test/parallel/test-child-process-spawn-error.js @@ -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) { diff --git a/test/parallel/test-fs-existssync-false.js b/test/parallel/test-fs-existssync-false.js index de5ecfa66ad922..096be6de38df9b 100644 --- a/test/parallel/test-fs-existssync-false.js +++ b/test/parallel/test-fs-existssync-false.js @@ -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) { diff --git a/test/parallel/test-fs-mkdir-rmdir.js b/test/parallel/test-fs-mkdir-rmdir.js index ff0da82bdf77e0..5e4982ba8576bf 100644 --- a/test/parallel/test-fs-mkdir-rmdir.js +++ b/test/parallel/test-fs-mkdir-rmdir.js @@ -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) { diff --git a/test/parallel/test-fs-mkdir.js b/test/parallel/test-fs-mkdir.js index 937c73aca4a4c2..18e9e973f14cce 100644 --- a/test/parallel/test-fs-mkdir.js +++ b/test/parallel/test-fs-mkdir.js @@ -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); } diff --git a/test/parallel/test-fs-mkdtemp.js b/test/parallel/test-fs-mkdtemp.js index 443dafa275630d..98b62aa315e0a4 100644 --- a/test/parallel/test-fs-mkdtemp.js +++ b/test/parallel/test-fs-mkdtemp.js @@ -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); } diff --git a/test/parallel/test-fs-symlink-dir-junction.js b/test/parallel/test-fs-symlink-dir-junction.js index cd9459bf44afe1..5bd7a8dcc4ecc3 100644 --- a/test/parallel/test-fs-symlink-dir-junction.js +++ b/test/parallel/test-fs-symlink-dir-junction.js @@ -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)); })); })); })); diff --git a/test/parallel/test-require-exceptions.js b/test/parallel/test-require-exceptions.js index 39298d3ec49987..132bffed0f6529 100644 --- a/test/parallel/test-require-exceptions.js +++ b/test/parallel/test-require-exceptions.js @@ -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))); } diff --git a/test/parallel/test-trace-events-all.js b/test/parallel/test-trace-events-all.js index 82dd4eac5a7624..7434715973a31b 100644 --- a/test/parallel/test-trace-events-all.js +++ b/test/parallel/test-trace-events-all.js @@ -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); diff --git a/test/parallel/test-trace-events-api.js b/test/parallel/test-trace-events-api.js index 5dd4c623c022bb..44193cec0e2bb3 100644 --- a/test/parallel/test-trace-events-api.js +++ b/test/parallel/test-trace-events-api.js @@ -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'); diff --git a/test/parallel/test-trace-events-async-hooks.js b/test/parallel/test-trace-events-async-hooks.js index 9b49cd1aeb70d3..13cd523de7ae9c 100644 --- a/test/parallel/test-trace-events-async-hooks.js +++ b/test/parallel/test-trace-events-async-hooks.js @@ -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); diff --git a/test/parallel/test-trace-events-binding.js b/test/parallel/test-trace-events-binding.js index f50c1321840980..7ce75ace330990 100644 --- a/test/parallel/test-trace-events-binding.js +++ b/test/parallel/test-trace-events-binding.js @@ -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'); diff --git a/test/parallel/test-trace-events-bootstrap.js b/test/parallel/test-trace-events-bootstrap.js index c0f8a49c855b92..8d5e00f00f8b0c 100644 --- a/test/parallel/test-trace-events-bootstrap.js +++ b/test/parallel/test-trace-events-bootstrap.js @@ -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'); diff --git a/test/parallel/test-trace-events-file-pattern.js b/test/parallel/test-trace-events-file-pattern.js index c8098a43979bb4..60a3296c642994 100644 --- a/test/parallel/test-trace-events-file-pattern.js +++ b/test/parallel/test-trace-events-file-pattern.js @@ -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); diff --git a/test/parallel/test-trace-events-fs-sync.js b/test/parallel/test-trace-events-fs-sync.js index 77ec0863406379..71ffc9da726f1b 100644 --- a/test/parallel/test-trace-events-fs-sync.js +++ b/test/parallel/test-trace-events-fs-sync.js @@ -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); diff --git a/test/parallel/test-trace-events-metadata.js b/test/parallel/test-trace-events-metadata.js index 8f9648902d13e1..0857d9c099175a 100644 --- a/test/parallel/test-trace-events-metadata.js +++ b/test/parallel/test-trace-events-metadata.js @@ -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'); diff --git a/test/parallel/test-trace-events-none.js b/test/parallel/test-trace-events-none.js index e6beea3b975355..4c2fe0ca216104 100644 --- a/test/parallel/test-trace-events-none.js +++ b/test/parallel/test-trace-events-none.js @@ -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( diff --git a/test/parallel/test-trace-events-perf.js b/test/parallel/test-trace-events-perf.js index 8ca5a41eac230e..27eef28fb12325 100644 --- a/test/parallel/test-trace-events-perf.js +++ b/test/parallel/test-trace-events-perf.js @@ -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'); diff --git a/test/parallel/test-trace-events-process-exit.js b/test/parallel/test-trace-events-process-exit.js index aeb9fc19c2982d..254963c2d3a751 100644 --- a/test/parallel/test-trace-events-process-exit.js +++ b/test/parallel/test-trace-events-process-exit.js @@ -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); diff --git a/test/parallel/test-trace-events-v8.js b/test/parallel/test-trace-events-v8.js index baabaa78f3984b..3c1fd48d67ec48 100644 --- a/test/parallel/test-trace-events-v8.js +++ b/test/parallel/test-trace-events-v8.js @@ -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); diff --git a/test/parallel/test-trace-events-vm.js b/test/parallel/test-trace-events-vm.js index 45724e3e4c88dc..758de3e964f7b2 100644 --- a/test/parallel/test-trace-events-vm.js +++ b/test/parallel/test-trace-events-vm.js @@ -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'); diff --git a/test/parallel/test-trace-events-worker-metadata.js b/test/parallel/test-trace-events-worker-metadata.js index 8d21d6b7ab9e0b..e672aeefd721cc 100644 --- a/test/parallel/test-trace-events-worker-metadata.js +++ b/test/parallel/test-trace-events-worker-metadata.js @@ -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); diff --git a/test/pummel/test-fs-watch-file-slow.js b/test/pummel/test-fs-watch-file-slow.js index 7b7065cffbfe64..cf7adc913db28c 100644 --- a/test/pummel/test-fs-watch-file-slow.js +++ b/test/pummel/test-fs-watch-file-slow.js @@ -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: From 4d0bc78221ef46aaa7a1753bf3cdca050c93758a Mon Sep 17 00:00:00 2001 From: Rich Trott Date: Mon, 6 Aug 2018 13:27:57 -0700 Subject: [PATCH 2/2] test: remove unused fileExists from index.mjs --- test/common/index.mjs | 2 -- 1 file changed, 2 deletions(-) diff --git a/test/common/index.mjs b/test/common/index.mjs index 51cf2a31df8f0d..74194975f1fc26 100644 --- a/test/common/index.mjs +++ b/test/common/index.mjs @@ -33,7 +33,6 @@ const { mustCallAtLeast, mustCallAsync, hasMultiLocalhost, - fileExists, skipIfEslintMissing, canCreateSymLink, getCallSite, @@ -92,7 +91,6 @@ export { mustCallAtLeast, mustCallAsync, hasMultiLocalhost, - fileExists, skipIfEslintMissing, canCreateSymLink, getCallSite,