Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
move checks into functions
  • Loading branch information
RaisinTen committed Jun 3, 2021
commit abdadd37ce8e27aeee6c99e263d4033712055684
86 changes: 26 additions & 60 deletions test/parallel/test-fs-rm.js
Original file line number Diff line number Diff line change
Expand Up @@ -289,8 +289,15 @@ function removeAsync(dir) {
// On Windows, we are allowed to access and modify the contents of a
// read-only folder.
{
// Check that deleting a file that cannot be accessed using rmsync throws
// https://github.com/nodejs/node/issues/38683
// Check that deleting a file that cannot be accessed using rmsync
// throws: https://github.com/nodejs/node/issues/38683
function isValidState(exists, err) {
return common.isWindows ?
(exists === false && err === null)) :
(exists === true && err?.code === 'EACCES'));
}

{
const dirname = nextDirPath();
const filePath = path.join(dirname, 'text.txt');

Expand Down Expand Up @@ -318,25 +325,12 @@ function removeAsync(dir) {
} catch {
}

let isValidState = true;
const exists = fs.existsSync(filePath);

if (common.isWindows &&
!(exists === false && err === null)) {
isValidState = false;
} else if (!common.isWindows &&
!(exists === true && err?.code === 'EACCES')) {
isValidState = false;
}

if (!isValidState) {
if (!isValidState(fs.existsSync(filePath), err)) {
throw err;
}
}

{
// Check that deleting a file that cannot be accessed using rmsync throws
// https://github.com/nodejs/node/issues/38683
const dirname = nextDirPath();
const filePath = path.join(dirname, 'text.txt');

Expand All @@ -357,27 +351,26 @@ function removeAsync(dir) {
} catch {
}

let isValidState = true;
const exists = fs.existsSync(filePath);

if (common.isWindows &&
!(exists === false && err === null)) {
isValidState = false;
} else if (!common.isWindows &&
!(exists === true && err?.code === 'EACCES')) {
isValidState = false;
}

if (!isValidState) {
if (!isValidState(fs.existsSync(filePath), err)) {
throw err;
}
}));
}
}

// On Windows, we are not allowed to delete a read-only directory.
{
// Check endless recursion.
// https://github.com/nodejs/node/issues/34580
// Check endless recursion.
// https://github.com/nodejs/node/issues/34580
function isValidState(exists, err) {
// TODO(RaisinTen): Replace the error code with 'EACCES' if this lands:
// https://github.com/libuv/libuv/pull/3193
return common.isWindows ?
(exists === true && err?.code === 'EPERM') :
(exists === true && err?.code === 'EACCES');
}

{
const dirname = nextDirPath();
fs.mkdirSync(dirname, { recursive: true });
const root = fs.mkdtempSync(path.join(dirname, 'fs-'));
Expand Down Expand Up @@ -408,27 +401,12 @@ function removeAsync(dir) {
} catch {
}

let isValidState = true;
const exists = fs.existsSync(root);

if (common.isWindows &&
!(exists === true && err?.code === 'EPERM')) {
// TODO(RaisinTen): Replace the error code with 'EACCES' if this lands:
// https://github.com/libuv/libuv/pull/3193
isValidState = false;
} else if (!common.isWindows &&
!(exists === true && err?.code === 'EACCES')) {
isValidState = false;
}

if (!isValidState) {
if (!isValidState(fs.existsSync(root), err)) {
throw err;
}
}

{
// Check endless recursion.
// https://github.com/nodejs/node/issues/34580
const dirname = nextDirPath();
fs.mkdirSync(dirname, { recursive: true });
const root = fs.mkdtempSync(path.join(dirname, 'fs-'));
Expand All @@ -452,23 +430,11 @@ function removeAsync(dir) {
} catch {
}

let isValidState = true;
const exists = fs.existsSync(root);

if (common.isWindows &&
!(exists === true && err?.code === 'EPERM')) {
// TODO(RaisinTen): Replace the error code with 'EACCES' if this
// lands: https://github.com/libuv/libuv/pull/3193
isValidState = false;
} else if (!common.isWindows &&
!(exists === true && err?.code === 'EACCES')) {
isValidState = false;
}

if (!isValidState) {
if (!isValidState(fs.existsSync(root), err)) {
throw err;
}
}));
}
}
}
}