From 065d9ce8aec15b53a00aa5cabedc39dcc6e419f0 Mon Sep 17 00:00:00 2001 From: David Goss Date: Tue, 3 Mar 2020 12:37:19 +0000 Subject: [PATCH 1/5] emit ndjson from message formatter --- src/formatter/message_formatter.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/formatter/message_formatter.ts b/src/formatter/message_formatter.ts index 0ffe7ba12..d177bdf21 100644 --- a/src/formatter/message_formatter.ts +++ b/src/formatter/message_formatter.ts @@ -1,11 +1,10 @@ import Formatter from '.' -import { messages } from 'cucumber-messages' export default class MessageFormatter extends Formatter { constructor(options) { super(options) options.eventBroadcaster.on('envelope', envelope => - this.log(messages.Envelope.encodeDelimited(envelope).finish()) + this.log(JSON.stringify(envelope.toJSON()) + '\n') ) } } From 992871ced343914d8acc36d3412b4c255806e66b Mon Sep 17 00:00:00 2001 From: David Goss Date: Wed, 4 Mar 2020 07:30:11 +0000 Subject: [PATCH 2/5] update cli documentation --- docs/cli.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/cli.md b/docs/cli.md index 7c58fd06c..8430d15de 100644 --- a/docs/cli.md +++ b/docs/cli.md @@ -44,7 +44,7 @@ This option may be used multiple times in order to output different formats to d If multiple formats are specified with the same output, only the last is used. Built-in formatters -* 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. +* message - prints each [message](https://github.com/cucumber/cucumber/tree/master/cucumber-messages) in NDJSON form, which can then be consumed by other tools. * json - prints the feature as JSON. * progress - prints one character per scenario (default). * progress-bar - prints a progress bar and outputs errors/warnings along the way. From 6d4e293d6aced61e6fab4535eb8b7d9daf2eaea4 Mon Sep 17 00:00:00 2001 From: David Goss Date: Sat, 14 Mar 2020 16:06:20 +0000 Subject: [PATCH 3/5] get everything passing with ndjson --- features/support/world.ts | 11 ++++------- package.json | 2 +- yarn.lock | 5 +++++ 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/features/support/world.ts b/features/support/world.ts index b5a416916..a45f05a86 100644 --- a/features/support/world.ts +++ b/features/support/world.ts @@ -8,7 +8,7 @@ import fs from 'fs' import path from 'path' import VError from 'verror' import _ from 'lodash' -import protobuf from 'protobufjs' +import ndjsonParse from 'ndjson-parse' import { messages } from 'cucumber-messages' interface ILastRun { @@ -80,14 +80,11 @@ export class World { stdout.end() result = { error, stdout: await toString(stdout), stderr } } - const envelopes: messages.Envelope[] = [] + let envelopes: messages.Envelope[] = [] const messageOutputPath = path.join(cwd, messageFilename) if (fs.existsSync(messageOutputPath)) { - const data = fs.readFileSync(messageOutputPath) - const reader = protobuf.Reader.create(data) - while (reader.pos < reader.len) { - envelopes.push(messages.Envelope.decodeDelimited(reader)) - } + const data = fs.readFileSync(messageOutputPath, { encoding: 'utf-8' }) + envelopes = ndjsonParse(data).map(messages.Envelope.fromObject) fs.writeFileSync( path.join(cwd, 'message.out.json'), JSON.stringify( diff --git a/package.json b/package.json index 857d07a00..23078c166 100644 --- a/package.json +++ b/package.json @@ -212,9 +212,9 @@ "lolex": "^5.1.1", "mocha": "^6.1.3", "mustache": "^3.0.1", + "ndjson-parse": "^1.0.4", "nyc": "^15.0.0", "prettier": "^1.5.2", - "protobufjs": "^6.8.8", "regenerator-runtime": "^0.13.3", "serve-static": "^1.10.0", "sinon": "^8.0.1", diff --git a/yarn.lock b/yarn.lock index 5634fbf63..c999a9f79 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2750,6 +2750,11 @@ natural-compare@^1.4.0: resolved "https://registry.yarnpkg.com/natural-compare/-/natural-compare-1.4.0.tgz#4abebfeed7541f2c27acfb29bdbbd15c8d5ba4f7" integrity sha1-Sr6/7tdUHywnrPspvbvRXI1bpPc= +ndjson-parse@^1.0.4: + version "1.0.4" + resolved "https://registry.yarnpkg.com/ndjson-parse/-/ndjson-parse-1.0.4.tgz#65c031147ea1b5fa6f692e4fd63ab75f89dbf648" + integrity sha512-xwglvz2dMbxvX4NAVKnww8xEJ4kp4+CKVseQQdtkA79yI3abPqyBYqk6A6HvNci5oS0cUsSHheMEV1c+9MWlEw== + next-tick@~1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/next-tick/-/next-tick-1.0.0.tgz#ca86d1fe8828169b0120208e3dc8424b9db8342c" From a19198f14abf92463930a1c75eedde1d7a421b21 Mon Sep 17 00:00:00 2001 From: David Goss Date: Sun, 15 Mar 2020 07:43:27 +0000 Subject: [PATCH 4/5] no need to write message output as json as well --- features/support/world.ts | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/features/support/world.ts b/features/support/world.ts index a45f05a86..55e5302ef 100644 --- a/features/support/world.ts +++ b/features/support/world.ts @@ -34,7 +34,7 @@ export class World { public globalExecutablePath: string async run(executablePath: string, inputArgs: string[]): Promise { - const messageFilename = 'message.out' + const messageFilename = 'message.ndjson' const args = ['node', executablePath] .concat(inputArgs, [ '--backtrace', @@ -85,14 +85,6 @@ export class World { if (fs.existsSync(messageOutputPath)) { const data = fs.readFileSync(messageOutputPath, { encoding: 'utf-8' }) envelopes = ndjsonParse(data).map(messages.Envelope.fromObject) - fs.writeFileSync( - path.join(cwd, 'message.out.json'), - JSON.stringify( - envelopes.map(e => e.toJSON()), - null, - 2 - ) - ) } if (this.debug) { console.log(result.stdout + result.stderr) // eslint-disable-line no-console From bef3ea423d3acd7d8bd7d38ecd95372b5ed600fd Mon Sep 17 00:00:00 2001 From: David Goss Date: Sun, 15 Mar 2020 07:57:36 +0000 Subject: [PATCH 5/5] add module def for ndjson-parse --- src/types/ndjson-parse/index.d.ts | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 src/types/ndjson-parse/index.d.ts diff --git a/src/types/ndjson-parse/index.d.ts b/src/types/ndjson-parse/index.d.ts new file mode 100644 index 000000000..fa7fae7fd --- /dev/null +++ b/src/types/ndjson-parse/index.d.ts @@ -0,0 +1,3 @@ +declare module 'ndjson-parse' { + export default function parse(input: string): any[] +}