From 44c581ef0ba7b11dc894d3124d8e33bbf88d85da Mon Sep 17 00:00:00 2001 From: Maria Paktiti Date: Wed, 2 Oct 2019 19:53:37 +0300 Subject: [PATCH] test: add more recursive fs.rmdir() tests Update tests to create more nested folders and files, like rimraf package tests do. Each test will create 28 nested directories and 210 files. It will also create different types of files, like symlinks. PR-URL: https://github.com/nodejs/node/pull/29815 Reviewed-By: Ben Coe --- test/parallel/test-fs-rmdir-recursive.js | 84 ++++++++++++++++++++---- 1 file changed, 73 insertions(+), 11 deletions(-) diff --git a/test/parallel/test-fs-rmdir-recursive.js b/test/parallel/test-fs-rmdir-recursive.js index b020221b27b..5f630b686a1 100644 --- a/test/parallel/test-fs-rmdir-recursive.js +++ b/test/parallel/test-fs-rmdir-recursive.js @@ -10,18 +10,56 @@ let count = 0; tmpdir.refresh(); -function makeNonEmptyDirectory() { - const dirname = `rmdir-recursive-${count}`; - fs.mkdirSync(path.join(dirname, 'foo', 'bar', 'baz'), { recursive: true }); +function makeNonEmptyDirectory(depth, files, folders, dirname, createSymLinks) { + fs.mkdirSync(dirname, { recursive: true }); fs.writeFileSync(path.join(dirname, 'text.txt'), 'hello', 'utf8'); - count++; - return dirname; + + const options = { flag: 'wx' }; + + for (let f = files; f > 0; f--) { + fs.writeFileSync(path.join(dirname, `f-${depth}-${f}`), '', options); + } + + if (createSymLinks) { + // Valid symlink + fs.symlinkSync( + `f-${depth}-1`, + path.join(dirname, `link-${depth}-good`), + 'file' + ); + + // Invalid symlink + fs.symlinkSync( + 'does-not-exist', + path.join(dirname, `link-${depth}-bad`), + 'file' + ); + } + + // File with a name that looks like a glob + fs.writeFileSync(path.join(dirname, '[a-z0-9].txt'), '', options); + + depth--; + if (depth <= 0) { + return; + } + + for (let f = folders; f > 0; f--) { + fs.mkdirSync( + path.join(dirname, `folder-${depth}-${f}`), + { recursive: true } + ); + makeNonEmptyDirectory( + depth, + files, + folders, + path.join(dirname, `d-${depth}-${f}`), + createSymLinks + ); + } } -// Test the asynchronous version. -{ - const dir = makeNonEmptyDirectory(); - +function removeAsync(dir) { // Removal should fail without the recursive option. fs.rmdir(dir, common.mustCall((err) => { assert.strictEqual(err.syscall, 'rmdir'); @@ -48,9 +86,31 @@ function makeNonEmptyDirectory() { })); } +// Test the asynchronous version +{ + // Create a 4-level folder hierarchy including symlinks + let dir = `rmdir-recursive-${count}`; + makeNonEmptyDirectory(4, 10, 2, dir, true); + removeAsync(dir); + + // Create a 2-level folder hierarchy without symlinks + count++; + dir = `rmdir-recursive-${count}`; + makeNonEmptyDirectory(2, 10, 2, dir, false); + removeAsync(dir); + + // Create a flat folder including symlinks + count++; + dir = `rmdir-recursive-${count}`; + makeNonEmptyDirectory(1, 10, 2, dir, true); + removeAsync(dir); +} + // Test the synchronous version. { - const dir = makeNonEmptyDirectory(); + count++; + const dir = `rmdir-recursive-${count}`; + makeNonEmptyDirectory(4, 10, 2, dir, true); // Removal should fail without the recursive option set to true. common.expectsError(() => { @@ -72,7 +132,9 @@ function makeNonEmptyDirectory() { // Test the Promises based version. (async () => { - const dir = makeNonEmptyDirectory(); + count++; + const dir = `rmdir-recursive-${count}`; + makeNonEmptyDirectory(4, 10, 2, dir, true); // Removal should fail without the recursive option set to true. assert.rejects(fs.promises.rmdir(dir), { syscall: 'rmdir' });