Skip to content

Commit

Permalink
feat: Running tests for a specific day (przeprogramowani#40)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
artur1989 committed Dec 25, 2023
1 parent 0f2f4b9 commit ae4c9f2
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 24 deletions.
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
121 changes: 99 additions & 22 deletions scripts/test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

0 comments on commit ae4c9f2

Please sign in to comment.