Skip to content

Commit

Permalink
feat: rule selector helper
Browse files Browse the repository at this point in the history
  • Loading branch information
ssube committed Jun 29, 2019
1 parent caca3fc commit a630571
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 32 deletions.
19 changes: 19 additions & 0 deletions src/rule.ts
Expand Up @@ -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 = [];
Expand Down
69 changes: 37 additions & 32 deletions test/TestRule.ts
@@ -1,54 +1,44 @@
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: '$',
})];

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]);
Expand All @@ -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');
Expand All @@ -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]);
});
});
});

0 comments on commit a630571

Please sign in to comment.