From 089725ef60e343e413ab0e87ec5580f51913a378 Mon Sep 17 00:00:00 2001 From: ssube Date: Sun, 17 Nov 2019 16:56:59 -0600 Subject: [PATCH] cover rule file and module validation --- test/TestApp.ts | 17 +++++++++++++++ test/rule/TestLoadRule.ts | 45 ++++++++++++++++++++++++++++++++++++++- 2 files changed, 61 insertions(+), 1 deletion(-) diff --git a/test/TestApp.ts b/test/TestApp.ts index c783694..2f0419c 100644 --- a/test/TestApp.ts +++ b/test/TestApp.ts @@ -13,6 +13,7 @@ const TEST_ARGS_SOURCE = ['--source', 'test.yml']; const TEST_FILES = { 'docs': { 'config.yml': 'data: {logger: {level: debug, name: test, stream: !stream stderr}}', + 'partial.yml': 'data: {logger: {name: test}}', }, 'rules.yml': `{ name: test, @@ -122,4 +123,20 @@ describeLeaks('main app', async () => { expect(status).to.equal(STATUS_SUCCESS); expect(result).to.equal('foo: 4\n'); }); + + it('should validate config before running', async () => { + mockFs(TEST_FILES); + + const status = await main([ + ...TEST_ARGS_PRE, + ...TEST_ARGS_CONFIG.slice(0, 3), + 'partial.yml', + ...TEST_ARGS_RULES, + ...TEST_ARGS_SOURCE, + ]); + + mockFs.restore(); + + expect(status).to.equal(STATUS_ERROR); + }); }); diff --git a/test/rule/TestLoadRule.ts b/test/rule/TestLoadRule.ts index 6380662..b25c471 100644 --- a/test/rule/TestLoadRule.ts +++ b/test/rule/TestLoadRule.ts @@ -68,6 +68,33 @@ describeLeaks('load rule file helper', async () => { expect(rules.length).to.equal(1); }); + + itLeaks('should validate rule files', async () => { + mockFS({ + test: `{ + name: foo, + definitions: [], + rules: {} + }` + }); + + const ctx = new VisitorContext({ + logger: NullLogger.global, + schemaOptions: { + coerce: false, + defaults: false, + mutate: false, + }, + }); + + const rules = await loadRuleFiles([ + 'test', + ], ctx); + + mockFS.restore(); + + expect(rules.length).to.equal(0); + }); }); describeLeaks('load rule path helper', async () => { @@ -174,5 +201,21 @@ describeLeaks('load rule module helper', async () => { return expect(loadRuleModules(['test'], ctx, requireStub)).to.eventually.deep.equal([]); }); - itLeaks('should validate rule module exports'); + itLeaks('should validate rule module exports', async () => { + const requireStub = stub().returns({ + name: 'test-rules', + rules: {}, + /* eslint-disable-next-line @typescript-eslint/no-explicit-any */ + }) as any; + const ctx = new VisitorContext({ + logger: NullLogger.global, + schemaOptions: { + coerce: false, + defaults: false, + mutate: false, + }, + }); + + return expect(loadRuleModules(['test'], ctx, requireStub)).to.eventually.deep.equal([]); + }); });