Skip to content

Commit

Permalink
test: fix "fs.rmSync is not a function" for old node.js versions
Browse files Browse the repository at this point in the history
add simplest polyfill for `fs.rmSync(path, {recursive: true})`
  • Loading branch information
owl-from-hogvarts committed May 5, 2021
1 parent 9541460 commit 0dd5a26
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
20 changes: 20 additions & 0 deletions test/rm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const fs = require('fs')
const path = require('path')

/** recursively delete files, symlinks (without following them) and dirs */
module.exports = function rmRecSync (pth) {
pth = path.normalize(pth)

rm(pth)

function rm (pth) {
const pathStat = fs.statSync(pth)
// trick with lstat is used to avoid following symlinks (especially junctions on windows)
if (pathStat.isDirectory() && !fs.lstatSync(pth).isSymbolicLink()) {
fs.readdirSync(pth).forEach((nextPath) => rm(path.join(pth, nextPath)))
fs.rmdirSync(pth)
} else {
fs.unlinkSync(pth)
}
}
}
9 changes: 8 additions & 1 deletion test/test-find-python.js
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,8 @@ test('find-python', { buffered: true }, (t) => {

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

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

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

// remove fixture
fs.rmSync(paths.testDir, { recursive: true })
if (fs.rmSync) {
fs.rmSync(paths.testDir, { recursive: true })
} else {
//
require('./rm.js')(paths.testDir)
}

t.end()
})
Expand Down

0 comments on commit 0dd5a26

Please sign in to comment.