From dc2d70daee76689d46a46a46c67cf29efbe3beae Mon Sep 17 00:00:00 2001 From: SimeonC <1085899+SimeonC@users.noreply.github.com> Date: Mon, 5 Aug 2024 14:32:42 +0900 Subject: [PATCH] fix: support reportDir mocha reporter option MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Adding other fixes to make file paths less “hardcoded” --- lib/cli.js | 12 +++++++++--- lib/json-stream.reporter.js | 9 ++++++--- lib/utility.js | 13 +++++++++---- 3 files changed, 24 insertions(+), 10 deletions(-) diff --git a/lib/cli.js b/lib/cli.js index f616e8c..4a7442e 100755 --- a/lib/cli.js +++ b/lib/cli.js @@ -103,9 +103,15 @@ async function start() { console.error( 'Some test suites likely terminated without passing on results, exit with error' ); - const missingTestResults = testSuitePaths.filter( - (path) => !timeMap.get(path) - ); + const missingTestResults = [].concat(testSuitePaths); + timeMap.forEach((_, pathEnd) => { + const index = missingTestResults.findIndex((p) => + path.resolve(p).endsWith(pathEnd) + ); + if (index > -1) { + missingTestResults.splice(index, 1); + } + }); console.log( `The following test suites are missing results: ${missingTestResults}` ); diff --git a/lib/json-stream.reporter.js b/lib/json-stream.reporter.js index 14dea14..381a183 100644 --- a/lib/json-stream.reporter.js +++ b/lib/json-stream.reporter.js @@ -10,7 +10,7 @@ var constants = require('mocha/lib/runner').constants; var path = require('path'); var fs = require('fs'); -const { resultsPath } = require('./shared-config'); +const { resultsPath: defaultResultsPath } = require('./shared-config'); const { EVENT_SUITE_END } = constants; @@ -43,7 +43,7 @@ function JSONStreamCustom(runner, options) { } runner.on(EVENT_SUITE_END, function () { - writeFile(cleanStatistics()); + writeFile.apply(self, [cleanStatistics()]); }); } @@ -54,10 +54,13 @@ function calculateDuration(start, end) { } function writeFile(statistics) { + const resultsPath = this.options.reporterOptions.reportDir + ? path.join(process.cwd(), this.options.reporterOptions.reportDir) + : defaultResultsPath; // replace forward and backward slash with _ to generate filename const fileName = statistics.file.replace(/\\|\//g, '_'); if (!fs.existsSync(resultsPath)) { - fs.mkdirSync(resultsPath); + fs.mkdirSync(resultsPath, { recursive: true }); } const specResultPath = path.join(resultsPath, `${fileName}.json`); fs.writeFileSync(specResultPath, JSON.stringify(statistics, null, 2)); diff --git a/lib/utility.js b/lib/utility.js index 0ed59c9..bf27f1c 100644 --- a/lib/utility.js +++ b/lib/utility.js @@ -24,10 +24,15 @@ function generateWeightsFile(specWeights, totalDuration, totalWeight) { }); const weightsJson = JSON.stringify(specWeights); try { - fs.writeFileSync(`${settings.weightsJSON}`, weightsJson, 'utf8'); - console.log('Weights file generated.') - } catch(e) { - console.error(e) + const filepath = path.join(process.cwd(), `${settings.weightsJSON}`); + const directory = path.dirname(filepath); + if (!fs.existsSync(directory)) { + fs.mkdirSync(directory, { recursive: true }); + } + fs.writeFileSync(filepath, weightsJson, 'utf8'); + console.log('Weights file generated.'); + } catch (e) { + console.error(e); } }