Skip to content

Commit 88532ac

Browse files
authored
format: change message formatter to output NDJSON instead of binary (#1295)
* emit ndjson from message formatter * update cli documentation * get everything passing with ndjson * no need to write message output as json as well * add module def for ndjson-parse
1 parent 0f9414b commit 88532ac

File tree

6 files changed

+17
-21
lines changed

6 files changed

+17
-21
lines changed

docs/cli.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ This option may be used multiple times in order to output different formats to d
4444
If multiple formats are specified with the same output, only the last is used.
4545

4646
Built-in formatters
47-
* message - prints the full [`cucumber-messages`](https://github.com/cucumber/cucumber/tree/master/cucumber-messages) object in [varint](https://developers.google.com/protocol-buffers/docs/encoding#varints)-delimited protobuf binary form, which can then be piped to other tools.
47+
* message - prints each [message](https://github.com/cucumber/cucumber/tree/master/cucumber-messages) in NDJSON form, which can then be consumed by other tools.
4848
* json - prints the feature as JSON.
4949
* progress - prints one character per scenario (default).
5050
* progress-bar - prints a progress bar and outputs errors/warnings along the way.

features/support/world.ts

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import fs from 'fs'
88
import path from 'path'
99
import VError from 'verror'
1010
import _ from 'lodash'
11-
import protobuf from 'protobufjs'
11+
import ndjsonParse from 'ndjson-parse'
1212
import { messages } from 'cucumber-messages'
1313

1414
interface ILastRun {
@@ -34,7 +34,7 @@ export class World {
3434
public globalExecutablePath: string
3535

3636
async run(executablePath: string, inputArgs: string[]): Promise<void> {
37-
const messageFilename = 'message.out'
37+
const messageFilename = 'message.ndjson'
3838
const args = ['node', executablePath]
3939
.concat(inputArgs, [
4040
'--backtrace',
@@ -80,22 +80,11 @@ export class World {
8080
stdout.end()
8181
result = { error, stdout: await toString(stdout), stderr }
8282
}
83-
const envelopes: messages.Envelope[] = []
83+
let envelopes: messages.Envelope[] = []
8484
const messageOutputPath = path.join(cwd, messageFilename)
8585
if (fs.existsSync(messageOutputPath)) {
86-
const data = fs.readFileSync(messageOutputPath)
87-
const reader = protobuf.Reader.create(data)
88-
while (reader.pos < reader.len) {
89-
envelopes.push(messages.Envelope.decodeDelimited(reader))
90-
}
91-
fs.writeFileSync(
92-
path.join(cwd, 'message.out.json'),
93-
JSON.stringify(
94-
envelopes.map(e => e.toJSON()),
95-
null,
96-
2
97-
)
98-
)
86+
const data = fs.readFileSync(messageOutputPath, { encoding: 'utf-8' })
87+
envelopes = ndjsonParse(data).map(messages.Envelope.fromObject)
9988
}
10089
if (this.debug) {
10190
console.log(result.stdout + result.stderr) // eslint-disable-line no-console

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,9 +223,9 @@
223223
"lolex": "^5.1.1",
224224
"mocha": "^6.1.3",
225225
"mustache": "^3.0.1",
226+
"ndjson-parse": "^1.0.4",
226227
"nyc": "^15.0.0",
227228
"prettier": "^1.5.2",
228-
"protobufjs": "^6.8.8",
229229
"regenerator-runtime": "^0.13.3",
230230
"serve-static": "^1.10.0",
231231
"sinon": "^8.0.1",

src/formatter/message_formatter.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import Formatter, { IFormatterOptions } from '.'
22
import { messages } from 'cucumber-messages'
3-
import IEnvelope = messages.IEnvelope
43

54
export default class MessageFormatter extends Formatter {
65
constructor(options: IFormatterOptions) {
76
super(options)
8-
options.eventBroadcaster.on('envelope', (envelope: IEnvelope) =>
9-
this.log(messages.Envelope.encodeDelimited(envelope).finish())
7+
options.eventBroadcaster.on('envelope', (envelope: messages.Envelope) =>
8+
this.log(JSON.stringify(envelope.toJSON()) + '\n')
109
)
1110
}
1211
}

src/types/ndjson-parse/index.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
declare module 'ndjson-parse' {
2+
export default function parse(input: string): any[]
3+
}

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2829,6 +2829,11 @@ natural-compare@^1.4.0:
28292829
resolved "https://registry.yarnpkg.com/natural-compare/-/natural-compare-1.4.0.tgz#4abebfeed7541f2c27acfb29bdbbd15c8d5ba4f7"
28302830
integrity sha1-Sr6/7tdUHywnrPspvbvRXI1bpPc=
28312831

2832+
ndjson-parse@^1.0.4:
2833+
version "1.0.4"
2834+
resolved "https://registry.yarnpkg.com/ndjson-parse/-/ndjson-parse-1.0.4.tgz#65c031147ea1b5fa6f692e4fd63ab75f89dbf648"
2835+
integrity sha512-xwglvz2dMbxvX4NAVKnww8xEJ4kp4+CKVseQQdtkA79yI3abPqyBYqk6A6HvNci5oS0cUsSHheMEV1c+9MWlEw==
2836+
28322837
next-tick@~1.0.0:
28332838
version "1.0.0"
28342839
resolved "https://registry.yarnpkg.com/next-tick/-/next-tick-1.0.0.tgz#ca86d1fe8828169b0120208e3dc8424b9db8342c"

0 commit comments

Comments
 (0)