Skip to content

Commit

Permalink
Fix tests after modifying dfu device handling
Browse files Browse the repository at this point in the history
  • Loading branch information
keeramis committed Jun 21, 2024
1 parent 8aae850 commit 5d614ce
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/cmd/binary.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class BinaryCommand {
await this._verifyBundle(parsedAppInfo, assets);
}

async createProtectedBinary({ saveTo, file, verbose}) {
async createProtectedBinary({ saveTo, file, verbose }) {
await this._checkFile(file);
let resBinaryName;

Expand Down
10 changes: 7 additions & 3 deletions src/cmd/device-protection.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
const os = require('os');
const path = require('path');
const fs = require('fs-extra');
const { createProtectedModule } = require('binary-version-reader');
const chalk = require('chalk');
const CLICommandBase = require('./base');
const spinnerMixin = require('../lib/spinner-mixin');
Expand Down Expand Up @@ -382,13 +380,19 @@ module.exports = class DeviceProtectionCommands extends CLICommandBase {
}
}

/**
* Waits for the device to reboot.
* This method waits for the device to reboot by checking if the device is ready to accept control requests.
* It waits for a maximum of 60 seconds with a 1-second interval.
*/
async _waitForDeviceToReboot() {
const start = Date.now();
while (Date.now() - start < REBOOT_TIME_MSEC) {
try {
await this._delay(REBOOT_INTERVAL_MSEC);
this.device = await usbUtils.reopenDevice({ id: this.deviceId });
const s = await this.device.getProtectionState();
// Waiting for any control request to work to ensure the device is ready
await this.device.getProtectionState();
break;
} catch (error) {
// ignore error
Expand Down
13 changes: 6 additions & 7 deletions src/cmd/device-protection.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -357,24 +357,23 @@ describe('DeviceProtectionCommands', () => {
describe('_withDevice', () => {
it('should execute a function with the device in normal (non-dfu) mode', async () => {
const fn = sinon.stub().resolves();
sinon.stub(deviceProtectionCommands, '_resetDevice').resolves();

await deviceProtectionCommands._withDevice(fn);
await deviceProtectionCommands._withDevice(true, fn);

expect(deviceProtectionCommands._getUsbDevice).to.have.been.calledOnce;
expect(deviceProtectionCommands._resetDevice).to.not.have.been.called;
expect(fn).to.have.been.calledOnce;
});

it('should execute a function with the device in dfu mode', async () => {
const fn = sinon.stub().resolves();
deviceProtectionCommands.device.isInDfuMode = true;
sinon.stub(deviceProtectionCommands, '_resetDevice').resolves();
sinon.stub(deviceProtectionCommands, '_putDeviceInSafeMode').resolves();
sinon.stub(deviceProtectionCommands, '_waitForDeviceToReboot').resolves();

await deviceProtectionCommands._withDevice(fn);
await deviceProtectionCommands._withDevice(true, fn);

expect(deviceProtectionCommands.device.enterDfuMode).to.have.been.calledOnce;
expect(deviceProtectionCommands._resetDevice).to.have.been.calledOnce;
expect(deviceProtectionCommands._putDeviceInSafeMode).to.have.been.calledOnce;
expect(deviceProtectionCommands._waitForDeviceToReboot).to.have.been.calledOnce;
expect(fn).to.have.been.calledOnce;
});
});
Expand Down

0 comments on commit 5d614ce

Please sign in to comment.