mirror of
https://github.com/balena-io/etcher.git
synced 2025-04-24 07:17:18 +00:00
fix: allow undefined drives and images in DriveConstraintsModel (#1016)
Currently the `DriveConstraintsModel` errors when given an `undefined` drive, or image; this commit changes that behaviour. Comes with tests. Signed-off-by: Juan Cruz Viotti <jviotti@openmailbox.org>
This commit is contained in:
parent
5ec460ae1e
commit
0f2cba38d1
@ -16,6 +16,8 @@
|
||||
|
||||
'use strict';
|
||||
|
||||
const _ = require('lodash');
|
||||
|
||||
/**
|
||||
* @summary Check if a drive is locked
|
||||
* @function
|
||||
@ -38,7 +40,7 @@
|
||||
* }
|
||||
*/
|
||||
exports.isDriveLocked = (drive) => {
|
||||
return Boolean(drive.protected || false);
|
||||
return Boolean(_.get(drive, 'protected', false));
|
||||
};
|
||||
|
||||
/**
|
||||
@ -60,7 +62,7 @@ exports.isDriveLocked = (drive) => {
|
||||
* }
|
||||
*/
|
||||
exports.isSystemDrive = (drive) => {
|
||||
return Boolean(drive.system || false);
|
||||
return Boolean(_.get(drive, 'system', false));
|
||||
};
|
||||
|
||||
/**
|
||||
@ -85,7 +87,7 @@ exports.isSystemDrive = (drive) => {
|
||||
* }
|
||||
*/
|
||||
exports.isDriveLargeEnough = (drive, image) => {
|
||||
return drive.size >= (image.size || 0);
|
||||
return _.get(drive, 'size', 0) >= _.get(image, 'size', 0);
|
||||
};
|
||||
|
||||
/**
|
||||
@ -145,5 +147,5 @@ exports.isDriveValid = (drive, image) => {
|
||||
* }
|
||||
*/
|
||||
exports.isDriveSizeRecommended = (drive, image) => {
|
||||
return drive.size >= (image.recommendedDriveSize || 0);
|
||||
return _.get(drive, 'size', 0) >= _.get(image, 'recommendedDriveSize', 0);
|
||||
};
|
||||
|
@ -39,6 +39,12 @@ describe('Shared: DriveConstraints', function() {
|
||||
m.chai.expect(result).to.be.false;
|
||||
});
|
||||
|
||||
it('should return false if the drive is undefined', function() {
|
||||
const result = constraints.isDriveLocked(undefined);
|
||||
|
||||
m.chai.expect(result).to.be.false;
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe('.isSystemDrive()', function() {
|
||||
@ -78,6 +84,12 @@ describe('Shared: DriveConstraints', function() {
|
||||
m.chai.expect(result).to.be.false;
|
||||
});
|
||||
|
||||
it('should return false if the drive is undefined', function() {
|
||||
const result = constraints.isSystemDrive(undefined);
|
||||
|
||||
m.chai.expect(result).to.be.false;
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe('.isDriveLargeEnough()', function() {
|
||||
@ -124,6 +136,31 @@ describe('Shared: DriveConstraints', function() {
|
||||
m.chai.expect(result).to.be.false;
|
||||
});
|
||||
|
||||
it('should return false if the drive is undefined', function() {
|
||||
const result = constraints.isDriveLargeEnough(undefined, {
|
||||
path: 'rpi.img',
|
||||
size: 1000000000
|
||||
});
|
||||
|
||||
m.chai.expect(result).to.be.false;
|
||||
});
|
||||
|
||||
it('should return true if the image is undefined', function() {
|
||||
const result = constraints.isDriveLargeEnough({
|
||||
device: '/dev/disk1',
|
||||
name: 'USB Drive',
|
||||
size: 1000000000,
|
||||
protected: false
|
||||
}, undefined);
|
||||
|
||||
m.chai.expect(result).to.be.true;
|
||||
});
|
||||
|
||||
it('should return false if the drive and image are undefined', function() {
|
||||
const result = constraints.isDriveLargeEnough(undefined, undefined);
|
||||
m.chai.expect(result).to.be.true;
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe('.isDriveSizeRecommended()', function() {
|
||||
@ -187,6 +224,32 @@ describe('Shared: DriveConstraints', function() {
|
||||
m.chai.expect(result).to.be.true;
|
||||
});
|
||||
|
||||
it('should return false if the drive is undefined', function() {
|
||||
const result = constraints.isDriveSizeRecommended(undefined, {
|
||||
path: 'rpi.img',
|
||||
size: 1000000000,
|
||||
recommendedDriveSize: 1000000000
|
||||
});
|
||||
|
||||
m.chai.expect(result).to.be.false;
|
||||
});
|
||||
|
||||
it('should return true if the image is undefined', function() {
|
||||
const result = constraints.isDriveSizeRecommended({
|
||||
device: '/dev/disk1',
|
||||
name: 'USB Drive',
|
||||
size: 2000000000,
|
||||
protected: false
|
||||
}, undefined);
|
||||
|
||||
m.chai.expect(result).to.be.true;
|
||||
});
|
||||
|
||||
it('should return false if the drive and image are undefined', function() {
|
||||
const result = constraints.isDriveSizeRecommended(undefined, undefined);
|
||||
m.chai.expect(result).to.be.true;
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe('.isDriveValid()', function() {
|
||||
|
Loading…
x
Reference in New Issue
Block a user