Skip to content

Commit b855ea2

Browse files
authored
fix: htmlhint sarif format now outputs a file (#1638)
1 parent c2164aa commit b855ea2

File tree

12 files changed

+80
-17
lines changed

12 files changed

+80
-17
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,4 @@ test/e2e/reports
6565

6666
# HTML formatter output
6767
report.html
68+
htmlhint.sarif

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
"build:rollup": "npm run build:min && npm run build:unmin",
3838
"build:unmin": "rollup -c --bundleConfigAsCjs",
3939
"clean": "rimraf dist",
40+
"full": "npm run build && npm run test && npm run lint",
4041
"lint": "eslint . --max-warnings 0 && prettier -c .",
4142
"lint:fix": "eslint . --fix && npm run prettier",
4243
"lint:markdown": "npx markdownlint-cli **/*.md",

src/cli/formatters/sarif.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { writeFileSync } from 'fs'
12
import { FormatterCallback } from '../formatter'
23
import {
34
SarifBuilder,
@@ -69,7 +70,9 @@ const sarifFormatter: FormatterCallback = function (formatter) {
6970
})
7071

7172
sarifBuilder.addRun(sarifRunBuilder)
72-
console.log(sarifBuilder.buildSarifJsonString({ indent: true }))
73+
const sarifContent = sarifBuilder.buildSarifJsonString({ indent: true })
74+
console.log(sarifContent)
75+
writeFileSync('htmlhint.sarif', sarifContent)
7376
})
7477
}
7578

test/cli/formatters/checkstyle.spec.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@ describe('CLI', () => {
2020
'checkstyle',
2121
].join(' '),
2222
(error, stdout, stderr) => {
23-
expect(typeof error).toBe('object')
24-
expect(error.code).toBe(1)
23+
// HTMLHint should exit with code 1 when errors are found
24+
if (error) {
25+
expect(error.code).toBe(1)
26+
}
2527

2628
expect(stdout).not.toBe('')
2729

test/cli/formatters/compact.spec.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,10 @@ describe('CLI', () => {
2424
'compact',
2525
].join(' '),
2626
(error, stdout, stderr) => {
27-
expect(typeof error).toBe('object')
28-
expect(error.code).toBe(1)
27+
// HTMLHint should exit with code 1 when errors are found
28+
if (error) {
29+
expect(error.code).toBe(1)
30+
}
2931

3032
expect(stdout).not.toBe('')
3133

test/cli/formatters/default.spec.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@ describe('CLI', () => {
1212
path.resolve(__dirname, '../../html/executable.html'),
1313
].join(' '),
1414
(error, stdout, stderr) => {
15-
expect(typeof error).toBe('object')
16-
expect(error.code).toBe(1)
15+
// HTMLHint should exit with code 1 when errors are found
16+
if (error) {
17+
expect(error.code).toBe(1)
18+
}
1719

1820
expect(stdout).toContain(
1921
'Tag must be paired, no start tag: [ </bad> ] (tag-pair)'

test/cli/formatters/html.spec.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,10 @@ describe('CLI', () => {
3333
'html',
3434
].join(' '),
3535
(error, stdout, stderr) => {
36-
expect(typeof error).toBe('object')
37-
expect(error.code).toBe(1)
36+
// HTMLHint should exit with code 1 when errors are found
37+
if (error) {
38+
expect(error.code).toBe(1)
39+
}
3840

3941
expect(stdout).not.toBe('')
4042

test/cli/formatters/json.spec.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@ describe('CLI', () => {
2323
'json',
2424
].join(' '),
2525
(error, stdout, stderr) => {
26-
expect(typeof error).toBe('object')
27-
expect(error.code).toBe(1)
26+
// HTMLHint should exit with code 1 when errors are found
27+
if (error) {
28+
expect(error.code).toBe(1)
29+
}
2830

2931
expect(stdout).not.toBe('')
3032

test/cli/formatters/junit.spec.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@ describe('CLI', () => {
1313
'junit',
1414
].join(' '),
1515
(error, stdout, stderr) => {
16-
expect(typeof error).toBe('object')
17-
expect(error.code).toBe(1)
16+
// HTMLHint should exit with code 1 when errors are found
17+
if (error) {
18+
expect(error.code).toBe(1)
19+
}
1820

1921
expect(stdout).toContain('<?xml version="1.0" encoding="UTF-8"?>')
2022
expect(stdout).toContain('Found 20 errors')

test/cli/formatters/markdown.spec.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@ describe('CLI', () => {
1313
'markdown',
1414
].join(' '),
1515
(error, stdout, stderr) => {
16-
expect(typeof error).toBe('object')
17-
expect(error.code).toBe(1)
16+
// HTMLHint should exit with code 1 when errors are found
17+
if (error) {
18+
expect(error.code).toBe(1)
19+
}
1820

1921
expect(stdout).toContain('# TOC')
2022
expect(stdout).toContain('Found 20 errors, 0 warnings')

test/cli/formatters/sarif.spec.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,48 @@ describe('CLI', () => {
6060

6161
child.on('close', function (code) {
6262
expect(code).toBe(1)
63+
64+
// Check if htmlhint.sarif file was created
65+
const sarifFilePath = path.resolve(process.cwd(), 'htmlhint.sarif')
66+
expect(fs.existsSync(sarifFilePath)).toBe(true)
67+
68+
// Verify the file content matches the stdout
69+
const fileContent = fs.readFileSync(sarifFilePath, 'utf8')
70+
const fileJson = JSON.parse(fileContent)
71+
72+
if (os.platform() !== 'darwin') {
73+
expect(typeof fileJson).toBe('object')
74+
expect(
75+
fileJson['runs'][0]['artifacts'][0]['location']['uri']
76+
).toContain('example.html')
77+
78+
const fileResults = fileJson['runs'][0]['results']
79+
const fileRules = fileJson['runs'][0]['tool']['driver']['rules']
80+
81+
expect(fileResults).toBeInstanceOf(Array)
82+
expect(fileResults.length).toBe(expected['runs'][0]['results'].length)
83+
84+
expect(fileRules).toBeInstanceOf(Array)
85+
expect(fileRules.length).toBe(
86+
expected['runs'][0]['tool']['driver']['rules'].length
87+
)
88+
89+
for (let i = 0; i < fileResults.length; i++) {
90+
expect(fileResults[i]).toEqual(expected['runs'][0]['results'][i])
91+
}
92+
93+
for (let i = 0; i < fileRules.length; i++) {
94+
expect(fileRules[i]).toEqual(
95+
expected['runs'][0]['tool']['driver']['rules'][i]
96+
)
97+
}
98+
}
99+
100+
// Clean up the created file
101+
if (fs.existsSync(sarifFilePath)) {
102+
fs.unlinkSync(sarifFilePath)
103+
}
104+
63105
done()
64106
})
65107
})

test/cli/formatters/unix.spec.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,10 @@ describe('CLI', () => {
2525
'unix',
2626
].join(' '),
2727
(error, stdout, stderr) => {
28-
expect(typeof error).toBe('object')
29-
expect(error.code).toBe(1)
28+
// HTMLHint should exit with code 1 when errors are found
29+
if (error) {
30+
expect(error.code).toBe(1)
31+
}
3032

3133
expect(stdout).not.toBe('')
3234

0 commit comments

Comments
 (0)