1
0
Fork 0

fix(tests): cover read config, rule module helpers

This commit is contained in:
ssube 2019-11-16 18:10:23 -06:00 committed by Sean Sube
parent 0f214ed2dc
commit c0eb3e0bfc
3 changed files with 48 additions and 5 deletions

View File

@ -140,13 +140,13 @@ export async function loadRulePaths(paths: Array<string>, ctx: VisitorContext):
return rules;
}
export async function loadRuleModules(modules: Array<string>, ctx: VisitorContext): Promise<Array<Rule>> {
export async function loadRuleModules(modules: Array<string>, ctx: VisitorContext, r = require): Promise<Array<Rule>> {
const rules = [];
for (const name of modules) {
try {
/* eslint-disable-next-line @typescript-eslint/no-var-requires */
const module: RuleSourceModule = require(name);
const module: RuleSourceModule = r(name);
// TODO: ensure module has definitions, name, and rules
if (!isNil(module.definitions)) {

View File

@ -1,7 +1,7 @@
import { expect } from 'chai';
import { join } from 'path';
import { loadConfig } from '../../src/config';
import { loadConfig, readConfig } from '../../src/config';
import { NotFoundError } from '../../src/error/NotFoundError';
import { describeLeaks, itLeaks } from '../helpers/async';
@ -15,3 +15,13 @@ describeLeaks('load config helper', async () => {
expect(loadConfig('missing.yml', join(__dirname, '..', 'docs'))).to.eventually.be.rejectedWith(NotFoundError)
);
});
describeLeaks('read config helper', async () => {
itLeaks('should consume enoent errors', async () =>
expect(readConfig(join('docs', 'missing.yml'))).to.eventually.equal(undefined)
);
itLeaks('should rethrow unknown errors', async () =>
expect(readConfig('test')).to.eventually.be.rejectedWith(Error)
);
});

View File

@ -1,9 +1,9 @@
import { expect } from 'chai';
import mockFS from 'mock-fs';
import { NullLogger } from 'noicejs';
import { spy } from 'sinon';
import { spy, stub } from 'sinon';
import { loadRuleFiles, loadRulePaths } from '../../src/rule';
import { loadRuleFiles, loadRuleModules, loadRulePaths } from '../../src/rule';
import { VisitorContext } from '../../src/visitor/VisitorContext';
import { describeLeaks, itLeaks } from '../helpers/async';
@ -126,3 +126,36 @@ describeLeaks('load rule path helper', async () => {
expect(rules.length).to.equal(2);
});
});
describeLeaks('load rule module helper', async () => {
itLeaks('should load rule modules', async () => {
const ctx = new VisitorContext({
logger: NullLogger.global,
schemaOptions: {
coerce: false,
defaults: false,
mutate: false,
},
});
const requireStub = stub().withArgs('test').returns({
definitions: {
foo: {
type: 'string',
},
},
name: 'test',
rules: [{
check: {},
desc: 'testing rule',
level: 'info',
name: 'test-rule',
tags: [],
}],
/* eslint-disable-next-line @typescript-eslint/no-explicit-any */
}) as any;
const rules = await loadRuleModules(['test'], ctx, requireStub);
expect(rules.length).to.equal(1);
});
itLeaks('should validate rule module exports');
});