From d4428a6ade0f7831e04d9eeaffc935dd6cc839d5 Mon Sep 17 00:00:00 2001 From: divlo Date: Sun, 15 Nov 2020 12:37:54 +0100 Subject: [PATCH] fix(scripts): better handle error case in solution --- scripts/languages-wrapper/execute.js | 8 ++++++-- scripts/languages-wrapper/execute.py | 2 +- scripts/languages-wrapper/execute.ts | 8 ++++++-- scripts/test.ts | 29 ++++++++++++++++++++-------- 4 files changed, 34 insertions(+), 13 deletions(-) diff --git a/scripts/languages-wrapper/execute.js b/scripts/languages-wrapper/execute.js index 70892af..cf9007c 100644 --- a/scripts/languages-wrapper/execute.js +++ b/scripts/languages-wrapper/execute.js @@ -9,8 +9,12 @@ const main = async () => { const inputFile = await fs.readFile(inputPath) const inputJSON = JSON.parse(inputFile) - const result = solution.apply(null, inputJSON) - await fs.writeFile(outputPath, JSON.stringify(result)) + try { + const result = solution.apply(null, inputJSON) + await fs.writeFile(outputPath, JSON.stringify(result)) + } catch (error) { + console.error(error) + } } main() diff --git a/scripts/languages-wrapper/execute.py b/scripts/languages-wrapper/execute.py index a8ff28b..0ce297a 100644 --- a/scripts/languages-wrapper/execute.py +++ b/scripts/languages-wrapper/execute.py @@ -1,4 +1,4 @@ -import os +import os import json from solution import solution diff --git a/scripts/languages-wrapper/execute.ts b/scripts/languages-wrapper/execute.ts index f60d46a..c858f43 100644 --- a/scripts/languages-wrapper/execute.ts +++ b/scripts/languages-wrapper/execute.ts @@ -11,8 +11,12 @@ const main = async () => { const inputFile = await fs.readFile(inputPath) const inputJSON = JSON.parse(inputFile.toString()) - const result = solution.apply(null, inputJSON) - await fs.writeFile(outputPath, JSON.stringify(result)) + try { + const result = solution.apply(null, inputJSON) + await fs.writeFile(outputPath, JSON.stringify(result)) + } catch (error) { + console.error(error) + } } main() diff --git a/scripts/test.ts b/scripts/test.ts index 1207eff..c75968d 100644 --- a/scripts/test.ts +++ b/scripts/test.ts @@ -8,13 +8,23 @@ import deleteAllFilesExceptOne from './utils/deleteAllFilesExceptOne' import emoji from 'node-emoji' import prettyMilliseconds from 'pretty-ms' import { table } from 'table' + +const handleStderror = async ( + stderr: string, + tempPath: string +): Promise => { + console.log(chalk.bgRedBright.black('Error occurred in your solution :')) + console.log(stderr) + await deleteAllFilesExceptOne(tempPath, '.gitignore') + process.exit(0) +} ;(async () => { const fs = fsWithCallbacks.promises const exec = util.promisify(childProcess.exec) const args = process.argv.slice(2) const [challengeName, solutionName] = args - if (!challengeName || !solutionName) { + if (challengeName == null || solutionName == null) { console.log(` Please specify the challenge and solution name: ${chalk.cyan(`npm run test [challenge-name] [solution-name]`)} @@ -55,7 +65,7 @@ import { table } from 'table' // Determinate the language to execute const solutionFilesName = await fs.readdir(solutionFolderPath) - let solutionFilePath + let solutionFilePath = null for (const solutionFileName of solutionFilesName) { const fileName = solutionFileName .split('.') @@ -66,7 +76,7 @@ import { table } from 'table' break } } - if (!solutionFilePath) { + if (solutionFilePath == null) { console.log(`The ${chalk.red('solution')} file was not found.`) process.exit(0) } @@ -79,7 +89,7 @@ import { table } from 'table' const languageToExecute = languages.find( language => language.extension === extensionSolution ) - if (!languageToExecute) { + if (languageToExecute == null) { console.log(`Sadly, this ${chalk.red('language')} is not supported yet.`) process.exit(0) } @@ -131,11 +141,14 @@ import { table } from 'table' // Execute script (create output.json) try { - await exec(`${languageToExecute.launch} ${executeLanguageTempPath}`) + const { stderr } = await exec( + `${languageToExecute.launch} ${executeLanguageTempPath}` + ) + if (stderr.length !== 0) { + await handleStderror(stderr, tempPath) + } } catch (error) { - console.log(chalk.bgRedBright.black(error.stderr)) - await deleteAllFilesExceptOne(tempPath, '.gitignore') - process.exit(0) + await handleStderror(error.stderr, tempPath) } // Read output.json