Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CSV reporter #203

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion bin/pa11y-ci.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const globby = require('globby');
const protocolify = require('protocolify');
const pkg = require('../package.json');
const commander = require('commander');

const csvReporter = require('../lib/reporters/csv');

// Here we're using Commander to specify the CLI options
commander
Expand Down Expand Up @@ -47,6 +47,10 @@ commander
'-j, --json',
'Output results as JSON'
)
.option(
'--csv',
'Output results as CSV'
)
.option(
'-T, --threshold <number>',
'permit this number of errors, warnings, or notices, otherwise fail with exit code 2',
Expand Down Expand Up @@ -96,6 +100,11 @@ Promise.resolve()
return value;
}));
}
// Output CSV if asked for it
if (commander.csv) {
const reporter = csvReporter();
reporter(report);
}
// Decide on an exit code based on whether
// errors are below threshold or everything passes
if (report.errors >= parseInt(commander.threshold, 10) && report.passes < report.total) {
Expand Down
3 changes: 2 additions & 1 deletion lib/helpers/resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ const loadReporter = require('./loader');

const reporterShorthand = {
cli: require.resolve('../reporters/cli.js'),
json: require.resolve('../reporters/json.js')
json: require.resolve('../reporters/json.js'),
csv: require.resolve('../reporters/csv.js')
};

module.exports = function resolveReporters(config = {}) {
Expand Down
74 changes: 74 additions & 0 deletions lib/reporters/csv.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
'use strict';

const fs = require('fs');
const {resolve, isAbsolute, dirname} = require('path');

function writeReport(fileName, data) {
try {
const dirPath = dirname(fileName);
if (!(fs.existsSync(dirPath) && fs.lstatSync(dirPath).isDirectory())) {
fs.mkdirSync(dirPath, {recursive: true});
}
fs.writeFileSync(fileName, data);
} catch (error) {
console.error(`Unable to write ${fileName}`);
console.error(error);
}
}

function resolveFile(fileName) {
if (typeof fileName !== 'string') {
return null;
}
return isAbsolute(fileName) ? fileName : resolve(process.cwd(), fileName);
}

function stringify(value) {
if (typeof value === 'undefined' || value === null || value === '') {
return '';
}

const str = String(value);
return needsQuote(str) ? quoteField(str) : str;
}

function quoteField(field) {
return `"${field.replace(/"/g, '""')}"`;
}

function needsQuote(str) {
return str.includes(',') || str.includes('\r') || str.includes('\n') || str.includes('"');
}

module.exports = function csvReporter(options = {}) {
const fileName = resolveFile(options.fileName);
return {
afterAll(report) {
let csvString = 'url,code,type,typeCode,message,context,selector,runner\n';
const results = report.results;
for (const url in results) {
if (Object.prototype.hasOwnProperty.call(results, url)) {
const urlErrors = report.results[url];
for (const errorRow of urlErrors) {
const typeCode = errorRow.typeCode;
const message = stringify(errorRow.message);
const context = stringify(errorRow.context);
const selector = stringify(errorRow.selector);
csvString += `${url},${errorRow.code},${errorRow.type},${typeCode},` +
`${message},${context},` +
`${selector},${errorRow.runner}\n`;
}
}
}

// If reporter options specify an output file, write to file.
// Otherwise, write to console for backwards compatibility with
// previous --csv CLI option.
if (fileName) {
writeReport(fileName, csvString);
} else {
console.log(csvString);
}
}
};
};