|
| 1 | +import fs from 'fs'; |
| 2 | +import path from 'path'; |
| 3 | + |
| 4 | +describe('Codecademy Docs Content', () => { |
| 5 | + it('adheres to content file structure', () => { |
| 6 | + // file names can only contain alphanumerics and hyphens |
| 7 | + const validateName = (pathName: string, name: string) => { |
| 8 | + const filteredName = name.replace(/[^A-Za-z0-9.-]/g, ''); |
| 9 | + |
| 10 | + // format so that test failures are more helpful |
| 11 | + if (filteredName !== name) { |
| 12 | + expect(`${pathName} - file name must only include alphanumerics or -`).toBe(''); |
| 13 | + } |
| 14 | + }; |
| 15 | + |
| 16 | + // nodes can have n directories of children and at most one .md file with the same name as its parent directory |
| 17 | + const checkNode = (nodePath: string) => { |
| 18 | + const children = fs.readdirSync(nodePath); |
| 19 | + |
| 20 | + children.forEach((child) => { |
| 21 | + const childPath = path.join(nodePath, child); |
| 22 | + validateName(childPath, child); |
| 23 | + |
| 24 | + if (fs.statSync(childPath).isDirectory()) { |
| 25 | + checkChild(childPath); |
| 26 | + } else { |
| 27 | + const nodeName = nodePath.split('/').slice(-1)[0]; |
| 28 | + expect(childPath).toBe(path.join(nodePath, `${nodeName}.md`)); |
| 29 | + } |
| 30 | + }); |
| 31 | + }; |
| 32 | + |
| 33 | + // children can only be directories of nodes |
| 34 | + const checkChild = (childPath: string) => { |
| 35 | + const nodes = fs.readdirSync(childPath); |
| 36 | + |
| 37 | + nodes.forEach((node) => { |
| 38 | + const nodePath = path.join(childPath, node); |
| 39 | + validateName(nodePath, node); |
| 40 | + |
| 41 | + // format so that test failures are more helpful |
| 42 | + if(!fs.statSync(nodePath).isDirectory()) { |
| 43 | + expect(`${nodePath} - expected a directory but got a file`).toBe(''); |
| 44 | + } |
| 45 | + checkNode(nodePath); |
| 46 | + }); |
| 47 | + }; |
| 48 | + |
| 49 | + checkChild(path.join(__dirname, 'content')); |
| 50 | + }); |
| 51 | +}); |
0 commit comments