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
reindent
  • Loading branch information
RaisinTen committed Jun 3, 2021
commit ea8e0a57dbe1bd80acf19810bdc25c3987918e56
196 changes: 98 additions & 98 deletions test/parallel/test-fs-rm.js
Original file line number Diff line number Diff line change
Expand Up @@ -289,58 +289,32 @@ 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
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');

fs.mkdirSync(dirname, { recursive: true });
fs.writeFileSync(filePath, 'hello');

fs.chmodSync(filePath, 0o444);
fs.chmodSync(dirname, 0o444);

let err = null;

try {
fs.rmSync(filePath, { force: true });
} catch (_err) {
err = _err;
// 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'));
}

try {
fs.chmodSync(dirname, 0o777);
} catch {
}

try {
fs.chmodSync(filePath, 0o777);
} catch {
}
{
const dirname = nextDirPath();
const filePath = path.join(dirname, 'text.txt');

if (!isValidState(fs.existsSync(filePath), err)) {
throw err;
}
}
fs.mkdirSync(dirname, { recursive: true });
fs.writeFileSync(filePath, 'hello');

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

fs.mkdirSync(dirname, { recursive: true });
fs.writeFileSync(filePath, 'hello');
let err = null;

fs.chmodSync(filePath, 0o444);
fs.chmodSync(dirname, 0o444);
try {
fs.rmSync(filePath, { force: true });
} catch (_err) {
err = _err;
}

fs.rm(filePath, { force: true }, common.mustCall((err) => {
try {
fs.chmodSync(dirname, 0o777);
} catch {
Expand All @@ -354,72 +328,69 @@ function removeAsync(dir) {
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
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();
const filePath = path.join(dirname, 'text.txt');

{
const dirname = nextDirPath();
fs.mkdirSync(dirname, { recursive: true });
const root = fs.mkdtempSync(path.join(dirname, 'fs-'));
const middle = path.join(root, 'middle');
const leaf = path.join(middle, 'leaf');
fs.mkdirSync(dirname, { recursive: true });
fs.writeFileSync(filePath, 'hello');

fs.mkdirSync(middle);
fs.mkdirSync(leaf);
fs.chmodSync(filePath, 0o444);
fs.chmodSync(dirname, 0o444);

fs.chmodSync(leaf, 0o555);
fs.chmodSync(middle, 0o555);
fs.rm(filePath, { force: true }, common.mustCall((err) => {
try {
fs.chmodSync(dirname, 0o777);
} catch {
}

let err = null;
try {
fs.chmodSync(filePath, 0o777);
} catch {
}

try {
fs.rmSync(root, { recursive: true });
} catch (_err) {
err = _err;
if (!isValidState(fs.existsSync(filePath), err)) {
throw err;
}
}));
}
}

try {
fs.chmodSync(middle, 0o777);
} catch {
// On Windows, we are not allowed to delete a read-only directory.
{
// 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');
}

try {
fs.chmodSync(leaf, 0o777);
} catch {
}
{
const dirname = nextDirPath();
fs.mkdirSync(dirname, { recursive: true });
const root = fs.mkdtempSync(path.join(dirname, 'fs-'));
const middle = path.join(root, 'middle');
const leaf = path.join(middle, 'leaf');

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

{
const dirname = nextDirPath();
fs.mkdirSync(dirname, { recursive: true });
const root = fs.mkdtempSync(path.join(dirname, 'fs-'));
const middle = path.join(root, 'middle');
const leaf = path.join(middle, 'leaf');
fs.chmodSync(leaf, 0o555);
fs.chmodSync(middle, 0o555);

fs.mkdirSync(middle);
fs.mkdirSync(leaf);
let err = null;

fs.chmodSync(leaf, 0o555);
fs.chmodSync(middle, 0o555);
try {
fs.rmSync(root, { recursive: true });
} catch (_err) {
err = _err;
}

fs.rm(root, { recursive: true }, common.mustCall((err) => {
try {
fs.chmodSync(middle, 0o777);
} catch {
Expand All @@ -433,8 +404,37 @@ function removeAsync(dir) {
if (!isValidState(fs.existsSync(root), err)) {
throw err;
}
}));
}
}

{
const dirname = nextDirPath();
fs.mkdirSync(dirname, { recursive: true });
const root = fs.mkdtempSync(path.join(dirname, 'fs-'));
const middle = path.join(root, 'middle');
const leaf = path.join(middle, 'leaf');

fs.mkdirSync(middle);
fs.mkdirSync(leaf);

fs.chmodSync(leaf, 0o555);
fs.chmodSync(middle, 0o555);

fs.rm(root, { recursive: true }, common.mustCall((err) => {
try {
fs.chmodSync(middle, 0o777);
} catch {
}

try {
fs.chmodSync(leaf, 0o777);
} catch {
}

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