2019-06-29 21:11:20 +00:00
|
|
|
import { expect } from 'chai';
|
2019-06-30 23:18:11 +00:00
|
|
|
import { ConsoleLogger } from 'noicejs';
|
|
|
|
import { mock } from 'sinon';
|
|
|
|
|
|
|
|
import { makeSelector, resolveRules, Rule, visitRules } from 'src/rule';
|
|
|
|
import { VisitorContext } from 'src/visitor/context';
|
2019-06-29 21:11:20 +00:00
|
|
|
|
|
|
|
const TEST_RULES = [new Rule({
|
|
|
|
name: 'foo',
|
|
|
|
desc: '',
|
|
|
|
level: 'info',
|
2019-06-29 21:27:10 +00:00
|
|
|
tags: ['all', 'foo'],
|
2019-06-29 21:11:20 +00:00
|
|
|
check: {},
|
|
|
|
select: '$',
|
|
|
|
}), new Rule({
|
|
|
|
name: 'bar',
|
|
|
|
desc: '',
|
|
|
|
level: 'warn',
|
2019-06-29 21:27:10 +00:00
|
|
|
tags: ['all', 'test'],
|
2019-06-29 21:11:20 +00:00
|
|
|
check: {},
|
|
|
|
select: '$',
|
|
|
|
}), new Rule({
|
|
|
|
name: 'bin',
|
|
|
|
desc: '',
|
|
|
|
level: 'warn',
|
2019-06-29 21:27:10 +00:00
|
|
|
tags: ['all', 'test'],
|
2019-06-29 21:11:20 +00:00
|
|
|
check: {},
|
|
|
|
select: '$',
|
|
|
|
})];
|
|
|
|
|
|
|
|
describe('rule resolver', () => {
|
|
|
|
describe('include by level', () => {
|
|
|
|
it('should include info rules', async () => {
|
2019-06-29 21:27:10 +00:00
|
|
|
const info = await resolveRules(TEST_RULES, makeSelector({
|
2019-06-29 21:11:20 +00:00
|
|
|
includeLevel: ['info'],
|
2019-06-29 21:27:10 +00:00
|
|
|
}));
|
2019-06-29 21:11:20 +00:00
|
|
|
|
|
|
|
expect(info.length).to.equal(1);
|
|
|
|
expect(info[0]).to.equal(TEST_RULES[0]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should include warn rules', async () => {
|
2019-06-29 21:27:10 +00:00
|
|
|
const info = await resolveRules(TEST_RULES, makeSelector({
|
2019-06-29 21:11:20 +00:00
|
|
|
includeLevel: ['warn'],
|
2019-06-29 21:27:10 +00:00
|
|
|
}));
|
2019-06-29 21:11:20 +00:00
|
|
|
|
|
|
|
expect(info.length).to.equal(2);
|
|
|
|
expect(info[0]).to.equal(TEST_RULES[1]);
|
|
|
|
expect(info[1]).to.equal(TEST_RULES[2]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('include by name', () => {
|
|
|
|
it('should include foo rules', async () => {
|
2019-06-29 21:27:10 +00:00
|
|
|
const rules = await resolveRules(TEST_RULES, makeSelector({
|
2019-06-29 21:11:20 +00:00
|
|
|
includeName: ['foo'],
|
2019-06-29 21:27:10 +00:00
|
|
|
}));
|
2019-06-29 21:11:20 +00:00
|
|
|
|
|
|
|
expect(rules.length).to.equal(1);
|
|
|
|
expect(rules[0].name).to.equal('foo');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('include by tag', () => {
|
|
|
|
it('should include test rules', async () => {
|
2019-06-29 21:27:10 +00:00
|
|
|
const rules = await resolveRules(TEST_RULES, makeSelector({
|
2019-06-29 21:11:20 +00:00
|
|
|
includeTag: ['test'],
|
2019-06-29 21:27:10 +00:00
|
|
|
}));
|
2019-06-29 21:11:20 +00:00
|
|
|
|
|
|
|
expect(rules.length).to.equal(2);
|
|
|
|
expect(rules[0]).to.equal(TEST_RULES[1]);
|
|
|
|
expect(rules[1]).to.equal(TEST_RULES[2]);
|
|
|
|
});
|
|
|
|
});
|
2019-06-29 21:27:10 +00:00
|
|
|
|
|
|
|
describe('exclude by name', () => {
|
|
|
|
it('should exclude foo rules', async () => {
|
|
|
|
const rules = await resolveRules(TEST_RULES, makeSelector({
|
|
|
|
excludeName: ['foo'],
|
|
|
|
includeTag: ['all'],
|
|
|
|
}));
|
|
|
|
|
|
|
|
expect(rules.length).to.equal(2);
|
|
|
|
expect(rules[0]).to.equal(TEST_RULES[1]);
|
|
|
|
expect(rules[1]).to.equal(TEST_RULES[2]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('exclude by tag', () => {
|
|
|
|
it('should exclude test rules', async () => {
|
|
|
|
const rules = await resolveRules(TEST_RULES, makeSelector({
|
|
|
|
excludeTag: ['test'],
|
|
|
|
includeTag: ['all'],
|
|
|
|
}));
|
|
|
|
|
|
|
|
expect(rules.length).to.equal(1);
|
|
|
|
expect(rules[0]).to.equal(TEST_RULES[0]);
|
|
|
|
});
|
|
|
|
});
|
2019-06-29 21:11:20 +00:00
|
|
|
});
|
2019-06-30 23:18:11 +00:00
|
|
|
|
|
|
|
describe('rule visitor', () => {
|
|
|
|
it('should only call visit for selected items', async () => {
|
|
|
|
const ctx = new VisitorContext({
|
|
|
|
coerce: false,
|
|
|
|
defaults: false,
|
|
|
|
logger: new ConsoleLogger(),
|
2019-07-11 05:16:00 +00:00
|
|
|
mutate: false,
|
2019-06-30 23:18:11 +00:00
|
|
|
});
|
|
|
|
const data = {};
|
|
|
|
const rule = new Rule({
|
|
|
|
name: 'foo',
|
|
|
|
desc: '',
|
|
|
|
level: 'info',
|
|
|
|
tags: [],
|
|
|
|
select: '$',
|
|
|
|
check: {},
|
|
|
|
});
|
|
|
|
|
|
|
|
const mockRule = mock(rule);
|
|
|
|
mockRule.expects('visit').never();
|
|
|
|
|
|
|
|
const pickStub = mockRule.expects('pick').once().withArgs(ctx, data);
|
|
|
|
pickStub.onFirstCall().returns(Promise.resolve([]));
|
|
|
|
pickStub.throws();
|
|
|
|
|
|
|
|
await visitRules(ctx, [rule], {});
|
|
|
|
|
|
|
|
mockRule.verify();
|
|
|
|
expect(ctx.errors.length).to.equal(0);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should call visit for each selected item', async () => {
|
|
|
|
const ctx = new VisitorContext({
|
|
|
|
coerce: false,
|
|
|
|
defaults: false,
|
|
|
|
logger: new ConsoleLogger(),
|
2019-07-11 05:16:00 +00:00
|
|
|
mutate: false,
|
2019-06-30 23:18:11 +00:00
|
|
|
});
|
|
|
|
const data = {};
|
|
|
|
const rule = new Rule({
|
|
|
|
name: 'foo',
|
|
|
|
desc: '',
|
|
|
|
level: 'info',
|
|
|
|
tags: [],
|
|
|
|
select: '$',
|
|
|
|
check: {},
|
|
|
|
});
|
|
|
|
|
|
|
|
const mockRule = mock(rule);
|
|
|
|
|
|
|
|
const pickStub = mockRule.expects('pick').once().withArgs(ctx, data);
|
|
|
|
pickStub.onFirstCall().returns(Promise.resolve([data]));
|
|
|
|
pickStub.throws();
|
|
|
|
|
|
|
|
const visitStub = mockRule.expects('visit').once().withArgs(ctx, data);
|
|
|
|
visitStub.onFirstCall().returns(Promise.resolve(ctx));
|
|
|
|
visitStub.throws();
|
|
|
|
|
|
|
|
await visitRules(ctx, [rule], {});
|
|
|
|
|
|
|
|
mockRule.verify();
|
|
|
|
expect(ctx.errors.length).to.equal(0);
|
2019-06-30 23:43:52 +00:00
|
|
|
});
|
2019-06-30 23:18:11 +00:00
|
|
|
});
|