Skip to content

Commit

Permalink
feat(config): adds updateComparisonReference option
Browse files Browse the repository at this point in the history
  • Loading branch information
AriPerkkio committed Jan 12, 2021
1 parent 378a2b3 commit d544578
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 9 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ module.exports = {
/** Optional boolean flag used to enable result comparison. Defaults to false. */
compare: false,

/** Optional boolean flag used to enable result comparison reference updating. Used only when compare is enable. Defaults to true. */
updateComparisonReference: true,

/** Optional time limit in seconds for the scan. Scan is interrupted after reaching the limit. Defaults to 5 hours 30 minutes. */
timeLimit: 5.5 * 60 * 60, // 5 hours 30 minutes

Expand Down
2 changes: 2 additions & 0 deletions eslint-remote-tester.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ module.exports = {

compare: false,

updateComparisonReference: true,

/**
* Optional callback invoked once scan is complete.
*
Expand Down
3 changes: 3 additions & 0 deletions lib/config/config-templates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ export const CONFIGURATION_FILE_TEMPLATE =
/** Optional boolean flag used to enable result comparison. Defaults to false. */
compare: false,
/** Optional boolean flag used to enable result comparison reference updating. Used only when compare is enable. Defaults to true. */
updateComparisonReference: true,
/**
* Optional callback invoked once scan is complete.
*
Expand Down
1 change: 1 addition & 0 deletions lib/config/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export interface Config {
cache: boolean;
timeLimit: number;
compare: boolean;
updateComparisonReference: boolean;
onComplete?: (
results: Result[],
comparisonResults: ComparisonResults | null
Expand Down
39 changes: 30 additions & 9 deletions lib/config/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const DEFAULT_TIME_LIMIT_SECONDS = 5.5 * 60 * 60;
const DEFAULT_CI = process.env.CI === 'true';
const DEFAULT_CACHE = true;
const DEFAULT_COMPARE = false;
const DEFAULT_UPDATE_COMPARISON_REFERENCE = true;

const UNKNOWN_RULE_REGEXP = /^Definition for rule (.*) was not found.$/;

Expand Down Expand Up @@ -73,6 +74,20 @@ function validateOptionalPositiveNumber(
}
}

/**
* Validate optional boolean:
* 1. Value is optional
* 2. Type is boolean
*/
function validateOptionalBoolean(
name: keyof Config,
value: boolean | undefined
) {
if (value != null && typeof value !== 'boolean') {
return `${name} (${value}) should be a boolean.`;
}
}

/**
* Validate given configuration
*/
Expand All @@ -93,6 +108,7 @@ export default async function validate(
cache,
timeLimit,
compare,
updateComparisonReference,
onComplete,
...unknownKeys
} = configToValidate;
Expand Down Expand Up @@ -152,9 +168,7 @@ export default async function validate(
validateOptionalPositiveNumber('maxFileSizeBytes', maxFileSizeBytes)
);

if (CI != null && typeof CI !== 'boolean') {
errors.push(`CI (${CI}) should be a boolean.`);
}
errors.push(validateOptionalBoolean('CI', CI));

if (logLevel && !LOG_LEVELS.includes(logLevel)) {
errors.push(
Expand All @@ -164,9 +178,7 @@ export default async function validate(
);
}

if (cache != null && typeof cache !== 'boolean') {
errors.push(`cache (${cache}) should be a boolean.`);
}
errors.push(validateOptionalBoolean('cache', cache));

if (resultParser && !RESULT_PARSERS.includes(resultParser)) {
errors.push(
Expand All @@ -182,9 +194,13 @@ export default async function validate(

errors.push(validateOptionalPositiveNumber('timeLimit', timeLimit));

if (compare != null && typeof compare !== 'boolean') {
errors.push(`compare (${compare}) should be a boolean.`);
}
errors.push(validateOptionalBoolean('compare', compare));
errors.push(
validateOptionalBoolean(
'updateComparisonReference',
updateComparisonReference
)
);

if (onComplete && typeof onComplete !== 'function') {
errors.push(`onComplete (${onComplete}) should be a function`);
Expand Down Expand Up @@ -239,6 +255,11 @@ export function getConfigWithDefaults(config: ConfigWithOptionals): Config {
timeLimit: config.timeLimit || DEFAULT_TIME_LIMIT_SECONDS,

compare: config.compare != null ? config.compare : DEFAULT_COMPARE,

updateComparisonReference:
config.updateComparisonReference != null
? config.updateComparisonReference
: DEFAULT_UPDATE_COMPARISON_REFERENCE,
};
}

Expand Down
29 changes: 29 additions & 0 deletions test/unit/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const DEFAULT_CONFIGURATION: ConfigWithOptionals = {
logLevel: undefined,
timeLimit: undefined,
compare: undefined,
updateComparisonReference: undefined,
onComplete: undefined,
};

Expand All @@ -48,6 +49,7 @@ describe('config', () => {
expect(config.pathIgnorePattern).toBe(undefined);
expect(config.cache).toBe(true);
expect(config.compare).toBe(false);
expect(config.updateComparisonReference).toBe(true);
expect(config.onComplete).toBe(undefined);
});

Expand All @@ -64,6 +66,7 @@ describe('config', () => {
expect(config.pathIgnorePattern).toBe(undefined);
expect(config.cache).toBe(true);
expect(config.compare).toBe(false);
expect(config.updateComparisonReference).toBe(true);
expect(config.onComplete).toBe(undefined);
});

Expand Down Expand Up @@ -305,6 +308,20 @@ describe('validateConfig', () => {
});
});

test('updateComparisonReference is optional', async () => {
await validateConfig({
...DEFAULT_CONFIGURATION,
updateComparisonReference: undefined,
});
});

test('updateComparisonReference accepts boolean', async () => {
await validateConfig({
...DEFAULT_CONFIGURATION,
updateComparisonReference: true,
});
});

test('onComplete is optional', async () => {
await validateConfig({
...DEFAULT_CONFIGURATION,
Expand Down Expand Up @@ -664,6 +681,18 @@ describe('validateConfig', () => {
);
});

test('updateComparisonReference requires boolean', async () => {
await validateConfig({
...DEFAULT_CONFIGURATION,
updateComparisonReference: 'text' as any,
});

const [validationError] = getConsoleLogCalls();
expect(validationError).toMatch(
'updateComparisonReference (text) should be a boolean.'
);
});

test('onComplete requires function', async () => {
await validateConfig({
...DEFAULT_CONFIGURATION,
Expand Down

0 comments on commit d544578

Please sign in to comment.