1
0
Fork 0

feat: rule selector helper

This commit is contained in:
ssube 2019-06-29 16:27:10 -05:00
parent caca3fca7d
commit a630571ce1
2 changed files with 56 additions and 32 deletions

View File

@ -35,6 +35,25 @@ export interface RuleSource {
rules: Array<RuleData>;
}
export function ensureArray<T>(val: Array<T> | undefined): Array<T> {
if (isNil(val)) {
return [];
} else {
return Array.from(val);
}
}
export function makeSelector(options: Partial<RuleSelector>) {
return {
excludeLevel: ensureArray(options.excludeLevel),
excludeName: ensureArray(options.excludeName),
excludeTag: ensureArray(options.excludeTag),
includeLevel: ensureArray(options.includeLevel),
includeName: ensureArray(options.includeName),
includeTag: ensureArray(options.includeTag),
};
}
export async function loadRules(paths: Array<string>, ajv: any): Promise<Array<Rule>> {
const parser = new YamlParser();
const rules = [];

View File

@ -1,25 +1,25 @@
import { expect } from 'chai';
import { Rule, resolveRules } from 'src/rule';
import { Rule, resolveRules, makeSelector } from 'src/rule';
const TEST_RULES = [new Rule({
name: 'foo',
desc: '',
level: 'info',
tags: ['foo'],
tags: ['all', 'foo'],
check: {},
select: '$',
}), new Rule({
name: 'bar',
desc: '',
level: 'warn',
tags: ['test'],
tags: ['all', 'test'],
check: {},
select: '$',
}), new Rule({
name: 'bin',
desc: '',
level: 'warn',
tags: ['test'],
tags: ['all', 'test'],
check: {},
select: '$',
})];
@ -27,28 +27,18 @@ const TEST_RULES = [new Rule({
describe('rule resolver', () => {
describe('include by level', () => {
it('should include info rules', async () => {
const info = await resolveRules(TEST_RULES, {
excludeLevel: [],
excludeName: [],
excludeTag: [],
const info = await resolveRules(TEST_RULES, makeSelector({
includeLevel: ['info'],
includeName: [],
includeTag: [],
});
}));
expect(info.length).to.equal(1);
expect(info[0]).to.equal(TEST_RULES[0]);
});
it('should include warn rules', async () => {
const info = await resolveRules(TEST_RULES, {
excludeLevel: [],
excludeName: [],
excludeTag: [],
const info = await resolveRules(TEST_RULES, makeSelector({
includeLevel: ['warn'],
includeName: [],
includeTag: [],
});
}));
expect(info.length).to.equal(2);
expect(info[0]).to.equal(TEST_RULES[1]);
@ -58,14 +48,9 @@ describe('rule resolver', () => {
describe('include by name', () => {
it('should include foo rules', async () => {
const rules = await resolveRules(TEST_RULES, {
excludeLevel: [],
excludeName: [],
excludeTag: [],
includeLevel: [],
const rules = await resolveRules(TEST_RULES, makeSelector({
includeName: ['foo'],
includeTag: [],
});
}));
expect(rules.length).to.equal(1);
expect(rules[0].name).to.equal('foo');
@ -74,18 +59,38 @@ describe('rule resolver', () => {
describe('include by tag', () => {
it('should include test rules', async () => {
const rules = await resolveRules(TEST_RULES, {
excludeLevel: [],
excludeName: [],
excludeTag: [],
includeLevel: [],
includeName: [],
const rules = await resolveRules(TEST_RULES, makeSelector({
includeTag: ['test'],
});
}));
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 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]);
});
});
});