Skip to content

Commit

Permalink
fix: only show error messages related to configured severities
Browse files Browse the repository at this point in the history
  • Loading branch information
theoludwig committed Aug 9, 2023
1 parent 284b9fb commit e3cd809
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/HTMLValidatorCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,13 @@ export class HTMLValidatorCommand extends Command {
results.push({ data, isSuccess: false })
const messagesTable: string[][] = []
for (const message of result.messages) {
if (
!severities.includes(message.type as Severity) &&
!severities.includes(message.subType as Severity)
) {
continue
}

const row: string[] = []
if (message.type === 'info') {
if (message.subType === 'warning') {
Expand Down
39 changes: 39 additions & 0 deletions src/__test__/HTMLValidatorCommand.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -374,4 +374,43 @@ await test('html-w3c-validator', async (t) => {
errors.join('\n')
)
})

await t.test('fails with invalid W3C HTML', async () => {
const workingDirectory = path.join(FIXTURES_PATH, 'error-invalid-w3c-html')
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.yellow('warning'),
'Consider adding a “lang” attribute to the “html” start tag to declare the language of this document.',
'line: 2, column: 16-6'
]
]
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')
)
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"files": ["./build/index.html", "./build/about.html"]
}
9 changes: 9 additions & 0 deletions src/__test__/fixtures/error-invalid-w3c-html/build/about.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>About</title>
</head>
<body></body>
</html>
9 changes: 9 additions & 0 deletions src/__test__/fixtures/error-invalid-w3c-html/build/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<!doctype html>
<html>
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Home</title>
</head>
<body></body>
</html>

0 comments on commit e3cd809

Please sign in to comment.