Skip to content

Commit

Permalink
Merge pull request #181 from ember-cli/kg-allow-env-from-config
Browse files Browse the repository at this point in the history
Allow setting env vars per scenario, independent of defining a command
  • Loading branch information
kategengler authored Jan 29, 2018
2 parents 4bf2f69 + 4f03692 commit 13c8962
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 9 deletions.
15 changes: 11 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
}
},
Expand Down
11 changes: 8 additions & 3 deletions lib/tasks/try-each.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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() {
Expand Down
3 changes: 3 additions & 0 deletions lib/utils/result-summary.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});

Expand Down
3 changes: 3 additions & 0 deletions smoke-test-app/all-commands.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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'
5 changes: 5 additions & 0 deletions smoke-test-app/fail-if-foo.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env bash

if [[ $FOO == "true" ]]; then
exit 1
fi
18 changes: 18 additions & 0 deletions test/fixtures/dummy-ember-try-config-different-env-vars.js
Original file line number Diff line number Diff line change
@@ -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: {}
}
]
};
55 changes: 53 additions & 2 deletions test/tasks/try-each-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,6 @@ describe('tryEach', () => {
expect(true).to.equal(false, 'Assertions should run');
});
});

});

describe('with stubbed dependency manager', () => {
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 13c8962

Please sign in to comment.