Last active
January 28, 2025 07:55
-
-
Save Yeaseen/2b7c63cfeb15d1aa582fc6d6f3f0db65 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
require('../common'); | |
const tmpdir = require('../common/tmpdir'); | |
const assert = require('node:assert'); | |
const fs = require('node:fs'); | |
const path = require('node:path'); | |
const { execSync } = require('child_process'); | |
tmpdir.refresh(); // Prepare a clean temporary directory | |
const dirPath = path.join(tmpdir.path, '速_dir'); | |
const filePath = path.join(dirPath, 'test_file.txt'); | |
// Create a directory and a file within it | |
fs.mkdirSync(dirPath, { recursive: true }); | |
fs.writeFileSync(filePath, 'This is a test file.'); | |
// Set permissions to simulate a permission denied scenario | |
if (process.platform === 'win32') { | |
// Windows: Deny delete permissions | |
execSync(`icacls "${filePath}" /deny Everyone:(D)`); | |
} else { | |
// Unix/Linux: Remove write permissions from the directory | |
fs.chmodSync(dirPath, 0o555); // Read and execute permissions only | |
} | |
// Attempt to delete the directory which should now fail | |
try { | |
fs.rmSync(dirPath, { recursive: true }); | |
assert.fail('Directory deletion should have failed due to permission restrictions.'); | |
} catch (err) { | |
// Verify that the error is due to permission restrictions | |
const expectedCode = process.platform === 'win32' ? 'EPERM' : 'EACCES'; | |
assert.strictEqual(err.code, expectedCode); | |
console.log(err.message); | |
console.log(err.path); | |
console.log(dirPath); | |
assert(err.message.includes(dirPath), 'Error message should include the path treated as a directory'); | |
} | |
// Cleanup - resetting permissions and removing the directory safely | |
if (process.platform === 'win32') { | |
// Remove the explicit permissions before attempting to delete | |
execSync(`icacls "${filePath}" /remove:d Everyone`); | |
} else { | |
// Reset permissions to allow deletion | |
fs.chmodSync(dirPath, 0o755); // Restore full permissions to the directory | |
} | |
// Attempt to clean up | |
fs.rmSync(dirPath, { recursive: true }); // This should now succeed | |
---------------------------------------- | |
'use strict'; | |
require('../common'); | |
const tmpdir = require('../common/tmpdir'); | |
const assert = require('node:assert'); | |
const fs = require('node:fs'); | |
const path = require('node:path'); | |
// This test ensures that fs.rmSync handles non-ASCII characters in file paths, | |
// and that errors contain correctly encoded paths and err.path values. | |
tmpdir.refresh(); // Prepare a clean temporary directory | |
// Define paths with non-ASCII characters | |
const dirPath = path.join(tmpdir.path, '速_dir'); | |
const filePath = path.join(tmpdir.path, '速.txt'); | |
// Create a directory and a file with non-ASCII characters | |
fs.mkdirSync(dirPath); | |
fs.writeFileSync(filePath, 'This is a test file with special characters.'); | |
fs.rmSync(filePath); | |
assert.strictEqual(fs.existsSync(filePath), false); | |
// Ensure rmSync throws an error when trying to remove a directory without recursive | |
assert.throws(() => { | |
fs.rmSync(dirPath, { recursive: false }); | |
}, (err) => { | |
// Assert the error code and check that the error message includes the correct non-ASCII path | |
assert.strictEqual(err.code, 'ERR_FS_EISDIR'); | |
assert(err.message.includes(dirPath), 'Error message should include the directory path'); | |
assert.strictEqual(err.path, dirPath); | |
return true; | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment