Skip to content

Commit

Permalink
breaking(test): drop shell.exec in killsim
Browse files Browse the repository at this point in the history
* Replace pgrep command with find-process
* Replace shell.exec with superspawn.spawn
  • Loading branch information
erisu committed May 20, 2020
1 parent b0f383f commit a0dc993
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 20 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"license": "Apache-2.0",
"devDependencies": {
"@cordova/eslint-config": "^2.0.0",
"find-process": "^1.4.3",
"jasmine": "^3.5.0",
"nyc": "^15.0.1",
"rewire": "^5.0.0",
Expand Down
50 changes: 30 additions & 20 deletions tests/scripts/killsim.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,37 @@
under the License.
*/

const shell = require('shelljs');
const { events } = require('cordova-common');
const path = require('path');
const findProcess = require('find-process');
const { superspawn } = require('cordova-common');

function killSimulator (processName) {
let result;
let return_code = 0;
// check iOS Simulator if running
const command = `pgrep -x "${processName}" > /dev/null`;
return_code = shell.exec(command).code;

// if iOS Simulator is running, kill it
if (return_code === 0) { // found
events.emit('log', `iOS Simulator is running as ("${processName}"), we're going to kill it.`);
result = shell.exec(`killall "${processName}"`);
if (result.code !== 0) {
events.emit('log', `Failed to kill process: ${processName}`);
} else {
events.emit('log', `Process was killed: ${processName}`);
}
}
return findProcess('name', processName)
.then(processList => processList.map(process => process.pid))
.then(pids => {
if (pids.length > 0) {
const filename = path.parse(processName).name;
return superspawn.spawn('killall', [filename], { printCommand: true, stdio: 'inherit' })
.then(
() => `Process was killed: ${processName}`,
error => `Failed to kill process: ${processName} with the error: ${error}`
);
}

return Promise.resolve(`No iOS Simulators were detected to stop with the processname of: ${processName}.`);
});
}

killSimulator('iOS Simulator'); // XCode 6
killSimulator('Simulator'); // XCode 7
Promise.all([
killSimulator('iOS Simulator.app'), // XCode 6
killSimulator('Simulator.app') // XCode 7
]).then(
output => loopPrint(output),
error => loopPrint(error)
);

function loopPrint (output) {
for (const message of output) {
console.log(message);
}
}

0 comments on commit a0dc993

Please sign in to comment.