From 35f570b3cac6f046dd34ebe1bbcb072d0681d529 Mon Sep 17 00:00:00 2001 From: artur1989 Date: Mon, 25 Dec 2023 22:57:57 +0100 Subject: [PATCH] feat: Running tests for a specific day (przeprogramowani#40) Changes: * Updated the search logic when only a day is provided. Now, it looks for the latest folder with the date closest to the current one and matching the given day. For example, if we run npm test 6, and there are folders with dates 2023-12-06 and 2024-12-06, while the current date is 2024-12-24, it will select the folder with the date 2024-12-06. This change is made to support future editions of Advent of Frontend. --- scripts/test.cjs | 61 +++++++++++++++++++++++++++++++++--------------- 1 file changed, 42 insertions(+), 19 deletions(-) diff --git a/scripts/test.cjs b/scripts/test.cjs index ab44990..e490dc4 100644 --- a/scripts/test.cjs +++ b/scripts/test.cjs @@ -1,12 +1,21 @@ const { exec } = require('child_process'); -const { existsSync } = require('fs'); +const { existsSync, readdirSync } = require('fs'); const { join } = require('path'); -const ADVENT_OF_FRONTEND_YEAR = '2023'; -const ADVENT_OF_FRONTEND_MONTH = '12'; - const VALID_DATE_PATTERN = /^\d{4}-\d{2}-\d{2}$/; +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 buildPath(type, testDateArg) { const dateHandlers = { 'date': (testDateArg) => { @@ -22,9 +31,35 @@ function buildPath(type, testDateArg) { 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'); + + const directories = readdirSync('./tasks'); + + let selectedDirectory; + directories + .sort() + .forEach(dir => { + const dirAsDate = new Date(dir); + + if (isValidDate(dir) + && dirAsDate <= new Date() + && dirAsDate.getDate() === testDay) { + selectedDirectory = dir; + } + }); + const now = new Date(); + const nowYear = now.getFullYear(); + const nowMonth = now.getMonth() + 1; + + if (!selectedDirectory) { + console.error(`Nie znaleziono folderu z datą: ${nowYear}-${nowMonth}-${day}.`); + return; + } + + const selectedDate = new Date(selectedDirectory); + const year = String(selectedDate.getFullYear()).padStart(2, '0'); + const month = String(selectedDate.getMonth() + 1).padStart(2, '0'); return `./tasks/${year}-${month}-${day}/index.test.ts`; }, }; @@ -32,18 +67,6 @@ function buildPath(type, testDateArg) { 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) => { handleTestExecution(error, stdout, stderr); @@ -59,8 +82,8 @@ function runAllTests() { function handleTestExecution(error, stdout, stderr) { if (error) { console.error(`exec error: ${error}`); - return; } + console.log(`stdout: ${stdout}`); console.error(`stderr: ${stderr}`); }