UNPKG

valence-test

Version:

Test applications running in Valence and 5250 screens with Fusion5250

107 lines (95 loc) 3.25 kB
const event = require('codeceptjs').event, fs = require('fs'), shortUId = require('short-unique-id'), shortId = new shortUId(), config = require(`${__dirname}/config.json`), helper = require(`${__dirname}/util/helper`), email = require(`${__dirname}/util/email`), fsExtra = require('fs-extra'), outputDir = 'output', baseOutputPath = `${__dirname}/${outputDir}`; module.exports = function () { const me = this; me.success = true; /** * create output folder if it doesn't exist */ if (!fs.existsSync(baseOutputPath)) { fs.mkdirSync(baseOutputPath); } /** * clear folder * @param path * @param removeFolder * @param seconds */ const clearFolder = function (path, removeFolder, seconds) { if (fs.existsSync(path)) { let stats, curPath; fs.readdirSync(path).forEach(function (file) { curPath = path + '/' + file; if (seconds) { stats = fs.lstatSync(curPath); if (stats.isDirectory() && ((new Date().getTime() - stats.ctime) / 1000) > seconds) { clearFolder(curPath, true); } } else { if (fs.lstatSync(curPath).isDirectory()) { clearFolder(curPath, true); } else { try { fs.unlinkSync(curPath); } catch (e) { // } } } }); try { if (removeFolder) { fs.rmdirSync(path); } } catch (e) { // } } }; clearFolder(`${baseOutputPath}/report`); /** * purge old results based on number of days from config */ if (helper.isEmpty(config.purgeResultsDays)) { config.purgeResultsDays = 5; } const purgeSeconds = parseInt(config.purgeResultsDays) * 24 * 60 * 60; clearFolder(baseOutputPath, false, purgeSeconds); /** * listen for a failed test */ event.dispatcher.on(event.test.failed, async function (test, error) { me.success = false; }); /** * after tests are completed create folder with results based on current date/time */ event.dispatcher.on(event.all.after, async function () { let d = new Date(), folderName = `${d.getUTCFullYear()}_${d.getUTCMonth()}_${d.getUTCDate()}_${d.getHours()}_${d.getUTCMinutes()}_${shortId.randomUUID(15)}`, reportFolder; /** * copy results */ try { fsExtra.copySync(`${baseOutputPath}/report`, `${baseOutputPath}/${folderName}`); reportFolder = `${outputDir}/${folderName}`; } catch (err) { reportFolder = `${outputDir}/report`; } /** * email */ if (!helper.isEmpty(config.email)) { email.send(reportFolder, me.success); } }) };