Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: separate test and doc checks
- Loading branch information
Showing
7 changed files
with
208 additions
and
187 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
#!/usr/bin/env node | ||
|
||
// @flow | ||
|
||
import fs from 'fs'; | ||
import path from 'path'; | ||
import { | ||
getRules, | ||
isFile | ||
} from './utilities'; | ||
|
||
const windows = (array, size) => { | ||
const output = []; | ||
|
||
for (let ii = 0; ii < array.length - size + 1; ii++) { | ||
output.push(array.slice(ii, ii + size)); | ||
} | ||
|
||
return output; | ||
}; | ||
|
||
const getDocIndexRules = () => { | ||
const content = fs.readFileSync(path.resolve(__dirname, '../../.README/README.md'), 'utf-8'); | ||
|
||
const rules = content.split('\n').map((line) => { | ||
const match = /^{"gitdown": "include", "file": "([^"]+)"}$/.exec(line); | ||
|
||
if (match === null) { | ||
return null; | ||
} else { | ||
return match[1].replace('./rules/', '').replace('.md', ''); | ||
} | ||
}).filter((rule) => { | ||
return rule !== null; | ||
}); | ||
|
||
if (rules.length === 0) { | ||
throw new Error('Docs checker is broken - it could not extract rules from docs index file.'); | ||
} | ||
|
||
return rules; | ||
}; | ||
|
||
const hasCorrectAssertions = (docPath, name) => { | ||
const content = fs.readFileSync(docPath, 'utf-8'); | ||
|
||
const match = /<!-- assertions ([a-zA-Z]+) -->/.exec(content); | ||
|
||
if (match === null) { | ||
return false; | ||
} else { | ||
return match[1] === name; | ||
} | ||
}; | ||
|
||
/** | ||
* Performed checks: | ||
* - file `/.README/rules/<rule>.md` exists | ||
* - file `/.README/rules/<rule>.md` contains correct assertions placeholder (`<!-- assertions ... -->`) | ||
* - rule is included in gitdown directive in `/.README/README.md` | ||
* - rules in `/.README/README.md` are alphabetically sorted | ||
*/ | ||
const checkDocs = (rulesNames) => { | ||
const docIndexRules = getDocIndexRules(); | ||
|
||
const sorted = windows(docIndexRules, 2) | ||
.every((chunk) => { | ||
return chunk[0] < chunk[1]; | ||
}); | ||
|
||
if (!sorted) { | ||
throw new Error('Rules are not alphabetically sorted in `.README/README.md` file.'); | ||
} | ||
|
||
const invalid = rulesNames.filter((names) => { | ||
const docPath = path.resolve(__dirname, '../../.README/rules', names[1] + '.md'); | ||
const docExists = isFile(docPath); | ||
const inIndex = docIndexRules.indexOf(names[1]) !== -1; | ||
const hasAssertions = docExists ? hasCorrectAssertions(docPath, names[0]) : false; | ||
|
||
return !(docExists && inIndex && hasAssertions); | ||
}); | ||
|
||
if (invalid.length > 0) { | ||
const invalidList = invalid | ||
.map((names) => { | ||
return names[0]; | ||
}).join(', '); | ||
|
||
throw new Error( | ||
'Docs checker encountered an error in: ' + invalidList + '. ' + | ||
'Make sure that for every rule you created documentation file with assertions placeholder in camelCase ' + | ||
'and included the file path in `.README/README.md` file.' | ||
); | ||
} | ||
}; | ||
|
||
checkDocs(getRules()); |
Oops, something went wrong.