Skip to content

Commit

Permalink
Add test to config-rule.ts
Browse files Browse the repository at this point in the history
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
  • Loading branch information
sarvaje authored and molant committed Apr 19, 2017
1 parent b99493e commit 44688ca
Show file tree
Hide file tree
Showing 40 changed files with 217 additions and 79 deletions.
2 changes: 1 addition & 1 deletion src/lib/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down
2 changes: 1 addition & 1 deletion src/lib/collectors/cdp/cdp-async-html.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/collectors/cdp/cdp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
2 changes: 1 addition & 1 deletion src/lib/collectors/jsdom/jsdom-async-html.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/collectors/jsdom/jsdom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
2 changes: 1 addition & 1 deletion src/lib/collectors/utils/requester.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/lib/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
37 changes: 17 additions & 20 deletions src/lib/config/config-rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
// ------------------------------------------------------------------------------
Expand All @@ -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]);
Expand All @@ -49,6 +43,12 @@ export const getSeverity = (config): Severity => {

};

const validateRule = (schema: Array<object>, 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 => {

Expand All @@ -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}`);
}

Expand All @@ -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]);
};
2 changes: 1 addition & 1 deletion src/lib/formatters/json/json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
26 changes: 0 additions & 26 deletions src/lib/interfaces.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/lib/rule-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

// ------------------------------------------------------------------------------
Expand Down
6 changes: 3 additions & 3 deletions src/lib/rules/disallowed-headers/disallowed-headers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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

// ------------------------------------------------------------------------------
Expand Down Expand Up @@ -56,7 +56,7 @@ const rule: IRuleBuilder = {
recommended: true
},
fixable: 'code',
schema: {
schema: [{
additionalProperties: false,
definitions: {
'string-array': {
Expand All @@ -71,7 +71,7 @@ const rule: IRuleBuilder = {
include: { $ref: '#/definitions/string-array' }
},
type: ['object', null]
},
}],
worksWithLocalFiles: false
}
};
Expand Down
2 changes: 1 addition & 1 deletion src/lib/rules/lang-attribute/lang-attribute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/lib/rules/manifest-exists/manifest-exists.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/lib/rules/manifest-is-valid/manifest-is-valid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions src/lib/rules/no-html-only-headers/no-html-only-headers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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

// ------------------------------------------------------------------------------
Expand Down Expand Up @@ -92,7 +92,7 @@ const rule: IRuleBuilder = {
recommended: true
},
fixable: 'code',
schema: {
schema: [{
additionalProperties: false,
definitions: {
'string-array': {
Expand All @@ -107,7 +107,7 @@ const rule: IRuleBuilder = {
include: { $ref: '#/definitions/string-array' }
},
type: ['object', null]
},
}],
worksWithLocalFiles: false
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/lib/sonar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down
26 changes: 26 additions & 0 deletions src/lib/types.ts
Original file line number Diff line number Diff line change
@@ -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;
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion src/lib/interfaces/rules.ts → src/lib/types/rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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> | any; // TODO: this shouldn't be an any
schema: Array<any>; // TODO: this shouldn't be an Array of any
/** If the rule works with local resources (file://...) */
worksWithLocalFiles: boolean;
};
Expand Down
2 changes: 1 addition & 1 deletion src/lib/utils/location-helpers.ts
Original file line number Diff line number Diff line change
@@ -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);

Expand Down
2 changes: 1 addition & 1 deletion src/lib/utils/resource-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion tests/helpers/rule-runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down
2 changes: 1 addition & 1 deletion tests/helpers/rule-test-type.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IProblemLocation } from '../../src/lib/interfaces';
import { IProblemLocation } from '../../src/lib/types';

export interface Report {
/** The message to validate */
Expand Down
2 changes: 1 addition & 1 deletion tests/lib/collectors/_common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {

Expand Down
2 changes: 1 addition & 1 deletion tests/lib/collectors/utils/requester.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading

0 comments on commit 44688ca

Please sign in to comment.