Skip to content

Commit

Permalink
test,fs: add fs.rm() tests for .git directories
Browse files Browse the repository at this point in the history
Git for Windows creates read-only files inside the .git directory.
fs.rm() was built in a way, to work around any EPERM error that can
happen while deleting the .git directory by turning the directory
into a writable one. This change adds a regression test for that.

Refs: isaacs/rimraf#21
Refs: nodejs#38810 (comment)
Signed-off-by: Darshan Sen <raisinten@gmail.com>

PR-URL: nodejs#42410
Reviewed-By: Ben Coe <bencoe@gmail.com>
Reviewed-By: Richard Lau <rlau@redhat.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
RaisinTen authored and xtx1130 committed Apr 25, 2022
1 parent 935896e commit e4c9ac2
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions test/parallel/test-fs-rm.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@ let count = 0;
const nextDirPath = (name = 'rm') =>
path.join(tmpdir.path, `${name}-${count++}`);

const isGitPresent = (() => {
try { execSync('git --version'); return true; } catch { return false; }
})();

function gitInit(gitDirectory) {
fs.mkdirSync(gitDirectory);
execSync('git init', { cwd: gitDirectory });
}

function makeNonEmptyDirectory(depth, files, folders, dirname, createSymLinks) {
fs.mkdirSync(dirname, { recursive: true });
fs.writeFileSync(path.join(dirname, 'text.txt'), 'hello', 'utf8');
Expand Down Expand Up @@ -130,6 +139,16 @@ function removeAsync(dir) {
}));
}

// Removing a .git directory should not throw an EPERM.
// Refs: https://github.com/isaacs/rimraf/issues/21.
if (isGitPresent) {
const gitDirectory = nextDirPath();
gitInit(gitDirectory);
fs.rm(gitDirectory, { recursive: true }, common.mustSucceed(() => {
assert.strictEqual(fs.existsSync(gitDirectory), false);
}));
}

// Test the synchronous version.
{
const dir = nextDirPath();
Expand Down Expand Up @@ -179,6 +198,15 @@ function removeAsync(dir) {
assert.throws(() => fs.rmSync(dir), { syscall: 'stat' });
}

// Removing a .git directory should not throw an EPERM.
// Refs: https://github.com/isaacs/rimraf/issues/21.
if (isGitPresent) {
const gitDirectory = nextDirPath();
gitInit(gitDirectory);
fs.rmSync(gitDirectory, { recursive: true });
assert.strictEqual(fs.existsSync(gitDirectory), false);
}

// Test the Promises based version.
(async () => {
const dir = nextDirPath();
Expand Down Expand Up @@ -230,6 +258,17 @@ function removeAsync(dir) {
}
})().then(common.mustCall());

// Removing a .git directory should not throw an EPERM.
// Refs: https://github.com/isaacs/rimraf/issues/21.
if (isGitPresent) {
(async () => {
const gitDirectory = nextDirPath();
gitInit(gitDirectory);
await fs.promises.rm(gitDirectory, { recursive: true });
assert.strictEqual(fs.existsSync(gitDirectory), false);
})().then(common.mustCall());
}

// Test input validation.
{
const dir = nextDirPath();
Expand Down

0 comments on commit e4c9ac2

Please sign in to comment.