Skip to content

Commit

Permalink
add tests to verify content file structure
Browse files Browse the repository at this point in the history
  • Loading branch information
christian-dinh committed Jul 27, 2021
1 parent 00f82d3 commit 55b590d
Show file tree
Hide file tree
Showing 10 changed files with 4,480 additions and 0 deletions.
87 changes: 87 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
version: 2.1

orbs:
cypress: cypress-io/[email protected]

references:
default_env: &default_env
docker:
- image: cypress/base:12.14.0
working_directory: ~/repo

repo_cache_key_1: &repo_cache_key_1 v1-cypress-repo-{{ arch}}-{{ .Branch }}-{{ .Revision }}
repo_cache_key_2: &repo_cache_key_2 v1-cypress-repo-{{ arch}}-{{ .Branch }}
repo_cache_key_3: &repo_cache_key_3 v1-cypress-repo-{{ arch}}

restore_repo: &restore_repo
restore_cache:
keys:
- *repo_cache_key_1
- *repo_cache_key_2
- *repo_cache_key_3

jobs:
checkout_code:
<<: *default_env
steps:
- *restore_repo
- checkout
- run: npx [email protected] --production=false --frozen-lockfile
- save_cache:
key: *repo_cache_key_1
paths:
- .

verify_formatting:
<<: *default_env
steps:
- *restore_repo
- run:
name: Verify Prettier
command: yarn prettier:verify

verify_compile:
<<: *default_env
steps:
- *restore_repo
- run:
name: Verify Typescript
command: yarn compile

verify_lint:
<<: *default_env
steps:
- *restore_repo
- run:
name: Verify ESLint
command: yarn lint --format junit --output-file /tmp/test-results/eslint.xml
- store_test_results:
path: /tmp/test-results

tests:
<<: *default_env
parallelism: 4
resource_class: large
steps:
- *restore_repo
- run:
name: Run test suite
command: yarn test

workflows:
version: 2
test:
jobs:
- checkout_code
- verify_compile:
requires:
- checkout_code
- verify_lint:
requires:
- checkout_code
- verify_formatting:
requires:
- checkout_code
- tests:
requires:
- checkout_code
3 changes: 3 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": ["@codecademy/eslint-config"]
}
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
.DS_Store
node_modules
package-lock.json
npm-debug.log*
yarn-debug.log*
yarn-error.log*
*.swp
*.swo
tmp
2 changes: 2 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
yarn.lock
1 change: 1 addition & 0 deletions .prettierrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('@codecademy/prettier-config');
51 changes: 51 additions & 0 deletions content.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import fs from 'fs';
import path from 'path';

describe('Codecademy Docs Content', () => {
it('adheres to content file structure', () => {
// file names can only contain alphanumerics and hyphens
const validateName = (pathName: string, name: string) => {
const filteredName = name.replace(/[^A-Za-z0-9.-]/g, '');

// format so that test failures are more helpful
if (filteredName !== name) {
expect(`${pathName} - file name must only include alphanumerics or -`).toBe('');
}
};

// nodes can have n directories of children and at most one .md file with the same name as its parent directory
const checkNode = (nodePath: string) => {
const children = fs.readdirSync(nodePath);

children.forEach((child) => {
const childPath = path.join(nodePath, child);
validateName(childPath, child);

if (fs.statSync(childPath).isDirectory()) {
checkChild(childPath);
} else {
const nodeName = nodePath.split('/').slice(-1)[0];
expect(childPath).toBe(path.join(nodePath, `${nodeName}.md`));
}
});
};

// children can only be directories of nodes
const checkChild = (childPath: string) => {
const nodes = fs.readdirSync(childPath);

nodes.forEach((node) => {
const nodePath = path.join(childPath, node);
validateName(nodePath, node);

// format so that test failures are more helpful
if(!fs.statSync(nodePath).isDirectory()) {
expect(`${nodePath} - expected a directory but got a file`).toBe('');
}
checkNode(nodePath);
});
};

checkChild(path.join(__dirname, 'content'));
});
});
7 changes: 7 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
clearMocks: true,
testMatch: ['**/*test.ts'],
testPathIgnorePatterns: ['<rootDir>/[/\\\\]node_modules[/\\\\]'],
};
23 changes: 23 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "CodecademyDocs",
"devDependencies": {
"@codecademy/eslint-config": "5.0.0",
"@codecademy/prettier-config": "^0.1.10",
"@codecademy/tsconfig": "^0.2.0",
"@types/jest": "^26.0.24",
"@types/node": "^16.4.3",
"eslint": "^7.25.0",
"jest": "^27.0.6",
"prettier": "^2.2.1",
"ts-jest": "^27.0.4",
"typescript": "^4.2.4"
},
"scripts": {
"compile": "tsc --noEmit",
"format": "prettier --ignore-path .prettierignore --write",
"format:verify": "prettier \"./**.ts\" --list-different",
"lint": "eslint \"./**.ts\" --max-warnings 0",
"test": "jest"
},
"version": "1.0.0"
}
16 changes: 16 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"extends": "@codecademy/tsconfig",
"compilerOptions": {
"allowJs": true,
"baseUrl": ".",
"isolatedModules": true,
"lib": ["dom", "dom.iterable", "esnext"],
"noEmit": true,
"noImplicitReturns": true,
"module": "commonjs",
"moduleResolution": "node",
"resolveJsonModule": true
},
"exclude": ["node_modules"],
"include": ["**/*.ts"]
}
Loading

0 comments on commit 55b590d

Please sign in to comment.