Skip to content

Commit 0dd5a26

Browse files
test: fix "fs.rmSync is not a function" for old node.js versions
add simplest polyfill for `fs.rmSync(path, {recursive: true})`
1 parent 9541460 commit 0dd5a26

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

test/rm.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
const fs = require('fs')
2+
const path = require('path')
3+
4+
/** recursively delete files, symlinks (without following them) and dirs */
5+
module.exports = function rmRecSync (pth) {
6+
pth = path.normalize(pth)
7+
8+
rm(pth)
9+
10+
function rm (pth) {
11+
const pathStat = fs.statSync(pth)
12+
// trick with lstat is used to avoid following symlinks (especially junctions on windows)
13+
if (pathStat.isDirectory() && !fs.lstatSync(pth).isSymbolicLink()) {
14+
fs.readdirSync(pth).forEach((nextPath) => rm(path.join(pth, nextPath)))
15+
fs.rmdirSync(pth)
16+
} else {
17+
fs.unlinkSync(pth)
18+
}
19+
}
20+
}

test/test-find-python.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,8 @@ test('find-python', { buffered: true }, (t) => {
309309

310310
// using "junction" to avoid permission error
311311
fs.symlinkSync(paths.pythonDir, path.resolve(paths.testDir, testString), 'junction')
312+
console.log('🚀 ~ file: test-find-python.js ~ line 312 ~ awaitt.test ~ path.resolve(paths.testDir, testString)', path.resolve(paths.testDir, testString))
313+
console.log('🚀 ~ file: test-find-python.js ~ line 312 ~ awaitt.test ~ paths.pythonDir', paths.pythonDir)
312314

313315
const { pythonFinderInstance, result } = promisifyPythonFinder(path.resolve(paths.testDir, 'python'))
314316

@@ -322,7 +324,12 @@ test('find-python', { buffered: true }, (t) => {
322324
})
323325

324326
// remove fixture
325-
fs.rmSync(paths.testDir, { recursive: true })
327+
if (fs.rmSync) {
328+
fs.rmSync(paths.testDir, { recursive: true })
329+
} else {
330+
//
331+
require('./rm.js')(paths.testDir)
332+
}
326333

327334
t.end()
328335
})

0 commit comments

Comments
 (0)