Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Improve test cleanup
  • Loading branch information
iansu committed Sep 18, 2020
commit e26dbbaf2e50939a67da9b3f63c4ac11e547aa40
43 changes: 25 additions & 18 deletions test/parallel/test-fs-rmdir-recursive.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@ function removeAsync(dir) {
assert.strictEqual(err.code, 'ERR_INVALID_ARG_VALUE');
assert.strictEqual(err.name, 'TypeError');
assert.match(err.message, /^The argument 'path' is not a directory\./);
fs.unlinkSync(filePath);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this needs a try/finally as well because these assert calls may throw

}));
fs.unlinkSync(filePath);
}

// Test the synchronous version.
Expand Down Expand Up @@ -147,15 +147,18 @@ function removeAsync(dir) {
const filePath = path.join(tmpdir.path, 'rmdir-sync-file.txt');
fs.writeFileSync(filePath, '');

assert.throws(() => {
fs.rmdirSync(filePath, { recursive: true });
}, {
code: 'ERR_INVALID_ARG_VALUE',
name: 'TypeError',
message: /^The argument 'path' is not a directory\./
});
try {
assert.throws(() => {
fs.rmdirSync(filePath, { recursive: true });
}, {
code: 'ERR_INVALID_ARG_VALUE',
name: 'TypeError',
message: /^The argument 'path' is not a directory\./
});
} finally {
fs.unlinkSync(filePath);
}

fs.unlinkSync(filePath);

// Recursive removal should succeed.
fs.rmdirSync(dir, { recursive: true });
Expand Down Expand Up @@ -189,17 +192,21 @@ function removeAsync(dir) {
});

// Should fail if target is a file
const filePath = path.join(tmpdir.path, 'rmdir-sync-file.txt');
const filePath = path.join(tmpdir.path, 'rmdir-promises-file.txt');
fs.writeFileSync(filePath, '');

assert.rejects(fs.promises.rmdir(
filePath,
{ recursive: true }
), {
code: 'ERR_INVALID_ARG_VALUE',
name: 'TypeError',
message: /^The argument 'path' is not a directory\./
});
try {
assert.rejects(fs.promises.rmdir(
filePath,
{ recursive: true }
), {
code: 'ERR_INVALID_ARG_VALUE',
name: 'TypeError',
message: /^The argument 'path' is not a directory\./
});
} finally {
fs.unlinkSync(filePath);
}

// Attempted removal should fail now because the directory is gone.
assert.rejects(fs.promises.rmdir(dir), { syscall: 'rmdir' });
Expand Down