Skip to content

Commit

Permalink
feat(result-comparator): include comparison results in config.onComplete
Browse files Browse the repository at this point in the history
  • Loading branch information
AriPerkkio committed Jan 12, 2021
1 parent e36d4c9 commit 378a2b3
Show file tree
Hide file tree
Showing 6 changed files with 210 additions and 8 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,14 @@ module.exports = {
* source: string,
* error: (string|undefined),
* }[]} results Results of the scan, if any
*
* @param {{
* added: {}[],
* removed: {}[]
* }} comparisonResults Comparison results of the scan, if any
* @returns {Promise<void>|void}
*/
onComplete: async function onComplete(results) {
onComplete: async function onComplete(results, comparisonResults) {
// Extend the process with custom features, e.g. send results to email, create issues to Github...
},
};
Expand Down
5 changes: 5 additions & 0 deletions eslint-remote-tester.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ module.exports = {
* source: string,
* error: (string|undefined),
* }[]} results Results of the scan, if any
*
* @param {{
* added: {}[],
* removed: {}[]
* }} comparisonResults Comparison results of the scan, if any
* @returns {Promise<void>|void}
*/
onComplete: undefined,
Expand Down
9 changes: 7 additions & 2 deletions lib/config/config-templates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,14 @@ export const CONFIGURATION_FILE_TEMPLATE =
* source: string,
* error: (string|undefined),
* }[]} results Results of the scan, if any
*
* @param {{
* added: {}[],
* removed: {}[]
* }} comparisonResults Comparison results of the scan, if any
* @returns {Promise<void>|void}
*/
onComplete: async function onComplete(results) {
onComplete: async function onComplete(results, comparisonResults) {
// Extend the process with custom features, e.g. send results to email, create issues to Github...
},
}`;
7 changes: 5 additions & 2 deletions lib/config/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Linter } from 'eslint';

import { Result } from '@file-client/result-templates';
import { ComparisonResults, Result } from '@file-client/result-templates';

type AllKeysOptional<T extends { [K: string]: any }> = {
[K in keyof T]?: T[K];
Expand All @@ -27,7 +27,10 @@ export interface Config {
cache: boolean;
timeLimit: number;
compare: boolean;
onComplete?: (results: Result[]) => Promise<void> | void;
onComplete?: (
results: Result[],
comparisonResults: ComparisonResults | null
) => Promise<void> | void;
}

type RequiredFields = Pick<Config, 'repositories' | 'extensions' | 'eslintrc'>;
Expand Down
12 changes: 9 additions & 3 deletions lib/progress-logger/exit-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
compareResults,
writeComparisonResults,
} from '@file-client';
import { ComparisonResults } from '@file-client/result-templates';
import { RESULT_COMPARISON_FINISHED } from './log-templates';
import { LogMessage } from './types';

Expand All @@ -12,12 +13,14 @@ import { LogMessage } from './types';
*/
export default async function onExit(): Promise<LogMessage[]> {
const messages: LogMessage[] = [];
const results = ResultsStore.getResults();
const errors = [];

const results = ResultsStore.getResults();
let comparisonResults: ComparisonResults | null = null;

if (config.compare) {
try {
const comparisonResults = compareResults(results);
comparisonResults = compareResults(results);
ResultsStore.setComparisonResults(comparisonResults);

messages.push({
Expand All @@ -38,7 +41,10 @@ export default async function onExit(): Promise<LogMessage[]> {

if (config.onComplete) {
try {
const onCompletePromise = config.onComplete(results);
const onCompletePromise = config.onComplete(
results,
comparisonResults
);

if (onCompletePromise instanceof Promise) {
await onCompletePromise;
Expand Down
178 changes: 178 additions & 0 deletions test/integration/integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -763,4 +763,182 @@ describe('integration', () => {
"
`);
});

