Skip to content

Commit 10b603c

Browse files
authored
test: add failing test for recursive mkdir on read-only fs
mkdirSync with { recursive: true } on a read-only filesystem throws ENOENT instead of the expected EROFS. This test demonstrates the bug by mounting a read-only tmpfs via sudo and verifying the error code. Refs: nodejs#47098 Refs: nodejs#48105 Signed-off-by: Kenny Yeo <[email protected]>
1 parent 43d5058 commit 10b603c

1 file changed

Lines changed: 28 additions & 0 deletions

File tree

test/parallel/test-fs-mkdir.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,34 @@ function nextdir() {
184184
}));
185185
}
186186

187+
// `mkdirp` when folder was readonly.
188+
if (common.isLinux) {
189+
const roTmpfsPath = path.join(tmpdir.path, 'ro-tmpfs');
190+
fs.mkdirSync(roTmpfsPath);
191+
192+
const { status, stderr } = child_process.spawnSync(
193+
'sudo', ['-n', 'mount', '-t', 'tmpfs', '-o', 'ro', 'tmpfs', roTmpfsPath],
194+
{ stdio: 'pipe', encoding: 'utf8' }
195+
);
196+
197+
if (status !== 0) {
198+
console.log(`skipping EROFS test: sudo mount failed (status=${status}): ${stderr}`);
199+
} else {
200+
const pathname = path.join(roTmpfsPath, nextdir());
201+
assert.throws(
202+
() => { fs.mkdirSync(pathname, { recursive: true }); },
203+
{
204+
code: 'EROFS',
205+
message: /EROFS:.*mkdir/,
206+
name: 'Error',
207+
syscall: 'mkdir',
208+
}
209+
);
210+
child_process.spawnSync('sudo', ['-n', 'umount', roTmpfsPath]);
211+
}
212+
fs.rmdirSync(roTmpfsPath);
213+
}
214+
187215
// `mkdirp` when path is a file.
188216
{
189217
const pathname = tmpdir.resolve(nextdir(), nextdir());

0 commit comments

Comments
 (0)