Skip to content

Commit f26b763

Browse files
authored
Scrub ansi from test output and stderr (#19)
* scrub ansi from test output and stderr * fix test case
1 parent 5f173cc commit f26b763

File tree

4 files changed

+40
-4
lines changed

4 files changed

+40
-4
lines changed

__tests__/renderer.test.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,4 +271,36 @@ describe('renderer', () => {
271271

272272
expect($('summary:contains(🖨️ Output)')).toHaveLength(0)
273273
})
274+
275+
it('scrubs ansi from stderr', async () => {
276+
const renderer = await getRenderer()
277+
const placeholder = 'no-ansi-please'
278+
renderer.stderr = `\u001b[31m${placeholder}\u001b[0m`
279+
await renderer.writeSummary()
280+
const $ = await loadSummaryHTML()
281+
282+
expect($(`details:contains(${placeholder})`)).toHaveLength(1)
283+
$(`details:contains(${placeholder})`).each((_, el) => {
284+
const text = $(el).text()
285+
if (text.includes(placeholder)) {
286+
expect(text).not.toContain('\u001b')
287+
}
288+
})
289+
})
290+
291+
it('scrubs ansi from test output', async () => {
292+
const renderer = await getRenderer()
293+
const placeholder = 'no-ansi-please'
294+
renderer.packageResults[1].events[0].output = `\u001b[31m${placeholder}\u001b[0m`
295+
await renderer.writeSummary()
296+
const $ = await loadSummaryHTML()
297+
298+
expect($(`details:contains(${placeholder})`)).toHaveLength(1)
299+
$(`details:contains(${placeholder})`).each((_, el) => {
300+
const text = $(el).text()
301+
if (text.includes(placeholder)) {
302+
expect(text).not.toContain('\u001b')
303+
}
304+
})
305+
})
274306
})

dist/index.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "go-test-action",
3-
"version": "0.4.1",
3+
"version": "0.5.0",
44
"description": "GitHub Action to run go tests with rich summary output and annotations.",
55
"main": "dist/index.js",
66
"scripts": {

src/renderer.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ class Renderer {
191191

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

@@ -275,11 +275,15 @@ ${pieData}
275275
<summary>🚨 Standard Error Output</summary>
276276
277277
\`\`\`
278-
${this.stderr}
278+
${this.scrubAnsi(this.stderr)}
279279
\`\`\`
280280
281281
</details>`
282282
}
283+
284+
private scrubAnsi(input: string): string {
285+
return input.replace(/\x1b\[[0-9;]*[a-zA-Z]/g, '')
286+
}
283287
}
284288

285289
export default Renderer

0 commit comments

Comments
 (0)