Skip to content

Commit 470e911

Browse files
RaisinTenjuanarbol
authored andcommitted
test,fs: add fs.rm() tests for .git directories
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: #38810 (comment) Signed-off-by: Darshan Sen <[email protected]> PR-URL: #42410 Reviewed-By: Ben Coe <[email protected]> Reviewed-By: Richard Lau <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 28a770e commit 470e911

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

test/parallel/test-fs-rm.js

+39
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,15 @@ let count = 0;
1616
const nextDirPath = (name = 'rm') =>
1717
path.join(tmpdir.path, `${name}-${count++}`);
1818

19+
const isGitPresent = (() => {
20+
try { execSync('git --version'); return true; } catch { return false; }
21+
})();
22+
23+
function gitInit(gitDirectory) {
24+
fs.mkdirSync(gitDirectory);
25+
execSync('git init', { cwd: gitDirectory });
26+
}
27+
1928
function makeNonEmptyDirectory(depth, files, folders, dirname, createSymLinks) {
2029
fs.mkdirSync(dirname, { recursive: true });
2130
fs.writeFileSync(path.join(dirname, 'text.txt'), 'hello', 'utf8');
@@ -130,6 +139,16 @@ function removeAsync(dir) {
130139
}));
131140
}
132141

142+
// Removing a .git directory should not throw an EPERM.
143+
// Refs: https://github.com/isaacs/rimraf/issues/21.
144+
if (isGitPresent) {
145+
const gitDirectory = nextDirPath();
146+
gitInit(gitDirectory);
147+
fs.rm(gitDirectory, { recursive: true }, common.mustSucceed(() => {
148+
assert.strictEqual(fs.existsSync(gitDirectory), false);
149+
}));
150+
}
151+
133152
// Test the synchronous version.
134153
{
135154
const dir = nextDirPath();
@@ -179,6 +198,15 @@ function removeAsync(dir) {
179198
assert.throws(() => fs.rmSync(dir), { syscall: 'stat' });
180199
}
181200

201+
// Removing a .git directory should not throw an EPERM.
202+
// Refs: https://github.com/isaacs/rimraf/issues/21.
203+
if (isGitPresent) {
204+
const gitDirectory = nextDirPath();
205+
gitInit(gitDirectory);
206+
fs.rmSync(gitDirectory, { recursive: true });
207+
assert.strictEqual(fs.existsSync(gitDirectory), false);
208+
}
209+
182210
// Test the Promises based version.
183211
(async () => {
184212
const dir = nextDirPath();
@@ -230,6 +258,17 @@ function removeAsync(dir) {
230258
}
231259
})().then(common.mustCall());
232260

261+
// Removing a .git directory should not throw an EPERM.
262+
// Refs: https://github.com/isaacs/rimraf/issues/21.
263+
if (isGitPresent) {
264+
(async () => {
265+
const gitDirectory = nextDirPath();
266+
gitInit(gitDirectory);
267+
await fs.promises.rm(gitDirectory, { recursive: true });
268+
assert.strictEqual(fs.existsSync(gitDirectory), false);
269+
})().then(common.mustCall());
270+
}
271+
233272
// Test input validation.
234273
{
235274
const dir = nextDirPath();

0 commit comments

Comments
 (0)