From c61fc53f5fce9064670f7425882e988e419dded1 Mon Sep 17 00:00:00 2001 From: Paul Taykalo Date: Wed, 19 Oct 2016 01:16:05 +0300 Subject: [PATCH] Support --setenv parameter --- spec/lib.spec.js | 26 +++++++++++++++ src/cli.js | 2 +- src/commands.js | 2 +- src/lib.js | 82 +++++++++++++++++++++++++++++++++++++----------- 4 files changed, 92 insertions(+), 20 deletions(-) diff --git a/spec/lib.spec.js b/spec/lib.spec.js index d832717..3b4ba2f 100644 --- a/spec/lib.spec.js +++ b/spec/lib.spec.js @@ -16,6 +16,7 @@ specific language governing permissions and limitations under the License. */ +var lib = require('../src/lib'); describe('lib end-to-end', function() { @@ -25,6 +26,31 @@ describe('lib end-to-end', function() { afterEach(function() { }); + describe('when parsing env variables', function() { + it("should return empty map on null value", function() { + expect(lib._parseEnvironmentVariables(null)).toEqual({}); + }); + it("should return empty map on undefined value", function() { + expect(lib._parseEnvironmentVariables(undefined)).toEqual({}); + }); + describe('without simctl fix', function() { + it("should return valid map for valid env variable", function() { + expect(lib._parseEnvironmentVariables(["KEY=VALUE"], false)).toEqual({"KEY":"VALUE"}); + }); + }); + describe('with simctl fix', function() { + it("should add SIMCTL_CHILD_ prefix to all keys", function() { + expect(lib._parseEnvironmentVariables(["KEY=VALUE", "KEY2=VALUE2"], true)) + .toEqual( + { + "SIMCTL_CHILD_KEY":"VALUE", + "SIMCTL_CHILD_KEY2":"VALUE2" + } + ); + }); + }); + }) + // it('', function(done) { // }); }); \ No newline at end of file diff --git a/src/cli.js b/src/cli.js index ba15ed1..f648dc6 100644 --- a/src/cli.js +++ b/src/cli.js @@ -57,7 +57,7 @@ function cli(inputArgs) { 'use-gdb': Boolean, 'uuid': String, 'env': String, - 'setenv': String, + 'setenv': Array, 'stdout': path, 'stderr': path, 'timeout': Number, diff --git a/src/commands.js b/src/commands.js index e801223..4c21991 100644 --- a/src/commands.js +++ b/src/commands.js @@ -57,7 +57,7 @@ var command_lib = { app_path = args.argv.remain[1]; - lib.launch(app_path, args.devicetypeid, args.log, args.exit, args.args); + lib.launch(app_path, args.devicetypeid, args.log, args.exit, args.setenv, args.args); }, install: function(args) { diff --git a/src/lib.js b/src/lib.js index 53c537a..772b367 100644 --- a/src/lib.js +++ b/src/lib.js @@ -223,6 +223,44 @@ function getDeviceFromDeviceTypeId(devicetypeid) { return ret_obj; } +// Parses array of KEY=Value strings into map of strings +// If fixsymctl == true, updates variables for correct usage with simctl +function parseEnvironmentVariables(envVariables, fixsymctl) { + envVariables = envVariables || []; + fixsymctl = typeof fixsymctl != 'undefined' ? fixsymctl : true; + + var envMap = {}; + envVariables.forEach(function(variable) { + var envPair = variable.split('=', 2); + if (envPair.length == 2) { + var key = envPair[0]; + var value = envPair[1]; + if (fixsymctl) { + key = 'SIMCTL_CHILD_' + key; + } + envMap[ key ] = value; + } + }); + return envMap; +} + +// Injects specified environt variables to the process and then runs action +// returns environment variables back to original state after action completes +function withInjectedEnvironmentVariablesToProcess(process, envVariables, action) { + var oldVariables = util._extend({}, process.env); + + // Inject additional environment variables to process + for (var key in envVariables) { + var value = envVariables[key]; + process.env[key] = value; + } + + action(); + + // restore old envs + process.env = oldVariables; +} + var lib = { init: function() { @@ -313,7 +351,7 @@ var lib = { }, //jscs:enable disallowUnusedParams - launch: function(app_path, devicetypeid, log, exit, argv) { + launch: function(app_path, devicetypeid, log, exit, setenv, argv) { var wait_for_debugger = false; var info_plist_path; var app_identifier; @@ -342,22 +380,27 @@ var lib = { } argv = argv || []; - - // get the deviceid from --devicetypeid - // --devicetypeid is a string in the form "devicetype, runtime_version" (optional: runtime_version) - var device = getDeviceFromDeviceTypeId(devicetypeid); - - // so now we have the deviceid, we can proceed - simctl.extensions.start(device.id); - simctl.install(device.id, app_path); - simctl.launch(wait_for_debugger, device.id, app_identifier, argv); - simctl.extensions.log(device.id, log); - if (log) { - console.log(util.format('logPath: %s', path.resolve(log))); - } - if (exit) { - process.exit(0); - } + setenv = setenv || []; + + var environmentVariables = parseEnvironmentVariables(setenv); + + withInjectedEnvironmentVariablesToProcess(process, environmentVariables, function() { + // get the deviceid from --devicetypeid + // --devicetypeid is a string in the form "devicetype, runtime_version" (optional: runtime_version) + var device = getDeviceFromDeviceTypeId(devicetypeid); + + // so now we have the deviceid, we can proceed + simctl.extensions.start(device.id); + simctl.install(device.id, app_path); + simctl.launch(wait_for_debugger, device.id, app_identifier, argv); + simctl.extensions.log(device.id, log); + if (log) { + console.log(util.format('logPath: %s', path.resolve(log))); + } + if (exit) { + process.exit(0); + } + }); }); }, @@ -407,7 +450,10 @@ var lib = { } simctl.extensions.start(device.id); - } + }, + + _parseEnvironmentVariables: parseEnvironmentVariables + }; module.exports = lib;