Skip to content

Commit

Permalink
fix: allow undefined drives and images in DriveConstraintsModel (#1016)
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Shou authored and Juan Cruz Viotti committed Jan 13, 2017
1 parent 5ec460a commit 0f2cba3
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 4 deletions.
10 changes: 6 additions & 4 deletions lib/shared/drive-constraints.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

'use strict';

const _ = require('lodash');

/**
* @summary Check if a drive is locked
* @function
Expand All @@ -38,7 +40,7 @@
* }
*/
exports.isDriveLocked = (drive) => {
return Boolean(drive.protected || false);
return Boolean(_.get(drive, 'protected', false));
};

/**
Expand All @@ -60,7 +62,7 @@ exports.isDriveLocked = (drive) => {
* }
*/
exports.isSystemDrive = (drive) => {
return Boolean(drive.system || false);
return Boolean(_.get(drive, 'system', false));
};

/**
Expand All @@ -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);
};

/**
Expand Down Expand Up @@ -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);
};
63 changes: 63 additions & 0 deletions tests/shared/drive-constraints.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit 0f2cba3

Please sign in to comment.