From c0eb3e0bfc684847edea44dd69af082b94f62673 Mon Sep 17 00:00:00 2001 From: ssube Date: Sat, 16 Nov 2019 18:10:23 -0600 Subject: [PATCH] fix(tests): cover read config, rule module helpers --- src/rule/index.ts | 4 ++-- test/config/TestConfig.ts | 12 +++++++++++- test/rule/TestLoadRule.ts | 37 +++++++++++++++++++++++++++++++++++-- 3 files changed, 48 insertions(+), 5 deletions(-) diff --git a/src/rule/index.ts b/src/rule/index.ts index f375ef4..7b22011 100644 --- a/src/rule/index.ts +++ b/src/rule/index.ts @@ -140,13 +140,13 @@ export async function loadRulePaths(paths: Array, ctx: VisitorContext): return rules; } -export async function loadRuleModules(modules: Array, ctx: VisitorContext): Promise> { +export async function loadRuleModules(modules: Array, ctx: VisitorContext, r = require): Promise> { 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)) { diff --git a/test/config/TestConfig.ts b/test/config/TestConfig.ts index 6c39a58..3e601f1 100644 --- a/test/config/TestConfig.ts +++ b/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'; @@ -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) + ); +}); diff --git a/test/rule/TestLoadRule.ts b/test/rule/TestLoadRule.ts index b632572..6b07923 100644 --- a/test/rule/TestLoadRule.ts +++ b/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'; @@ -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'); +});