Skip to content

Commit

Permalink
Added support for writing output to file (close cucumber#90)
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanjanse committed Aug 7, 2014
1 parent 0d445d2 commit 5f011e1
Show file tree
Hide file tree
Showing 9 changed files with 145 additions and 4 deletions.
7 changes: 7 additions & 0 deletions lib/cucumber/cli/argument_parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,17 @@ var ArgumentParser = function(argv) {
return format;
},

getReportFile: function getReportFile() {
var reportFile = self.getOptionOrDefault(ArgumentParser.REPORT_FILE_OPTION_NAME, null);
return reportFile;
},

getKnownOptionDefinitions: function getKnownOptionDefinitions() {
var definitions = {};
definitions[ArgumentParser.REQUIRE_OPTION_NAME] = [path, Array];
definitions[ArgumentParser.TAGS_OPTION_NAME] = [String, Array];
definitions[ArgumentParser.FORMAT_OPTION_NAME] = String;
definitions[ArgumentParser.REPORT_FILE_OPTION_NAME] = String;
definitions[ArgumentParser.HELP_FLAG_NAME] = Boolean;
definitions[ArgumentParser.VERSION_FLAG_NAME] = Boolean;
definitions[ArgumentParser.COFFEE_SCRIPT_SNIPPETS_FLAG_NAME] = Boolean;
Expand Down Expand Up @@ -124,6 +130,7 @@ ArgumentParser.FORMAT_OPTION_SHORT_NAME = "f";
ArgumentParser.DEFAULT_FORMAT_VALUE = "progress";
ArgumentParser.TAGS_OPTION_NAME = "tags";
ArgumentParser.TAGS_OPTION_SHORT_NAME = "t";
ArgumentParser.REPORT_FILE_OPTION_NAME = "report";
ArgumentParser.HELP_FLAG_NAME = "help";
ArgumentParser.HELP_FLAG_SHORT_NAME = "h";
ArgumentParser.DEFAULT_HELP_FLAG_VALUE = false;
Expand Down
4 changes: 4 additions & 0 deletions lib/cucumber/cli/configuration.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ var Configuration = function(argv) {
return rule;
},

getReportFile: function getReportFile() {
return argumentParser.getReportFile();
},

isHelpRequested: function isHelpRequested() {
var isHelpRequested = argumentParser.isHelpRequested();
return isHelpRequested;
Expand Down
22 changes: 21 additions & 1 deletion lib/cucumber/runtime.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
var Runtime = function(configuration) {
var Cucumber = require('../cucumber');

var fs = require('fs');

var callback;
var listeners = Cucumber.Type.Collection();

var self = {
Expand All @@ -10,13 +13,30 @@ var Runtime = function(configuration) {
var features = self.getFeatures();
var supportCodeLibrary = self.getSupportCodeLibrary();
var astTreeWalker = Runtime.AstTreeWalker(features, supportCodeLibrary, listeners);
astTreeWalker.walk(callback);

self.setCallback(callback);
astTreeWalker.walk(self.finish);
},

finish: function finish(result) {
if (configuration.getReportFile() !== null) {
fs.writeFileSync(configuration.getReportFile(), listeners.getLast().getLogs(), { flag: 'w' });
}
self.getCallback()(result);
},

attachListener: function attachListener(listener) {
listeners.add(listener);
},

setCallback: function setCallback(newCallback) {
callback = newCallback;
},

getCallback: function getCallback() {
return callback;
},

getFeatures: function getFeatures() {
var featureSources = configuration.getFeatureSources();
var astFilter = configuration.getAstFilter();
Expand Down
5 changes: 5 additions & 0 deletions lib/cucumber/volatile_configuration.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ var VolatileConfiguration = function VolatileConfiguration(features, supportCode

options = options || {};
var tagGroupStrings = options['tags'] || [];
var reportFile = options['report'] || null;

var self = {
getFeatureSources: function getFeatureSources() {
Expand Down Expand Up @@ -39,6 +40,10 @@ var VolatileConfiguration = function VolatileConfiguration(features, supportCode
var tagGroup = tagGroupParser.parse();
var rule = Cucumber.Ast.Filter.AnyOfTagsRule(tagGroup);
return rule;
},

getReportFile: function getReportFile() {
return reportFile;
}
};
return self;
Expand Down
23 changes: 23 additions & 0 deletions spec/cucumber/cli/argument_parser_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ describe("Cucumber.Cli.ArgumentParser", function () {
expect(knownOptionDefinitions[Cucumber.Cli.ArgumentParser.FORMAT_OPTION_NAME]).toEqual(String);
});

it("defines a --report flag", function () {
var knownOptionDefinitions = argumentParser.getKnownOptionDefinitions();
expect(knownOptionDefinitions[Cucumber.Cli.ArgumentParser.REPORT_FILE_OPTION_NAME]).toEqual(String);
});

it("defines a --help flag", function () {
var knownOptionDefinitions = argumentParser.getKnownOptionDefinitions();
expect(knownOptionDefinitions[Cucumber.Cli.ArgumentParser.HELP_FLAG_NAME]).toEqual(Boolean);
Expand Down Expand Up @@ -303,6 +308,24 @@ describe("Cucumber.Cli.ArgumentParser", function () {
});
});

describe("getReportFile()", function () {
var reportFile;

beforeEach(function () {
reportFile = createSpy("reportFile");
spyOn(argumentParser, 'getOptionOrDefault').andReturn(reportFile);
});

it("gets the reportFile option value", function () {
argumentParser.getReportFile();
expect(argumentParser.getOptionOrDefault).toHaveBeenCalledWith(Cucumber.Cli.ArgumentParser.REPORT_FILE_OPTION_NAME, null);
});

it("returns the reportFile", function () {
expect(argumentParser.getReportFile()).toBe(reportFile);
});
});

describe("isHelpRequested()", function () {
var isHelpRequested;

Expand Down
17 changes: 17 additions & 0 deletions spec/cucumber/cli/configuration_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,23 @@ describe("Cucumber.Cli.Configuration", function () {
});
});

describe("getReportFile()", function () {
beforeEach(function () {
spyOnStub(argumentParser, "getReportFile");
});

it("asks the argument parser for the report file path", function () {
configuration.getReportFile();
expect(argumentParser.getReportFile).toHaveBeenCalled();
});

it("returns the answer from the argument parser", function () {
var reportFile = createSpy("report file path");
argumentParser.getReportFile.andReturn(reportFile);
expect(configuration.getReportFile()).toBe(reportFile);
});
});

describe("isHelpRequired()", function () {
beforeEach(function () {
spyOnStub(argumentParser, 'isHelpRequested');
Expand Down
51 changes: 49 additions & 2 deletions spec/cucumber/runtime_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ describe("Cucumber.Runtime", function() {
var Cucumber = requireLib('cucumber');
var configuration;
var runtime;
var supportCodeLibrary, listeners;
var supportCodeLibrary, listeners, fs;

beforeEach(function() {
fs = require("fs");
listeners = createSpyWithStubs("listener collection", {add: null});
configuration = createSpy("configuration");
spyOn(Cucumber.Type, 'Collection').andReturn(listeners);
Expand Down Expand Up @@ -69,7 +70,53 @@ describe("Cucumber.Runtime", function() {

it("tells the AST tree walker to walk", function() {
runtime.start(callback);
expect(astTreeWalker.walk).toHaveBeenCalledWith(callback);
expect(astTreeWalker.walk).toHaveBeenCalledWith(runtime.finish);
});
});

describe("finish()", function () {
var result, callback, formatter, logs;

beforeEach(function () {
result = createSpy("AST tree walker result");
callback = createSpy("callback");
formatter = createSpy("formatter");
logs = createSpy("formatter logs");
spyOnStub(listeners, "getLast").andReturn(formatter);
spyOnStub(formatter, "getLogs").andReturn(logs);
spyOn(fs, "writeFileSync");
spyOn(runtime, "getCallback").andReturn(callback);
});

it("calls the callback", function() {
spyOnStub(configuration, "getReportFile").andReturn(null);
runtime.finish(result);
expect(callback).toHaveBeenCalled();
});

it("does write to output to file", function () {
spyOnStub(configuration, "getReportFile").andReturn("reportFile");
runtime.finish(result);
expect(fs.writeFileSync).toHaveBeenCalledWith("reportFile", logs, { flag: "w" });
});

it("does not write to output to file if not specified", function () {
spyOnStub(configuration, "getReportFile").andReturn(null);
runtime.finish(result);
expect(fs.writeFileSync).not.toHaveBeenCalled();
});
});

describe("getCallback() [setCallback()]", function() {
var callback;

beforeEach(function() {
callback = createSpy("callback");
});

it("returns the callback set with setCallback()", function() {
runtime.setCallback(callback);
expect(runtime.getCallback()).toBe(callback);
});
});

Expand Down
16 changes: 15 additions & 1 deletion spec/cucumber/volatile_configuration_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ describe("Cucumber.VolatileConfiguration", function() {
beforeEach(function() {
supportCodeLibrary = createSpy("support code library");
spyOn(Cucumber.SupportCode, 'Library').andReturn(supportCodeLibrary);
featureSources = createSpy("feature source");
featureSources = createSpy("feature source");
supportCodeInitializer = createSpy("support code initializer");
configuration = Cucumber.VolatileConfiguration(featureSources, supportCodeInitializer);
context.configuration = configuration;
Expand Down Expand Up @@ -138,4 +138,18 @@ describe("Cucumber.VolatileConfiguration", function() {
expect(returned).toBe(rule);
});
});

describe("getReportFile()", function () {
var reportFile;

it("returns 'null' when no report specified", function () {
expect(configuration.getReportFile()).toEqual(null);
});

it("returns the specified reports", function () {
reportFile = createSpy("report file path");
configuration = Cucumber.VolatileConfiguration(featureSources, supportCodeInitializer, {report: reportFile});
expect(configuration.getReportFile()).toEqual(reportFile);
});
});
});
4 changes: 4 additions & 0 deletions spec/support/configurations_shared_examples.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,8 @@ itBehavesLikeAllCucumberConfigurations = function itBehavesLikeAllCucumberConfig
it("supplies the AST filter", function() {
expect(configuration.getAstFilter).toBeAFunction();
});

it("supplies the report file location", function() {
expect(configuration.getReportFile).toBeAFunction();
});
}

0 comments on commit 5f011e1

Please sign in to comment.