1
1
mirror of https://github.com/theoludwig/html-w3c-validator.git synced 2024-11-09 22:08:12 +01:00

fix: print correctly validation results

There was an error thrown by `table` library to print validation results
when there were 2 (or more) errors, with one error with line/column
and the other error without specified line/column.

Fixes #6
This commit is contained in:
Théo LUDWIG 2024-11-09 15:45:31 +01:00
parent e53c80d578
commit cc6a1278a1
Signed by: theoludwig
GPG Key ID: ADFE5A563D718F3B
5 changed files with 82 additions and 11 deletions

View File

@ -184,10 +184,17 @@ export class HTMLValidatorCommand extends Command {
row.push(chalk.red(message.type))
}
row.push(message.message)
if (message.extract != null) {
if (
message.extract != null &&
message.lastLine != null &&
message.firstColumn != null &&
message.lastColumn != null
) {
row.push(
`line: ${message.lastLine}, column: ${message.firstColumn}-${message.lastColumn}`,
)
} else {
row.push("")
}
messagesTable.push(row)
}

View File

@ -52,8 +52,8 @@ await test("html-w3c-validator", async (t) => {
"succeeds and validate the html correctly (example without working directory)",
async () => {
const logs: string[] = []
sinon.stub(console, "log").value((log: string) => {
logs.push(log)
sinon.stub(console, "log").value((...log: string[]) => {
logs.push(...log)
})
const consoleLogSpy = sinon.spy(console, "log")
const stream = new PassThrough()
@ -63,7 +63,7 @@ await test("html-w3c-validator", async (t) => {
stderr: stream,
})
stream.end()
assert.strictEqual(exitCode, 0)
assert.strictEqual(exitCode, 0, logs.join("\n"))
assert.strictEqual(
consoleLogSpy.calledWith(
logSymbols.success,
@ -88,8 +88,8 @@ await test("html-w3c-validator", async (t) => {
async () => {
const workingDirectory = path.join(FIXTURES_PATH, "success")
const logs: string[] = []
sinon.stub(console, "log").value((log: string) => {
logs.push(log)
sinon.stub(console, "log").value((...log: string[]) => {
logs.push(...log)
})
const consoleLogSpy = sinon.spy(console, "log")
const stream = new PassThrough()
@ -102,7 +102,7 @@ await test("html-w3c-validator", async (t) => {
},
)
stream.end()
assert.strictEqual(exitCode, 0)
assert.strictEqual(exitCode, 0, logs.join("\n"))
assert.strictEqual(
consoleLogSpy.calledWith(logSymbols.success, "./build/index.html"),
true,
@ -116,6 +116,53 @@ await test("html-w3c-validator", async (t) => {
},
)
await t.test(
"fails by validating the html correctly with 2 errors: one with line/column, the other without (GitHub issue #6)",
async () => {
const workingDirectory = path.join(FIXTURES_PATH, "issue-6")
const errors: string[] = []
sinon.stub(console, "error").value((error: string) => {
errors.push(error)
})
const consoleErrorSpy = sinon.spy(console, "error")
const stream = new PassThrough()
const exitCode = await cli.run(
[`--current-working-directory=${workingDirectory}`],
{
stdin: process.stdin,
stdout: stream,
stderr: stream,
},
)
stream.end()
assert.strictEqual(exitCode, 1)
const messagesTable = [
[
chalk.red("error"),
"The character encoding was not declared. Proceeding using “windows-1252”.",
"",
],
[
chalk.yellow("warning"),
"Consider adding a “lang” attribute to the “html” start tag to declare the language of this document.",
"line: 2, column: 16-7",
],
]
assert.strictEqual(
consoleErrorSpy.calledWith(
chalk.bold.red("Error:") + " HTML validation (W3C) failed!",
),
true,
errors.join("\n"),
)
assert.strictEqual(
consoleErrorSpy.calledWith(table(messagesTable)),
true,
errors.join("\n"),
)
},
)
await t.test("fails with not found config", async () => {
const workingDirectory = path.join(FIXTURES_PATH, "error-config-not-found")
const configPath = path.join(workingDirectory, CONFIG_FILE_NAME)

View File

@ -0,0 +1,5 @@
{
"$schema": "https://raw.githubusercontent.com/theoludwig/html-w3c-validator/master/schema/html-w3c-validatorrc-schema.json",
"files": ["./build/index.html"],
"severities": ["info", "warning", "error"]
}

View File

@ -0,0 +1,12 @@
<!DOCTYPE html>
<title>Have We Stopped Killing Yet?</title>
<link rel=icon href=favicon.ico>
<link rel=stylesheet href=setup/default.css>
<meta name=viewport content="initial-scale=1,width=device-width">
<meta property="og:image" content="https://mirrors.meiert.org/havewestoppedkillingyet.org/media/redrum.png">
<h1>Have We Stopped Killing Yet?</h1>
<p><strong>No.</strong>
<p>This is <a href=https://meiert.com/en/blog/on-taking-life/>unacceptable</a>.
<p>We dont need arms and armies and industrial slaughterhouses.
<p>We need respect for life.
<p>Everyone can <em>do something</em>. Speaking up is doing something. <a href=https://meiert.com/en/><img src=https://hell.meiert.org/core/png/meiert-logo-80x80-alt.png alt="Jens Oliver Meiert"></a>

View File

@ -7,9 +7,9 @@ export interface ValidationMessage {
subType?: "warning" | "fatal" | "internal" | "io" | "schema"
message: string
extract?: string
lastLine: number
firstColumn: number
lastColumn: number
lastLine?: number
firstColumn?: number
lastColumn?: number
}
export interface ValidateHTMLResult {
@ -30,7 +30,7 @@ export const validateHTML = async (
},
})
if (!response.ok) {
throw new Error(`Failed to validate HTML`)
throw new Error(response.statusText)
}
const result = (await response.json()) as ValidateHTMLResult
return result