/
checkTests.js
66 lines (53 loc) · 1.66 KB
/
checkTests.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// @flow
import fs from 'fs';
import path from 'path';
import {
getRules,
isFile,
} from './utilities';
const getTestIndexRules = () => {
const content = fs.readFileSync(path.resolve(__dirname, '../../tests/rules/index.js'), 'utf-8');
const result = content.split('\n').reduce((acc, line) => {
if (acc.inRulesArray) {
if (line === '];') {
acc.inRulesArray = false;
} else {
acc.rules.push(line.replace(/^\s*'([^']+)',?$/, '$1'));
}
} else if (line === 'const reportingRules = [') {
acc.inRulesArray = true;
}
return acc;
}, {
inRulesArray: false,
rules: [],
});
const rules = result.rules;
if (rules.length === 0) {
throw new Error('Tests checker is broken - it could not extract rules from test index file.');
}
return rules;
};
/**
* Performed checks:
* - file `/tests/rules/assertions/<rule>.js` exists
* - rule is included in `reportingRules` variable in `/tests/rules/index.js`
*/
const checkTests = (rulesNames) => {
const testIndexRules = getTestIndexRules();
const invalid = rulesNames.filter((names) => {
const testExists = isFile(path.resolve(__dirname, '../../tests/rules/assertions', names[0] + '.js'));
const inIndex = testIndexRules.includes(names[1]);
return !(testExists && inIndex);
});
if (invalid.length > 0) {
const invalidList = invalid.map((names) => {
return names[0];
}).join(', ');
throw new Error(
'Tests checker encountered an error in: ' + invalidList + '. ' +
'Make sure that for every rule you created test suite and included the rule name in `tests/rules/index.js` file.',
);
}
};
checkTests(getRules());