cover pojso rule instantiation
This commit is contained in:
parent
7fce9649ec
commit
91e5a93db8
|
@ -97,16 +97,15 @@ export function createRuleSources(options: Partial<RuleSources>): RuleSources {
|
||||||
}
|
}
|
||||||
|
|
||||||
export function isPOJSO(val: any): val is RuleData {
|
export function isPOJSO(val: any): val is RuleData {
|
||||||
const pojsoProto = Reflect.getPrototypeOf({});
|
return Reflect.getPrototypeOf(val) === Reflect.getPrototypeOf({});
|
||||||
return Reflect.getPrototypeOf(val) === pojsoProto;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function loadRuleSource(data: RuleSourceData | RuleSourceModule, ctx: VisitorContext): Promise<Array<Rule>> {
|
export async function loadRuleSource(data: RuleSourceModule, ctx: VisitorContext): Promise<Array<Rule>> {
|
||||||
if (doesExist(data.definitions)) {
|
if (doesExist(data.definitions)) {
|
||||||
ctx.addSchema(data.name, data.definitions);
|
ctx.addSchema(data.name, data.definitions);
|
||||||
}
|
}
|
||||||
|
|
||||||
return Array.from(data.rules).map((it: Rule | RuleData) => {
|
return data.rules.map((it: Rule | RuleData) => {
|
||||||
if (isPOJSO(it)) {
|
if (isPOJSO(it)) {
|
||||||
return new SchemaRule(it);
|
return new SchemaRule(it);
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -1,9 +1,10 @@
|
||||||
import { expect } from 'chai';
|
import { expect } from 'chai';
|
||||||
import mockFS from 'mock-fs';
|
import mockFS from 'mock-fs';
|
||||||
import { NullLogger } from 'noicejs';
|
import { LogLevel, NullLogger } from 'noicejs';
|
||||||
import { spy, stub } from 'sinon';
|
import { spy, stub } from 'sinon';
|
||||||
|
|
||||||
import { loadRuleFiles, loadRuleModules, loadRulePaths } from '../../src/rule';
|
import { loadRuleFiles, loadRuleModules, loadRulePaths, loadRuleSource } from '../../src/rule';
|
||||||
|
import { SchemaRule } from '../../src/rule/SchemaRule';
|
||||||
import { VisitorContext } from '../../src/visitor/VisitorContext';
|
import { VisitorContext } from '../../src/visitor/VisitorContext';
|
||||||
import { describeLeaks, itLeaks } from '../helpers/async';
|
import { describeLeaks, itLeaks } from '../helpers/async';
|
||||||
|
|
||||||
|
@ -20,6 +21,17 @@ const EXAMPLE_RULES = `{
|
||||||
}]
|
}]
|
||||||
}`;
|
}`;
|
||||||
|
|
||||||
|
function testContext() {
|
||||||
|
return new VisitorContext({
|
||||||
|
logger: NullLogger.global,
|
||||||
|
schemaOptions: {
|
||||||
|
coerce: false,
|
||||||
|
defaults: false,
|
||||||
|
mutate: false,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
describeLeaks('load rule file helper', async () => {
|
describeLeaks('load rule file helper', async () => {
|
||||||
itLeaks('should add schema', async () => {
|
itLeaks('should add schema', async () => {
|
||||||
mockFS({
|
mockFS({
|
||||||
|
@ -158,14 +170,7 @@ describeLeaks('load rule path helper', async () => {
|
||||||
|
|
||||||
describeLeaks('load rule module helper', async () => {
|
describeLeaks('load rule module helper', async () => {
|
||||||
itLeaks('should load rule modules', async () => {
|
itLeaks('should load rule modules', async () => {
|
||||||
const ctx = new VisitorContext({
|
const ctx = testContext();
|
||||||
logger: NullLogger.global,
|
|
||||||
schemaOptions: {
|
|
||||||
coerce: false,
|
|
||||||
defaults: false,
|
|
||||||
mutate: false,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
const requireStub = stub().withArgs('test').returns({
|
const requireStub = stub().withArgs('test').returns({
|
||||||
name: 'test',
|
name: 'test',
|
||||||
rules: [{
|
rules: [{
|
||||||
|
@ -184,14 +189,7 @@ describeLeaks('load rule module helper', async () => {
|
||||||
itLeaks('should handle errors loading rule modules', async () => {
|
itLeaks('should handle errors loading rule modules', async () => {
|
||||||
/* eslint-disable-next-line @typescript-eslint/no-explicit-any */
|
/* eslint-disable-next-line @typescript-eslint/no-explicit-any */
|
||||||
const requireStub = stub().throws(new Error('could not load this module')) as any;
|
const requireStub = stub().throws(new Error('could not load this module')) as any;
|
||||||
const ctx = new VisitorContext({
|
const ctx = testContext();
|
||||||
logger: NullLogger.global,
|
|
||||||
schemaOptions: {
|
|
||||||
coerce: false,
|
|
||||||
defaults: false,
|
|
||||||
mutate: false,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
return expect(loadRuleModules(['test'], ctx, requireStub)).to.eventually.deep.equal([]);
|
return expect(loadRuleModules(['test'], ctx, requireStub)).to.eventually.deep.equal([]);
|
||||||
});
|
});
|
||||||
|
@ -202,14 +200,7 @@ describeLeaks('load rule module helper', async () => {
|
||||||
rules: {},
|
rules: {},
|
||||||
/* eslint-disable-next-line @typescript-eslint/no-explicit-any */
|
/* eslint-disable-next-line @typescript-eslint/no-explicit-any */
|
||||||
}) as any;
|
}) as any;
|
||||||
const ctx = new VisitorContext({
|
const ctx = testContext();
|
||||||
logger: NullLogger.global,
|
|
||||||
schemaOptions: {
|
|
||||||
coerce: false,
|
|
||||||
defaults: false,
|
|
||||||
mutate: false,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
return expect(loadRuleModules(['test'], ctx, requireStub)).to.eventually.deep.equal([]);
|
return expect(loadRuleModules(['test'], ctx, requireStub)).to.eventually.deep.equal([]);
|
||||||
});
|
});
|
||||||
|
@ -225,14 +216,7 @@ describeLeaks('load rule module helper', async () => {
|
||||||
rules: [],
|
rules: [],
|
||||||
/* eslint-disable-next-line @typescript-eslint/no-explicit-any */
|
/* eslint-disable-next-line @typescript-eslint/no-explicit-any */
|
||||||
}) as any;
|
}) as any;
|
||||||
const ctx = new VisitorContext({
|
const ctx = testContext();
|
||||||
logger: NullLogger.global,
|
|
||||||
schemaOptions: {
|
|
||||||
coerce: false,
|
|
||||||
defaults: false,
|
|
||||||
mutate: false,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
await loadRuleModules(['test'], ctx, requireStub);
|
await loadRuleModules(['test'], ctx, requireStub);
|
||||||
const schema = ctx.compile({
|
const schema = ctx.compile({
|
||||||
|
@ -242,4 +226,22 @@ describeLeaks('load rule module helper', async () => {
|
||||||
expect(schema(2)).to.equal(false);
|
expect(schema(2)).to.equal(false);
|
||||||
expect(schema('foo')).to.equal(true);
|
expect(schema('foo')).to.equal(true);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
itLeaks('should not instantiate class instances', async () => {
|
||||||
|
class TestRule extends SchemaRule {}
|
||||||
|
const ctx = testContext();
|
||||||
|
|
||||||
|
const rules = await loadRuleSource({
|
||||||
|
name: 'test',
|
||||||
|
rules: [new TestRule({
|
||||||
|
check: {},
|
||||||
|
desc: 'test',
|
||||||
|
level: LogLevel.Info,
|
||||||
|
name: 'test',
|
||||||
|
select: '$',
|
||||||
|
tags: [],
|
||||||
|
})],
|
||||||
|
}, ctx);
|
||||||
|
expect(rules[0]).to.be.an.instanceOf(TestRule);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue