Skip to content

Commit

Permalink
fix(tests): cover read config, rule module helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
ssube committed Nov 17, 2019
1 parent 0f214ed commit c0eb3e0
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/rule/index.ts
Expand Up @@ -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)) {
Expand Down
12 changes: 11 additions & 1 deletion test/config/TestConfig.ts
@@ -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';

Expand All @@ -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)
);
});
37 changes: 35 additions & 2 deletions test/rule/TestLoadRule.ts
@@ -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';

Expand Down Expand Up @@ -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');
});

0 comments on commit c0eb3e0

Please sign in to comment.