Skip to content

Commit b0e813a

Browse files
authored
Merge pull request #198 from PaulTaykalo/feature/inject-environment-variables
ios-sim 4.x - setenv is not supported fix
2 parents 25a6142 + 314b611 commit b0e813a

File tree

4 files changed

+92
-20
lines changed

4 files changed

+92
-20
lines changed

spec/lib.spec.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
specific language governing permissions and limitations
1717
under the License.
1818
*/
19+
var lib = require('../src/lib');
1920

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

@@ -25,6 +26,31 @@ describe('lib end-to-end', function() {
2526
afterEach(function() {
2627
});
2728

29+
describe('when parsing env variables', function() {
30+
it("should return empty map on null value", function() {
31+
expect(lib._parseEnvironmentVariables(null)).toEqual({});
32+
});
33+
it("should return empty map on undefined value", function() {
34+
expect(lib._parseEnvironmentVariables(undefined)).toEqual({});
35+
});
36+
describe('without simctl fix', function() {
37+
it("should return valid map for valid env variable", function() {
38+
expect(lib._parseEnvironmentVariables(["KEY=VALUE"], false)).toEqual({"KEY":"VALUE"});
39+
});
40+
});
41+
describe('with simctl fix', function() {
42+
it("should add SIMCTL_CHILD_ prefix to all keys", function() {
43+
expect(lib._parseEnvironmentVariables(["KEY=VALUE", "KEY2=VALUE2"], true))
44+
.toEqual(
45+
{
46+
"SIMCTL_CHILD_KEY":"VALUE",
47+
"SIMCTL_CHILD_KEY2":"VALUE2"
48+
}
49+
);
50+
});
51+
});
52+
})
53+
2854
// it('', function(done) {
2955
// });
3056
});

src/cli.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ function cli(inputArgs) {
5757
'use-gdb': Boolean,
5858
'uuid': String,
5959
'env': String,
60-
'setenv': String,
60+
'setenv': Array,
6161
'stdout': path,
6262
'stderr': path,
6363
'timeout': Number,

src/commands.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ var command_lib = {
5757

5858
app_path = args.argv.remain[1];
5959

60-
lib.launch(app_path, args.devicetypeid, args.log, args.exit, args.args);
60+
lib.launch(app_path, args.devicetypeid, args.log, args.exit, args.setenv, args.args);
6161
},
6262

6363
install: function(args) {

src/lib.js

Lines changed: 64 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,44 @@ function getDeviceFromDeviceTypeId(devicetypeid) {
218218
return ret_obj;
219219
}
220220

221+
// Parses array of KEY=Value strings into map of strings
222+
// If fixsymctl == true, updates variables for correct usage with simctl
223+
function parseEnvironmentVariables(envVariables, fixsymctl) {
224+
envVariables = envVariables || [];
225+
fixsymctl = typeof fixsymctl != 'undefined' ? fixsymctl : true;
226+
227+
var envMap = {};
228+
envVariables.forEach(function(variable) {
229+
var envPair = variable.split('=', 2);
230+
if (envPair.length == 2) {
231+
var key = envPair[0];
232+
var value = envPair[1];
233+
if (fixsymctl) {
234+
key = 'SIMCTL_CHILD_' + key;
235+
}
236+
envMap[ key ] = value;
237+
}
238+
});
239+
return envMap;
240+
}
241+
242+
// Injects specified environt variables to the process and then runs action
243+
// returns environment variables back to original state after action completes
244+
function withInjectedEnvironmentVariablesToProcess(process, envVariables, action) {
245+
var oldVariables = util._extend({}, process.env);
246+
247+
// Inject additional environment variables to process
248+
for (var key in envVariables) {
249+
var value = envVariables[key];
250+
process.env[key] = value;
251+
}
252+
253+
action();
254+
255+
// restore old envs
256+
process.env = oldVariables;
257+
}
258+
221259
// replace hyphens in iPad Pro name which differ in 'Device Types' and 'Devices'
222260
function filterDeviceName(deviceName) {
223261
// replace hyphens in iPad Pro name which differ in 'Device Types' and 'Devices'
@@ -305,7 +343,7 @@ var lib = {
305343
},
306344
//jscs:enable disallowUnusedParams
307345

308-
launch: function(app_path, devicetypeid, log, exit, argv) {
346+
launch: function(app_path, devicetypeid, log, exit, setenv, argv) {
309347
var wait_for_debugger = false;
310348
var info_plist_path;
311349
var app_identifier;
@@ -334,22 +372,27 @@ var lib = {
334372
}
335373

336374
argv = argv || [];
337-
338-
// get the deviceid from --devicetypeid
339-
// --devicetypeid is a string in the form "devicetype, runtime_version" (optional: runtime_version)
340-
var device = getDeviceFromDeviceTypeId(devicetypeid);
341-
342-
// so now we have the deviceid, we can proceed
343-
simctl.extensions.start(device.id);
344-
simctl.install(device.id, app_path);
345-
simctl.launch(wait_for_debugger, device.id, app_identifier, argv);
346-
simctl.extensions.log(device.id, log);
347-
if (log) {
348-
console.log(util.format('logPath: %s', path.resolve(log)));
349-
}
350-
if (exit) {
351-
process.exit(0);
352-
}
375+
setenv = setenv || [];
376+
377+
var environmentVariables = parseEnvironmentVariables(setenv);
378+
379+
withInjectedEnvironmentVariablesToProcess(process, environmentVariables, function() {
380+
// get the deviceid from --devicetypeid
381+
// --devicetypeid is a string in the form "devicetype, runtime_version" (optional: runtime_version)
382+
var device = getDeviceFromDeviceTypeId(devicetypeid);
383+
384+
// so now we have the deviceid, we can proceed
385+
simctl.extensions.start(device.id);
386+
simctl.install(device.id, app_path);
387+
simctl.launch(wait_for_debugger, device.id, app_identifier, argv);
388+
simctl.extensions.log(device.id, log);
389+
if (log) {
390+
console.log(util.format('logPath: %s', path.resolve(log)));
391+
}
392+
if (exit) {
393+
process.exit(0);
394+
}
395+
});
353396
});
354397
},
355398

@@ -399,7 +442,10 @@ var lib = {
399442
}
400443

401444
simctl.extensions.start(device.id);
402-
}
445+
},
446+
447+
_parseEnvironmentVariables: parseEnvironmentVariables
448+
403449
};
404450

405451
module.exports = lib;

0 commit comments

Comments
 (0)