Skip to content

Commit 258674b

Browse files
committed
chore: Renamed cli option to --adb-remove-old-artifacts and some minor tweaks to the related tests
1 parent a5e335b commit 258674b

File tree

6 files changed

+131
-109
lines changed

6 files changed

+131
-109
lines changed

src/cmd/run.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export type CmdRunParams = {|
4545
adbPort?: string,
4646
adbDevice?: string,
4747
adbDiscoveryTimeout?: number,
48-
adbCleanArtifacts?: boolean,
48+
adbRemoveOldArtifacts?: boolean,
4949
firefoxApk?: string,
5050
firefoxApkComponent?: string,
5151

@@ -88,7 +88,7 @@ export default async function run(
8888
adbPort,
8989
adbDevice,
9090
adbDiscoveryTimeout,
91-
adbCleanArtifacts,
91+
adbRemoveOldArtifacts,
9292
firefoxApk,
9393
firefoxApkComponent,
9494
// Chromium CLI options.
@@ -167,7 +167,7 @@ export default async function run(
167167
adbPort,
168168
adbBin,
169169
adbDiscoveryTimeout,
170-
adbCleanArtifacts,
170+
adbRemoveOldArtifacts,
171171

172172
// Injected dependencies.
173173
firefoxApp,

src/extension-runners/firefox-android.js

Lines changed: 19 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ export type FirefoxAndroidExtensionRunnerParams = {|
7171
adbPort?: string,
7272
adbDevice?: string,
7373
adbDiscoveryTimeout?: number,
74-
adbCleanArtifacts?: boolean,
74+
adbRemoveOldArtifacts?: boolean,
7575
firefoxApk?: string,
7676
firefoxApkComponent?: string,
7777

@@ -385,30 +385,6 @@ export class FirefoxAndroidExtensionRunner {
385385
await adbUtils.amForceStopAPK(selectedAdbDevice, selectedFirefoxApk);
386386
}
387387

388-
async adbOldArtifactsDir(removeArtifacts?: boolean) {
389-
const {
390-
adbUtils,
391-
selectedAdbDevice,
392-
} = this;
393-
394-
const foundDirectories = await adbUtils.checkOrCleanArtifacts(
395-
selectedAdbDevice, removeArtifacts
396-
);
397-
398-
if (!foundDirectories) {
399-
return;
400-
}
401-
if (removeArtifacts) {
402-
log.info('Old web-ext artifacts have been found and removed ' +
403-
`from ${selectedAdbDevice} device`);
404-
} else {
405-
log.warn(
406-
`Old artifacts directories have been found on ${selectedAdbDevice} ` +
407-
'device. Use --adb-clean-artifacts to remove them automatically.'
408-
);
409-
}
410-
}
411-
412388
async adbCheckRuntimePermissions() {
413389
const {
414390
adbUtils,
@@ -456,6 +432,7 @@ export class FirefoxAndroidExtensionRunner {
456432
params: {
457433
customPrefs,
458434
firefoxApp,
435+
adbRemoveOldArtifacts,
459436
},
460437
} = this;
461438
// Create the preferences file and the Fennec temporary profile.
@@ -466,8 +443,23 @@ export class FirefoxAndroidExtensionRunner {
466443
customPrefs,
467444
});
468445

469-
//Checking for older artifacts
470-
await this.adbOldArtifactsDir(this.params.adbCleanArtifacts);
446+
// Check if there are any artifacts dirs from previous runs and
447+
// automatically remove them if adbRemoteOldArtifacts is true.
448+
const foundOldArtifacts = await adbUtils.detectOrRemoveOldArtifacts(
449+
selectedAdbDevice, adbRemoveOldArtifacts
450+
);
451+
452+
if (foundOldArtifacts) {
453+
if (adbRemoveOldArtifacts) {
454+
log.info('Old web-ext artifacts have been found and removed ' +
455+
`from ${selectedAdbDevice} device`);
456+
} else {
457+
log.warn(
458+
`Old artifacts directories have been found on ${selectedAdbDevice} ` +
459+
'device. Use --adb-remove-old-artifacts to remove them automatically.'
460+
);
461+
}
462+
}
471463

472464
// Choose a artifacts dir name for the assets pushed to the
473465
// Android device.

src/program.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -663,8 +663,8 @@ Example: $0 --help run.
663663
type: 'number',
664664
requiresArg: true,
665665
},
666-
'adb-clean-artifacts': {
667-
describe: 'Remove old artifact directories from the adb device',
666+
'adb-remove-old-artifacts': {
667+
describe: 'Remove old artifacts directories from the adb device',
668668
demandOption: false,
669669
type: 'boolean',
670670
},

src/util/adb.js

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ import {
88
} from '../errors';
99
import {createLogger} from '../util/logger';
1010

11-
const DEVICE_DIR_BASE = '/sdcard';
12-
const ARTIFACTS_DIR_PREFIX = '/web-ext-artifacts';
11+
export const DEVICE_DIR_BASE = '/sdcard/';
12+
export const ARTIFACTS_DIR_PREFIX = 'web-ext-artifacts-';
13+
1314
const log = createLogger(__filename);
1415

1516
export type ADBUtilsParams = {|
@@ -197,7 +198,7 @@ export default class ADBUtils {
197198
return artifactsDir;
198199
}
199200

200-
artifactsDir = `${DEVICE_DIR_BASE}${ARTIFACTS_DIR_PREFIX}-${Date.now()}`;
201+
artifactsDir = `${DEVICE_DIR_BASE}${ARTIFACTS_DIR_PREFIX}${Date.now()}`;
201202

202203
const testDirOut = (await this.runShellCommand(
203204
deviceId, `test -d ${artifactsDir} ; echo $?`
@@ -217,38 +218,38 @@ export default class ADBUtils {
217218
return artifactsDir;
218219
}
219220

220-
async checkOrCleanArtifacts(
221-
deviceId: string, remove?: boolean
221+
async detectOrRemoveOldArtifacts(
222+
deviceId: string, removeArtifactDirs?: boolean = false
222223
): Promise<boolean> {
223224
const {adbClient} = this;
224225

225-
let found = false;
226-
log.debug('Finding older artifacts');
226+
log.debug('Checking adb device for existing web-ext artifacts dirs');
227227

228228
return wrapADBCall(async () => {
229229
const files = await adbClient.readdir(deviceId, DEVICE_DIR_BASE);
230+
let found = false;
230231

231232
for (const file of files) {
232233
if (!file.isDirectory() ||
233-
!file.name.startsWith('web-ext-artifacts-')) {
234+
!file.name.startsWith(ARTIFACTS_DIR_PREFIX)) {
234235
continue;
235236
}
236237

237-
if (!remove) {
238+
// Return earlier if we only need to warn the user that some
239+
// existing artifacts dirs have been found on the adb device.
240+
if (!removeArtifactDirs) {
238241
return true;
239242
}
240243

241244
found = true;
242245

243-
const artifactsDir = `${DEVICE_DIR_BASE}/${file.name}`;
246+
const artifactsDir = `${DEVICE_DIR_BASE}${file.name}`;
244247

245248
log.debug(
246-
`Removing ${artifactsDir} artifacts directory on ${deviceId} device`
249+
`Removing artifacts directory ${artifactsDir} from device ${deviceId}`
247250
);
248251

249-
await this.runShellCommand(deviceId, [
250-
'rm', '-rf', artifactsDir,
251-
]);
252+
await this.runShellCommand(deviceId, ['rm', '-rf', artifactsDir]);
252253
}
253254

254255
return found;
@@ -269,9 +270,7 @@ export default class ADBUtils {
269270
`Removing ${artifactsDir} artifacts directory on ${deviceId} device`
270271
);
271272

272-
await this.runShellCommand(deviceId, [
273-
'rm', '-rf', artifactsDir,
274-
]);
273+
await this.runShellCommand(deviceId, ['rm', '-rf', artifactsDir]);
275274
}
276275

277276
async pushFile(

tests/unit/test-extension-runners/test.firefox-android.js

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,7 @@ function prepareSelectedDeviceAndAPKParams(
130130
startFirefoxAPK: sinon.spy(() => Promise.resolve()),
131131
setupForward: sinon.spy(() => Promise.resolve()),
132132
clearArtifactsDir: sinon.spy(() => Promise.resolve()),
133-
checkOrCleanArtifacts: sinon.spy(
134-
() => Promise.resolve(true)
135-
),
133+
detectOrRemoveOldArtifacts: sinon.spy(() => Promise.resolve(true)),
136134
setUserAbortDiscovery: sinon.spy(() => {}),
137135
ensureRequiredAPKRuntimePermissions: sinon.spy(() => Promise.resolve()),
138136
...adbOverrides,
@@ -360,11 +358,12 @@ describe('util/extension-runners/firefox-android', () => {
360358
);
361359
});
362360

363-
it('check for older artifacts', async () => {
361+
it('does check for existing artifacts dirs', async () => {
364362
const adbOverrides = {
365363
getOrCreateArtifactsDir: sinon.spy(
366364
() => Promise.resolve('/sdcard/web-ext-dir')
367365
),
366+
detectOrRemoveOldArtifacts: sinon.spy(() => Promise.resolve(false)),
368367
};
369368
const overriddenProperties = {
370369
params: {
@@ -373,7 +372,7 @@ describe('util/extension-runners/firefox-android', () => {
373372
buildSourceDir: sinon.spy(() => Promise.resolve({
374373
extensionPath: fakeBuiltExtensionPath,
375374
})),
376-
adbCleanArtifacts: false,
375+
adbRemoveOldArtifacts: false,
377376
},
378377
};
379378
const {
@@ -386,21 +385,26 @@ describe('util/extension-runners/firefox-android', () => {
386385
await runnerInstance.run();
387386

388387
sinon.assert.calledWithMatch(
389-
fakeADBUtils.checkOrCleanArtifacts,
388+
fakeADBUtils.detectOrRemoveOldArtifacts,
390389
'emulator-1',
391390
false,
392391
);
393392

393+
// Ensure the old artifacts are checked or removed after stopping the
394+
// apk and before creating the new artifacts dir.
394395
sinon.assert.callOrder(
395396
fakeADBUtils.amForceStopAPK,
396-
fakeADBUtils.checkOrCleanArtifacts
397+
fakeADBUtils.detectOrRemoveOldArtifacts,
398+
fakeADBUtils.getOrCreateArtifactsDir
397399
);
398400
});
399-
it('remove plausible older artifacts', async () => {
401+
402+
it('does optionally remove older artifacts dirs', async () => {
400403
const adbOverrides = {
401404
getOrCreateArtifactsDir: sinon.spy(
402405
() => Promise.resolve('/sdcard/web-ext-dir')
403406
),
407+
detectOrRemoveOldArtifacts: sinon.spy(() => Promise.resolve(true)),
404408
};
405409
const overriddenProperties = {
406410
params: {
@@ -409,7 +413,7 @@ describe('util/extension-runners/firefox-android', () => {
409413
buildSourceDir: sinon.spy(() => Promise.resolve({
410414
extensionPath: fakeBuiltExtensionPath,
411415
})),
412-
adbCleanArtifacts: true,
416+
adbRemoveOldArtifacts: true,
413417
},
414418
};
415419
const {
@@ -422,14 +426,17 @@ describe('util/extension-runners/firefox-android', () => {
422426
await runnerInstance.run();
423427

424428
sinon.assert.calledWithMatch(
425-
fakeADBUtils.checkOrCleanArtifacts,
429+
fakeADBUtils.detectOrRemoveOldArtifacts,
426430
'emulator-1',
427431
true,
428432
);
429433

434+
// Ensure the old artifacts are checked or removed after stopping the
435+
// apk and before creating the new artifacts dir.
430436
sinon.assert.callOrder(
431437
fakeADBUtils.amForceStopAPK,
432-
fakeADBUtils.checkOrCleanArtifacts
438+
fakeADBUtils.detectOrRemoveOldArtifacts,
439+
fakeADBUtils.getOrCreateArtifactsDir
433440
);
434441
});
435442

0 commit comments

Comments
 (0)