Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cli: strip ansi from stderr if no support #2035

Merged
merged 10 commits into from Jun 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Expand Up @@ -11,6 +11,9 @@ Please see [CONTRIBUTING.md](https://github.com/cucumber/cucumber/blob/master/CO
### Added
- Add `willBeRetried` to the parameter passed to `After` hook functions ([#2045](https://github.com/cucumber/cucumber-js/pull/2045))

### Fixed
- Prevent outputting ANSI escapes to `stderr` if it can't display them ([#2035](https://github.com/cucumber/cucumber-js/pull/2035))

## [8.2.2] - 2022-05-27
### Changed
- Use latest HTML formatter with better handling for scenario outlines
Expand Down
10 changes: 3 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Expand Up @@ -213,6 +213,7 @@
"durations": "^3.4.2",
"figures": "^3.2.0",
"glob": "^7.1.6",
"has-ansi": "^4.0.1",
"indent-string": "^4.0.0",
"is-stream": "^2.0.0",
"knuth-shuffle-seeded": "^1.0.6",
Expand All @@ -225,6 +226,7 @@
"semver": "7.3.7",
"stack-chain": "^2.0.0",
"string-argv": "^0.3.1",
"strip-ansi": "6.0.1",
"supports-color": "^8.1.1",
"tmp": "^0.2.1",
"util-arity": "^1.1.0",
Expand Down Expand Up @@ -273,7 +275,6 @@
"express": "4.18.1",
"fs-extra": "10.1.0",
"genversion": "3.1.1",
"has-ansi": "^4.0.1",
"mocha": "10.0.0",
"mustache": "4.2.0",
"nyc": "15.1.0",
Expand All @@ -285,7 +286,6 @@
"sinon-chai": "3.7.0",
"stream-buffers": "3.0.2",
"stream-to-string": "1.2.0",
"strip-ansi": "6.0.1",
"ts-node": "10.8.0",
"tsd": "0.20.0",
"typescript": "4.7.2"
Expand Down
20 changes: 19 additions & 1 deletion src/api/formatters.ts
Expand Up @@ -10,12 +10,16 @@ import path from 'path'
import { DEFAULT_CUCUMBER_PUBLISH_URL } from '../formatter/publish'
import HttpStream from '../formatter/http_stream'
import { Writable } from 'stream'
import { supportsColor } from 'supports-color'
import { IRunOptionsFormats } from './types'
import hasAnsi from 'has-ansi'
import stripAnsi from 'strip-ansi'

export async function initializeFormatters({
env,
cwd,
stdout,
stderr,
logger,
onStreamError,
eventBroadcaster,
Expand All @@ -26,6 +30,7 @@ export async function initializeFormatters({
env: NodeJS.ProcessEnv
cwd: string
stdout: IFormatterStream
stderr: IFormatterStream
logger: Console
onStreamError: () => void
eventBroadcaster: EventEmitter
Expand Down Expand Up @@ -88,7 +93,7 @@ export async function initializeFormatters({
const readerStream = new Writable({
objectMode: true,
write: function (responseBody: string, encoding, writeCallback) {
logger.error(responseBody)
logger.error(sanitisePublishOutput(responseBody, stderr))
writeCallback()
},
})
Expand All @@ -100,3 +105,16 @@ export async function initializeFormatters({
await Promise.all(formatters.map(async (f) => await f.finished()))
}
}

/*
This is because the Cucumber Reports service returns a pre-formatted console message
including ANSI escapes, so if our stderr stream doesn't support those we need to
strip them back out. Ideally we should get structured data from the service and
compose the console message on this end.
*/
function sanitisePublishOutput(raw: string, stderr: IFormatterStream) {
if (!supportsColor(stderr) && hasAnsi(raw)) {
return stripAnsi(raw)
}
return raw
}
1 change: 1 addition & 0 deletions src/api/run_cucumber.ts
Expand Up @@ -58,6 +58,7 @@ export async function runCucumber(
env,
cwd,
stdout,
stderr,
logger,
onStreamError: () => (formatterStreamError = true),
eventBroadcaster,
Expand Down
18 changes: 12 additions & 6 deletions src/cli/publish_banner.ts
@@ -1,31 +1,37 @@
import chalk from 'chalk'
import Table from 'cli-table3'

const chalkInstance = chalk.stderr

const underlineBoldCyan = (x: string): string =>
chalk.underline(chalk.bold(chalk.cyan(x)))
chalkInstance.underline(chalkInstance.bold(chalkInstance.cyan(x)))

const formattedReportUrl = underlineBoldCyan('https://reports.cucumber.io')
const formattedEnv =
chalk.cyan('CUCUMBER_PUBLISH_ENABLED') + '=' + chalk.cyan('true')
chalkInstance.cyan('CUCUMBER_PUBLISH_ENABLED') +
'=' +
chalkInstance.cyan('true')
const formattedMoreInfoUrl = underlineBoldCyan(
'https://cucumber.io/docs/cucumber/environment-variables/'
)

const text = `\
Share your Cucumber Report with your team at ${formattedReportUrl}

Command line option: ${chalk.cyan('--publish')}
Command line option: ${chalkInstance.cyan('--publish')}
Environment variable: ${formattedEnv}

More information at ${formattedMoreInfoUrl}

To disable this message, add this to your ${chalk.bold('./cucumber.js')}:
${chalk.bold("module.exports = { default: '--publish-quiet' }")}`
To disable this message, add this to your ${chalkInstance.bold(
'./cucumber.js'
)}:
${chalkInstance.bold("module.exports = { default: '--publish-quiet' }")}`

const table = new Table({
style: {
head: [],
border: ['green'],
border: chalkInstance.supportsColor ? ['green'] : [],
},
})

Expand Down