Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test,fs: add fs.rm() tests for .git directories #42410

Merged
Changes from all commits
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
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