Skip to content

Commit 4bc9b95

Browse files
committed
test: add ENOTDIR additional error coverage for rmSync
1 parent cf6ddd0 commit 4bc9b95

File tree

1 file changed

+10
-37
lines changed

1 file changed

+10
-37
lines changed

test/parallel/test-fs-rmSync-special-char-additional-error.js

Lines changed: 10 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -4,48 +4,21 @@ const tmpdir = require('../common/tmpdir');
44
const assert = require('node:assert');
55
const fs = require('node:fs');
66
const path = require('node:path');
7-
const { execSync } = require('child_process');
87

9-
tmpdir.refresh(); // Prepare a clean temporary directory
8+
tmpdir.refresh();
109

11-
const dirPath = path.join(tmpdir.path, '速速速_dir');
12-
const filePath = path.join(dirPath, 'test_file.txt');
10+
const file = path.join(tmpdir.path, '速_file');
11+
fs.writeFileSync(file, 'x');
1312

14-
// Create a directory and a file within it
15-
fs.mkdirSync(dirPath, { recursive: true });
16-
fs.writeFileSync(filePath, 'This is a test file.');
17-
18-
// Set permissions to simulate a permission denied scenario
19-
if (process.platform === 'win32') {
20-
// Windows: Deny delete permissions
21-
execSync(`icacls "${filePath}" /deny Everyone:(D)`);
22-
} else {
23-
// Unix/Linux: Remove write permissions from the directory
24-
fs.chmodSync(dirPath, 0o555); // Read and execute permissions only
25-
}
13+
// Treat that file as if it were a directory, so that the error message includes the path treated as a directory, not the file.
14+
const badPath = path.join(file, 'child');
2615

2716
// Attempt to delete the directory which should now fail
2817
try {
29-
fs.rmSync(dirPath, { recursive: true });
18+
fs.rmSync(badPath, { recursive: true });
3019
} catch (err) {
31-
if (process.platform === 'win32') {
32-
assert.strictEqual(err.code, 'EPERM');
33-
} else {
34-
// POSIX: rmSync may surface different errors
35-
assert(['EACCES', 'EPERM', 'ENOTEMPTY'].includes(err.code), err.code);
36-
}
37-
assert.strictEqual(err.path, dirPath);
38-
assert(err.message.includes(dirPath), 'Error message should include the path treated as a directory');
20+
// Verify that the error is due to the path being treated as a directory
21+
assert.strictEqual(err.code, 'ENOTDIR');
22+
assert.strictEqual(err.path, badPath);
23+
assert(err.message.includes(badPath), 'Error message should include the path treated as a directory');
3924
}
40-
41-
// Cleanup - resetting permissions and removing the directory safely
42-
if (process.platform === 'win32') {
43-
// Remove the explicit permissions before attempting to delete
44-
execSync(`icacls "${filePath}" /remove:d Everyone`);
45-
} else {
46-
// Reset permissions to allow deletion
47-
fs.chmodSync(dirPath, 0o755); // Restore full permissions to the directory
48-
}
49-
50-
// Attempt to clean up
51-
fs.rmSync(dirPath, { recursive: true }); // This should now succeed

0 commit comments

Comments
 (0)