test: use assert.rejects() and assert.throws()

Replace try-catch blocks in tests with `assert.rejects()` and
`assert.throws()`.

PR-URL: https://github.com/nodejs/node/pull/27207
Fixes: https://github.com/nodejs/node/issues/27198
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com>
Reviewed-By: Ben Coe <bencoe@gmail.com>
This commit is contained in:
Richard Lau 2019-04-16 03:26:28 +01:00
parent 2400cbcf7c
commit c6c37e9e85
2 changed files with 45 additions and 40 deletions

View File

@ -122,14 +122,15 @@ function nextdir() {
fs.mkdirSync(path.dirname(pathname)); fs.mkdirSync(path.dirname(pathname));
fs.writeFileSync(pathname, '', 'utf8'); fs.writeFileSync(pathname, '', 'utf8');
try { assert.throws(
fs.mkdirSync(pathname, { recursive: true }); () => { fs.mkdirSync(pathname, { recursive: true }); },
throw new Error('unreachable'); {
} catch (err) { code: 'EEXIST',
assert.notStrictEqual(err.message, 'unreachable'); message: /EEXIST: .*mkdir/,
assert.strictEqual(err.code, 'EEXIST'); name: 'Error',
assert.strictEqual(err.syscall, 'mkdir'); syscall: 'mkdir',
} }
);
} }
// mkdirpSync when part of the path is a file. // mkdirpSync when part of the path is a file.
@ -140,14 +141,15 @@ function nextdir() {
fs.mkdirSync(path.dirname(filename)); fs.mkdirSync(path.dirname(filename));
fs.writeFileSync(filename, '', 'utf8'); fs.writeFileSync(filename, '', 'utf8');
try { assert.throws(
fs.mkdirSync(pathname, { recursive: true }); () => { fs.mkdirSync(pathname, { recursive: true }); },
throw new Error('unreachable'); {
} catch (err) { code: 'ENOTDIR',
assert.notStrictEqual(err.message, 'unreachable'); message: /ENOTDIR: .*mkdir/,
assert.strictEqual(err.code, 'ENOTDIR'); name: 'Error',
assert.strictEqual(err.syscall, 'mkdir'); syscall: 'mkdir',
} }
);
} }
// `mkdirp` when folder does not yet exist. // `mkdirp` when folder does not yet exist.
@ -195,14 +197,15 @@ if (common.isMainThread && (common.isLinux || common.isOSX)) {
fs.mkdirSync(pathname); fs.mkdirSync(pathname);
process.chdir(pathname); process.chdir(pathname);
fs.rmdirSync(pathname); fs.rmdirSync(pathname);
try { assert.throws(
fs.mkdirSync('X', { recursive: true }); () => { fs.mkdirSync('X', { recursive: true }); },
throw new Error('unreachable'); {
} catch (err) { code: 'ENOENT',
assert.notStrictEqual(err.message, 'unreachable'); message: /ENOENT: .*mkdir/,
assert.strictEqual(err.code, 'ENOENT'); name: 'Error',
assert.strictEqual(err.syscall, 'mkdir'); syscall: 'mkdir',
} }
);
fs.mkdir('X', { recursive: true }, (err) => { fs.mkdir('X', { recursive: true }, (err) => {
assert.strictEqual(err.code, 'ENOENT'); assert.strictEqual(err.code, 'ENOENT');
assert.strictEqual(err.syscall, 'mkdir'); assert.strictEqual(err.syscall, 'mkdir');

View File

@ -299,14 +299,15 @@ async function getHandle(dest) {
const dir = path.join(tmpDir, nextdir(), nextdir()); const dir = path.join(tmpDir, nextdir(), nextdir());
await mkdir(path.dirname(dir)); await mkdir(path.dirname(dir));
await writeFile(dir); await writeFile(dir);
try { assert.rejects(
await mkdir(dir, { recursive: true }); mkdir(dir, { recursive: true }),
throw new Error('unreachable'); {
} catch (err) { code: 'EEXIST',
assert.notStrictEqual(err.message, 'unreachable'); message: /EEXIST: .*mkdir/,
assert.strictEqual(err.code, 'EEXIST'); name: 'Error',
assert.strictEqual(err.syscall, 'mkdir'); syscall: 'mkdir',
} }
);
} }
// `mkdirp` when part of the path is a file. // `mkdirp` when part of the path is a file.
@ -315,14 +316,15 @@ async function getHandle(dest) {
const dir = path.join(file, nextdir(), nextdir()); const dir = path.join(file, nextdir(), nextdir());
await mkdir(path.dirname(file)); await mkdir(path.dirname(file));
await writeFile(file); await writeFile(file);
try { assert.rejects(
await mkdir(dir, { recursive: true }); mkdir(dir, { recursive: true }),
throw new Error('unreachable'); {
} catch (err) { code: 'ENOTDIR',
assert.notStrictEqual(err.message, 'unreachable'); message: /ENOTDIR: .*mkdir/,
assert.strictEqual(err.code, 'ENOTDIR'); name: 'Error',
assert.strictEqual(err.syscall, 'mkdir'); syscall: 'mkdir',
} }
);
} }
// mkdirp ./ // mkdirp ./