From ae4c9f2e0139584340400b348ca581291c6ff6dc Mon Sep 17 00:00:00 2001 From: artur1989 Date: Mon, 25 Dec 2023 16:49:39 +0100 Subject: [PATCH] feat: Running tests for a specific day (przeprogramowani#40) Changes: * Added the option to run tests for a particular day. You can use a date like "YYYY-MM-DD", a number from 1 to 24, or run all tests if no specific argument is given. * Removed "test:all" script --- package.json | 3 +- scripts/test.cjs | 121 ++++++++++++++++++++++++++++++++++++++--------- 2 files changed, 100 insertions(+), 24 deletions(-) diff --git a/package.json b/package.json index d362796..f70dd7f 100644 --- a/package.json +++ b/package.json @@ -9,8 +9,7 @@ "create": "node ./scripts/create-template.js", "create:missing": "node ./scripts/create-template.js missing", "create:month": "node ./scripts/create-month-templates.js", - "test": "node ./scripts/test.cjs", - "test:all": "jest" + "test": "node ./scripts/test.cjs" }, "devDependencies": { "@types/jest": "29.5.10", diff --git a/scripts/test.cjs b/scripts/test.cjs index 5ec4984..ab44990 100644 --- a/scripts/test.cjs +++ b/scripts/test.cjs @@ -2,31 +2,108 @@ const { exec } = require('child_process'); const { existsSync } = require('fs'); const { join } = require('path'); -const date = new Date(); -const year = date.getFullYear(); -const month = String(date.getMonth() + 1).padStart(2, '0'); -const day = String(date.getDate()).padStart(2, '0'); -const testPath = `./tasks/${year}-${month}-${day}/index.test.ts`; +const ADVENT_OF_FRONTEND_YEAR = '2023'; +const ADVENT_OF_FRONTEND_MONTH = '12'; -const fullTestPath = join(process.cwd(), testPath); +const VALID_DATE_PATTERN = /^\d{4}-\d{2}-\d{2}$/; -if (existsSync(fullTestPath)) { +function buildPath(type, testDateArg) { + const dateHandlers = { + 'date': (testDateArg) => { + const date = new Date(testDateArg); + const year = date.getFullYear(); + const month = String(date.getMonth() + 1).padStart(2, '0'); + const day = String(date.getDate()).padStart(2, '0'); + return `./tasks/${year}-${month}-${day}/index.test.ts`; + }, + 'numeric': (testDateArg) => { + const testDay = parseInt(testDateArg); + if (testDay < 1 || testDay > 24) { + console.error('Podaj dzień z zakresu od 1 do 24.'); + return; + } + const year = ADVENT_OF_FRONTEND_YEAR; + const month = ADVENT_OF_FRONTEND_MONTH; + const day = String(testDay).padStart(2, '0'); + return `./tasks/${year}-${month}-${day}/index.test.ts`; + }, + }; + + return dateHandlers[type](testDateArg); +} + +function isValidDate(str) { + const date = new Date(str); + return !isNaN(date) && + VALID_DATE_PATTERN.test(str); +} + +function isNumeric(str) { + return typeof str === 'string' && + !isNaN(str) && + !isNaN(parseInt(str)); +} + +function runTests(testPath) { exec(`jest --colors ${testPath}`, (error, stdout, stderr) => { - if (error) { - console.error(`exec error: ${error}`); - return; - } - console.log(`stdout: ${stdout}`); - console.error(`stderr: ${stderr}`); + handleTestExecution(error, stdout, stderr); }); -} else { - console.log(`Nie znaleziono folderu z dzisiejszą datą: ${testPath}. Uruchamiam wszystkie testy.`); - exec('jest --colors', (error, stdout, stderr) => { - if (error) { - console.error(`exec error: ${error}`); - return; - } - console.log(`stdout: ${stdout}`); - console.error(`stderr: ${stderr}`); +} + +function runAllTests() { + exec(`jest --colors`, (error, stdout, stderr) => { + handleTestExecution(error, stdout, stderr); }); +} + +function handleTestExecution(error, stdout, stderr) { + if (error) { + console.error(`exec error: ${error}`); + return; + } + console.log(`stdout: ${stdout}`); + console.error(`stderr: ${stderr}`); +} + +function getValidDateHandler(testDateArg) { + if (isValidDate(testDateArg)) { + return 'date'; + } else if (isNumeric(testDateArg)) { + return 'numeric'; + } else { + return null; + } +} + +function getTestPath(testDateArg) { + const handlerType = getValidDateHandler(testDateArg); + + if (handlerType) { + return buildPath(handlerType, testDateArg); + } + + console.error('Podany argument jest nieprawidłowy lub format daty jest nieobsługiwany.'); +} + +function runTestsForDay(testDateArg) { + const testPath = getTestPath(testDateArg); + if (!testPath) { + return; + } + + const fullTestPath = join(process.cwd(), testPath); + + if (existsSync(fullTestPath)) { + runTests(testPath); + } else { + console.log(`Nie znaleziono folderu z datą: ${testPath}.`); + } +} + +const testDateArg = process.argv[2]; + +if (testDateArg) { + runTestsForDay(testDateArg); +} else { + runAllTests(); } \ No newline at end of file