Skip to content

Commit

Permalink
Merge pull request #198 from PaulTaykalo/feature/inject-environment-v…
Browse files Browse the repository at this point in the history
…ariables

ios-sim 4.x - setenv is not supported fix
  • Loading branch information
shazron authored Jun 10, 2017
2 parents 25a6142 + 314b611 commit b0e813a
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 20 deletions.
26 changes: 26 additions & 0 deletions spec/lib.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
specific language governing permissions and limitations
under the License.
*/
var lib = require('../src/lib');

describe('lib end-to-end', function() {

Expand All @@ -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) {
// });
});
2 changes: 1 addition & 1 deletion src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ function cli(inputArgs) {
'use-gdb': Boolean,
'uuid': String,
'env': String,
'setenv': String,
'setenv': Array,
'stdout': path,
'stderr': path,
'timeout': Number,
Expand Down
2 changes: 1 addition & 1 deletion src/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
82 changes: 64 additions & 18 deletions src/lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,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;
}

// replace hyphens in iPad Pro name which differ in 'Device Types' and 'Devices'
function filterDeviceName(deviceName) {
// replace hyphens in iPad Pro name which differ in 'Device Types' and 'Devices'
Expand Down Expand Up @@ -305,7 +343,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;
Expand Down Expand Up @@ -334,22 +372,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);
}
});
});
},

Expand Down Expand Up @@ -399,7 +442,10 @@ var lib = {
}

simctl.extensions.start(device.id);
}
},

_parseEnvironmentVariables: parseEnvironmentVariables

};

module.exports = lib;

0 comments on commit b0e813a

Please sign in to comment.