From f7d12e37d96f212f49f284269938bfba22d70bc7 Mon Sep 17 00:00:00 2001 From: Rich Trott Date: Tue, 31 Jan 2017 09:47:43 -0800 Subject: [PATCH] test: make test-fs-access stricter Change regular expression matching in `assert.throws()` to match the entire error message. In `assert.throws()` that uses a function for matching rather than a regular expression, add checks for the `message` property and the error's constructor. Also, refactored to remove unnecessary temp file handling. No need to remove temp files after the test. Each test is responsible for clearing the temp directory if it needs to use it. --- test/parallel/test-fs-access.js | 40 ++++++++++++++------------------- 1 file changed, 17 insertions(+), 23 deletions(-) diff --git a/test/parallel/test-fs-access.js b/test/parallel/test-fs-access.js index 4157f92f8b9021..34077ce1ffe132 100644 --- a/test/parallel/test-fs-access.js +++ b/test/parallel/test-fs-access.js @@ -7,16 +7,7 @@ const doesNotExist = path.join(common.tmpDir, '__this_should_not_exist'); const readOnlyFile = path.join(common.tmpDir, 'read_only_file'); const readWriteFile = path.join(common.tmpDir, 'read_write_file'); -const removeFile = function(file) { - try { - fs.unlinkSync(file); - } catch (err) { - // Ignore error - } -}; - const createFileWithPerms = function(file, mode) { - removeFile(file); fs.writeFileSync(file, ''); fs.chmodSync(file, mode); }; @@ -91,16 +82,16 @@ fs.access(readOnlyFile, fs.W_OK, common.mustCall((err) => { })); assert.throws(() => { - fs.access(100, fs.F_OK, (err) => {}); -}, /path must be a string or Buffer/); + fs.access(100, fs.F_OK, () => { common.fail('callback should not run'); }); +}, /^TypeError: path must be a string or Buffer$/); assert.throws(() => { fs.access(__filename, fs.F_OK); -}, /"callback" argument must be a function/); +}, /^TypeError: "callback" argument must be a function$/); assert.throws(() => { fs.access(__filename, fs.F_OK, {}); -}, /"callback" argument must be a function/); +}, /^TypeError: "callback" argument must be a function$/); assert.doesNotThrow(() => { fs.accessSync(__filename); @@ -112,13 +103,16 @@ assert.doesNotThrow(() => { fs.accessSync(readWriteFile, mode); }); -assert.throws(() => { - fs.accessSync(doesNotExist); -}, (err) => { - return err.code === 'ENOENT' && err.path === doesNotExist; -}); - -process.on('exit', () => { - removeFile(readOnlyFile); - removeFile(readWriteFile); -}); +assert.throws( + () => { fs.accessSync(doesNotExist); }, + (err) => { + assert.strictEqual(err.code, 'ENOENT'); + assert.strictEqual(err.path, doesNotExist); + assert.strictEqual( + err.message, + `ENOENT: no such file or directory, access '${doesNotExist}'` + ); + assert.strictEqual(err.constructor, Error); + return true; + } +);