Skip to content

Commit

Permalink
feat(*): Add rule result for pretty reporter
Browse files Browse the repository at this point in the history
  • Loading branch information
wadackel committed Dec 19, 2020
1 parent 332f7a9 commit f668f8a
Show file tree
Hide file tree
Showing 20 changed files with 259 additions and 191 deletions.
1 change: 0 additions & 1 deletion packages/cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,6 @@ acot run <paths..> [flags]
--connection-timeout Timeout ms for connecting to the host server.
--browser-timeout Timeout ms to wait for pooled browsers.
--ready-timeout Timeout ms waiting for page load.
--performance Dump the execution time per rule.
--chrome-channel Channel to search local Chromium. One of "puppeteer", "canary", "stable", "*". (default: "*")
--chrome-executable-path Executable Chromium path.
--launch-options JSON string of launch config for Puppeteer.
Expand Down
31 changes: 0 additions & 31 deletions packages/cli/src/commands/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,34 +11,13 @@ import type {
ResolvedConfig,
Runner,
RunnerFactoryConfig,
Timing,
} from '@acot/types';
import { parseViewport } from '@acot/utils';
import isCI from 'is-ci';
import table from 'markdown-table';
import { createCommand } from '../command';
import { debug } from '../logging';
import { createDefaultRunner } from '../runner';

const generateTimingTable = (timing: Timing) => {
const entries = Object.entries(timing);
const total = entries.reduce((acc, cur) => acc + cur[1], 0);

const rows = entries
.sort((a, b) => b[1] - a[1])
.slice(0, 20)
.map((row) => {
const ms = row[1];
return [row[0], ms.toFixed(3), `${((ms / total) * 100).toFixed(1)}%`];
});

rows.unshift(['Rule', 'Time (ms)', 'Relative']);

return table(rows, {
align: ['l', 'r', 'r'],
});
};

export default createCommand({
name: 'run',
summary: 'Running an audit.',
Expand Down Expand Up @@ -107,11 +86,6 @@ export default createCommand({
default: 1000 * 30,
description: 'Timeout ms waiting for page load.',
},
performance: {
type: 'boolean',
default: false,
description: 'Dump the execution time per rule.',
},
'chrome-channel': {
type: 'string',
description:
Expand Down Expand Up @@ -251,11 +225,6 @@ export default createCommand({

const summary = await runner.run();

if (args.performance) {
logger.print(generateTimingTable(summary.timing));
logger.print('');
}

const maxWarnings = args['max-warnings'];
let code = summary.errorCount > 0 ? 1 : 0;

Expand Down
4 changes: 4 additions & 0 deletions packages/core/src/__tests__/rule-context.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ describe('rule-context', () => {
page: page as any,
options: ['error' as const, null],
results: [],
measure: () => 0,
};

const factory = async (partial: Partial<CreateRuleContextParams> = {}) => {
Expand Down Expand Up @@ -95,6 +96,7 @@ describe('rule-context', () => {

expect(results).toEqual([
{
duration: 0,
htmlpath,
imagepath,
process: defaults.process,
Expand Down Expand Up @@ -141,6 +143,7 @@ describe('rule-context', () => {

expect(results).toEqual([
{
duration: 0,
htmlpath,
imagepath: null,
process: defaults.process,
Expand Down Expand Up @@ -176,6 +179,7 @@ describe('rule-context', () => {

expect(results).toEqual([
{
duration: 0,
htmlpath,
imagepath: null,
process: defaults.process,
Expand Down
45 changes: 27 additions & 18 deletions packages/core/src/acot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,16 @@ import type {
TestDescriptor,
TestResult,
LaunchOptions,
Stat,
} from '@acot/types';
import Emittery from 'emittery';
import _ from 'lodash';
import type { Viewport } from 'puppeteer-core';
import { createStat } from '@acot/factory';
import { BrowserPool } from './browser-pool';
import { debug } from './logging';
import { RuleStore } from './rule-store';
import { Tester } from './tester';
import { TimingTracker } from './timing-tracker';

export type AcotConfig = {
cwd: string;
Expand Down Expand Up @@ -102,7 +103,6 @@ export class Acot implements Core {
}

public async audit(): Promise<Summary> {
const tracker = new TimingTracker();
const urls = this._testers.map((tester) => tester.url());

// working directory
Expand All @@ -127,7 +127,6 @@ export class Acot implements Core {

const context = {
pool: this._pool,
tracker,
};

const list = await Promise.allSettled(
Expand All @@ -136,9 +135,7 @@ export class Acot implements Core {
}),
);

debug('time: %O', tracker);

const summary = this._summarize(results, tracker);
const summary = this._summarize(results);

await this._emitter.emit('audit:complete', [summary]);

Expand Down Expand Up @@ -168,24 +165,36 @@ export class Acot implements Core {
return summary;
}

private _summarize(results: TestResult[], tracker: TimingTracker): Summary {
const stat = results.reduce(
(acc, cur) => ({
passCount: acc.passCount + cur.passCount,
errorCount: acc.errorCount + cur.errorCount,
warningCount: acc.warningCount + cur.warningCount,
}),
private _summarize(results: TestResult[]): Summary {
const rulesAndStat = results.reduce<Pick<Summary, keyof Stat | 'rules'>>(
(acc, cur) => {
acc.duration += cur.duration;
acc.passCount += cur.passCount;
acc.errorCount += cur.errorCount;
acc.warningCount += cur.warningCount;

Object.keys(cur.rules).forEach((rule) => {
if (acc.rules[rule] == null) {
acc.rules[rule] = createStat();
}

acc.rules[rule].duration += cur.rules[rule].duration;
acc.rules[rule].passCount += cur.rules[rule].passCount;
acc.rules[rule].errorCount += cur.rules[rule].errorCount;
acc.rules[rule].warningCount += cur.rules[rule].warningCount;
});

return acc;
},
{
passCount: 0,
errorCount: 0,
warningCount: 0,
...createStat(),
rules: {},
},
);

return {
timing: tracker.flush(),
...rulesAndStat,
results: _.orderBy(results, [(res) => res.url], ['asc']),
...stat,
};
}

Expand Down
5 changes: 5 additions & 0 deletions packages/core/src/rule-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { element2selector } from 'puppeteer-element2selector';
import filenamify from 'filenamify';
import { createTestcaseResult } from '@acot/factory';
import { debug } from './logging';
import type { TimingMeasure } from './timing';
const rootDebug = require('debug')('acot');

const writeFile = promisify(fs.writeFile);
Expand Down Expand Up @@ -66,6 +67,7 @@ export type CreateRuleContextParams = {
page: Page;
options: [ReportType, RuleOptions | null];
results: TestcaseResult[];
measure: TimingMeasure;
};

export const createRuleContext = ({
Expand All @@ -78,6 +80,7 @@ export const createRuleContext = ({
page,
options,
results,
measure,
}: CreateRuleContextParams): RuleContext => {
const log = rootDebug.extend(rule);

Expand All @@ -101,6 +104,7 @@ export const createRuleContext = ({
process,
status,
rule,
duration: measure(),
message,
tags: [...ruleTags, ...(reportTags ?? [])],
selector,
Expand All @@ -115,6 +119,7 @@ export const createRuleContext = ({
process,
status: 'error',
rule,
duration: measure(),
message: e.message,
}),
);
Expand Down

0 comments on commit f668f8a

Please sign in to comment.