Skip to content

Ensure stopApplication works correctly #90

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

Merged
merged 2 commits into from
May 9, 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
3 changes: 2 additions & 1 deletion lib/declarations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ interface IDevice {

interface ISimctl {
launch(deviceId: string, applicationIdentifier: string): string;
terminate(deviceId: string, appIdentifier: string): string;
install(deviceId: string, applicationPath: string): void;
uninstall(deviceId: string, applicationIdentifier: string, opts?: any): void;
notifyPost(deviceId: string, notification: string): void;
Expand All @@ -49,7 +50,7 @@ interface ISimulator extends INameGetter {
installApplication(deviceId: string, applicationPath: string): void;
uninstallApplication(deviceId: string, appIdentifier: string): void;
startApplication(deviceId: string, appIdentifier: string): string;
stopApplication(deviceId: string, appIdentifier: string): string;
stopApplication(deviceId: string, appIdentifier: string, bundleExecutable: string): string;
printDeviceLog(deviceId: string, launchResult?: string): any;
getDeviceLogProcess(deviceId: string): any;
startSimulator(): void;
Expand Down
25 changes: 21 additions & 4 deletions lib/iphone-simulator-xcode-simctl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,26 @@ export class XCodeSimctlSimulator extends IPhoneSimulatorNameGetter implements I
return this.simctl.launch(deviceId, appIdentifier);
}

public stopApplication(deviceId: string, cfBundleExecutable: string): string {
public stopApplication(deviceId: string, appIdentifier: string, bundleExecutable: string): string {
try {
return childProcess.execSync(`killall ${cfBundleExecutable}`, { skipError: true });
let xcodeMajorVersion: number = null;
try {
const xcodeVersion = xcode.getXcodeVersionData();
xcodeMajorVersion = +xcodeVersion.major;
} catch(err) {
// Ignore the error.
}

if (xcodeMajorVersion && xcodeMajorVersion < 8) {
// Xcode 7.x does not have support for `xcrun simctl terminate` command
const resultOfKill = childProcess.execSync(`killall ${bundleExecutable}`, { skipError: true });
// killall command does not terminate the processes immediately and we have to wait a little bit,
// just to ensure all related processes and services are dead.
utils.sleep(0.5);
return resultOfKill;
} else {
return this.simctl.terminate(deviceId, appIdentifier);
}
} catch (e) {
}
}
Expand Down Expand Up @@ -157,11 +174,11 @@ export class XCodeSimctlSimulator extends IPhoneSimulatorNameGetter implements I
// startSimulaltor doesn't always finish immediately, and the subsequent
// install fails since the simulator is not running.
// Give it some time to start before we attempt installing.
utils.sleep(1000);
utils.sleep(1);
}
}

private killSimulator(): void {
childProcess.execSync("pkill -9 -f Simulator");
}
}
}
4 changes: 4 additions & 0 deletions lib/simctl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ export class Simctl implements ISimctl {
return result;
}

public terminate(deviceId: string, appIdentifier: string): string {
return this.simctlExec("terminate", [deviceId, appIdentifier]);
}

public install(deviceId: string, applicationPath: string): void {
return this.simctlExec("install", [deviceId, applicationPath]);
}
Expand Down
12 changes: 5 additions & 7 deletions lib/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import * as childProcess from "./child-process";

export function stringify(arr: string[], delimiter?: string): string {
delimiter = delimiter || ", ";
return arr.join(delimiter);
Expand All @@ -8,10 +10,6 @@ export function getCurrentEpochTime(): number {
return dateTime.getTime();
}

export function sleep(ms: number): void {
let startTime = getCurrentEpochTime();
let currentTime = getCurrentEpochTime();
while ((currentTime - startTime) < ms) {
currentTime = getCurrentEpochTime();
}
}
export function sleep(seconds: number): void {
childProcess.execSync(`sleep ${seconds}`);
}
4 changes: 2 additions & 2 deletions lib/xcode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ export function getPathFromXcodeSelect(): string {

export function getXcodeVersionData(): IXcodeVersionData {
let rawData = childProcess.execSync("xcodebuild -version");
let lines = rawData.split("\n");
let lines = rawData.toString().split("\n");
let parts = lines[0].split(" ")[1].split(".");
return {
major: parts[0],
minor: parts[1],
build: lines[1].split("Build version ")[1]
}
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ios-sim-portable",
"version": "2.0.2",
"version": "3.0.0",
"description": "",
"main": "./lib/ios-sim.js",
"scripts": {
Expand Down