Skip to content

Commit

Permalink
Fix many problems for Nougat
Browse files Browse the repository at this point in the history
  • Loading branch information
imurchie committed Sep 20, 2016
1 parent 16a71ff commit 46871b8
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 10 deletions.
10 changes: 9 additions & 1 deletion lib/tools/adb-commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,15 @@ methods.getPIDsByName = async function (name) {
if (name.length > 15) {
name = name.substr(name.length - 15);
}
let stdout = await this.shell(["set `ps | grep '" + name + "'`;echo $2"]);
let stdout;
try {
stdout = await this.shell(["ps", name]);
} catch (err) {
// on some systems `name` is not accepted
// so get **all** the processes and skip wrong ones below
log.debug(`Unable to get process for '${name}'. Retrieving all processes.`);
stdout = await this.shell(["ps"]);
}
stdout = stdout.trim();
let pids = [];
for (let line of stdout.split("\n")) {
Expand Down
13 changes: 11 additions & 2 deletions lib/tools/apk-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ apkUtilsMethods.waitForActivityOrNot = async function (pkg, activity, not,
log.debug(`Waiting for pkg: '${pkg}' and activity: '${activity}'` +
`${not ? ' not' : ''} to be focused`);
let endAt = Date.now() + waitMs;

let possibleActivityNames = [];
let allActivities = activity.split(",");
for (let oneActivity of allActivities) {
Expand Down Expand Up @@ -180,7 +180,16 @@ apkUtilsMethods.install = async function (apk, replace = true, timeout = 60000)
if (replace) {
await this.adbExec(['install', '-r', apk], {timeout});
} else {
await this.adbExec(['install', apk], {timeout});
try {
await this.adbExec(['install', apk], {timeout});
} catch (err) {
// on some systems this will throw an error if the app already
// exists
if (err.message.indexOf('INSTALL_FAILED_ALREADY_EXISTS') === -1) {
throw err;
}
log.debug(`Application '${apk}' already installed. Continuing.`);
}
}
};

Expand Down
19 changes: 13 additions & 6 deletions lib/tools/system-calls.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ systemCallMethods.adbExec = async function (cmd, opts = {}) {
cmd = [cmd];
}
let args = this.executable.defaultArgs.concat(cmd);
log.debug(`Running ${this.executable.path} with args: ` +
log.debug(`Running '${this.executable.path}' with args: ` +
`${JSON.stringify(args)}`);
let {stdout} = await exec(this.executable.path, args, opts);
// sometimes ADB prints out stupid stdout warnings that we don't want
Expand Down Expand Up @@ -472,11 +472,18 @@ systemCallMethods.fileExists = async function (remotePath) {
};

systemCallMethods.ls = async function (remotePath) {
let stdout = await this.shell(['ls', remotePath]);
let lines = stdout.split("\n");
return lines.map(l => l.trim())
.filter(Boolean)
.filter(l => l.indexOf("No such file") === -1);
try {
let stdout = await this.shell(['ls', remotePath]);
let lines = stdout.split("\n");
return lines.map(l => l.trim())
.filter(Boolean)
.filter(l => l.indexOf("No such file") === -1);
} catch (err) {
if (err.message.indexOf('No such file or directory') === -1) {
throw err;
}
return [];
}
};

export default systemCallMethods;
2 changes: 1 addition & 1 deletion test/unit/adb-commands-specs.js
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,7 @@ describe('adb commands', () => {
describe('getPIDsByName', withMocks({adb}, (mocks) => {
it('should call shell and parse pids correctly', async () => {
mocks.adb.expects("shell")
.once().withExactArgs(["set `ps | grep '.contactmanager'`;echo $2"])
.once().withExactArgs(['ps', '.contactmanager'])
.returns(psOutput);
(await adb.getPIDsByName(contactManagerPackage))[0].should.equal(5078);
mocks.adb.verify();
Expand Down

0 comments on commit 46871b8

Please sign in to comment.