diff --git a/features/cli.feature b/features/cli.feature index 6c5a353d8..6d9e93995 100644 --- a/features/cli.feature +++ b/features/cli.feature @@ -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 @@ -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 + diff --git a/lib/cucumber/cli.js b/lib/cucumber/cli.js index ee0555add..e445a0b4a 100644 --- a/lib/cucumber/cli.js +++ b/lib/cucumber/cli.js @@ -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\ diff --git a/lib/cucumber/cli/argument_parser.js b/lib/cucumber/cli/argument_parser.js index a248f05ee..bab1c0b0f 100644 --- a/lib/cucumber/cli/argument_parser.js +++ b/lib/cucumber/cli/argument_parser.js @@ -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]; @@ -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; }, @@ -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; }, @@ -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'); diff --git a/lib/cucumber/cli/configuration.js b/lib/cucumber/cli/configuration.js index 30b6c523e..6037bb0cf 100644 --- a/lib/cucumber/cli/configuration.js +++ b/lib/cucumber/cli/configuration.js @@ -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; diff --git a/lib/cucumber/runtime.js b/lib/cucumber/runtime.js index 4b8d1371b..dff07fe6f 100644 --- a/lib/cucumber/runtime.js +++ b/lib/cucumber/runtime.js @@ -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); }, diff --git a/lib/cucumber/runtime/ast_tree_walker.js b/lib/cucumber/runtime/ast_tree_walker.js index 7291e9809..ee478953b 100644 --- a/lib/cucumber/runtime/ast_tree_walker.js +++ b/lib/cucumber/runtime/ast_tree_walker.js @@ -1,4 +1,4 @@ -var AstTreeWalker = function(features, supportCodeLibrary, listeners) { +var AstTreeWalker = function(features, supportCodeLibrary, listeners, environment) { var Cucumber = require('../../cucumber'); var world; @@ -55,7 +55,7 @@ var AstTreeWalker = function(features, supportCodeLibrary, listeners) { hookedUpScenarioVisit, callback ); - }); + }, environment); }, visitRow: function visitRow(row, scenario,callback){ diff --git a/lib/cucumber/support_code/library.js b/lib/cucumber/support_code/library.js index fd44e8828..3bf0bf94d 100644 --- a/lib/cucumber/support_code/library.js +++ b/lib/cucumber/support_code/library.js @@ -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); } }; diff --git a/spec/cucumber/cli/argument_parser_spec.js b/spec/cucumber/cli/argument_parser_spec.js index 34bdc8380..bee7f9892 100644 --- a/spec/cucumber/cli/argument_parser_spec.js +++ b/spec/cucumber/cli/argument_parser_spec.js @@ -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 () { @@ -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 () { diff --git a/spec/cucumber/cli/configuration_spec.js b/spec/cucumber/cli/configuration_spec.js index a3f85f024..cac6e7853 100644 --- a/spec/cucumber/cli/configuration_spec.js +++ b/spec/cucumber/cli/configuration_spec.js @@ -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'); diff --git a/spec/cucumber/cli_spec.js b/spec/cucumber/cli_spec.js index b6aa258b0..fbea25976 100644 --- a/spec/cucumber/cli_spec.js +++ b/spec/cucumber/cli_spec.js @@ -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'); @@ -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(); + }); + }); + */ }); }); diff --git a/spec/cucumber/runtime_spec.js b/spec/cucumber/runtime_spec.js index 5d81feac8..d92ebcb48 100644 --- a/spec/cucumber/runtime_spec.js +++ b/spec/cucumber/runtime_spec.js @@ -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() { diff --git a/spec/cucumber/support_code/library_spec.js b/spec/cucumber/support_code/library_spec.js index 6a79854f1..ab881541f 100644 --- a/spec/cucumber/support_code/library_spec.js +++ b/spec/cucumber/support_code/library_spec.js @@ -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'); }); @@ -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; @@ -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() { @@ -471,4 +492,4 @@ describe("Cucumber.SupportCode.Library", function() { }); }); }); -}); \ No newline at end of file +});