Last active
June 28, 2025 05:21
-
-
Save bakkot/ab45130efaf218185dd6a21ace390694 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
// test/cannot-remove.spec.js | |
'use strict'; | |
const assert = require('assert'); | |
const fs = require('fs'); | |
const path = require('path'); | |
const os = require('os'); | |
const {describe, it, before, after} = require('node:test'); | |
it('fs.rmdirSync must throw when self is locked', () => { | |
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'rmdir-test-')); | |
if (process.platform === 'win32') { | |
// On Windows, make the directory readonly | |
const { execSync } = require('child_process'); | |
execSync(`attrib +R "${dir}" /S /D`); | |
} else { | |
// On Unix, remove write permissions | |
fs.chmodSync(dir, 0o555); | |
} | |
assert.throws( | |
() => fs.rmdirSync(dir), | |
err => err && (err.code === 'EPERM' || err.code === 'EACCES'), | |
'Expected fs.rmdirSync to fail when directory is read-only' | |
); | |
if (process.platform === 'win32') { | |
// Remove readonly attribute on Windows | |
const { execSync } = require('child_process'); | |
execSync(`attrib -R "${dir}" /S /D`, { stdio: 'ignore' }); | |
} else { | |
// Restore write permissions on Unix | |
fs.chmodSync(dir, 0o755); | |
} | |
fs.rmdirSync(dir); | |
}); | |
it('fs.rmdirSync must throw when parent is locked', () => { | |
const parent = fs.mkdtempSync(path.join(os.tmpdir(), 'rmdir-test-')); | |
const child = fs.mkdtempSync(path.join(parent, 'child-')); | |
if (process.platform === 'win32') { | |
// On Windows, make the directory readonly | |
const { execSync } = require('child_process'); | |
execSync(`attrib +R "${parent}" /S /D`); | |
} else { | |
// On Unix, remove write permissions | |
fs.chmodSync(parent, 0o555); | |
} | |
assert.throws( | |
() => fs.rmdirSync(child), | |
err => err && (err.code === 'EPERM' || err.code === 'EACCES'), | |
'Expected fs.rmdirSync to fail when directory is read-only' | |
); | |
if (process.platform === 'win32') { | |
// Remove readonly attribute on Windows | |
const { execSync } = require('child_process'); | |
execSync(`attrib -R "${parent}" /S /D`, { stdio: 'ignore' }); | |
} else { | |
// Restore write permissions on Unix | |
fs.chmodSync(parent, 0o755); | |
} | |
fs.rmdirSync(child); | |
fs.rmdirSync(parent); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment