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

Fix json formatter to write result to file continuously #21

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions lib/parallel_cucumber/formatters/formatter.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ var Formatter = function(options) {

var self = new Events.EventEmitter();

if (options.outFilePath) {
self._out = FS.createWriteStream(options.outFilePath, { flags: 'w' });
self.outFilePath = options.outFilePath;

if (self.outFilePath) {
self._out = FS.createWriteStream(self.outFilePath, { flags: 'w' });
self._outNeedsClosing = true;
}
else {
Expand Down
22 changes: 19 additions & 3 deletions lib/parallel_cucumber/formatters/json_formatter.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var JsonFormatter = function(options) {
var ParallelCucumber = require('../../parallel_cucumber');
var OS = require('os');
var FS = require('fs');
var Debug = require('debug')('parallel-cucumber-js');

var self = ParallelCucumber.Formatters.Formatter(options);
Expand All @@ -27,12 +28,27 @@ var JsonFormatter = function(options) {
Debug('New feature');
self._features.push(currentFeature);
}

if (self.outFilePath) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@peter-reperger the file output code seems to be both in JsonFormatter and Formatter now. Previously the file output code was only in Formatter. Is there a way to keep the file output code in only Formatter? Is there anything unique about the file output logic in JsonFormatter?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@simondean Yes, this unique solution was needed because the continuous output reporting can only be done for json output. In this case, we can update the json file after every scenario, while in case of other formatters like console, this is not possible. So that is why I used this approach.

var jsonPart = JSON.stringify(self._features).slice(0, -1);
self._writeToFile(jsonPart);
}
};

self._writeToFile = function (data) {
self._out = FS.createWriteStream(self.outFilePath, {flags: 'w'});
self._outNeedsClosing = true;
self._out.write(data, 'utf8');
};

self.end = function(callback) {
var json = JSON.stringify(self._features);
self._write(json);
self._write(OS.EOL);
var jsonFinal = JSON.stringify(self._features);
if (self.outFilePath) {
self._writeToFile(jsonFinal + OS.EOL);
}
else {
self._write(jsonFinal + OS.EOL);
}

self._superEnd(callback);
};
Expand Down