Skip to content

Commit

Permalink
Scrub ansi from test output and stderr (#19)
Browse files Browse the repository at this point in the history
* scrub ansi from test output and stderr

* fix test case
  • Loading branch information
robherley committed Sep 5, 2024
1 parent 5f173cc commit f26b763
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 4 deletions.
32 changes: 32 additions & 0 deletions __tests__/renderer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,4 +271,36 @@ describe('renderer', () => {

expect($('summary:contains(🖨️ Output)')).toHaveLength(0)
})

it('scrubs ansi from stderr', async () => {
const renderer = await getRenderer()
const placeholder = 'no-ansi-please'
renderer.stderr = `\u001b[31m${placeholder}\u001b[0m`
await renderer.writeSummary()
const $ = await loadSummaryHTML()

expect($(`details:contains(${placeholder})`)).toHaveLength(1)
$(`details:contains(${placeholder})`).each((_, el) => {
const text = $(el).text()
if (text.includes(placeholder)) {
expect(text).not.toContain('\u001b')
}
})
})

it('scrubs ansi from test output', async () => {
const renderer = await getRenderer()
const placeholder = 'no-ansi-please'
renderer.packageResults[1].events[0].output = `\u001b[31m${placeholder}\u001b[0m`
await renderer.writeSummary()
const $ = await loadSummaryHTML()

expect($(`details:contains(${placeholder})`)).toHaveLength(1)
$(`details:contains(${placeholder})`).each((_, el) => {
const text = $(el).text()
if (text.includes(placeholder)) {
expect(text).not.toContain('\u001b')
}
})
})
})
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "go-test-action",
"version": "0.4.1",
"version": "0.5.0",
"description": "GitHub Action to run go tests with rich summary output and annotations.",
"main": "dist/index.js",
"scripts": {
Expand Down
8 changes: 6 additions & 2 deletions src/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ class Renderer {

if (!this.omit.has(OmitOption.PackageOutput)) {
details += `<details><summary>🖨️ Output</summary><pre><code>${
packageResult.output() || '(none)'
this.scrubAnsi(packageResult.output()) || '(none)'
}</code></pre></details>`
}

Expand Down Expand Up @@ -275,11 +275,15 @@ ${pieData}
<summary>🚨 Standard Error Output</summary>
\`\`\`
${this.stderr}
${this.scrubAnsi(this.stderr)}
\`\`\`
</details>`
}

private scrubAnsi(input: string): string {
return input.replace(/\x1b\[[0-9;]*[a-zA-Z]/g, '')
}
}

export default Renderer

0 comments on commit f26b763

Please sign in to comment.