diff --git a/src/HTMLValidatorCommand.ts b/src/HTMLValidatorCommand.ts index 1926869..90ac418 100644 --- a/src/HTMLValidatorCommand.ts +++ b/src/HTMLValidatorCommand.ts @@ -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) } diff --git a/src/__test__/HTMLValidatorCommand.test.ts b/src/__test__/HTMLValidatorCommand.test.ts index 12cdc3c..2b0f7b4 100644 --- a/src/__test__/HTMLValidatorCommand.test.ts +++ b/src/__test__/HTMLValidatorCommand.test.ts @@ -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) diff --git a/src/__test__/fixtures/issue-6/.html-w3c-validatorrc.json b/src/__test__/fixtures/issue-6/.html-w3c-validatorrc.json new file mode 100644 index 0000000..7610224 --- /dev/null +++ b/src/__test__/fixtures/issue-6/.html-w3c-validatorrc.json @@ -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"] +} diff --git a/src/__test__/fixtures/issue-6/build/index.html b/src/__test__/fixtures/issue-6/build/index.html new file mode 100644 index 0000000..af83dfd --- /dev/null +++ b/src/__test__/fixtures/issue-6/build/index.html @@ -0,0 +1,12 @@ + +
No. +
This is unacceptable. +
We don’t need arms and armies and industrial slaughterhouses. +
We need respect for life. +
Everyone can do something. Speaking up is doing something. diff --git a/src/validateHTML.ts b/src/validateHTML.ts index e169c99..e5c9dbf 100644 --- a/src/validateHTML.ts +++ b/src/validateHTML.ts @@ -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