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:
parent
e53c80d578
commit
cc6a1278a1
@ -184,10 +184,17 @@ export class HTMLValidatorCommand extends Command {
|
|||||||
row.push(chalk.red(message.type))
|
row.push(chalk.red(message.type))
|
||||||
}
|
}
|
||||||
row.push(message.message)
|
row.push(message.message)
|
||||||
if (message.extract != null) {
|
if (
|
||||||
|
message.extract != null &&
|
||||||
|
message.lastLine != null &&
|
||||||
|
message.firstColumn != null &&
|
||||||
|
message.lastColumn != null
|
||||||
|
) {
|
||||||
row.push(
|
row.push(
|
||||||
`line: ${message.lastLine}, column: ${message.firstColumn}-${message.lastColumn}`,
|
`line: ${message.lastLine}, column: ${message.firstColumn}-${message.lastColumn}`,
|
||||||
)
|
)
|
||||||
|
} else {
|
||||||
|
row.push("")
|
||||||
}
|
}
|
||||||
messagesTable.push(row)
|
messagesTable.push(row)
|
||||||
}
|
}
|
||||||
|
@ -52,8 +52,8 @@ await test("html-w3c-validator", async (t) => {
|
|||||||
"succeeds and validate the html correctly (example without working directory)",
|
"succeeds and validate the html correctly (example without working directory)",
|
||||||
async () => {
|
async () => {
|
||||||
const logs: string[] = []
|
const logs: string[] = []
|
||||||
sinon.stub(console, "log").value((log: string) => {
|
sinon.stub(console, "log").value((...log: string[]) => {
|
||||||
logs.push(log)
|
logs.push(...log)
|
||||||
})
|
})
|
||||||
const consoleLogSpy = sinon.spy(console, "log")
|
const consoleLogSpy = sinon.spy(console, "log")
|
||||||
const stream = new PassThrough()
|
const stream = new PassThrough()
|
||||||
@ -63,7 +63,7 @@ await test("html-w3c-validator", async (t) => {
|
|||||||
stderr: stream,
|
stderr: stream,
|
||||||
})
|
})
|
||||||
stream.end()
|
stream.end()
|
||||||
assert.strictEqual(exitCode, 0)
|
assert.strictEqual(exitCode, 0, logs.join("\n"))
|
||||||
assert.strictEqual(
|
assert.strictEqual(
|
||||||
consoleLogSpy.calledWith(
|
consoleLogSpy.calledWith(
|
||||||
logSymbols.success,
|
logSymbols.success,
|
||||||
@ -88,8 +88,8 @@ await test("html-w3c-validator", async (t) => {
|
|||||||
async () => {
|
async () => {
|
||||||
const workingDirectory = path.join(FIXTURES_PATH, "success")
|
const workingDirectory = path.join(FIXTURES_PATH, "success")
|
||||||
const logs: string[] = []
|
const logs: string[] = []
|
||||||
sinon.stub(console, "log").value((log: string) => {
|
sinon.stub(console, "log").value((...log: string[]) => {
|
||||||
logs.push(log)
|
logs.push(...log)
|
||||||
})
|
})
|
||||||
const consoleLogSpy = sinon.spy(console, "log")
|
const consoleLogSpy = sinon.spy(console, "log")
|
||||||
const stream = new PassThrough()
|
const stream = new PassThrough()
|
||||||
@ -102,7 +102,7 @@ await test("html-w3c-validator", async (t) => {
|
|||||||
},
|
},
|
||||||
)
|
)
|
||||||
stream.end()
|
stream.end()
|
||||||
assert.strictEqual(exitCode, 0)
|
assert.strictEqual(exitCode, 0, logs.join("\n"))
|
||||||
assert.strictEqual(
|
assert.strictEqual(
|
||||||
consoleLogSpy.calledWith(logSymbols.success, "./build/index.html"),
|
consoleLogSpy.calledWith(logSymbols.success, "./build/index.html"),
|
||||||
true,
|
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 () => {
|
await t.test("fails with not found config", async () => {
|
||||||
const workingDirectory = path.join(FIXTURES_PATH, "error-config-not-found")
|
const workingDirectory = path.join(FIXTURES_PATH, "error-config-not-found")
|
||||||
const configPath = path.join(workingDirectory, CONFIG_FILE_NAME)
|
const configPath = path.join(workingDirectory, CONFIG_FILE_NAME)
|
||||||
|
5
src/__test__/fixtures/issue-6/.html-w3c-validatorrc.json
Normal file
5
src/__test__/fixtures/issue-6/.html-w3c-validatorrc.json
Normal 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"]
|
||||||
|
}
|
12
src/__test__/fixtures/issue-6/build/index.html
Normal file
12
src/__test__/fixtures/issue-6/build/index.html
Normal 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 don’t 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>
|
@ -7,9 +7,9 @@ export interface ValidationMessage {
|
|||||||
subType?: "warning" | "fatal" | "internal" | "io" | "schema"
|
subType?: "warning" | "fatal" | "internal" | "io" | "schema"
|
||||||
message: string
|
message: string
|
||||||
extract?: string
|
extract?: string
|
||||||
lastLine: number
|
lastLine?: number
|
||||||
firstColumn: number
|
firstColumn?: number
|
||||||
lastColumn: number
|
lastColumn?: number
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface ValidateHTMLResult {
|
export interface ValidateHTMLResult {
|
||||||
@ -30,7 +30,7 @@ export const validateHTML = async (
|
|||||||
},
|
},
|
||||||
})
|
})
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
throw new Error(`Failed to validate HTML`)
|
throw new Error(response.statusText)
|
||||||
}
|
}
|
||||||
const result = (await response.json()) as ValidateHTMLResult
|
const result = (await response.json()) as ValidateHTMLResult
|
||||||
return result
|
return result
|
||||||
|
Loading…
Reference in New Issue
Block a user