Skip to content

Commit a195b4f

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 1aff9fa commit a195b4f

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

test/parallel/test-fs-rm.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,15 @@ let count = 0;
1515
const nextDirPath = (name = 'rm') =>
1616
path.join(tmpdir.path, `${name}-${count++}`);
1717

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

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

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

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

0 commit comments

Comments
 (0)