diff --git a/src/rule.ts b/src/rule.ts index 7ac8f71..836707d 100644 --- a/src/rule.ts +++ b/src/rule.ts @@ -35,6 +35,25 @@ export interface RuleSource { rules: Array; } +export function ensureArray(val: Array | undefined): Array { + if (isNil(val)) { + return []; + } else { + return Array.from(val); + } +} + +export function makeSelector(options: Partial) { + 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, ajv: any): Promise> { const parser = new YamlParser(); const rules = []; diff --git a/test/TestRule.ts b/test/TestRule.ts index f0f26ea..fac2a78 100644 --- a/test/TestRule.ts +++ b/test/TestRule.ts @@ -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]); + }); + }); });