From 44688ca2a18384b85b0325a0067ef2ad136dc6f1 Mon Sep 17 00:00:00 2001 From: Jesus David Garcia Gomez Date: Wed, 19 Apr 2017 14:09:10 -0700 Subject: [PATCH] Add test to config-rule.ts Fix issue in `resource-loader.ts` tests Rename `interfaces` to `types` Change rule `schema` type to `Array` Fix some issues in `config-rule.ts` Refactor validateRule method in `config-rule.ts` ---------------------------------- Fix #6 --- src/lib/cli.ts | 2 +- src/lib/collectors/cdp/cdp-async-html.ts | 2 +- src/lib/collectors/cdp/cdp.ts | 2 +- src/lib/collectors/jsdom/jsdom-async-html.ts | 2 +- src/lib/collectors/jsdom/jsdom.ts | 2 +- src/lib/collectors/utils/requester.ts | 2 +- src/lib/config.ts | 2 +- src/lib/config/config-rules.ts | 37 +++-- src/lib/formatters/json/json.ts | 2 +- src/lib/interfaces.ts | 26 ---- src/lib/rule-context.ts | 2 +- .../disallowed-headers/disallowed-headers.ts | 6 +- .../rules/lang-attribute/lang-attribute.ts | 2 +- .../rules/manifest-exists/manifest-exists.ts | 2 +- .../manifest-file-extension.ts | 2 +- .../manifest-is-valid/manifest-is-valid.ts | 2 +- .../no-friendly-error-pages.ts | 2 +- .../no-html-only-headers.ts | 6 +- .../no-protocol-relative-urls.ts | 2 +- src/lib/sonar.ts | 2 +- src/lib/types.ts | 26 ++++ src/lib/{interfaces => types}/asynchtml.ts | 0 src/lib/{interfaces => types}/collectors.ts | 0 src/lib/{interfaces => types}/events.ts | 0 src/lib/{interfaces => types}/formatters.ts | 0 src/lib/{interfaces => types}/network.ts | 0 src/lib/{interfaces => types}/plugins.ts | 0 src/lib/{interfaces => types}/problems.ts | 0 src/lib/{interfaces => types}/rules.ts | 2 +- src/lib/utils/location-helpers.ts | 2 +- src/lib/utils/resource-loader.ts | 2 +- tests/helpers/rule-runner.ts | 2 +- tests/helpers/rule-test-type.ts | 2 +- tests/lib/collectors/_common.ts | 2 +- tests/lib/collectors/utils/requester.ts | 2 +- tests/lib/config/config-rules.ts | 141 ++++++++++++++++++ .../formatters/fixtures/list-of-problems.ts | 2 +- tests/lib/formatters/json.ts | 2 +- tests/lib/utils/location-helpers.ts | 2 +- tests/lib/utils/resource-loader.ts | 2 +- 40 files changed, 217 insertions(+), 79 deletions(-) delete mode 100644 src/lib/interfaces.ts create mode 100644 src/lib/types.ts rename src/lib/{interfaces => types}/asynchtml.ts (100%) rename src/lib/{interfaces => types}/collectors.ts (100%) rename src/lib/{interfaces => types}/events.ts (100%) rename src/lib/{interfaces => types}/formatters.ts (100%) rename src/lib/{interfaces => types}/network.ts (100%) rename src/lib/{interfaces => types}/plugins.ts (100%) rename src/lib/{interfaces => types}/problems.ts (100%) rename src/lib/{interfaces => types}/rules.ts (91%) create mode 100644 tests/lib/config/config-rules.ts diff --git a/src/lib/cli.ts b/src/lib/cli.ts index 34d447e7f75..59e3e43b146 100644 --- a/src/lib/cli.ts +++ b/src/lib/cli.ts @@ -22,7 +22,7 @@ import { loadJSONFile } from './utils/file-loader'; import * as logger from './utils/logging'; import { options } from './ui/options'; import * as resourceLoader from './utils/resource-loader'; -import { Severity } from './interfaces'; +import { Severity } from './types'; import * as sonar from './sonar'; import * as validator from './config/config-validator'; diff --git a/src/lib/collectors/cdp/cdp-async-html.ts b/src/lib/collectors/cdp/cdp-async-html.ts index 469849fbe6e..183af3eabfd 100644 --- a/src/lib/collectors/cdp/cdp-async-html.ts +++ b/src/lib/collectors/cdp/cdp-async-html.ts @@ -1,4 +1,4 @@ -import { IAsyncHTMLDocument, IAsyncHTMLElement } from '../../interfaces'; //eslint-disable-line +import { IAsyncHTMLDocument, IAsyncHTMLElement } from '../../types'; //eslint-disable-line /** An implementation of AsyncHTMLDocument on top of the Chrome Debugging Protocol */ export class CDPAsyncHTMLDocument implements IAsyncHTMLDocument { diff --git a/src/lib/collectors/cdp/cdp.ts b/src/lib/collectors/cdp/cdp.ts index 5130380017a..9ea4525ec8d 100644 --- a/src/lib/collectors/cdp/cdp.ts +++ b/src/lib/collectors/cdp/cdp.ts @@ -21,7 +21,7 @@ import { ICollector, ICollectorBuilder, IElementFoundEvent, IFetchEndEvent, ITraverseUpEvent, ITraverseDownEvent, INetworkData, URL -} from '../../interfaces'; +} from '../../types'; /* eslint-enable */ import { launchChrome } from './cdp-launcher'; import { normalizeHeaders } from '../utils/normalize-headers'; diff --git a/src/lib/collectors/jsdom/jsdom-async-html.ts b/src/lib/collectors/jsdom/jsdom-async-html.ts index 78e56435777..444402e3891 100644 --- a/src/lib/collectors/jsdom/jsdom-async-html.ts +++ b/src/lib/collectors/jsdom/jsdom-async-html.ts @@ -1,4 +1,4 @@ -import { IAsyncHTMLDocument, IAsyncHTMLElement } from '../../interfaces'; //eslint-disable-line +import { IAsyncHTMLDocument, IAsyncHTMLElement } from '../../types'; //eslint-disable-line /** An implementation of AsyncHTMLDocument on top of JSDDOM */ export class JSDOMAsyncHTMLDocument implements IAsyncHTMLDocument { diff --git a/src/lib/collectors/jsdom/jsdom.ts b/src/lib/collectors/jsdom/jsdom.ts index 3da74bdd218..9151f788690 100644 --- a/src/lib/collectors/jsdom/jsdom.ts +++ b/src/lib/collectors/jsdom/jsdom.ts @@ -30,7 +30,7 @@ import { IAsyncHTMLDocument, IAsyncHTMLElement, ICollector, ICollectorBuilder, IElementFoundEvent, IFetchEndEvent, IFetchErrorEvent, ITraverseDownEvent, ITraverseUpEvent, INetworkData, URL -} from '../../interfaces'; +} from '../../types'; /* eslint-enable */ import { JSDOMAsyncHTMLElement } from './jsdom-async-html'; import * as logger from '../../utils/logging'; diff --git a/src/lib/collectors/utils/requester.ts b/src/lib/collectors/utils/requester.ts index 930f7d0cbfa..2186ff63119 100644 --- a/src/lib/collectors/utils/requester.ts +++ b/src/lib/collectors/utils/requester.ts @@ -13,7 +13,7 @@ import * as request from 'request'; import * as iconv from 'iconv-lite'; import { debug as d } from '../../utils/debug'; -import { INetworkData } from '../../interfaces'; //eslint-disable-line +import { INetworkData } from '../../types'; //eslint-disable-line import { RedirectManager } from './redirects'; const debug = d(__filename); diff --git a/src/lib/config.ts b/src/lib/config.ts index 55ce51e4ac2..33f53f79a07 100644 --- a/src/lib/config.ts +++ b/src/lib/config.ts @@ -16,7 +16,7 @@ import * as path from 'path'; import * as shell from 'shelljs'; import { debug as d } from './utils/debug'; -import { IConfig } from './interfaces'; //eslint-disable-line no-unused-vars +import { IConfig } from './types'; //eslint-disable-line no-unused-vars import { loadJSFile, loadJSONFile} from './utils/file-loader'; const debug = d(__filename); diff --git a/src/lib/config/config-rules.ts b/src/lib/config/config-rules.ts index e046cbdbbe2..ea61d411871 100644 --- a/src/lib/config/config-rules.ts +++ b/src/lib/config/config-rules.ts @@ -9,17 +9,11 @@ import * as schemaValidator from 'is-my-json-valid'; import { debug as d } from '../utils/debug'; -import { IRuleBuilder } from '../interfaces'; // eslint-disable-line no-unused-vars +import { IRuleBuilder } from '../types'; // eslint-disable-line no-unused-vars +import { Severity } from '../types/problems'; const debug = d(__filename); -// TODO: This is duplicated in types. Need to split types in different files as needed -enum Severity { - off = 0, - warning = 1, - error = 2 -} - // ------------------------------------------------------------------------------ // Public // ------------------------------------------------------------------------------ @@ -35,7 +29,7 @@ export const getSeverity = (config): Severity => { } else if (typeof config === 'number') { // Ex.: "rule-name": 2 - configuredSeverity = Severity[config]; + configuredSeverity = config; } else if (Array.isArray(config)) { // Ex.: "rule-name": ["warning", {}] configuredSeverity = getSeverity(config[0]); @@ -49,6 +43,12 @@ export const getSeverity = (config): Severity => { }; +const validateRule = (schema: Array, ruleConfig: object): boolean => { + const validator = schemaValidator(schema); + + return validator(ruleConfig); +}; + /** Validates that a rule has a valid configuration based on its schema */ export const validate = (rule: IRuleBuilder, config, ruleId: string): boolean => { @@ -61,7 +61,7 @@ export const validate = (rule: IRuleBuilder, config, ruleId: string): boolean => const configuredSeverity = getSeverity(config); - if (!configuredSeverity) { + if (configuredSeverity === null) { throw new Error(`Invalid severity configured for ${ruleId}`); } @@ -71,26 +71,23 @@ export const validate = (rule: IRuleBuilder, config, ruleId: string): boolean => // Only way to have something else to validate is if rule config // is similar to: "rule-name": ["warning", {}]. Otherwise it's // already valid if we reach this point. - if (!Array.isArray(config) && Array.isArray(schema) && schema.length === 0) { + if (!Array.isArray(config) || (Array.isArray(schema) && schema.length === 0)) { return true; } // We could have several valid schemas for the same rule if (Array.isArray(schema)) { - // No schema configuration - if (schema.length === 0 && config.length === 1) { + // No schema configuration + if (config.length === 1) { return true; } - return schema.find((sch) => { - const validateRule = schemaValidator(sch); - - return validateRule(config[1]); + // The result has to be a boolean + return schema.some((sch) => { + return validateRule(sch, config[1]); }); } - const validateRule = schemaValidator(rule.meta.schema); - - return validateRule(config[1]); + return validateRule(rule.meta.schema, config[1]); }; diff --git a/src/lib/formatters/json/json.ts b/src/lib/formatters/json/json.ts index 4be6718dca6..d4f66314fb7 100644 --- a/src/lib/formatters/json/json.ts +++ b/src/lib/formatters/json/json.ts @@ -10,7 +10,7 @@ import * as _ from 'lodash'; import { debug as d } from '../../utils/debug'; -import { IFormatter } from '../../interfaces'; // eslint-disable-line no-unused-vars +import { IFormatter } from '../../types'; // eslint-disable-line no-unused-vars import * as logger from '../../utils/logging'; const debug = d(__filename); diff --git a/src/lib/interfaces.ts b/src/lib/interfaces.ts deleted file mode 100644 index aa1854b0c95..00000000000 --- a/src/lib/interfaces.ts +++ /dev/null @@ -1,26 +0,0 @@ -/* eslint-disable no-unused-vars */ -import * as url from 'url'; - -import { ICollectorBuilder } from './interfaces/collectors'; -import { IFormatter } from './interfaces/formatters'; -import { IPluginBuilder } from './interfaces/plugins'; -import { IRuleBuilder } from './interfaces/rules'; - -export * from './interfaces/asynchtml'; -export * from './interfaces/collectors'; -export * from './interfaces/events'; -export * from './interfaces/formatters'; -export * from './interfaces/network'; -export * from './interfaces/plugins'; -export * from './interfaces/problems'; -export * from './interfaces/rules'; - -export interface IConfig { - sonarConfig?; -} - -/** A resource required by Sonar: Collector, Formatter, Plugin, Rule. */ -export type Resource = ICollectorBuilder | IFormatter | IPluginBuilder | IRuleBuilder; - -/** An alias for url.Url. */ -export type URL = url.Url; diff --git a/src/lib/rule-context.ts b/src/lib/rule-context.ts index 9934a4ce4f0..3cea773405a 100644 --- a/src/lib/rule-context.ts +++ b/src/lib/rule-context.ts @@ -6,7 +6,7 @@ */ import { Sonar } from './sonar'; // eslint-disable-line no-unused-vars -import { IAsyncHTMLElement, IProblemLocation, Severity } from './interfaces'; // eslint-disable-line no-unused-vars +import { IAsyncHTMLElement, IProblemLocation, Severity } from './types'; // eslint-disable-line no-unused-vars import { findProblemLocation } from './utils/location-helpers'; // ------------------------------------------------------------------------------ diff --git a/src/lib/rules/disallowed-headers/disallowed-headers.ts b/src/lib/rules/disallowed-headers/disallowed-headers.ts index 0278af0db8e..0268f8b2199 100644 --- a/src/lib/rules/disallowed-headers/disallowed-headers.ts +++ b/src/lib/rules/disallowed-headers/disallowed-headers.ts @@ -7,7 +7,7 @@ // ------------------------------------------------------------------------------ import { getIncludedHeaders, mergeIgnoreIncludeArrays } from '../../utils/rule-helpers'; -import { IFetchEndEvent, IRule, IRuleBuilder } from '../../interfaces'; // eslint-disable-line no-unused-vars +import { IFetchEndEvent, IRule, IRuleBuilder } from '../../types'; // eslint-disable-line no-unused-vars import { RuleContext } from '../../rule-context'; // eslint-disable-line no-unused-vars // ------------------------------------------------------------------------------ @@ -56,7 +56,7 @@ const rule: IRuleBuilder = { recommended: true }, fixable: 'code', - schema: { + schema: [{ additionalProperties: false, definitions: { 'string-array': { @@ -71,7 +71,7 @@ const rule: IRuleBuilder = { include: { $ref: '#/definitions/string-array' } }, type: ['object', null] - }, + }], worksWithLocalFiles: false } }; diff --git a/src/lib/rules/lang-attribute/lang-attribute.ts b/src/lib/rules/lang-attribute/lang-attribute.ts index a6aeadec477..a2ab95dfe26 100644 --- a/src/lib/rules/lang-attribute/lang-attribute.ts +++ b/src/lib/rules/lang-attribute/lang-attribute.ts @@ -8,7 +8,7 @@ // ------------------------------------------------------------------------------ import { debug as d } from '../../utils/debug'; -import { IElementFoundEvent, IRule, IRuleBuilder } from '../../interfaces'; // eslint-disable-line no-unused-vars +import { IElementFoundEvent, IRule, IRuleBuilder } from '../../types'; // eslint-disable-line no-unused-vars import { RuleContext } from '../../rule-context'; // eslint-disable-line no-unused-vars const debug = d(__filename); diff --git a/src/lib/rules/manifest-exists/manifest-exists.ts b/src/lib/rules/manifest-exists/manifest-exists.ts index ab6919ca5a8..b8d7923de34 100644 --- a/src/lib/rules/manifest-exists/manifest-exists.ts +++ b/src/lib/rules/manifest-exists/manifest-exists.ts @@ -10,7 +10,7 @@ import * as url from 'url'; import { debug as d } from '../../utils/debug'; -import { IElementFoundEvent, ITraverseEndEvent, IRule, IRuleBuilder } from '../../interfaces'; // eslint-disable-line no-unused-vars +import { IElementFoundEvent, ITraverseEndEvent, IRule, IRuleBuilder } from '../../types'; // eslint-disable-line no-unused-vars import { RuleContext } from '../../rule-context'; // eslint-disable-line no-unused-vars const debug = d(__filename); diff --git a/src/lib/rules/manifest-file-extension/manifest-file-extension.ts b/src/lib/rules/manifest-file-extension/manifest-file-extension.ts index dad6dd118eb..d435333f950 100644 --- a/src/lib/rules/manifest-file-extension/manifest-file-extension.ts +++ b/src/lib/rules/manifest-file-extension/manifest-file-extension.ts @@ -10,7 +10,7 @@ import * as path from 'path'; import { debug as d} from '../../utils/debug'; -import { IElementFoundEvent, IRule, IRuleBuilder } from '../../interfaces'; // eslint-disable-line no-unused-vars +import { IElementFoundEvent, IRule, IRuleBuilder } from '../../types'; // eslint-disable-line no-unused-vars import { RuleContext } from '../../rule-context'; // eslint-disable-line no-unused-vars const debug = d(__filename); diff --git a/src/lib/rules/manifest-is-valid/manifest-is-valid.ts b/src/lib/rules/manifest-is-valid/manifest-is-valid.ts index c901361f3aa..22b1722e612 100644 --- a/src/lib/rules/manifest-is-valid/manifest-is-valid.ts +++ b/src/lib/rules/manifest-is-valid/manifest-is-valid.ts @@ -9,7 +9,7 @@ import * as url from 'url'; import { debug as d } from '../../utils/debug'; -import { IElementFoundEvent, IRule, IRuleBuilder } from '../../interfaces'; // eslint-disable-line no-unused-vars +import { IElementFoundEvent, IRule, IRuleBuilder } from '../../types'; // eslint-disable-line no-unused-vars import { RuleContext } from '../../rule-context'; // eslint-disable-line no-unused-vars const debug = d(__filename); diff --git a/src/lib/rules/no-friendly-error-pages/no-friendly-error-pages.ts b/src/lib/rules/no-friendly-error-pages/no-friendly-error-pages.ts index 151bc722807..975c38a774c 100644 --- a/src/lib/rules/no-friendly-error-pages/no-friendly-error-pages.ts +++ b/src/lib/rules/no-friendly-error-pages/no-friendly-error-pages.ts @@ -10,7 +10,7 @@ import * as url from 'url'; import { debug as d } from '../../utils/debug'; -import { IFetchEndEvent, ITraverseEndEvent, IRule, IRuleBuilder } from '../../interfaces'; // eslint-disable-line no-unused-vars +import { IFetchEndEvent, ITraverseEndEvent, IRule, IRuleBuilder } from '../../types'; // eslint-disable-line no-unused-vars import { RuleContext } from '../../rule-context'; // eslint-disable-line no-unused-vars const debug = d(__filename); diff --git a/src/lib/rules/no-html-only-headers/no-html-only-headers.ts b/src/lib/rules/no-html-only-headers/no-html-only-headers.ts index 71c72112920..3d9e8b37fa6 100644 --- a/src/lib/rules/no-html-only-headers/no-html-only-headers.ts +++ b/src/lib/rules/no-html-only-headers/no-html-only-headers.ts @@ -8,7 +8,7 @@ // ------------------------------------------------------------------------------ import { getIncludedHeaders, mergeIgnoreIncludeArrays } from '../../utils/rule-helpers'; -import { IFetchEndEvent, IResponse, IRule, IRuleBuilder } from '../../interfaces'; // eslint-disable-line no-unused-vars +import { IFetchEndEvent, IResponse, IRule, IRuleBuilder } from '../../types'; // eslint-disable-line no-unused-vars import { RuleContext } from '../../rule-context'; // eslint-disable-line no-unused-vars // ------------------------------------------------------------------------------ @@ -92,7 +92,7 @@ const rule: IRuleBuilder = { recommended: true }, fixable: 'code', - schema: { + schema: [{ additionalProperties: false, definitions: { 'string-array': { @@ -107,7 +107,7 @@ const rule: IRuleBuilder = { include: { $ref: '#/definitions/string-array' } }, type: ['object', null] - }, + }], worksWithLocalFiles: false } }; diff --git a/src/lib/rules/no-protocol-relative-urls/no-protocol-relative-urls.ts b/src/lib/rules/no-protocol-relative-urls/no-protocol-relative-urls.ts index b2d8bdc2083..d88c4ed8116 100644 --- a/src/lib/rules/no-protocol-relative-urls/no-protocol-relative-urls.ts +++ b/src/lib/rules/no-protocol-relative-urls/no-protocol-relative-urls.ts @@ -8,7 +8,7 @@ // ------------------------------------------------------------------------------ import { debug as d } from '../../utils/debug'; -import { IElementFoundEvent, IRule, IRuleBuilder } from '../../interfaces'; // eslint-disable-line no-unused-vars +import { IElementFoundEvent, IRule, IRuleBuilder } from '../../types'; // eslint-disable-line no-unused-vars import { RuleContext } from '../../rule-context'; // eslint-disable-line no-unused-vars const debug = d(__filename); diff --git a/src/lib/sonar.ts b/src/lib/sonar.ts index 71986e742c3..0e077cd85b4 100644 --- a/src/lib/sonar.ts +++ b/src/lib/sonar.ts @@ -13,7 +13,7 @@ import { EventEmitter2 as EventEmitter } from 'eventemitter2'; import { debug as d } from './utils/debug'; import { getSeverity } from './config/config-rules'; -import { ICollector, IElementFoundEvent, IFetchEndEvent, IProblem, IProblemLocation, IRule, Severity, URL } from './interfaces'; // eslint-disable-line no-unused-vars +import { ICollector, IElementFoundEvent, IFetchEndEvent, IProblem, IProblemLocation, IRule, Severity, URL } from './types'; // eslint-disable-line no-unused-vars import * as resourceLoader from './utils/resource-loader'; import { RuleContext } from './rule-context'; diff --git a/src/lib/types.ts b/src/lib/types.ts new file mode 100644 index 00000000000..ec1530691b4 --- /dev/null +++ b/src/lib/types.ts @@ -0,0 +1,26 @@ +/* eslint-disable no-unused-vars */ +import * as url from 'url'; + +import { ICollectorBuilder } from './types/collectors'; +import { IFormatter } from './types/formatters'; +import { IPluginBuilder } from './types/plugins'; +import { IRuleBuilder } from './types/rules'; + +export * from './types/asynchtml'; +export * from './types/collectors'; +export * from './types/events'; +export * from './types/formatters'; +export * from './types/network'; +export * from './types/plugins'; +export * from './types/problems'; +export * from './types/rules'; + +export interface IConfig { + sonarConfig?; +} + +/** A resource required by Sonar: Collector, Formatter, Plugin, Rule. */ +export type Resource = ICollectorBuilder | IFormatter | IPluginBuilder | IRuleBuilder; + +/** An alias for url.Url. */ +export type URL = url.Url; diff --git a/src/lib/interfaces/asynchtml.ts b/src/lib/types/asynchtml.ts similarity index 100% rename from src/lib/interfaces/asynchtml.ts rename to src/lib/types/asynchtml.ts diff --git a/src/lib/interfaces/collectors.ts b/src/lib/types/collectors.ts similarity index 100% rename from src/lib/interfaces/collectors.ts rename to src/lib/types/collectors.ts diff --git a/src/lib/interfaces/events.ts b/src/lib/types/events.ts similarity index 100% rename from src/lib/interfaces/events.ts rename to src/lib/types/events.ts diff --git a/src/lib/interfaces/formatters.ts b/src/lib/types/formatters.ts similarity index 100% rename from src/lib/interfaces/formatters.ts rename to src/lib/types/formatters.ts diff --git a/src/lib/interfaces/network.ts b/src/lib/types/network.ts similarity index 100% rename from src/lib/interfaces/network.ts rename to src/lib/types/network.ts diff --git a/src/lib/interfaces/plugins.ts b/src/lib/types/plugins.ts similarity index 100% rename from src/lib/interfaces/plugins.ts rename to src/lib/types/plugins.ts diff --git a/src/lib/interfaces/problems.ts b/src/lib/types/problems.ts similarity index 100% rename from src/lib/interfaces/problems.ts rename to src/lib/types/problems.ts diff --git a/src/lib/interfaces/rules.ts b/src/lib/types/rules.ts similarity index 91% rename from src/lib/interfaces/rules.ts rename to src/lib/types/rules.ts index 7a641a24811..0785e32e2bc 100644 --- a/src/lib/interfaces/rules.ts +++ b/src/lib/types/rules.ts @@ -9,7 +9,7 @@ export interface IRuleBuilder { /** If this rule can autofix the issue or not */ fixable?: string; /** The schema the rule configuration must follow in order to be valid */ - schema: Array | any; // TODO: this shouldn't be an any + schema: Array; // TODO: this shouldn't be an Array of any /** If the rule works with local resources (file://...) */ worksWithLocalFiles: boolean; }; diff --git a/src/lib/utils/location-helpers.ts b/src/lib/utils/location-helpers.ts index 907f5bf6e24..f67dd8f91b2 100644 --- a/src/lib/utils/location-helpers.ts +++ b/src/lib/utils/location-helpers.ts @@ -1,5 +1,5 @@ import { debug as d } from './debug'; -import { IAsyncHTMLElement, IProblemLocation } from './../interfaces'; // eslint-disable-line no-unused-vars +import { IAsyncHTMLElement, IProblemLocation } from './../types'; // eslint-disable-line no-unused-vars const debug = d(__filename); diff --git a/src/lib/utils/resource-loader.ts b/src/lib/utils/resource-loader.ts index 4629f28918e..ddb1ed2725a 100644 --- a/src/lib/utils/resource-loader.ts +++ b/src/lib/utils/resource-loader.ts @@ -15,7 +15,7 @@ import * as _ from 'lodash'; import * as globby from 'globby'; import { debug as d } from './debug'; -import { ICollectorBuilder, IFormatter, IPluginBuilder, Resource, IRuleBuilder } from '../interfaces'; // eslint-disable-line no-unused-vars +import { ICollectorBuilder, IFormatter, IPluginBuilder, Resource, IRuleBuilder } from '../types'; // eslint-disable-line no-unused-vars const debug = d(__filename); diff --git a/tests/helpers/rule-runner.ts b/tests/helpers/rule-runner.ts index 0e5f5620937..399f9008b27 100644 --- a/tests/helpers/rule-runner.ts +++ b/tests/helpers/rule-runner.ts @@ -7,7 +7,7 @@ import * as url from 'url'; import { test, ContextualTestContext } from 'ava'; // eslint-disable-line no-unused-vars import { createServer } from './test-server'; -import { IElementFoundEvent, INetworkData, IRule, IRuleBuilder } from '../../src/lib/interfaces'; // eslint-disable-line no-unused-vars +import { IElementFoundEvent, INetworkData, IRule, IRuleBuilder } from '../../src/lib/types'; // eslint-disable-line no-unused-vars import { RuleTest } from './rule-test-type'; // eslint-disable-line no-unused-vars import * as Sonar from '../../src/lib/sonar'; diff --git a/tests/helpers/rule-test-type.ts b/tests/helpers/rule-test-type.ts index 3ce2021901a..7e2f1264c2b 100644 --- a/tests/helpers/rule-test-type.ts +++ b/tests/helpers/rule-test-type.ts @@ -1,4 +1,4 @@ -import { IProblemLocation } from '../../src/lib/interfaces'; +import { IProblemLocation } from '../../src/lib/types'; export interface Report { /** The message to validate */ diff --git a/tests/lib/collectors/_common.ts b/tests/lib/collectors/_common.ts index 7589bf1c9f6..ea7e2a38eb5 100644 --- a/tests/lib/collectors/_common.ts +++ b/tests/lib/collectors/_common.ts @@ -15,7 +15,7 @@ import * as sinon from 'sinon'; import test from 'ava'; import { createServer } from '../../helpers/test-server'; -import { ICollector, ICollectorBuilder } from '../../../src/lib/interfaces'; // eslint-disable-line no-unused-vars +import { ICollector, ICollectorBuilder } from '../../../src/lib/types'; // eslint-disable-line no-unused-vars const testCollector = (collectorBuilder: ICollectorBuilder) => { diff --git a/tests/lib/collectors/utils/requester.ts b/tests/lib/collectors/utils/requester.ts index 81f9fd1f20b..5cf0e1b332e 100644 --- a/tests/lib/collectors/utils/requester.ts +++ b/tests/lib/collectors/utils/requester.ts @@ -8,7 +8,7 @@ import * as pify from 'pify'; import { createServer } from '../../../helpers/test-server'; import { Requester } from '../../../../src/lib/collectors/utils/requester'; -import { INetworkData } from '../../../../src/lib/interfaces'; +import { INetworkData } from '../../../../src/lib/types'; const compress = pify(zlib.gzip); const text = `This is a text diff --git a/tests/lib/config/config-rules.ts b/tests/lib/config/config-rules.ts new file mode 100644 index 00000000000..875d5126021 --- /dev/null +++ b/tests/lib/config/config-rules.ts @@ -0,0 +1,141 @@ +import test from 'ava'; + +import * as configRules from '../../../src/lib/config/config-rules'; +import { IRuleBuilder } from '../../../src/lib/types/rules'; //eslint-disable-line no-unused-vars + +const ruleEmptySchema: IRuleBuilder = { + create(config) { //eslint-disable-line no-unused-vars + return null; + }, + meta: { schema: [], worksWithLocalFiles: false } +}; + +const ruleWithSchema: IRuleBuilder = { + create(config) { //eslint-disable-line no-unused-vars + return null; + }, + meta: { + schema: [{ + additionalProperties: false, + definitions: { + 'string-array': { + items: { type: 'string' }, + minItems: 1, + type: 'array', + uniqueItems: true + } + }, + properties: { + ignore: { $ref: '#/definitions/string-array' }, + include: { $ref: '#/definitions/string-array' } + }, + type: ['object', null] + }], + worksWithLocalFiles: false + } +}; + +test('getSeverity with an string should return the right value', (t) => { + const data = new Map([ + ['off', 0], + ['warning', 1], + ['error', 2], + ['invalid', null], + ['', null] + ]); + + for (const [key, value] of data) { + const severity = configRules.getSeverity(key); + + t.is(severity, value); + } +}); + +test('getSeverity with a number should return the right value', (t) => { + const data = new Map([ + [0, 0], + [1, 1], + [2, 2], + [3, null], + [-1, null] + ]); + + for (const [key, value] of data) { + const severity = configRules.getSeverity(key); + + t.is(severity, value); + } +}); + +test('getSeverity with an array should return the right value', (t) => { + const data = new Map([ + [['off', {}], 0], + [['warning', {}], 1], + [['error', {}], 2], + [['invalid', {}], null], + [[0, {}], 0], + [[1, {}], 1], + [[2, {}], 2], + [[3, {}], null], + [[-1, {}], null] + ]); + + for (const [key, value] of data) { + const severity = configRules.getSeverity(key); + + t.is(severity, value); + } +}); + +test('validate should return false if config is an object', (t) => { + const valid = configRules.validate(ruleEmptySchema, { warning: true }, '1'); + + t.false(valid); +}); + +test('validate should throw an exception if the severity is not valid', (t) => { + const data = new Set(['invalid', -1, ['invalid', {}]]); + + for (const value of data) { + t.throws(() => { + configRules.validate(ruleEmptySchema, value, '1'); + }, Error); + } +}); + +test('validate should return true if config is not an array', (t) => { + const valid = configRules.validate(ruleEmptySchema, 'off', '1'); + + t.true(valid); +}); + +test('validate should return true if the schema is an empty array', (t) => { + const valid = configRules.validate(ruleEmptySchema, ['off', {}], '1'); + + t.true(valid); +}); + +test('validate should return true if config is an array with just an element', (t) => { + const valid = configRules.validate(ruleWithSchema, ['warning'], '1'); + + t.true(valid); +}); + +test(`validate should return true if the configuration of a rule is valid`, (t) => { + const validConfiguration = ['warning', { + ignore: ['Server'], + include: ['Custom-Header'] + }]; + + const valid = configRules.validate(ruleWithSchema, validConfiguration, '1'); + + t.true(valid); +}); + +test(`validate should return false if the configuration of a rule is invalid`, (t) => { + const invvalidConfiguration = ['warning', { ignore: 'Server' }]; + + const valid = configRules.validate(ruleWithSchema, invvalidConfiguration, '1'); + + t.false(valid); +}); diff --git a/tests/lib/formatters/fixtures/list-of-problems.ts b/tests/lib/formatters/fixtures/list-of-problems.ts index 5a04a2ecc91..6338a607090 100644 --- a/tests/lib/formatters/fixtures/list-of-problems.ts +++ b/tests/lib/formatters/fixtures/list-of-problems.ts @@ -1,4 +1,4 @@ -import { IProblem, Severity } from '../../../../src/lib/interfaces'; //eslint-disable-line no-unused-vars +import { IProblem, Severity } from '../../../../src/lib/types'; //eslint-disable-line no-unused-vars const multipleproblems: Array = [{ column: 10, diff --git a/tests/lib/formatters/json.ts b/tests/lib/formatters/json.ts index 941aaa6c6ea..ea0169e2705 100644 --- a/tests/lib/formatters/json.ts +++ b/tests/lib/formatters/json.ts @@ -8,7 +8,7 @@ proxyquire('../../../src/lib/formatters/json/json', { '../../utils/logging': log import json from '../../../src/lib/formatters/json/json'; import * as problems from './fixtures/list-of-problems'; -import { Severity } from '../../../src/lib/interfaces'; +import { Severity } from '../../../src/lib/types'; test.beforeEach((t) => { // const log = sinon.spy(logger, 'log'); diff --git a/tests/lib/utils/location-helpers.ts b/tests/lib/utils/location-helpers.ts index a396a0541aa..202c85bcd68 100644 --- a/tests/lib/utils/location-helpers.ts +++ b/tests/lib/utils/location-helpers.ts @@ -8,7 +8,7 @@ import * as pify from 'pify'; import { readFile } from '../../../src/lib/utils/misc'; const getPage = pify(jsdom.env); -import { IAsyncHTMLElement } from '../../../src/lib/interfaces'; +import { IAsyncHTMLElement } from '../../../src/lib/types'; import { findInElement, findProblemLocation, findElementLocation } from '../../../src/lib/utils/location-helpers'; import { JSDOMAsyncHTMLElement } from '../../../src/lib/collectors/jsdom/jsdom-async-html'; diff --git a/tests/lib/utils/resource-loader.ts b/tests/lib/utils/resource-loader.ts index cbd73562824..a54c47e3784 100644 --- a/tests/lib/utils/resource-loader.ts +++ b/tests/lib/utils/resource-loader.ts @@ -11,7 +11,7 @@ const fakeGlobby = { sync() { } }; test.beforeEach((t) => { t.context.fakeGlobby = fakeGlobby; - sinon.stub(fakeGlobby, 'sync').returns(['src/lib/collectors/cdp/cdp.js', 'src/lib/collectors/cdp2/cdp.js']); + sinon.stub(fakeGlobby, 'sync').returns([path.join(process.cwd(), 'dist/src/lib/collectors/cdp/cdp.js'), path.join(process.cwd(), 'dist/src/lib/collectors/cdp2/cdp.js')]); }); test.afterEach((t) => {