From 202f28685f933a48ecc6c5bd120de17d4f43a435 Mon Sep 17 00:00:00 2001 From: Vladimir Voronkov Date: Tue, 6 Aug 2024 13:23:21 +0300 Subject: [PATCH] Improve db file cleanups for test coverage hooks --- test/helpers/helpers.core.js | 60 ++++++++++++++++++++---------------- 1 file changed, 34 insertions(+), 26 deletions(-) diff --git a/test/helpers/helpers.core.js b/test/helpers/helpers.core.js index 9428a074..12348ede 100644 --- a/test/helpers/helpers.core.js +++ b/test/helpers/helpers.core.js @@ -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) } }