Skip to content

Commit

Permalink
test: make test-fs-access stricter
Browse files Browse the repository at this point in the history
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.

PR-URL: nodejs#11087
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Adrian Estrada <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Daniel Bevenius <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
Reviewed-By: Yuta Hiroto <[email protected]>
  • Loading branch information
Trott authored and italoacasas committed Feb 14, 2017
1 parent e2d9c23 commit 3d35dcf
Showing 1 changed file with 17 additions and 23 deletions.
40 changes: 17 additions & 23 deletions test/parallel/test-fs-access.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
Expand Down Expand Up @@ -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);
Expand All @@ -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;
}
);

0 comments on commit 3d35dcf

Please sign in to comment.