Skip to content
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
13 changes: 11 additions & 2 deletions lib/commands/file-actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,14 @@ async function scanMedia(this: AndroidDriver, remotePath: string): Promise<void>
if ((await this.adb.getApiLevel()) >= 29) {
await this.settingsApp.scanMedia(remotePath);
} else {
await this.adb.shell(['am', 'broadcast', '-a', ANDROID_MEDIA_RESCAN_INTENT, '-d', `file://${remotePath}`]);
await this.adb.shell([
'am',
'broadcast',
'-a',
ANDROID_MEDIA_RESCAN_INTENT,
'-d',
util.quote(`file://${remotePath}`),
]);
}
} catch (e) {
const err = e as {stderr?: string; message?: string};
Expand All @@ -298,7 +305,9 @@ function createFSTests(adb: ADB) {
const performRemoteFsCheck = async (p: string, op: 'd' | 'f' | 'e', runAs?: string): Promise<boolean> => {
const passFlag = '__PASS__';
const checkCmd = `[ -${op} '${escapePath(p)}' ] && echo ${passFlag}`;
const fullCmd = runAs ? `run-as ${util.quote(runAs)} ${checkCmd}` : checkCmd;
// Older Android images provide [ as a shell builtin, not an executable
// that run-as can invoke directly.
const fullCmd = runAs ? `run-as ${util.quote(runAs)} sh -c '${escapePath(checkCmd)}'` : checkCmd;
try {
return (await adb.shell([fullCmd])).includes(passFlag);
} catch {
Expand Down
33 changes: 30 additions & 3 deletions test/unit/commands/file-actions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,27 @@ describe('File action argument preservation', {skip: process.platform === 'win32
sandbox.restore();
});

for (const apiLevel of [26, 28]) {
it(`preserves the media scan URI on API ${apiLevel}`, async function () {
const target = `/data/local/tmp/${fileName}\nnext.txt`;
sandbox.stub(support.tempDir, 'path').resolves('local-file');
sandbox.stub(support.fs, 'exists').resolves(false);
sandbox.stub(support.fs, 'writeFile').resolves();
sandbox.stub(driver.adb, 'push').resolves();
sandbox.stub(driver.adb, 'getApiLevel').resolves(apiLevel);
const shell = sandbox.stub(driver.adb, 'shell').resolves('');
await driver.pushFile(target, 'YXBwaXVt');
assert.deepStrictEqual(await parseDeviceCommand(shell.firstCall.args[0]), [
'am',
'broadcast',
'-a',
'android.intent.action.MEDIA_SCANNER_SCAN_FILE',
'-d',
`file://${target}`,
]);
});
}

for (const operation of ['pull', 'push'] as const) {
it(`preserves package names and paths through ${operation} and cleanup`, async function () {
sandbox.stub(support.tempDir, 'path').resolves('local-file');
Expand Down Expand Up @@ -187,10 +208,16 @@ describe('File action argument preservation', {skip: process.platform === 'win32
const prefix = inContainer ? ['run-as', packageId] : [];
assert.deepStrictEqual(commands, [
...(inContainer ? [[...prefix, 'ls']] : []),
[...prefix, '[', '-e', target, ']'],
[...prefix, '[', '-f', target, ']'],
inContainer
? [...prefix, 'sh', '-c', `[ -e '${target.replace(/'/g, `'"'"'`)}' ] && echo __PASS__`]
: ['[', '-e', target, ']'],
inContainer
? [...prefix, 'sh', '-c', `[ -f '${target.replace(/'/g, `'"'"'`)}' ] && echo __PASS__`]
: ['[', '-f', target, ']'],
[...prefix, 'rm', '-f', target],
[...prefix, '[', '-e', target, ']'],
inContainer
? [...prefix, 'sh', '-c', `[ -e '${target.replace(/'/g, `'"'"'`)}' ] && echo __PASS__`]
: ['[', '-e', target, ']'],
]);
});
}
Expand Down
Loading