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

Theme checks grouping improvements #217

Closed
wants to merge 5 commits into from
Closed
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
45 changes: 34 additions & 11 deletions bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,34 +97,49 @@ function outputResult(result) {
}

function getSummary(theme) {
let summaryText = '';
const errorCount = theme.results.error.length;
const warnCount = theme.results.warning.length;
const pluralize = require('pluralize');
const checkSymbol = '\u2713';
// NOTE: had to subtract the number of 'invisible' formating symbols
// needs update if formatting above changes
let hiddenSymbols = 0;
let summaryText = '';

if (errorCount === 0 && warnCount === 0) {
if (theme.results.fatal.length === 0
&& theme.results.error.length === 0
&& theme.results.warning.length === 0) {
summaryText = `${chalk.green(checkSymbol)} Your theme is compatible with Ghost ${theme.checkedVersion}`;
} else {
summaryText = `Your theme has`;

if (!_.isEmpty(theme.results.error)) {
summaryText += chalk.red.bold(` ${pluralize('error', theme.results.error.length, true)}`);
if (!_.isEmpty(theme.results.fatal)) {
summaryText += chalk.red.bold(` ${pluralize('fatal error', theme.results.fatal.length, true)}`);
hiddenSymbols += 19;
}

if (!_.isEmpty(theme.results.error) && !_.isEmpty(theme.results.warning)) {
summaryText += ' and';
if (!_.isEmpty(theme.results.error)) {
if (!_.isEmpty(theme.results.fatal)){
if (!_.isEmpty(theme.results.warning)){
summaryText += ',';
} else {
summaryText += ' and';
}
}

summaryText += chalk.red.bold(` ${pluralize('error', theme.results.error.length, true)}`);
hiddenSymbols += 19;
}

if (!_.isEmpty(theme.results.warning)) {
if (!_.isEmpty(theme.results.error) || !_.isEmpty(theme.results.fatal)){
summaryText += ' and';
}

summaryText += chalk.yellow.bold(` ${pluralize('warning', theme.results.warning.length, true)}`);
hiddenSymbols += 19;
}

summaryText += '!';

// NOTE: had to subtract the number of 'invisible' formating symbols
// needs update if formatting above changes
const hiddenSymbols = 38;
summaryText += '\n' + _.repeat('-', (summaryText.length - hiddenSymbols));
}

Expand All @@ -138,6 +153,14 @@ function outputResults(theme, options) {

ui.log('\n' + getSummary(theme));

if (!_.isEmpty(theme.results.fatal)) {
ui.log(chalk.red.bold('\nFatal errors'));
ui.log(chalk.red.bold('------------'));
ui.log(chalk.red('Must be fixed to activate the theme.\n'));

_.each(theme.results.fatal, outputResult);
}

if (!_.isEmpty(theme.results.error)) {
ui.log(chalk.red.bold('\nErrors'));
ui.log(chalk.red.bold('------'));
Expand Down
61 changes: 43 additions & 18 deletions lib/format.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ const levelWeights = {
recommendation: 1
};

let format;
let calcScore;

calcScore = function calcScore(results, stats) {
const calcScore = (results, stats) => {
var maxScore, actualScore, balancedScore;

maxScore = _.reduce(levelWeights, function (max, weight, level) {
Expand Down Expand Up @@ -51,19 +48,24 @@ const formatCLI = (theme) => {
/**
* TODO: This needs cleaning up, a lot
*/
format = function format(theme, options = {}) {
options = _.extend({onlyFatalErrors: false, sortByFiles: false}, options);
const format = (theme, options = {}) => {
options = _.extend({
onlyFatalErrors: false,
sortByFiles: false
}, options);

const checkVersion = _.get(options, 'checkVersion', 'latest');
const ruleSet = spec.get([checkVersion]);
const processedCodes = [];
let hasFatalErrors = false;
const stats = {
error: 0,
warning: 0,
recommendation: 0,
fatal: 0
};

var processedCodes = [],
hasFatalErrors = false,
stats = {
error: 0,
warning: 0,
recommendation: 0
};

theme.results.fatal = [];
theme.results.error = [];
theme.results.warning = [];
theme.results.recommendation = [];
Expand All @@ -75,10 +77,13 @@ format = function format(theme, options = {}) {
if (rule.fatal && options.onlyFatalErrors || options.onlyFatalErrors === false) {
if (rule.fatal) {
hasFatalErrors = true;
theme.results.fatal.push(_.extend({}, _.merge({}, {fatal: false}, rule), info, {code: code}));
stats.fatal += 1;
} else {
theme.results[rule.level].push(_.extend({}, _.merge({}, {fatal: false}, rule), info, {code: code}));
stats[rule.level] += 1;
}

theme.results[rule.level].push(_.extend({}, _.merge({}, {fatal: false}, rule), info, {code: code}));
stats[rule.level] += 1;
processedCodes.push(code);
}
});
Expand All @@ -103,9 +108,26 @@ format = function format(theme, options = {}) {
// CASE 2: (default): sort by rules
if (options.sortByFiles) {
const recommendationsByFile = {};
const fatalByFile = {};
const errorsByFile = {};
const warningsByFile = {};

theme.results.fatal.forEach((error) => {
const failures = error.failures;

if (!failures || !failures.length) {
return;
}

failures.forEach((failure) => {
if (!fatalByFile.hasOwnProperty(failure.ref)) {
fatalByFile[failure.ref] = [];
}

fatalByFile[failure.ref].push(error);
});
});

theme.results.error.forEach((error) => {
const failures = error.failures;

Expand Down Expand Up @@ -159,6 +181,11 @@ format = function format(theme, options = {}) {
byFiles: recommendationsByFile
};

theme.results.fatal = {
all: theme.results.fatal,
byFiles: fatalByFile
};

theme.results.error = {
all: theme.results.error,
byFiles: errorsByFile
Expand All @@ -168,8 +195,6 @@ format = function format(theme, options = {}) {
all: theme.results.warning,
byFiles: warningsByFile
};
} else {
theme.results.error = _.orderBy(theme.results.error, 'fatal', 'desc');
}

if (options.format) {
Expand Down
63 changes: 30 additions & 33 deletions test/general.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -408,27 +408,11 @@ describe('format', function () {
checker(themePath('005-compile/invalid')).then((theme) => {
theme = format(theme);

theme.results.error.length.should.eql(16);
theme.results.error[0].fatal.should.eql(true);
// theme.results.error[1].fatal.should.eql(true);
// theme.results.error[2].fatal.should.eql(true);
theme.results.error[3].fatal.should.eql(false);
// theme.results.error[10].fatal.should.eql(false);
theme.results.error.length.should.eql(15);
theme.results.fatal.length.should.eql(1);

done();
}).catch(done);
});

it('assert sorting', function (done) {
checker(themePath('is-empty')).then((theme) => {
theme = format(theme);

theme.results.error[0].fatal.should.eql(true);
theme.results.error[1].fatal.should.eql(true);
theme.results.error[4].fatal.should.eql(false);
theme.results.error[10].fatal.should.eql(false);
theme.results.error[11].fatal.should.eql(false);
theme.results.error[12].fatal.should.eql(false);
theme.results.error[0].fatal.should.eql(false);
theme.results.fatal[0].fatal.should.eql(true);

done();
}).catch(done);
Expand All @@ -446,13 +430,16 @@ describe('format', function () {
theme.results.warning.all.length.should.eql(3);
theme.results.warning.byFiles['default.hbs'].length.should.eql(2);

theme.results.error.all.length.should.eql(16);
theme.results.error.all.length.should.eql(15);
theme.results.fatal.all.length.should.eql(1);

// 1 rule has file references
theme.results.error.byFiles['author.hbs'].length.should.eql(1);
theme.results.error.byFiles['page.hbs'].length.should.eql(1);
theme.results.error.byFiles['post.hbs'].length.should.eql(1);
theme.results.error.byFiles['index.hbs'].length.should.eql(1);
theme.results.fatal.byFiles['author.hbs'].length.should.eql(1);
theme.results.fatal.byFiles['page.hbs'].length.should.eql(1);
theme.results.fatal.byFiles['post.hbs'].length.should.eql(1);
theme.results.fatal.byFiles['index.hbs'].length.should.eql(1);

theme.results.error.byFiles['default.hbs'].length.should.eql(1);
theme.results.error.byFiles['package.json'].length.should.eql(9);

done();
Expand All @@ -468,14 +455,24 @@ describe('format', function () {
theme.results.recommendation.all.length.should.eql(2);
theme.results.recommendation.byFiles['package.json'].length.should.eql(1);

theme.results.error.all.length.should.eql(88);
theme.results.fatal.all.length.should.eql(23);
theme.results.error.all.length.should.eql(65);
theme.results.warning.all.length.should.eql(5);

theme.results.error.byFiles['assets/my.css'].length.should.eql(3);
theme.results.error.byFiles['default.hbs'].length.should.eql(17);
theme.results.error.byFiles['post.hbs'].length.should.eql(54);
theme.results.error.byFiles['partials/mypartial.hbs'].length.should.eql(5);
theme.results.error.byFiles['index.hbs'].length.should.eql(9);

theme.results.error.byFiles['default.hbs'].length.should.eql(13);
theme.results.fatal.byFiles['default.hbs'].length.should.eql(4);

theme.results.error.byFiles['post.hbs'].length.should.eql(36);
theme.results.fatal.byFiles['post.hbs'].length.should.eql(18);

theme.results.error.byFiles['partials/mypartial.hbs'].length.should.eql(2);
theme.results.fatal.byFiles['partials/mypartial.hbs'].length.should.eql(3);

theme.results.error.byFiles['index.hbs'].length.should.eql(2);
theme.results.fatal.byFiles['index.hbs'].length.should.eql(7);

theme.results.error.byFiles['error.hbs'].length.should.eql(1);

done();
Expand All @@ -486,10 +483,10 @@ describe('format', function () {
return checker(themePath('001-deprecations/latest/invalid_all')).then((theme) => {
theme = format(theme, {format: 'cli'});

theme.results.error[0].rule.should.equal('Replace \u001b[36m{{pageUrl}}\u001b[39m with \u001b[36m{{page_url}}\u001b[39m');
theme.results.fatal[0].rule.should.equal('Replace \u001b[36m{{pageUrl}}\u001b[39m with \u001b[36m{{page_url}}\u001b[39m');

theme.results.error[0].details.should.startWith(`The helper \u001b[36m{{pageUrl}}\u001b[39m was replaced with \u001b[36m{{page_url}}\u001b[39m.\n`);
theme.results.error[0].details.should.endWith(`Find more information about the \u001b[36m{{page_url}}\u001b[39m helper here (https://themes.ghost.org/docs/page_url).`);
theme.results.fatal[0].details.should.startWith(`The helper \u001b[36m{{pageUrl}}\u001b[39m was replaced with \u001b[36m{{page_url}}\u001b[39m.\n`);
theme.results.fatal[0].details.should.endWith(`Find more information about the \u001b[36m{{page_url}}\u001b[39m helper here (https://themes.ghost.org/docs/page_url).`);
});
});
});