From b64c2900b953026eec3a4ee37f6f68ad9efa78b8 Mon Sep 17 00:00:00 2001 From: William Entriken Date: Fri, 30 Aug 2024 00:38:42 -0400 Subject: [PATCH] update test cases --- test/build-html-validate.mjs | 1 - test/fixtures-html-validate-should-fail.mjs | 238 +++++++++++-------- test/plugin.html-validate.external-links.mjs | 2 +- 3 files changed, 135 insertions(+), 106 deletions(-) diff --git a/test/build-html-validate.mjs b/test/build-html-validate.mjs index d8c52da..c9a17e9 100644 --- a/test/build-html-validate.mjs +++ b/test/build-html-validate.mjs @@ -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)); diff --git a/test/fixtures-html-validate-should-fail.mjs b/test/fixtures-html-validate-should-fail.mjs index 2464a77..4e552c2 100644 --- a/test/fixtures-html-validate-should-fail.mjs +++ b/test/fixtures-html-validate-should-fail.mjs @@ -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": " is missing ", - "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: ' is missing ', + 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/bootstrap@5.3.1/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/bootstrap@5.3.1/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); } -}); \ No newline at end of file +}); diff --git a/test/plugin.html-validate.external-links.mjs b/test/plugin.html-validate.external-links.mjs index 13549e7..8efa6f9 100644 --- a/test/plugin.html-validate.external-links.mjs +++ b/test/plugin.html-validate.external-links.mjs @@ -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); }