Skip to content

Commit

Permalink
Initial work for passing arbitrary environment flag
Browse files Browse the repository at this point in the history
  • Loading branch information
Jop de Klein committed Apr 11, 2014
1 parent 4207bd4 commit d625615
Show file tree
Hide file tree
Showing 12 changed files with 105 additions and 9 deletions.
8 changes: 8 additions & 0 deletions features/cli.feature
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,13 @@ Feature: Command line interface
"""

Scenario: run with passing a custom environment variable
When I run `cucumber.js --environment foo`
Then it should pass with:
"""
foo (no not really, this test is pending)
"""

Scenario: display Cucumber version
When I run `cucumber.js --version`
Then I see the version of Cucumber
Expand All @@ -103,3 +110,4 @@ Feature: Command line interface
Scenario: display help (short flag)
When I run `cucumber.js -h`
Then I see the help of Cucumber

3 changes: 3 additions & 0 deletions lib/cucumber/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ var Cli = function(argv) {
summary : prints a summary only, after all\n\
scenarios were executed\n\
\n\
-e, --environment STRING Allows you to specify custom variables.\n\
It will be available on the World context.\n\
\n\
--coffee Display step definition snippets in CoffeeScript.\n\
\n\
-v, --version Display Cucumber.js's version.\n\
Expand Down
9 changes: 9 additions & 0 deletions lib/cucumber/cli/argument_parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ var ArgumentParser = function(argv) {
return format;
},

getEnvironment: function getEnvironment() {
var environment = self.getOptionOrDefault(ArgumentParser.ENVIRONMENT_OPTION_NAME, '');
return environment;
},

getKnownOptionDefinitions: function getKnownOptionDefinitions() {
var definitions = {};
definitions[ArgumentParser.REQUIRE_OPTION_NAME] = [path, Array];
Expand All @@ -70,6 +75,7 @@ var ArgumentParser = function(argv) {
definitions[ArgumentParser.HELP_FLAG_NAME] = Boolean;
definitions[ArgumentParser.VERSION_FLAG_NAME] = Boolean;
definitions[ArgumentParser.COFFEE_SCRIPT_SNIPPETS_FLAG_NAME] = Boolean;
definitions[ArgumentParser.ENVIRONMENT_OPTION_NAME] = String;
return definitions;
},

Expand All @@ -78,6 +84,7 @@ var ArgumentParser = function(argv) {
definitions[ArgumentParser.REQUIRE_OPTION_SHORT_NAME] = [ArgumentParser.LONG_OPTION_PREFIX + ArgumentParser.REQUIRE_OPTION_NAME];
definitions[ArgumentParser.FORMAT_OPTION_SHORT_NAME] = [ArgumentParser.LONG_OPTION_PREFIX + ArgumentParser.FORMAT_OPTION_NAME];
definitions[ArgumentParser.HELP_FLAG_SHORT_NAME] = [ArgumentParser.LONG_OPTION_PREFIX + ArgumentParser.HELP_FLAG_NAME];
definitions[ArgumentParser.ENVIRONMENT_OPTION_SHORT_NAME] = [ArgumentParser.LONG_OPTION_PREFIX + ArgumentParser.ENVIRONMENT_OPTION_NAME];
return definitions;
},

Expand Down Expand Up @@ -130,6 +137,8 @@ ArgumentParser.VERSION_FLAG_NAME = "version";
ArgumentParser.DEFAULT_VERSION_FLAG_VALUE = false;
ArgumentParser.COFFEE_SCRIPT_SNIPPETS_FLAG_NAME = "coffee";
ArgumentParser.DEFAULT_COFFEE_SCRIPT_SNIPPETS_FLAG_VALUE = false;
ArgumentParser.ENVIRONMENT_OPTION_NAME = "environment";
ArgumentParser.ENVIRONMENT_OPTION_SHORT_NAME = "e";
ArgumentParser.FeaturePathExpander = require('./argument_parser/feature_path_expander');
ArgumentParser.PathExpander = require('./argument_parser/path_expander');
ArgumentParser.SupportCodePathExpander = require('./argument_parser/support_code_path_expander');
Expand Down
5 changes: 5 additions & 0 deletions lib/cucumber/cli/configuration.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ var Configuration = function(argv) {
return rules;
},

getEnvironment: function getEnvironment() {
var environment = argumentParser.getEnvironment();
return environment;
},

isHelpRequested: function isHelpRequested() {
var isHelpRequested = argumentParser.isHelpRequested();
return isHelpRequested;
Expand Down
8 changes: 7 additions & 1 deletion lib/cucumber/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,13 @@ var Runtime = function(configuration) {
throw new Error(Runtime.START_MISSING_CALLBACK_ERROR);
var features = self.getFeatures();
var supportCodeLibrary = self.getSupportCodeLibrary();
var astTreeWalker = Runtime.AstTreeWalker(features, supportCodeLibrary, listeners);
var environment;
// Currently the environment can only be set via CLI
if (typeof configuration.getEnvironment === 'function') {
environment = configuration.getEnvironment(supportCodeLibrary);
}
var astTreeWalker = Runtime.AstTreeWalker(
features, supportCodeLibrary, listeners, environment);
astTreeWalker.walk(callback);
},

Expand Down
4 changes: 2 additions & 2 deletions lib/cucumber/runtime/ast_tree_walker.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var AstTreeWalker = function(features, supportCodeLibrary, listeners) {
var AstTreeWalker = function(features, supportCodeLibrary, listeners, environment) {
var Cucumber = require('../../cucumber');

var world;
Expand Down Expand Up @@ -55,7 +55,7 @@ var AstTreeWalker = function(features, supportCodeLibrary, listeners) {
hookedUpScenarioVisit,
callback
);
});
}, environment);
},

visitRow: function visitRow(row, scenario,callback){
Expand Down
4 changes: 2 additions & 2 deletions lib/cucumber/support_code/library.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,12 @@ var Library = function(supportCodeDefinition) {
return listeners;
},

instantiateNewWorld: function instantiateNewWorld(callback) {
instantiateNewWorld: function instantiateNewWorld(callback, environment) {
var world = new worldConstructor(function (explicitWorld) {
process.nextTick(function () { // release the constructor
callback(explicitWorld || world);
});
});
}, environment);
}
};

Expand Down
13 changes: 13 additions & 0 deletions spec/cucumber/cli/argument_parser_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ describe("Cucumber.Cli.ArgumentParser", function () {
var knownOptionDefinitions = argumentParser.getKnownOptionDefinitions();
expect(knownOptionDefinitions[Cucumber.Cli.ArgumentParser.VERSION_FLAG_NAME]).toEqual(Boolean);
});

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

describe("getShortenedOptionDefinitions()", function () {
Expand Down Expand Up @@ -110,6 +115,14 @@ describe("Cucumber.Cli.ArgumentParser", function () {
expect(shortenedOptionDefinitions[aliasName]).toEqual(aliasValue);
});

it("defines an alias to --environment as -e", function () {
var optionName = Cucumber.Cli.ArgumentParser.LONG_OPTION_PREFIX + Cucumber.Cli.ArgumentParser.ENVIRONMENT_OPTION_NAME;
var aliasName = Cucumber.Cli.ArgumentParser.ENVIRONMENT_OPTION_SHORT_NAME;
var aliasValue = [optionName];
var shortenedOptionDefinitions = argumentParser.getShortenedOptionDefinitions();
expect(shortenedOptionDefinitions[aliasName]).toEqual(aliasValue);
});

});

describe("getFeatureFilePaths()", function () {
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 @@ -258,6 +258,23 @@ describe("Cucumber.Cli.Configuration", function () {
});
});

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

it("asks the argument parser what the environment variable was", function () {
configuration.getEnvironment();
expect(argumentParser.getEnvironment).toHaveBeenCalled();
});

it("returns the answer from the argument parser", function () {
var getEnvironment = createSpy("foo");
argumentParser.getEnvironment.andReturn(getEnvironment);
expect(configuration.getEnvironment()).toBe(getEnvironment);
});
});

describe("isHelpRequired()", function () {
beforeEach(function () {
spyOnStub(argumentParser, 'isHelpRequested');
Expand Down
15 changes: 14 additions & 1 deletion spec/cucumber/cli_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe("Cucumber.Cli", function() {
var configuration, callback;

beforeEach(function() {
configuration = createSpyWithStubs("CLI configuration", {isVersionRequested: false, isHelpRequested: false});
configuration = createSpyWithStubs("CLI configuration", {isVersionRequested: false, isHelpRequested: false, environment: 'foo'});
callback = createSpy("callback");
spyOn(Cucumber.Cli, 'Configuration').andReturn(configuration);
spyOn(cli, 'displayHelp');
Expand Down Expand Up @@ -104,6 +104,19 @@ describe("Cucumber.Cli", function() {
expect(cli.displayVersion).not.toHaveBeenCalledWith(callback);
});
});

/*
describe("when environment is specified", function() {
beforeEach(function() {
configuration.getEnvironment.andReturn('foo');
});
it("runs the suite with environment", function () {
cli.run(callback);
expect(cli.runSuiteWithConfiguration).toHaveBeenCalled();
});
});
*/
});
});

Expand Down
3 changes: 2 additions & 1 deletion spec/cucumber/runtime_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ describe("Cucumber.Runtime", function() {

it("creates a new AST tree walker", function() {
runtime.start(callback);
expect(Cucumber.Runtime.AstTreeWalker).toHaveBeenCalledWith(features, supportCodeLibrary, listeners);
expect(Cucumber.Runtime.AstTreeWalker).toHaveBeenCalledWith(
features, supportCodeLibrary, listeners, undefined);
});

it("tells the AST tree walker to walk", function() {
Expand Down
25 changes: 23 additions & 2 deletions spec/cucumber/support_code/library_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -398,12 +398,14 @@ describe("Cucumber.SupportCode.Library", function() {
expect(worldInstance.constructor).toBe(worldConstructor);
});

describe("world constructor callback", function() {
describe("world constructor callback without environment", function() {
var worldConstructorCompletionCallback;
var environment;

beforeEach(function() {
library.instantiateNewWorld(callback);
worldConstructorCompletionCallback = worldConstructor.mostRecentCall.args[0];
environment = worldConstructor.mostRecentCall.args[1]
spyOn(process, 'nextTick');
});

Expand All @@ -412,6 +414,10 @@ describe("Cucumber.SupportCode.Library", function() {
expect(process.nextTick).toHaveBeenCalledWithAFunctionAsNthParameter(1);
});

it("doesn't forward an environment variable to the world", function () {
expect(environment).toEqual(undefined);
});

describe("next tick registered function", function() {
var nextTickFunction;

Expand Down Expand Up @@ -444,6 +450,21 @@ describe("Cucumber.SupportCode.Library", function() {

});
});

describe("world constructor callback with environment", function() {
var environment;

beforeEach(function() {
library.instantiateNewWorld(callback, 'FOO');
worldConstructorCompletionCallback = worldConstructor.mostRecentCall.args[0];
environment = worldConstructor.mostRecentCall.args[1]
spyOn(process, 'nextTick');
});

it("forwards an optional environment variable to the world", function () {
expect(environment).toEqual('FOO');
});
});
});

describe("when the default World constructor is replaced by a custom one", function() {
Expand Down Expand Up @@ -471,4 +492,4 @@ describe("Cucumber.SupportCode.Library", function() {
});
});
});
});
});

0 comments on commit d625615

Please sign in to comment.