feat: rule selector helper
This commit is contained in:
parent
caca3fca7d
commit
a630571ce1
19
src/rule.ts
19
src/rule.ts
|
@ -35,6 +35,25 @@ export interface RuleSource {
|
||||||
rules: Array<RuleData>;
|
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>> {
|
export async function loadRules(paths: Array<string>, ajv: any): Promise<Array<Rule>> {
|
||||||
const parser = new YamlParser();
|
const parser = new YamlParser();
|
||||||
const rules = [];
|
const rules = [];
|
||||||
|
|
|
@ -1,25 +1,25 @@
|
||||||
import { expect } from 'chai';
|
import { expect } from 'chai';
|
||||||
import { Rule, resolveRules } from 'src/rule';
|
import { Rule, resolveRules, makeSelector } from 'src/rule';
|
||||||
|
|
||||||
const TEST_RULES = [new Rule({
|
const TEST_RULES = [new Rule({
|
||||||
name: 'foo',
|
name: 'foo',
|
||||||
desc: '',
|
desc: '',
|
||||||
level: 'info',
|
level: 'info',
|
||||||
tags: ['foo'],
|
tags: ['all', 'foo'],
|
||||||
check: {},
|
check: {},
|
||||||
select: '$',
|
select: '$',
|
||||||
}), new Rule({
|
}), new Rule({
|
||||||
name: 'bar',
|
name: 'bar',
|
||||||
desc: '',
|
desc: '',
|
||||||
level: 'warn',
|
level: 'warn',
|
||||||
tags: ['test'],
|
tags: ['all', 'test'],
|
||||||
check: {},
|
check: {},
|
||||||
select: '$',
|
select: '$',
|
||||||
}), new Rule({
|
}), new Rule({
|
||||||
name: 'bin',
|
name: 'bin',
|
||||||
desc: '',
|
desc: '',
|
||||||
level: 'warn',
|
level: 'warn',
|
||||||
tags: ['test'],
|
tags: ['all', 'test'],
|
||||||
check: {},
|
check: {},
|
||||||
select: '$',
|
select: '$',
|
||||||
})];
|
})];
|
||||||
|
@ -27,28 +27,18 @@ const TEST_RULES = [new Rule({
|
||||||
describe('rule resolver', () => {
|
describe('rule resolver', () => {
|
||||||
describe('include by level', () => {
|
describe('include by level', () => {
|
||||||
it('should include info rules', async () => {
|
it('should include info rules', async () => {
|
||||||
const info = await resolveRules(TEST_RULES, {
|
const info = await resolveRules(TEST_RULES, makeSelector({
|
||||||
excludeLevel: [],
|
|
||||||
excludeName: [],
|
|
||||||
excludeTag: [],
|
|
||||||
includeLevel: ['info'],
|
includeLevel: ['info'],
|
||||||
includeName: [],
|
}));
|
||||||
includeTag: [],
|
|
||||||
});
|
|
||||||
|
|
||||||
expect(info.length).to.equal(1);
|
expect(info.length).to.equal(1);
|
||||||
expect(info[0]).to.equal(TEST_RULES[0]);
|
expect(info[0]).to.equal(TEST_RULES[0]);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should include warn rules', async () => {
|
it('should include warn rules', async () => {
|
||||||
const info = await resolveRules(TEST_RULES, {
|
const info = await resolveRules(TEST_RULES, makeSelector({
|
||||||
excludeLevel: [],
|
|
||||||
excludeName: [],
|
|
||||||
excludeTag: [],
|
|
||||||
includeLevel: ['warn'],
|
includeLevel: ['warn'],
|
||||||
includeName: [],
|
}));
|
||||||
includeTag: [],
|
|
||||||
});
|
|
||||||
|
|
||||||
expect(info.length).to.equal(2);
|
expect(info.length).to.equal(2);
|
||||||
expect(info[0]).to.equal(TEST_RULES[1]);
|
expect(info[0]).to.equal(TEST_RULES[1]);
|
||||||
|
@ -58,14 +48,9 @@ describe('rule resolver', () => {
|
||||||
|
|
||||||
describe('include by name', () => {
|
describe('include by name', () => {
|
||||||
it('should include foo rules', async () => {
|
it('should include foo rules', async () => {
|
||||||
const rules = await resolveRules(TEST_RULES, {
|
const rules = await resolveRules(TEST_RULES, makeSelector({
|
||||||
excludeLevel: [],
|
|
||||||
excludeName: [],
|
|
||||||
excludeTag: [],
|
|
||||||
includeLevel: [],
|
|
||||||
includeName: ['foo'],
|
includeName: ['foo'],
|
||||||
includeTag: [],
|
}));
|
||||||
});
|
|
||||||
|
|
||||||
expect(rules.length).to.equal(1);
|
expect(rules.length).to.equal(1);
|
||||||
expect(rules[0].name).to.equal('foo');
|
expect(rules[0].name).to.equal('foo');
|
||||||
|
@ -74,18 +59,38 @@ describe('rule resolver', () => {
|
||||||
|
|
||||||
describe('include by tag', () => {
|
describe('include by tag', () => {
|
||||||
it('should include test rules', async () => {
|
it('should include test rules', async () => {
|
||||||
const rules = await resolveRules(TEST_RULES, {
|
const rules = await resolveRules(TEST_RULES, makeSelector({
|
||||||
excludeLevel: [],
|
|
||||||
excludeName: [],
|
|
||||||
excludeTag: [],
|
|
||||||
includeLevel: [],
|
|
||||||
includeName: [],
|
|
||||||
includeTag: ['test'],
|
includeTag: ['test'],
|
||||||
});
|
}));
|
||||||
|
|
||||||
expect(rules.length).to.equal(2);
|
expect(rules.length).to.equal(2);
|
||||||
expect(rules[0]).to.equal(TEST_RULES[1]);
|
expect(rules[0]).to.equal(TEST_RULES[1]);
|
||||||
expect(rules[1]).to.equal(TEST_RULES[2]);
|
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]);
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue