diff --git a/src/cmd/device-protection.js b/src/cmd/device-protection.js index 487d833a2..c51d75e3e 100644 --- a/src/cmd/device-protection.js +++ b/src/cmd/device-protection.js @@ -333,11 +333,11 @@ module.exports = class DeviceProtectionCommands extends CLICommandBase { */ async _withDevice(fn) { try { - await this.getUsbDevice(this.device); + await this._getUsbDevice(this.device); if (this.device.isInDfuMode) { await this._resetDevice(this.device); - await this.getUsbDevice(this.device); + await this._getUsbDevice(this.device); } return await fn(); @@ -366,7 +366,7 @@ module.exports = class DeviceProtectionCommands extends CLICommandBase { * @param {Object} dev - The USB device instance. * @returns {Promise} */ - async getUsbDevice(dev) { + async _getUsbDevice(dev) { if (!dev || dev.isOpen === false) { this.device = await usbUtils.getOneUsbDevice({ api: this.api, idOrName: this.deviceId, ui: this.ui }); this.deviceId = this.device._id; diff --git a/src/cmd/device-protection.test.js b/src/cmd/device-protection.test.js index 31f094131..e52013dea 100644 --- a/src/cmd/device-protection.test.js +++ b/src/cmd/device-protection.test.js @@ -6,6 +6,11 @@ describe('DeviceProtectionCommands', () => { beforeEach(() => { deviceProtectionCommands = new DeviceProtectionCommands(); + sinon.stub(deviceProtectionCommands, '_getUsbDevice').resolves(); + deviceProtectionCommands.device = { + isInDfuMode: false, + unprotectDevice: sinon.stub() + }; }); afterEach(() => { @@ -351,30 +356,23 @@ describe('DeviceProtectionCommands', () => { describe('_withDevice', () => { it('should execute a function with the device in normal mode', async () => { const fn = sinon.stub().resolves(); - sinon.stub(deviceProtectionCommands, 'getUsbDevice').resolves(); - deviceProtectionCommands.device = { - isInDfuMode: false - }; sinon.stub(deviceProtectionCommands, '_resetDevice').resolves(); await deviceProtectionCommands._withDevice(fn); - expect(deviceProtectionCommands.getUsbDevice).to.have.been.calledOnce; + 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(); - sinon.stub(deviceProtectionCommands, 'getUsbDevice').resolves(); - deviceProtectionCommands.device = { - isInDfuMode: true - }; + deviceProtectionCommands.device.isInDfuMode = true; sinon.stub(deviceProtectionCommands, '_resetDevice').resolves(); await deviceProtectionCommands._withDevice(fn); - expect(deviceProtectionCommands.getUsbDevice).to.have.been.calledTwice; + expect(deviceProtectionCommands._getUsbDevice).to.have.been.calledTwice; expect(deviceProtectionCommands._resetDevice).to.have.been.calledOnce; expect(fn).to.have.been.calledOnce; });