Skip to content

Commit

Permalink
update test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
fulldecent committed Aug 30, 2024
1 parent e02f3da commit b64c290
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 106 deletions.
1 change: 0 additions & 1 deletion test/build-html-validate.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ var allTestsPassed = true;
const validateTargets = async () => {
for (const target of targets) {
try {
console.log('🔍 ' + target);
const report = await htmlValidate.validateFile(target);
if (!report.valid) {
console.log(formatter(report.results));
Expand Down
238 changes: 134 additions & 104 deletions test/fixtures-html-validate-should-fail.mjs
Original file line number Diff line number Diff line change
@@ -1,151 +1,181 @@
// Documentation: https://html-validate.org/dev/using-api.html

import { HtmlValidate } from "html-validate";
import plugin from "./plugin.html-validate.mjs";
import { HtmlValidate } from 'html-validate';
import plugin from './plugin.html-validate.mjs';
// We prefer to use FileSystemConfigLoader, see https://gitlab.com/html-validate/html-validate/-/issues/230#note_1670756378
const htmlValidate = new HtmlValidate({
extends: ["html-validate:recommended"],
extends: ['html-validate:recommended'],
plugins: [plugin],
rules: {
"mailto-awesome": "error",
"external-links": "error",
"internal-links": "error",
"https-links": "error",
"no-jquery": "error",
"canonical-link": "error",
"latest-packages": "error",
'mailto-awesome': 'error',
'external-links': 'error',
'internal-links': 'error',
'https-links': 'error',
'no-jquery': 'error',
'canonical-link': 'error',
'latest-packages': 'error',
},
});
let allTestsPassed = true;

const specifications = [
{
filePath: "test/fixtures/mailto-not-awesome.html",
filePath: 'test/fixtures/mailto-not-awesome.html',
messages: [
{
"ruleId": "mailto-awesome",
"severity": 2,
"message": "mailto link must have a subject and body",
"offset": 196,
"line": 9,
"column": 6,
"size": 1,
"selector": "html > body > a",
"ruleUrl": "https://github.com/fulldecent/github-pages-template/#mailto-awesome"
}
ruleId: 'mailto-awesome',
severity: 2,
message: 'mailto link must have a subject and body',
offset: 196,
line: 9,
column: 6,
size: 1,
selector: 'html > body > a',
ruleUrl: 'https://github.com/fulldecent/github-pages-template/#mailto-awesome',
},
],
}, {
filePath: "test/fixtures/external-link-broken.html",
},
{
filePath: 'test/fixtures/external-link-broken.html',
messages: [
{
"ruleId": "external-links",
"severity": 2,
"message": "external link is broken: https://freehorses.example.com/free-horses-on-1998-04-01-only.html",
"offset": 196,
"line": 9,
"column": 6,
"size": 1,
"selector": "html > body > a:nth-child(1)",
"ruleUrl": "https://github.com/fulldecent/github-pages-template/#external-links"
}
ruleId: 'external-links',
severity: 2,
message: 'external link is broken: https://freehorses.example.com/free-horses-on-1998-04-01-only.html',
offset: 196,
line: 9,
column: 6,
size: 1,
selector: 'html > body > a:nth-child(1)',
ruleUrl: 'https://github.com/fulldecent/github-pages-template/#external-links',
},
],
}, {
filePath: "test/fixtures/internal-link-broken.html",
},
{
filePath: 'test/fixtures/internal-link-broken.html',
messages: [
{
"ruleId": "internal-links",
"severity": 2,
"message": "Internal link /free-horses-on-1998-04-01-only.html is broken in file test/fixtures/internal-link-broken.html at line 9, column 6",
"offset": 196,
"line": 9,
"column": 6,
"size": 1,
"selector": "html > body > a",
"ruleUrl": "https://github.com/fulldecent/github-pages-template/#internal-links"
}
ruleId: 'internal-links',
severity: 2,
message:
'Internal link /free-horses-on-1998-04-01-only.html is broken in file test/fixtures/internal-link-broken.html at line 9, column 6',
offset: 196,
line: 9,
column: 6,
size: 1,
selector: 'html > body > a',
ruleUrl: 'https://github.com/fulldecent/github-pages-template/#internal-links',
},
],
}, {
filePath: "test/fixtures/ensure-https.html",
},
{
filePath: 'test/fixtures/ensure-https.html',
messages: [
{
"ruleId": "https-links",
"severity": 2,
"message": "external link is insecure and accessible via HTTPS: http://en.wikipedia.org/wiki/Horse",
"offset": 196,
"line": 9,
"column": 6,
"size": 1,
"selector": "html > body > a",
"ruleUrl": "https://github.com/fulldecent/github-pages-template/#https-links"
}
ruleId: 'external-links',
severity: 2,
message: 'external link http://en.wikipedia.org/wiki/Horse redirects to: https://en.wikipedia.org/wiki/Horse',
offset: 196,
line: 9,
column: 6,
size: 1,
selector: 'html > body > a',
ruleUrl: 'https://github.com/fulldecent/github-pages-template/#external-links',
},
{
ruleId: 'https-links',
severity: 2,
message: 'external link is insecure and accessible via HTTPS: http://en.wikipedia.org/wiki/Horse',
offset: 196,
line: 9,
column: 6,
size: 1,
selector: 'html > body > a',
ruleUrl: 'https://github.com/fulldecent/github-pages-template/#https-links',
},
],
}, {
"filePath": "test/fixtures/using-jquery.html",
"messages": [
},
{
filePath: 'test/fixtures/using-jquery.html',
messages: [
{
"ruleId": "no-jquery",
"severity": 2,
"message": "script tag with src including jQuery",
"offset": 123,
"line": 6,
"column": 6,
"size": 6,
"selector": "html > head > script",
"ruleUrl": "https://github.com/fulldecent/github-pages-template/#no-jquery"
}
ruleId: 'no-jquery',
severity: 2,
message: 'script tag with src including jQuery',
offset: 123,
line: 6,
column: 6,
size: 6,
selector: 'html > head > script',
ruleUrl: 'https://github.com/fulldecent/github-pages-template/#no-jquery',
},
],
}, {
"filePath": "test/fixtures/canonical-link-missing.html",
"messages": [
{
"ruleId": "canonical-link",
"severity": 2,
"message": "<head> is missing <link rel=\"canonical\" ...>",
"size": 0,
"selector": null,
"ruleUrl": "https://github.com/fulldecent/github-pages-template/#canonical"
}
]
}, {
"filePath": "test/fixtures/old-package.html",
"messages": [
},
{
filePath: 'test/fixtures/canonical-link-missing.html',
messages: [
{
ruleId: 'canonical-link',
severity: 2,
message: '<head> is missing <link rel="canonical" ...>',
size: 0,
selector: null,
ruleUrl: 'https://github.com/fulldecent/github-pages-template/#canonical',
},
],
},
{
filePath: 'test/fixtures/old-package.html',
messages: [
{
"ruleId": "latest-packages",
"severity": 2,
"message": "using outdated package version https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js",
"offset": 123,
"line": 6,
"column": 6,
"size": 6,
"selector": "html > head > script"
}
]
}
ruleId: 'latest-packages',
severity: 2,
message: 'using outdated package version https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js',
offset: 123,
line: 6,
column: 6,
size: 6,
selector: 'html > head > script',
},
],
},
];

