Skip to content

Commit

Permalink
test: Use chalk for output styling (#4015)
Browse files Browse the repository at this point in the history
  • Loading branch information
ExE-Boss authored and Elchi3 committed May 6, 2019
1 parent 8e14583 commit f0d3e7e
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 78 deletions.
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"devDependencies": {
"ajv": "^6.10.0",
"better-ajv-errors": "^0.6.4",
"chalk": "^2.4.2",
"compare-versions": "^3.4.0",
"mdn-confluence": "^1.0.3",
"ora": "^3.4.0",
Expand Down
3 changes: 2 additions & 1 deletion scripts/release-notes.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const { execSync } = require('child_process');
const http = require('https');
const readline = require('readline');
const chalk = require('chalk');

const bcd = require('..');

Expand Down Expand Up @@ -137,7 +138,7 @@ const main = async () => {
- ${totalContributors} total contributors
- ${stars} total stargazers`;

console.log('\n\x1b[1mOpen this URL in a browser:\x1b[0m');
console.log(chalk.bold('\nOpen this URL in a browser:'));
console.log(makeURL(version, body));
};

Expand Down
9 changes: 5 additions & 4 deletions test/test-browsers.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';
const path = require('path');
const chalk = require('chalk');

/**
* @typedef {import('../types').Identifier} Identifier
Expand Down Expand Up @@ -98,7 +99,7 @@ function testBrowsers(filename) {
const data = require(filename);

if (!category) {
console.warn('\x1b[1;30m Browsers – Unknown category \x1b[0m');
console.warn(chalk.blackBright(' Browsers – Unknown category'));
return false;
}

Expand Down Expand Up @@ -129,11 +130,11 @@ function testBrowsers(filename) {
if (!processData(data, displayBrowsers, requiredBrowsers, category, logger)) {
return false;
} else {
console.error(
`\x1b[ Browsers – ${errors.length} ${
console.error(chalk.red(
` Browsers – ${errors.length} ${
errors.length === 1 ? 'error' : 'errors'
}:`,
);
));
for (const error of errors) {
console.error(` ${error}`);
}
Expand Down
11 changes: 7 additions & 4 deletions test/test-prefix.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
'use strict';
const path = require('path');
const chalk = require('chalk');

function checkPrefix(data, category, errors, prefix, path="") {
for (var key in data) {
if (key === "prefix" && typeof(data[key]) === "string") {
if (data[key].includes(prefix)) {
var error = `\x1b[31m${prefix} prefix is wrong for key: ${path}\x1b[0m`;
var error = chalk.red(`${prefix} prefix is wrong for key: ${path}`);
var rules = [
category == "api" && !data[key].startsWith(prefix),
category == "css" && !data[key].startsWith(`-${prefix}`)
Expand Down Expand Up @@ -48,9 +49,11 @@ function testPrefix(filename) {
var errors = processData(data, category);

if (errors.length) {
console.error(`\x1b[31m Prefix – ${errors.length} ${
errors.length === 1 ? 'error' : 'errors'
}:`);
console.error(chalk.red(
` Prefix – ${errors.length} ${
errors.length === 1 ? 'error' : 'errors'
}:`
));
for (const error of errors) {
console.error(` ${error}`);
}
Expand Down
11 changes: 6 additions & 5 deletions test/test-schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
const Ajv = require('ajv');
const betterAjvErrors = require('better-ajv-errors');
const path = require('path');
const chalk = require('chalk');

const ajv = new Ajv({ jsonPointers: true, allErrors: true });

Expand All @@ -18,12 +19,12 @@ function testSchema(dataFilename, schemaFilename = './../schemas/compat-data.sch
if (valid) {
return false;
} else {
console.error(`\x1b[31m File : ${path.relative(process.cwd(), dataFilename)}`);
console.error(
`\x1b[31m JSON schema – ${ajv.errors.length} ${
console.error(chalk.red(` File : ${path.relative(process.cwd(), dataFilename)}`));
console.error(chalk.red(
` JSON schema – ${ajv.errors.length} ${
ajv.errors.length === 1 ? 'error' : 'errors'
}:\x1b[0m`,
);
}:`,
));
// Output messages by one since better-ajv-errors wrongly joins messages
// (see https://github.com/atlassian/better-ajv-errors/pull/21)
ajv.errors.forEach(e => {
Expand Down
89 changes: 45 additions & 44 deletions test/test-style.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
const fs = require('fs');
const path = require('path');
const url = require('url');
const chalk = require('chalk');
const { platform } = require('os');

/** Determines if the OS is Windows */
Expand Down Expand Up @@ -117,9 +118,9 @@ function jsonDiff(actual, expected) {

for (var i = 0; i < actualLines.length; i++) {
if (actualLines[i] !== expectedLines[i]) {
return `#${i + 1}\x1b[0m
return chalk`#${i + 1}{reset
Actual: ${escapeInvisibles(actualLines[i])}
Expected: ${escapeInvisibles(expectedLines[i])}`;
Expected: ${escapeInvisibles(expectedLines[i])}}`;
}
}
}
Expand All @@ -144,31 +145,31 @@ function testStyle(filename) {

if (actual !== expected) {
hasErrors = true;
console.error(`\x1b[31m File : ${path.relative(process.cwd(), filename)}`);
console.error(`\x1b[31m Style – Error on line ${jsonDiff(actual, expected)}`);
console.error(chalk.red(` File : ${path.relative(process.cwd(), filename)}`));
console.error(chalk.red(` Style – Error on line ${jsonDiff(actual, expected)}`));
}

if (expected !== expectedSorting) {
hasErrors = true;
console.error(`\x1b[31m File : ${path.relative(process.cwd(), filename)}`);
console.error(`\x1b[31m Browser name sorting – Error on line ${jsonDiff(
console.error(chalk.red(` File : ${path.relative(process.cwd(), filename)}`));
console.error(chalk.red(` Browser name sorting – Error on line ${jsonDiff(
expected,
expectedSorting,
)}`);
)}`));
}

const bugzillaMatch = actual.match(String.raw`https?://bugzilla\.mozilla\.org/show_bug\.cgi\?id=(\d+)`);
if (bugzillaMatch) {
// use https://bugzil.la/1000000 instead
hasErrors = true;
console.error(
`\x1b[33m Style ${indexToPos(
console.error(chalk.yellow(
` Style ${indexToPos(
actual,
bugzillaMatch.index,
)} – Use shortenable URL (${
bugzillaMatch[0]
} → https://bugzil.la/${bugzillaMatch[1]}).\x1b[0m`,
);
} → https://bugzil.la/${bugzillaMatch[1]}).`,
));
}

{
Expand All @@ -189,10 +190,10 @@ function testStyle(filename) {

if (protocol !== 'https') {
hasErrors = true;
console.error(`\x1b[33m Style ${indexToPos(
console.error(chalk.yellow(` Style ${indexToPos(
actual,
match.index,
)} – Use HTTPS URL (http://${domain}/${bugId} → https://${domain}/${bugId}).\x1b[0m`);
)} – Use HTTPS URL (http://${domain}/${bugId} → https://${domain}/${bugId}).`));
}

if (domain !== 'bugzil.la') {
Expand All @@ -201,24 +202,24 @@ function testStyle(filename) {

if (/^bug $/.test(before)) {
hasErrors = true;
console.error(`\x1b[33m Style ${indexToPos(
console.error(chalk.yellow(` Style ${indexToPos(
actual,
match.index,
)} – Move word "bug" into link text ("${before}<a href='...'>${linkText}</a>" → "<a href='...'>${before}${bugId}</a>").\x1b[0m`);
)} – Move word "bug" into link text ("${before}<a href='...'>${linkText}</a>" → "<a href='...'>${before}${bugId}</a>").`));
} else if (linkText === `Bug ${bugId}`) {
if (!/(\. |")$/.test(before)) {
hasErrors = true;
console.error(`\x1b[33m Style ${indexToPos(
console.error(chalk.yellow(` Style ${indexToPos(
actual,
match.index,
)} – Use lowercase "bug" word within sentence ("Bug ${bugId}" → "bug ${bugId}").\x1b[0m`);
)} – Use lowercase "bug" word within sentence ("Bug ${bugId}" → "bug ${bugId}").`));
}
} else if (linkText !== `bug ${bugId}`) {
hasErrors = true;
console.error(`\x1b[33m Style ${indexToPos(
console.error(chalk.yellow(` Style ${indexToPos(
actual,
match.index,
)} – Use standard link text ("${linkText}" → "bug ${bugId}").\x1b[0m`);
)} – Use standard link text ("${linkText}" → "bug ${bugId}").`));
}
}
} while (match != null);
Expand All @@ -228,72 +229,72 @@ function testStyle(filename) {
if (crbugMatch) {
// use https://crbug.com/100000 instead
hasErrors = true;
console.error(
`\x1b[33m Style ${indexToPos(
console.error(chalk.yellow(
` Style ${indexToPos(
actual,
crbugMatch.index,
)} – Use shortenable URL (${
crbugMatch[0]
} → https://crbug.com/${crbugMatch[1]}).\x1b[0m`,
);
} → https://crbug.com/${crbugMatch[1]}).`,
));
}

const webkitMatch = actual.match(String.raw`https?://bugs\.webkit\.org/show_bug\.cgi\?id=(\d+)`);
if (webkitMatch) {
// use https://webkit.org/b/100000 instead
hasErrors = true;
console.error(
`\x1b[33m Style ${indexToPos(
console.error(chalk.yellow(
` Style ${indexToPos(
actual,
webkitMatch.index,
)} – Use shortenable URL (${
webkitMatch[0]
} → https://webkit.org/b/${webkitMatch[1]}).\x1b[0m`,
);
} → https://webkit.org/b/${webkitMatch[1]}).`,
));
}

const mdnUrlMatch = actual.match(String.raw`https?://developer.mozilla.org/(\w\w-\w\w)/(.*?)(?=["'\s])`);
if (mdnUrlMatch) {
hasErrors = true;
console.error(
`\x1b[33m Style ${indexToPos(
console.error(chalk.yellow(
` Style ${indexToPos(
actual,
mdnUrlMatch.index,
)} – Use non-localized MDN URL (${
mdnUrlMatch[0]
} → https://developer.mozilla.org/${mdnUrlMatch[2]}).\x1b[0m`,
);
} → https://developer.mozilla.org/${mdnUrlMatch[2]}).`,
));
}

const msdevUrlMatch = actual.match(String.raw`https?://developer.microsoft.com/(\w\w-\w\w)/(.*?)(?=["'\s])`);
if (msdevUrlMatch) {
hasErrors = true;
console.error(
`\x1b[33m Style ${indexToPos(
console.error(chalk.yellow(
` Style ${indexToPos(
actual,
msdevUrlMatch.index,
)} – Use non-localized Microsoft Developer URL (${
msdevUrlMatch[0]
} → https://developer.microsoft.com${msdevUrlMatch[2]}).\x1b[0m`,
);
} → https://developer.microsoft.com${msdevUrlMatch[2]}).`,
));
}

let constructorMatch = actual.match(String.raw`"<code>([^)]*?)</code> constructor"`)
if (constructorMatch) {
hasErrors = true;
console.error(
`\x1b[33m Style ${indexToPos(
console.error(chalk.yellow(
` Style ${indexToPos(
actual,
constructorMatch.index,
)} – Use parentheses in constructor description: ${
constructorMatch[1]
}${constructorMatch[1]}()\x1b[0m`,
);
}${constructorMatch[1]}()`,
));
}

if (actual.includes("href=\\\"")) {
hasErrors = true;
console.error('\x1b[33m Style – Found \\" but expected \' for <a href>.\x1b[0m');
console.error(chalk.yellow(' Style – Found \\" but expected \' for <a href>.'));
}

const regexp = new RegExp(String.raw`<a href='([^'>]+)'>((?:.(?!</a>))*.)</a>`, 'g');
Expand All @@ -302,14 +303,14 @@ function testStyle(filename) {
var a_url = url.parse(match[1]);
if (a_url.hostname === null) {
hasErrors = true;
console.error(
`\x1b[33m Style ${indexToPos(
console.error(chalk.yellow(
` Style ${indexToPos(
actual,
match.index,
)} – Include hostname in URL: ${
match[1]
} → https://developer.mozilla.org/${match[1]}\x1b[0m`,
);
} → https://developer.mozilla.org/${match[1]}`,
));
}
}

Expand Down
Loading

0 comments on commit f0d3e7e

Please sign in to comment.