1
0
Fork 0
salty-dog/test/rule/TestRule.ts

136 lines
3.9 KiB
TypeScript
Raw Normal View History

import { expect } from 'chai';
2019-11-16 01:47:38 +00:00
import { LogLevel } from 'noicejs';
2019-11-14 13:56:49 +00:00
import { createRuleSelector, createRuleSources, resolveRules } from '../../src/rule';
import { SchemaRule } from '../../src/rule/SchemaRule';
import { describeLeaks, itLeaks } from '../helpers/async';
const TEST_RULES = [new SchemaRule({
check: {},
desc: '',
2019-11-11 03:52:56 +00:00
level: LogLevel.Info,
name: 'foo',
select: '$',
tags: ['all', 'foo'],
}), new SchemaRule({
check: {},
desc: '',
2019-11-11 03:52:56 +00:00
level: LogLevel.Warn,
name: 'bar',
select: '$',
tags: ['all', 'test'],
}), new SchemaRule({
check: {},
desc: '',
2019-11-11 03:52:56 +00:00
level: LogLevel.Warn,
name: 'bin',
select: '$',
tags: ['all', 'test'],
})];
describeLeaks('rule resolver', async () => {
describeLeaks('include by level', async () => {
itLeaks('should include info rules', async () => {
const info = await resolveRules(TEST_RULES, createRuleSelector({
2019-11-11 03:52:56 +00:00
includeLevel: [LogLevel.Info],
}));
expect(info.length).to.equal(1);
expect(info[0]).to.equal(TEST_RULES[0]);
});
itLeaks('should include warn rules', async () => {
const info = await resolveRules(TEST_RULES, createRuleSelector({
2019-11-11 03:52:56 +00:00
includeLevel: [LogLevel.Warn],
}));
expect(info.length).to.equal(2);
expect(info[0]).to.equal(TEST_RULES[1]);
expect(info[1]).to.equal(TEST_RULES[2]);
});
});
describeLeaks('include by name', async () => {
itLeaks('should include foo rules', async () => {
const rules = await resolveRules(TEST_RULES, createRuleSelector({
includeName: ['foo'],
}));
expect(rules.length).to.equal(1);
expect(rules[0].name).to.equal('foo');
});
});
describeLeaks('include by tag', async () => {
itLeaks('should include test rules', async () => {
const rules = await resolveRules(TEST_RULES, createRuleSelector({
includeTag: ['test'],
}));
expect(rules.length).to.equal(2);
expect(rules[0]).to.equal(TEST_RULES[1]);
expect(rules[1]).to.equal(TEST_RULES[2]);
});
});
describeLeaks('exclude by name', async () => {
itLeaks('should exclude foo rules', async () => {
const rules = await resolveRules(TEST_RULES, createRuleSelector({
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]);
});
});
describeLeaks('exclude by tag', async () => {
itLeaks('should exclude test rules', async () => {
const rules = await resolveRules(TEST_RULES, createRuleSelector({
excludeTag: ['test'],
includeTag: ['all'],
}));
expect(rules.length).to.equal(1);
expect(rules[0]).to.equal(TEST_RULES[0]);
});
});
describeLeaks('exclude by level', async () => {
itLeaks('should exclude warn rules', async () => {
const rules = await resolveRules(TEST_RULES, createRuleSelector({
excludeLevel: [LogLevel.Warn],
includeTag: ['all'],
}));
expect(rules.length).to.equal(1);
expect(rules[0]).to.equal(TEST_RULES[0]);
});
});
});
describe('create rule sources helper', () => {
it('should ensure every field is an array', () => {
const sources = createRuleSources({});
expect(sources).to.have.deep.property('ruleFile', []);
expect(sources).to.have.deep.property('ruleModule', []);
expect(sources).to.have.deep.property('rulePath', []);
});
});
describe('create rule selector helper', () => {
it('should ensure every field is an array', () => {
const sources = createRuleSelector({});
expect(sources).to.have.deep.property('excludeLevel', []);
expect(sources).to.have.deep.property('excludeName', []);
expect(sources).to.have.deep.property('excludeTag', []);
expect(sources).to.have.deep.property('includeLevel', []);
expect(sources).to.have.deep.property('includeName', []);
expect(sources).to.have.deep.property('includeTag', []);
2019-10-29 03:23:10 +00:00
});
});