Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switched to booting the device directly (related to https://github.co… #184

Merged
merged 3 commits into from
Jul 4, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 36 additions & 6 deletions detox/src/devices/Fbsimctl.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const fs = require('fs');
const _ = require('lodash');
const log = require('npmlog');
const exec = require('../utils/exec');
const retry = require('../utils/retry');

// FBSimulatorControl command line docs
// https://github.com/facebook/FBSimulatorControl/issues/250
Expand Down Expand Up @@ -39,13 +40,42 @@ class Fbsimctl {
}

async boot(udid) {
const statusLogs = {
trying: `trying to boot device...`,
successful: `device ${udid} booted`
};
let initialState;
await retry({retries: 10, interval: 1000}, async() => {
const initialStateCmdResult = await this._execFbsimctlCommand({args: `${udid} list`}, undefined, 1);
initialState = _.get(initialStateCmdResult, 'stdout', '') === '' ? undefined :
_.get(JSON.parse(_.get(initialStateCmdResult, 'stdout')), 'subject.state');
if(initialState === undefined) {
log.info(`Couldn't get the state of ${udid}`);
throw `Couldn't get the state of the device`;
}
if(initialState === 'Shutting Down') {
log.info(`Waiting until the device ${udid} shuts down`);
throw `The device is in 'Shutting Down' state`;
}
});

if(initialState === 'Booted') {
log.info(`The device ${udid} is already booted`);
return;
}

if(initialState === 'Booting') {
log.info(`The device ${udid} is already booting`);
} else {
const launchBin = "bash -c '`xcode-select -p`/Applications/Simulator.app/Contents/MacOS/Simulator " +
`--args -CurrentDeviceUDID ${udid} -ConnectHardwareKeyboard 0 ` +
"-DeviceSetPath ~/Library/Developer/CoreSimulator/Devices > /dev/null 2>&1 < /dev/null &'";
await exec.execWithRetriesAndLogs(launchBin, undefined, {
trying: `launching the simulator ${udid}...`,
successful: 'the device launch initiated'
}, 1);
}

const options = {args: `--state=shutdown --state=shutting-down ${udid} boot`};
return await this._execFbsimctlCommand(options, statusLogs);
return await this._execFbsimctlCommand({args: `--state booted ${udid} list`}, {
trying: `waiting for the device to boot...`,
successful: `device ${udid} booted`
});
}

async install(udid, absPath) {
Expand Down
44 changes: 41 additions & 3 deletions detox/src/devices/Fbsimctl.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ describe('Fbsimctl', () => {
fs = require('fs');
jest.mock('../utils/exec');
exec = require('../utils/exec').execWithRetriesAndLogs;

jest.setMock('../utils/retry', async (options, func) => {
return await func(1);
});
Fbsimctl = require('./Fbsimctl');
fbsimctl = new Fbsimctl();
});
Expand Down Expand Up @@ -54,8 +56,44 @@ describe('Fbsimctl', () => {
}
});

it(`boot() - is triggering fbsimctl boot`, async() => {
await validateFbsimctlisCalledOn(fbsimctl, async () => fbsimctl.boot(simUdid));
it(`boot() - when shutting down, should wait for the device`, async() => {
fbsimctl._execFbsimctlCommand = jest.fn(() => ({stdout: `{"subject": {"state": "Shutting Down"}}`}));

try {
await fbsimctl.boot(simUdid);
fail('should throw');
} catch (ex) {
expect(ex).toBe("The device is in 'Shutting Down' state");
}
});

it(`boot() - when state is undefined, should wait for the device`, async() => {
fbsimctl._execFbsimctlCommand = jest.fn(() => ({}));

try {
await fbsimctl.boot(simUdid);
fail('should throw');
} catch (ex) {
expect(ex).toBe("Couldn't get the state of the device");
}
});

it(`boot() - when booted, should not wait for the device to boot`, async() => {
fbsimctl._execFbsimctlCommand = jest.fn(() => ({stdout: `{"subject": {"state": "Booted"}}`}));
await fbsimctl.boot(simUdid);
expect(exec).toHaveBeenCalledTimes(0);
});

it(`boot() - when booting, should not call exec`, async() => {
fbsimctl._execFbsimctlCommand = jest.fn(() => ({stdout: `{"subject": {"state": "Booting"}}`}));
await fbsimctl.boot(simUdid);
expect(exec).toHaveBeenCalledTimes(0);
});

it(`boot() - when shutdown, should call exec`, async() => {
fbsimctl._execFbsimctlCommand = jest.fn(() => ({stdout: `{"subject": {"state": "Shutdown"}}`}));
await fbsimctl.boot(simUdid);
expect(exec).toHaveBeenCalledTimes(1);
});

it(`install() - is triggering fbsimctl install`, async() => {
Expand Down