From 4f03692b6de0d7d082d9e3155b3a6bf33c0765c5 Mon Sep 17 00:00:00 2001 From: Katie Gengler Date: Mon, 29 Jan 2018 00:33:33 -0500 Subject: [PATCH] Allow setting env vars per scenario, independent of defining a command --- README.md | 15 +++-- lib/tasks/try-each.js | 11 +++- lib/utils/result-summary.js | 3 + smoke-test-app/all-commands.sh | 3 + smoke-test-app/fail-if-foo.sh | 5 ++ ...mmy-ember-try-config-different-env-vars.js | 18 ++++++ test/tasks/try-each-test.js | 55 ++++++++++++++++++- 7 files changed, 101 insertions(+), 9 deletions(-) create mode 100755 smoke-test-app/fail-if-foo.sh create mode 100644 test/fixtures/dummy-ember-try-config-different-env-vars.js diff --git a/README.md b/README.md index ab00fd7a..03eb1b6f 100644 --- a/README.md +++ b/README.md @@ -135,10 +135,17 @@ module.exports = function() { }, }, { - name: 'Ember 1.11.0-beta.5', - bower: { - dependencies: { - 'ember': '1.11.0-beta.5' + name: 'Ember 2.11.0', + /* + `env` can be set per scenario, with environment variables to set for the command being run. + This will be merged with process.env + */ + env: { + ENABLE_NEW_DASHBOARD: true + }, + npm: { + devDependencies: { + 'ember-source': '2.11.0' } } }, diff --git a/lib/tasks/try-each.js b/lib/tasks/try-each.js index f13341fa..d74218c3 100644 --- a/lib/tasks/try-each.js +++ b/lib/tasks/try-each.js @@ -62,12 +62,13 @@ module.exports = CoreObject.extend({ scenario: scenario.name, allowedToFail: !!scenario.allowedToFail, dependencyState: scenarioDependencyState, + envState: scenario.env, command: command.join(' '), }; debug('With:\n', runResults); - return task._runCommand({ commandArgs: command, commandOptions: task._commandOptions() }).then((result) => { + return task._runCommand({ commandArgs: command, commandOptions: task._commandOptions(scenario.env) }).then((result) => { if (task._canceling) { return; } runResults.result = result; @@ -112,8 +113,12 @@ module.exports = CoreObject.extend({ return runCommand(this.project.root, options.commandArgs, options.commandOptions); }, - _commandOptions() { - return this.commandOptions; + _commandOptions(env) { + let options = this.commandOptions || {}; + if (env) { + options.env = Object.assign({}, process.env, env); + } + return options; }, _defaultCommandArgs() { diff --git a/lib/utils/result-summary.js b/lib/utils/result-summary.js index ef8d30b8..fd50e8a0 100644 --- a/lib/utils/result-summary.js +++ b/lib/utils/result-summary.js @@ -31,6 +31,9 @@ module.exports = CoreObject.extend({ } task.ui.writeLine(colorAndMessage); task.ui.writeLine(`Command run: ${scenario.command}`); + if (scenario.envState) { + task.ui.writeLine(`with env: ${JSON.stringify(scenario.envState, null, 2)}`); + } task._printDependencyTable(scenario.dependencyState); }); diff --git a/smoke-test-app/all-commands.sh b/smoke-test-app/all-commands.sh index bc3f3c8f..a7002ddc 100755 --- a/smoke-test-app/all-commands.sh +++ b/smoke-test-app/all-commands.sh @@ -53,3 +53,6 @@ ember try:one default --- FOO=5 ./fail-if-no-foo.sh # Custom, compound commands ember try:one default --- 'echo 1 && echo 2' + +# Environment variables from config +ember try:each --config-path='../test/fixtures/dummy-ember-try-config-different-env-vars.js' diff --git a/smoke-test-app/fail-if-foo.sh b/smoke-test-app/fail-if-foo.sh new file mode 100755 index 00000000..dcff3ef6 --- /dev/null +++ b/smoke-test-app/fail-if-foo.sh @@ -0,0 +1,5 @@ +#!/usr/bin/env bash + +if [[ $FOO == "true" ]]; then + exit 1 +fi diff --git a/test/fixtures/dummy-ember-try-config-different-env-vars.js b/test/fixtures/dummy-ember-try-config-different-env-vars.js new file mode 100644 index 00000000..28673ed5 --- /dev/null +++ b/test/fixtures/dummy-ember-try-config-different-env-vars.js @@ -0,0 +1,18 @@ +module.exports = { + useYarn: true, + scenarios: [ + { + name: 'test1', + command: './fail-if-no-foo.sh', + env: { + FOO: true + }, + npm: {} + }, + { + name: 'test2', + command: './fail-if-foo.sh', + npm: {} + } + ] +}; diff --git a/test/tasks/try-each-test.js b/test/tasks/try-each-test.js index 07a15b8e..ff7e8557 100644 --- a/test/tasks/try-each-test.js +++ b/test/tasks/try-each-test.js @@ -204,7 +204,6 @@ describe('tryEach', () => { expect(true).to.equal(false, 'Assertions should run'); }); }); - }); describe('with stubbed dependency manager', () => { @@ -463,7 +462,7 @@ describe('tryEach', () => { }); }); - it('allows passing in of the command to run', function() { + it('allows passing in of the command to run (overrides config file)', function() { // With stubbed dependency manager, timing out is warning for accidentally not using the stub this.timeout(1200); @@ -617,6 +616,58 @@ describe('tryEach', () => { }); }); + describe('configurable env', () => { + it('runs command with env from config', function() { + // With stubbed dependency manager, timing out is warning for accidentally not using the stub + this.timeout(1200); + + let config = { + scenarios: [{ + name: 'first', + command: 'true', + env: { + USE_THIS: 'yep', + }, + }, { + name: 'second', + command: 'true', + }], + }; + + process.env['THIS_SHOULD_EXIST_IN_CMD_OPTS'] = 'true'; + let actualOptions = []; + let mockedRun = generateMockRun('true', (actualCommand, actualArgs, opts) => { + actualOptions.push(opts); + return RSVP.resolve(0); + }); + mockery.registerMock('./run', mockedRun); + + let output = []; + let outputFn = function(log) { + output.push(log); + }; + + let TryEachTask = require('../../lib/tasks/try-each'); + let tryEachTask = new TryEachTask({ + ui: { writeLine: outputFn }, + project: { root: tmpdir }, + config, + dependencyManagerAdapters: [new StubDependencyAdapter()], + _on() {}, + }); + + return tryEachTask.run(config.scenarios, {}).then((exitCode) => { + expect(exitCode).to.equal(0, 'exits 0 when all scenarios succeed'); + + expect(output).to.include('Scenario first: SUCCESS'); + expect(output).to.include('Scenario second: SUCCESS'); + + expect(actualOptions[0].env).to.include({ USE_THIS: 'yep', 'THIS_SHOULD_EXIST_IN_CMD_OPTS': 'true' }); + expect(actualOptions[1].env).to.eql(undefined); + }); + }); + }); + it('sets EMBER_TRY_CURRENT_SCENARIO', function() { // With stubbed dependency manager, timing out is warning for accidentally not using the stub this.timeout(1200);