Skip to content

Commit

Permalink
Improve db file cleanups for test coverage hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
ZIMkaRU committed Aug 6, 2024
1 parent a5b79b3 commit 202f286
Showing 1 changed file with 34 additions and 26 deletions.
60 changes: 34 additions & 26 deletions test/helpers/helpers.core.js
Original file line number Diff line number Diff line change
@@ -1,47 +1,55 @@
'use strict'

const { promisify } = require('util')
const path = require('path')
const fs = require('fs')

const readdir = promisify(fs.readdir)
const unlink = promisify(fs.unlink)
const mkdir = promisify(fs.mkdir)
const {
readdir,
mkdir,
rm
} = require('node:fs/promises')

const rmDB = async (
dir,
exclude = ['.gitkeep'],
isThrownError
exclude = ['.gitkeep']
) => {
try {
const files = await readdir(dir)
const promisesArr = files.map((file) => {
if (exclude.every(exFile => exFile !== file)) {
return unlink(path.join(dir, file))
const files = await readdir(
dir,
{ withFileTypes: true }
)

for (const dirent of files) {
const { name } = dirent

if (
!dirent.isFile() ||
exclude.some((exFile) => exFile === name)
) {
continue
}

return null
})

const res = await Promise.all(promisesArr)

return res
} catch (err) {
if (!isThrownError) {
return
const filePath = path.join(dir, name)
await rm(
filePath,
{
force: true,
maxRetries: 5,
recursive: true,
retryDelay: 200
}
)
}

throw err
} catch (err) {
console.log(err)
}
}

const rmAllFiles = async (dir, exclude) => {
try {
await rmDB(dir, exclude, true)
await rmDB(dir, exclude)
await mkdir(dir, { recursive: true })
} catch (err) {
if (err.syscall === 'scandir') {
await mkdir(dir)
}
console.log(err)
}
}

Expand Down

0 comments on commit 202f286

Please sign in to comment.