// async for all tests
const tests = specifications.map(async ({ filePath, messages }) => {
const report = await htmlValidate.validateFile(filePath);
const expectedString = JSON.stringify(messages, null, 2);
const actualString = report.results.length === 0
? "[]"
: JSON.stringify(report.results[0].messages, null, 2);
const actualString = report.results.length === 0 ? '[]' : JSON.stringify(report.results[0].messages, null, 2);

if (expectedString === actualString) {
console.log(`✅ ${filePath}`);
} else {
console.error(`❌ ${filePath} did not produce expected result.`);
console.error(`\x1b[31m` + expectedString.split("\n").map(line => `- ${line}`).join("\n") + `\x1b[0m`);
console.error(`\x1b[32m` + actualString.split("\n").map(line => `+ ${line}`).join("\n") + `\x1b[0m`);
console.error(
`\x1b[31m` +
expectedString
.split('\n')
.map((line) => `- ${line}`)
.join('\n') +
`\x1b[0m`
);
console.error(
`\x1b[32m` +
actualString
.split('\n')
.map((line) => `+ ${line}`)
.join('\n') +
`\x1b[0m`
);
allTestsPassed = false;
}
});

Promise.all(tests).then(() => {
if (allTestsPassed) {
console.log("Test of all fixtures produced expected results.\n");
console.log('Test of all fixtures produced expected results.\n');
} else {
console.error("❌ Tests of fixtures did not produce expected results.");
console.error('❌ Tests of fixtures did not produce expected results.');
process.exit(1);
}
});
});
2 changes: 1 addition & 1 deletion test/plugin.html-validate.external-links.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ export default class ExternalLinksRule extends Rule {
const errorOutput = error.stderr ? error.stderr.toString() : error.message;
this.report({
node: element,
message: `Failed to check external link ${url}: ${errorOutput}`,
message: `external link is broken: ${url}: ${errorOutput}`,
});
this.db.prepare('REPLACE INTO urls (url, status, redirect_to, time) VALUES (?, -1, NULL, unixepoch())').run(url);
}
Expand Down

0 comments on commit b64c290

Please sign in to comment.