fix(tests): cover rule loading from file/path
This commit is contained in:
parent
a9047f7268
commit
85b81ea86c
|
@ -44,6 +44,7 @@
|
|||
"@types/lodash": "4.14.146",
|
||||
"@types/minimatch": "3.0.3",
|
||||
"@types/mocha": "5.2.7",
|
||||
"@types/mock-fs": "^4.10.0",
|
||||
"@types/recursive-readdir": "2.2.0",
|
||||
"@types/sinon": "7.5.0",
|
||||
"@types/sinon-chai": "3.2.3",
|
||||
|
@ -69,6 +70,7 @@
|
|||
"lodash": "4.17.15",
|
||||
"minimatch": "3.0.4",
|
||||
"mocha": "6.2.2",
|
||||
"mock-fs": "^4.10.3",
|
||||
"noicejs": "3.0.0",
|
||||
"nyc": "14.1.1",
|
||||
"recursive-readdir": "2.2.2",
|
||||
|
|
|
@ -0,0 +1,129 @@
|
|||
import { expect } from 'chai';
|
||||
import mockFS from 'mock-fs';
|
||||
import { NullLogger } from 'noicejs';
|
||||
import { spy } from 'sinon';
|
||||
|
||||
import { loadRuleFiles, loadRulePaths } from '../../src/rule';
|
||||
import { VisitorContext } from '../../src/visitor/VisitorContext';
|
||||
import { describeLeaks, itLeaks } from '../helpers/async';
|
||||
|
||||
const EXAMPLE_EMPTY = '{name: foo, definitions: {}, rules: []}';
|
||||
const EXAMPLE_RULES = `{
|
||||
name: foo,
|
||||
definitions: {},
|
||||
rules: [{
|
||||
name: test,
|
||||
level: info,
|
||||
tags: []
|
||||
}]
|
||||
}`;
|
||||
|
||||
describeLeaks('load rule file helper', async () => {
|
||||
itLeaks('should add schema', async () => {
|
||||
mockFS({
|
||||
test: EXAMPLE_EMPTY,
|
||||
});
|
||||
|
||||
const ctx = new VisitorContext({
|
||||
innerOptions: {
|
||||
coerce: false,
|
||||
defaults: false,
|
||||
mutate: false,
|
||||
},
|
||||
logger: NullLogger.global,
|
||||
});
|
||||
const schemaSpy = spy(ctx, 'addSchema');
|
||||
|
||||
const rules = await loadRuleFiles([
|
||||
'test',
|
||||
], ctx);
|
||||
|
||||
mockFS.restore();
|
||||
|
||||
expect(schemaSpy).to.have.been.calledWith('foo');
|
||||
expect(rules.length).to.equal(0);
|
||||
});
|
||||
|
||||
itLeaks('should load rules', async () => {
|
||||
mockFS({
|
||||
test: EXAMPLE_RULES,
|
||||
});
|
||||
|
||||
const ctx = new VisitorContext({
|
||||
innerOptions: {
|
||||
coerce: false,
|
||||
defaults: false,
|
||||
mutate: false,
|
||||
},
|
||||
logger: NullLogger.global,
|
||||
});
|
||||
|
||||
const rules = await loadRuleFiles([
|
||||
'test',
|
||||
], ctx);
|
||||
|
||||
mockFS.restore();
|
||||
|
||||
expect(rules.length).to.equal(1);
|
||||
});
|
||||
});
|
||||
|
||||
describeLeaks('load rule path helper', async () => {
|
||||
itLeaks('should only load matching rule files', async () => {
|
||||
mockFS({
|
||||
test: {
|
||||
'bin.nope': '{}', // will parse but throw on lack of rules
|
||||
'foo.yml': EXAMPLE_RULES,
|
||||
},
|
||||
});
|
||||
|
||||
const ctx = new VisitorContext({
|
||||
innerOptions: {
|
||||
coerce: false,
|
||||
defaults: false,
|
||||
mutate: false,
|
||||
},
|
||||
logger: NullLogger.global,
|
||||
});
|
||||
|
||||
const rules = await loadRulePaths([
|
||||
'test',
|
||||
], ctx);
|
||||
|
||||
mockFS.restore();
|
||||
|
||||
expect(rules.length).to.equal(1);
|
||||
});
|
||||
|
||||
itLeaks('should recursively load rule files', async () => {
|
||||
|
||||
mockFS({
|
||||
test: {
|
||||
'bar-dir': {
|
||||
'bar.yml': EXAMPLE_RULES.replace(/foo/g, 'bar'),
|
||||
},
|
||||
'bin.nope': '{}', // will parse but throw on lack of rules
|
||||
'some-dir': {
|
||||
'foo.yml': EXAMPLE_RULES,
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
const ctx = new VisitorContext({
|
||||
innerOptions: {
|
||||
coerce: false,
|
||||
defaults: false,
|
||||
mutate: false,
|
||||
},
|
||||
logger: NullLogger.global,
|
||||
});
|
||||
|
||||
const rules = await loadRulePaths([
|
||||
'test',
|
||||
], ctx);
|
||||
|
||||
mockFS.restore();
|
||||
|
||||
expect(rules.length).to.equal(2);
|
||||
});
|
||||
});
|
12
yarn.lock
12
yarn.lock
|
@ -250,6 +250,13 @@
|
|||
resolved "https://artifacts.apextoaster.com/repository/group-npm/@types/mocha/-/mocha-5.2.7.tgz#315d570ccb56c53452ff8638738df60726d5b6ea"
|
||||
integrity sha512-NYrtPht0wGzhwe9+/idPaBB+TqkY9AhTvOLMkThm0IoEfLaiVQZwBwyJ5puCkO3AUCWrmcoePjp2mbFocKy4SQ==
|
||||
|
||||
"@types/mock-fs@^4.10.0":
|
||||
version "4.10.0"
|
||||
resolved "https://artifacts.apextoaster.com/repository/group-npm/@types/mock-fs/-/mock-fs-4.10.0.tgz#460061b186993d76856f669d5317cda8a007c24b"
|
||||
integrity sha512-FQ5alSzmHMmliqcL36JqIA4Yyn9jyJKvRSGV3mvPh108VFatX7naJDzSG4fnFQNZFq9dIx0Dzoe6ddflMB2Xkg==
|
||||
dependencies:
|
||||
"@types/node" "*"
|
||||
|
||||
"@types/node@*":
|
||||
version "12.7.8"
|
||||
resolved "https://artifacts.apextoaster.com/repository/group-npm/@types/node/-/node-12.7.8.tgz#cb1bf6800238898bc2ff6ffa5702c3cadd350708"
|
||||
|
@ -2297,6 +2304,11 @@ mocha@6.2.2:
|
|||
yargs-parser "13.1.1"
|
||||
yargs-unparser "1.6.0"
|
||||
|
||||
mock-fs@^4.10.3:
|
||||
version "4.10.3"
|
||||
resolved "https://artifacts.apextoaster.com/repository/group-npm/mock-fs/-/mock-fs-4.10.3.tgz#d0550663dd2b5d33a7c1b8713c6925aab07a04ae"
|
||||
integrity sha512-bcukePBvuA3qovmq0Qtqu9+1APCIGkFHnsozrPIVromt5XFGGgkQSfaN0H6RI8gStHkO/hRgimvS3tooNes4pQ==
|
||||
|
||||
modify-values@^1.0.0:
|
||||
version "1.0.1"
|
||||
resolved "https://artifacts.apextoaster.com/repository/group-npm/modify-values/-/modify-values-1.0.1.tgz#b3939fa605546474e3e3e3c63d64bd43b4ee6022"
|
||||
|
|
Loading…
Reference in New Issue