test('calls onComplete hook with the comparison results', async () => {
await runProductionBuild({
compare: true,
CI: true,
rulesUnderTesting: [
'no-compare-neg-zero', // Used in initial scan, not in second
// 'no-undef', // Used in second scan, not in first
'no-empty', // Used in both scans
],
eslintrc: {
root: true,
extends: ['eslint:all'],
},
});

const { output } = await runProductionBuild({
compare: true,
CI: true,
rulesUnderTesting: [
// 'no-compare-neg-zero', // Used in initial scan, not in second
'no-undef', // Used in second scan, not in first
'no-empty', // Used in both scans
],
eslintrc: {
root: true,
extends: ['eslint:all'],
},
onComplete: function onComplete(_, comparisonResults) {
console.log('[TEST-ON-COMPLETE-START]');

for (const type of ['added', 'removed']) {
console.log(`[${type.toUpperCase()}]`);
// @ts-ignore
comparisonResults[type].forEach(result => {
Object.entries(result).forEach(([key, value]) => {
const block = `[${key.toUpperCase()}]`;

console.log('.');
console.log(block);
console.log(value);
console.log(block);
});
});
console.log(`[${type.toUpperCase()}]`);
}

console.log('[TEST-ON-COMPLETE-END]');
},
});

const [onCompleteCall] = output
.join('\n')
.match(
/\[TEST-ON-COMPLETE-START\]([\s|\S]*)\[TEST-ON-COMPLETE-END\]/
)!;

expect(onCompleteCall).toMatchInlineSnapshot(`
"[TEST-ON-COMPLETE-START]
[ADDED]
.
[REPOSITORY]
eslint-remote-tester-integration-test-target
[REPOSITORY]
.
[REPOSITORYOWNER]
AriPerkkio
[REPOSITORYOWNER]
.
[RULE]
no-undef
[RULE]
.
[MESSAGE]
'window' is not defined.
[MESSAGE]
.
[PATH]
AriPerkkio/eslint-remote-tester-integration-test-target/expected-to-crash-linter.js
[PATH]
.
[LINK]
https://github.com/AriPerkkio/eslint-remote-tester-integration-test-target/blob/HEAD/expected-to-crash-linter.js#L2-L2
[LINK]
.
[EXTENSION]
js
[EXTENSION]
.
[SOURCE]
// Identifier.name = attributeForCrashing
window.attributeForCrashing();
[SOURCE]
.
[ERROR]
undefined
[ERROR]
.
[REPOSITORY]
eslint-remote-tester-integration-test-target
[REPOSITORY]
.
[REPOSITORYOWNER]
AriPerkkio
[REPOSITORYOWNER]
.
[RULE]
no-undef
[RULE]
.
[MESSAGE]
'bar' is not defined.
[MESSAGE]
.
[PATH]
AriPerkkio/eslint-remote-tester-integration-test-target/index.js
[PATH]
.
[LINK]
https://github.com/AriPerkkio/eslint-remote-tester-integration-test-target/blob/HEAD/index.js#L1-L1
[LINK]
.
[EXTENSION]
js
[EXTENSION]
.
[SOURCE]
var foo = bar;
if (foo) {
}
var p = {
[SOURCE]
.
[ERROR]
undefined
[ERROR]
[ADDED]
[REMOVED]
.
[REPOSITORY]
eslint-remote-tester-integration-test-target
[REPOSITORY]
.
[REPOSITORYOWNER]
AriPerkkio
[REPOSITORYOWNER]
.
[RULE]
no-compare-neg-zero
[RULE]
.
[MESSAGE]
Do not use the '===' operator to compare against -0.
[MESSAGE]
.
[PATH]
AriPerkkio/eslint-remote-tester-integration-test-target/index.js
[PATH]
.
[LINK]
https://github.com/AriPerkkio/eslint-remote-tester-integration-test-target/blob/HEAD/index.js#L14-L14
[LINK]
.
[EXTENSION]
js
[EXTENSION]
.
[SOURCE]
};
p.getName();
if (foo === -0) {
// prevent no-empty
}
[SOURCE]
[REMOVED]
[TEST-ON-COMPLETE-END]"
`);
});
});

0 comments on commit 378a2b3

Please sign in to comment.