From a3529b604ffb73db34c571dd6ea187584b85ed66 Mon Sep 17 00:00:00 2001 From: Anthony Pena Date: Tue, 23 Apr 2024 00:04:59 +0200 Subject: [PATCH] feat: more repo tests --- tests/slides.test.ts | 77 +++++++++++++++++++++++++++++++++----------- tests/steps.test.ts | 7 ++++ 2 files changed, 66 insertions(+), 18 deletions(-) create mode 100644 tests/steps.test.ts diff --git a/tests/slides.test.ts b/tests/slides.test.ts index 1481ac2..b70a232 100644 --- a/tests/slides.test.ts +++ b/tests/slides.test.ts @@ -3,28 +3,12 @@ import fs from 'node:fs'; import path from 'node:path'; const DOCS_PATH = '../docs'; +const STEPS_PATH = '../steps'; const MARKDOWN_PATH = `${DOCS_PATH}/markdown`; const SCRIPT_SLIDES_JS_PATH = `${DOCS_PATH}/scripts/slides.js`; -const slides: string[] = ( - await import( - 'data:text/javascript;charset=utf-8,' + - encodeURIComponent( - fs - .readFileSync(path.resolve(SCRIPT_SLIDES_JS_PATH), 'utf-8') - .split('\n') - .filter((line) => !line.includes('SfeirThemeInitializer')) - .join('\n') - .replace('function formation() {', 'export default function formation() {') - ) - ) -) - .default() - .map((slide) => slide.path); - describe('should have only defined slides', () => { it.each(slides)('%s referenced in slides.js does exists', (slidePath: string) => { - console.log(slidePath); expect(slidePath).toBeDefined(); expect(fs.existsSync(path.resolve(path.join(MARKDOWN_PATH, slidePath)))).toBeTruthy(); }); @@ -35,8 +19,65 @@ describe('all slides should be used', () => { .readdirSync(path.resolve(MARKDOWN_PATH), { encoding: 'utf-8', recursive: true }) .filter((path) => path.endsWith('.md')); it.each(slideFilesPath)('%s is referenced in slides.js', (slideFilePath: string) => { - console.log(slideFilePath); expect(slideFilePath).toBeDefined(); expect(slides.includes(slideFilePath)).toBeTruthy(); }); }); + +describe('every lab should point out the correct command to run the exercise', () => { + it.each(labSlides)('%s', (slidePath) => { + const slide = readMd(slidePath); + const command = slide.split('\n').find(isLabCommandRow)!; + expect(command).toBeDefined(); + expect(fs.existsSync(path.resolve(path.join(STEPS_PATH, command.split('npm run ')[1].trim())))).toBeTruthy(); + }); +}); + +describe('every exercise should be pointed out in a lab slide', () => { + const exercises = ( + JSON.parse(fs.readFileSync(path.resolve(path.join(STEPS_PATH, 'package.json')), 'utf-8')).workspaces as string[] + ).filter((dir) => !dir.endsWith('-solution') && dir !== 'api'); + const commandsInLabSlides = labSlides + .map(readMd) + .flatMap((slide) => slide.split('\n').filter(isLabCommandRow)!) + .map(getCommandFromLabCommandRow); + it.each(exercises)('%s', (exercisePath) => { + expect(commandsInLabSlides).toContain(exercisePath); + }); +}); +describe('every lab should use a lab slide', () => { + it.each(labSlides)('%s', (slidePath) => { + const slide = readMd(slidePath); + const rows = slide.split('\n').map((rows) => rows.trim()); + expect(rows).toContain(''); + expect(rows).toContain('## Lab'); + }); +}); + +const slides: string[] = (await import(getSlidesJsImport())).default().map((slide) => slide.path); +const labSlides = slides.filter((path) => path.includes('-lab-')); + +function getSlidesJsImport() { + return ( + 'data:text/javascript;charset=utf-8,' + + encodeURIComponent( + fs + .readFileSync(path.resolve(SCRIPT_SLIDES_JS_PATH), 'utf-8') + .split('\n') + .filter((line) => !line.includes('SfeirThemeInitializer')) + .join('\n') + .replace('function formation() {', 'export default function formation() {') + ) + ); +} + +function readMd(mdPath: string) { + return fs.readFileSync(path.resolve(path.join(MARKDOWN_PATH, mdPath)), 'utf-8'); +} + +function isLabCommandRow(row: string): boolean { + return row.includes('npm run'); +} +function getCommandFromLabCommandRow(row: string): string { + return row.split('npm run ')[1].trim(); +} diff --git a/tests/steps.test.ts b/tests/steps.test.ts new file mode 100644 index 0000000..2cfa787 --- /dev/null +++ b/tests/steps.test.ts @@ -0,0 +1,7 @@ +import { describe, expect, it } from 'vitest'; + +describe.todo('every exercise or solution should be declared in the workspace'); +describe.todo('every exercise or solution should have a dedicated script with the same name'); +describe.todo("solution's README.md should be identical to exercise's one"); +describe.todo("exercise's package.json name should have same name as the directory"); +describe.todo("solution's package.json name should have same name as the directory");