Skip to content

Commit 3d35dcf

Browse files
Trottitaloacasas
authored andcommitted
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. PR-URL: #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]>
1 parent e2d9c23 commit 3d35dcf

File tree

1 file changed

+17
-23
lines changed

1 file changed

+17
-23
lines changed

test/parallel/test-fs-access.js

+17-23
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,7 @@ const doesNotExist = path.join(common.tmpDir, '__this_should_not_exist');
77
const readOnlyFile = path.join(common.tmpDir, 'read_only_file');
88
const readWriteFile = path.join(common.tmpDir, 'read_write_file');
99

10-
const removeFile = function(file) {
11-
try {
12-
fs.unlinkSync(file);
13-
} catch (err) {
14-
// Ignore error
15-
}
16-
};
17-
1810
const createFileWithPerms = function(file, mode) {
19-
removeFile(file);
2011
fs.writeFileSync(file, '');
2112
fs.chmodSync(file, mode);
2213
};
@@ -91,16 +82,16 @@ fs.access(readOnlyFile, fs.W_OK, common.mustCall((err) => {
9182
}));
9283

9384
assert.throws(() => {
94-
fs.access(100, fs.F_OK, (err) => {});
95-
}, /path must be a string or Buffer/);
85+
fs.access(100, fs.F_OK, () => { common.fail('callback should not run'); });
86+
}, /^TypeError: path must be a string or Buffer$/);
9687

9788
assert.throws(() => {
9889
fs.access(__filename, fs.F_OK);
99-
}, /"callback" argument must be a function/);
90+
}, /^TypeError: "callback" argument must be a function$/);
10091

10192
assert.throws(() => {
10293
fs.access(__filename, fs.F_OK, {});
103-
}, /"callback" argument must be a function/);
94+
}, /^TypeError: "callback" argument must be a function$/);
10495

10596
assert.doesNotThrow(() => {
10697
fs.accessSync(__filename);
@@ -112,13 +103,16 @@ assert.doesNotThrow(() => {
112103
fs.accessSync(readWriteFile, mode);
113104
});
114105

115-
assert.throws(() => {
116-
fs.accessSync(doesNotExist);
117-
}, (err) => {
118-
return err.code === 'ENOENT' && err.path === doesNotExist;
119-
});
120-
121-
process.on('exit', () => {
122-
removeFile(readOnlyFile);
123-
removeFile(readWriteFile);
124-
});
106+
assert.throws(
107+
() => { fs.accessSync(doesNotExist); },
108+
(err) => {
109+
assert.strictEqual(err.code, 'ENOENT');
110+
assert.strictEqual(err.path, doesNotExist);
111+
assert.strictEqual(
112+
err.message,
113+
`ENOENT: no such file or directory, access '${doesNotExist}'`
114+
);
115+
assert.strictEqual(err.constructor, Error);
116+
return true;
117+
}
118+
);

0 commit comments

Comments
 (0)