Skip to content

Commit

Permalink
feat: Running tests for a specific day (#40)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
artur1989 committed Dec 25, 2023
1 parent ae4c9f2 commit 35f570b
Showing 1 changed file with 42 additions and 19 deletions.
61 changes: 42 additions & 19 deletions scripts/test.cjs
Original file line number Diff line number Diff line change
@@ -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) => {
Expand All @@ -22,28 +31,42 @@ 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`;
},
};

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);
Expand All @@ -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}`);
}
Expand Down

0 comments on commit 35f570b

Please sign in to comment.