Skip to content
Permalink
Browse files

Breaking: Move target to options in Formatter.format

  • Loading branch information...
sarvaje authored and molant committed Mar 20, 2019
1 parent 640bd86 commit e2ad04570df3fc8e968bfd412c617b49759b7ae4
@@ -69,7 +69,7 @@ export default class WebExtensionFormatter implements IFormatter {
});
}

public format(problems: Problem[], target: string, options: FormatterOptions) {
public format(problems: Problem[], options: FormatterOptions) {

// The browser extension always provides resources to the formatter.
const resources = options.resources!;
@@ -66,7 +66,7 @@ const main = async (userConfig: Config) => {

const config: Configuration = {
browserslist: determineBrowserslist(userConfig.browserslist),
connector: { name: 'web-extension', options: { } },
connector: { name: 'web-extension', options: {} },
extends: undefined,
formatters: ['web-extension'],
hints: hintsConfig,
@@ -93,7 +93,10 @@ const main = async (userConfig: Config) => {
const problems = await engine.executeOn(new URL(location.href));

engine.formatters.forEach((formatter) => {
formatter.format(problems, location.href, { resources });
formatter.format(problems, {
resources,
target: location.href
});
});
};

@@ -132,7 +132,7 @@ export default class CodeframeFormatter implements IFormatter {
* Format the problems grouped by `resource` name and sorted by line and column number,
* indicating where in the element there is an error.
*/
public async format(messages: Problem[], target: string | undefined, options: FormatterOptions = {}) {
public async format(messages: Problem[], options: FormatterOptions = {}) {
debug('Formatting results');

if (messages.length === 0) {
@@ -198,7 +198,7 @@ test(`Codeframe formatter called with the output option should write the result
const formatter = new CodeframeFormatter();
const outputFile = 'output.json';

formatter.format(problems.codeframeproblems, null, { output: outputFile });
formatter.format(problems.codeframeproblems, { output: outputFile });

const log = t.context.loggingLogSpy;
const writeFileStub = t.context.writeFileAsyncDefaultStub;
@@ -38,11 +38,12 @@ const startRow = 5;
*/

export default class ExcelFormatter implements IFormatter {
public async format(messages: Problem[], /* istanbul ignore next */ target = '', options: FormatterOptions = {}) {
public async format(messages: Problem[], options: FormatterOptions = {}) {
if (messages.length === 0) {
return;
}

const target = options.target || '';
const resources: _.Dictionary<Problem[]> = _.groupBy(messages, 'resource');
const workbook = new Excel.Workbook();
const sortedResources = _.sortBy(Object.keys(resources));
@@ -29,15 +29,15 @@ test(`Excel formatter doesn't print anything if no values`, async (t) => {
const formatter = new t.context.ExcelFormatter();
const spy = t.context.spy;

await formatter.format(problems.noproblems, '');
await formatter.format(problems.noproblems);

t.is(spy.callCount, 0);
});

test(`Excel formatter generates the right number of sheets with the good content`, async (t) => {
const formatter = new t.context.ExcelFormatter();

await formatter.format(problems.multipleproblems, 'http://myresource.com:8080/');
await formatter.format(problems.multipleproblems, { target: 'http://myresource.com:8080/' });

const workbook = new Excel.Workbook();
const filePath = path.join(process.cwd(), 'http-myresource-com-8080.xlsx');
@@ -62,7 +62,7 @@ test(`Excel formatter generates the right number of sheets with the good content
const formatter = new t.context.ExcelFormatter();
const filePath = path.join(process.cwd(), 'test.xlsx');

await formatter.format(problems.multipleproblems, undefined, { output: filePath });
await formatter.format(problems.multipleproblems, { output: filePath });

const workbook = new Excel.Workbook();

@@ -91,10 +91,11 @@ export default class HTMLFormatter implements IFormatter {
}

/** Format the problems grouped by `resource` name and sorted by line and column number */
public async format(problems: Problem[], /* istanbul ignore next */ target = '', /* istanbul ignore next */ options: FormatterOptions = {}) {
public async format(problems: Problem[], /* istanbul ignore next */ options: FormatterOptions = {}) {

debug('Formatting results');

const target = options.target || '';
const result = new AnalysisResult(target, options);
const categoryList: string[] = getCategoryList(options.resources);

@@ -47,7 +47,7 @@ test(`HTML formatter returns the right object`, async (t) => {
const HTMLFormatter = loadScript(t.context);
const formatter = new HTMLFormatter();

const result: Result = await formatter.format(problems.noproblems, 'http://example.com');
const result: Result = await formatter.format(problems.noproblems, { target: 'http://example.com' });

t.plan((result.categories.length * 2) + 2);

@@ -64,7 +64,7 @@ test(`HTML formatter returns the right number of erros and warnings`, async (t)
const HTMLFormatter = loadScript(t.context);
const formatter = new HTMLFormatter();

const result: Result = await formatter.format(problems.multipleproblems, 'http://example.com');
const result: Result = await formatter.format(problems.multipleproblems, { target: 'http://example.com' });

t.plan(13);

@@ -109,7 +109,10 @@ test(`HTML formatter return the right value for isFinish`, async (t) => {
const HTMLFormatter = loadScript(t.context);
const formatter = new HTMLFormatter();

const result: Result = await formatter.format(problems.multipleproblems, 'http://example.com', { status: 'error' });
const result: Result = await formatter.format(problems.multipleproblems, {
status: 'error',
target: 'http://example.com'
});

t.is(result.isFinish, true);
});
@@ -118,7 +121,10 @@ test(`HTML formatter return the right scan time`, async (t) => {
const HTMLFormatter = loadScript(t.context);
const formatter = new HTMLFormatter();

const result: Result = await formatter.format(problems.multipleproblems, 'http://example.com', { scanTime: 4500000 });
const result: Result = await formatter.format(problems.multipleproblems, {
scanTime: 4500000,
target: 'http://example.com'
});

t.is(result.scanTime, '01:15:00');
});
@@ -127,8 +133,11 @@ test(`HTML formatter return the right third party logo url`, async (t) => {
const HTMLFormatter = loadScript(t.context);
const formatter = new HTMLFormatter();

const result1: Result = await formatter.format(problems.multipleproblems, 'http://example.com', {});
const result2: Result = await formatter.format(problems.multipleproblems, 'http://example.com', { isScanner: true });
const result1: Result = await formatter.format(problems.multipleproblems, { target: 'http://example.com' });
const result2: Result = await formatter.format(problems.multipleproblems, {
isScanner: true,
target: 'http://example.com'
});

const category1 = result1.getCategoryByName(Category.development);
const category2 = result2.getCategoryByName(Category.development);
@@ -160,7 +169,10 @@ test(`HTML formatter create copy and generate the right files`, async (t) => {
const HTMLFormatter = loadScript(t.context);
const formatter = new HTMLFormatter();

await formatter.format(problems.noproblems, 'http://example.com', { config: {} });
await formatter.format(problems.noproblems, {
config: {},
target: 'http://example.com'
});

t.true(fsExtraCopySpy.calledOnce);
t.true(fsExtraRemoveSpy.calledOnce);
@@ -182,7 +194,11 @@ test(`HTML formatter create copy and generate the right files if an output is pr
const formatter = new HTMLFormatter();
const outputFolder = path.join(process.cwd(), 'outputfolder');

await formatter.format(problems.noproblems, 'http://example.com', { config: {}, output: outputFolder });
await formatter.format(problems.noproblems, {
config: {},
output: outputFolder,
target: 'http://example.com'
});

t.true(fsExtraCopySpy.calledOnce);
t.true(fsExtraRemoveSpy.calledOnce);
@@ -204,7 +220,10 @@ test(`HTML formatter shoudn't copy and generate any file if option noGenerateFil
const HTMLFormatter = loadScript(t.context);
const formatter = new HTMLFormatter();

await formatter.format(problems.noproblems, 'http://example.com', { noGenerateFiles: true });
await formatter.format(problems.noproblems, {
noGenerateFiles: true,
target: 'http://example.com'
});

t.false(fsExtraCopySpy.called);
t.false(fsExtraRemoveSpy.called);
@@ -35,7 +35,7 @@ const debug = d(__filename);

export default class JSONFormatter implements IFormatter {
/** Format the problems grouped by `resource` name and sorted by line and column number */
public async format(messages: Problem[], target: string | undefined, options: FormatterOptions = {}) {
public async format(messages: Problem[], options: FormatterOptions = {}) {

debug('Formatting results');

@@ -207,7 +207,7 @@ test(`JSON formatter called with the output option should write the result in th
const formatter = new JsonFormatter();
const outputFile = 'output.json';

formatter.format(problems.multipleproblems, null, { output: outputFile });
formatter.format(problems.multipleproblems, { output: outputFile });

const loggingLogSpy = t.context.loggingLogSpy;
const writeFileAsyncDefaultStub = t.context.writeFileAsyncDefaultStub;
@@ -226,7 +226,7 @@ test('JSON formatter only save one file with the result even if there is multipl
const formatter = new JsonFormatter();
const outputFile = path.join(process.cwd(), '..', '..', 'output.json');

formatter.format(problems.multipleproblemsandresoruces, null, { output: outputFile });
formatter.format(problems.multipleproblemsandresoruces, { output: outputFile });

const loggingLogSpy = t.context.loggingLogSpy;
const writeFileAsyncDefaultStub = t.context.writeFileAsyncDefaultStub;
@@ -51,7 +51,7 @@ const printPosition = (position: number, text: string) => {

export default class StylishFormatter implements IFormatter {
/** Format the problems grouped by `resource` name and sorted by line and column number */
public async format(messages: Problem[], target: string | undefined, options: FormatterOptions = {}) {
public async format(messages: Problem[], options: FormatterOptions = {}) {

debug('Formatting results');

@@ -144,7 +144,7 @@ test(`Stylish formatter called with the output option should write the result in
const formatter = new StylishFormatter();
const outputFile = 'test.txt';

formatter.format(problems.multipleproblemsandresources, undefined, { output: outputFile });
formatter.format(problems.multipleproblemsandresources, { output: outputFile });

const log = t.context.loggingLogSpy;
const writeFileStub = t.context.writeFileAsyncDefaultStub;
@@ -36,7 +36,7 @@ const debug = d(__filename);

export default class SummaryFormatter implements IFormatter {
/** Format the problems grouped by `resource` name and sorted by line and column number */
public async format(messages: Problem[], target: string | undefined, options: FormatterOptions = {}) {
public async format(messages: Problem[], options: FormatterOptions = {}) {
debug('Formatting results');

if (messages.length === 0) {
@@ -147,7 +147,7 @@ test(`Summary formatter called with the output option should write the result in
const SummaryFormatter = loadScript(t.context);
const formatter = new SummaryFormatter();

formatter.format(problems.summaryErrorWarnings, undefined, { output: outputFile });
formatter.format(problems.summaryErrorWarnings, { output: outputFile });

tableData.push(['random-hint', '1 error', '1 warning']);

@@ -365,12 +365,13 @@ export default async (actions: CLIOptions): Promise<boolean> => {
output: actions.output ? path.resolve(cwd(), actions.output) : undefined,
resources,
scanTime,
target,
version: loadHintPackage().version
};

if (engine) {
await each(engine.formatters, async (formatter: IFormatter) => {
await formatter.format(reports, target, formatterOptions);
await formatter.format(reports, formatterOptions);
});
}
};
@@ -3,14 +3,15 @@ import { UserConfig, HintResources } from '../types';

export type FormatterOptions = {
config?: UserConfig;
/** Start time (queued in online scanner) ISO string */
date?: string;
isScanner?: boolean;
noGenerateFiles?: boolean;
output?: string;
resources?: HintResources;
scanTime?: number;
status?: string;
/** Start time (queued in online scanner) ISO string */
date?: string;
target?: string;
version?: string;
};

@@ -20,5 +21,5 @@ export interface IFormatterConstructor {

/** A format function that will output the results obtained by hint */
export interface IFormatter {
format(problems: Problem[], target?: string, options?: FormatterOptions): void;
format(problems: Problem[], options?: FormatterOptions): void;
}

0 comments on commit e2ad045

Please sign in to comment.
You can’t perform that action at this time.