diff --git a/lib/browser/modules/drive-scanner.js b/lib/browser/modules/drive-scanner.js index 29143a9c..d22b7beb 100644 --- a/lib/browser/modules/drive-scanner.js +++ b/lib/browser/modules/drive-scanner.js @@ -24,14 +24,13 @@ const angular = require('angular'); const _ = require('lodash'); const EventEmitter = require('events').EventEmitter; const electron = require('electron'); +const drivelist = require('drivelist'); if (window.mocha) { var path = require('path'); var srcPath = path.join(__dirname, '..', '..', 'src'); - var drives = electron.remote.require(path.join(srcPath, 'drives')); var dialog = electron.remote.require(path.join(srcPath, 'dialog')); } else { - var drives = electron.remote.require('./src/drives'); var dialog = electron.remote.require('./src/dialog'); } @@ -100,7 +99,19 @@ driveScanner.service('DriveScannerService', function($q, $interval, $timeout) { * }); */ this.scan = function() { - return $q.when(drives.listRemovable()); + var deferred = $q.defer(); + + drivelist.list(function(error, drives) { + if (error) { + return deferred.reject(error); + } + + return deferred.resolve(_.filter(drives, function(drive) { + return !drive.system; + })); + }); + + return deferred.promise; }; /** diff --git a/lib/src/drives.js b/lib/src/drives.js deleted file mode 100644 index 4ee8f8b8..00000000 --- a/lib/src/drives.js +++ /dev/null @@ -1,41 +0,0 @@ -/* - * Copyright 2016 Resin.io - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -'use strict'; - -const Bluebird = require('bluebird'); -const drivelist = Bluebird.promisifyAll(require('drivelist')); - -/** - * @summary List all available removable drives - * @function - * @public - * - * @fulfil {Object[]} - available removable drives - * @returns {Promise} - * - * @example - * drives.listRemovable().then(function(drives) { - * drives.forEach(function(drive) { - * console.log(drive.device); - * }); - * }); - */ -exports.listRemovable = function() { - return drivelist.listAsync().filter(function(drive) { - return !drive.system; - }); -}; diff --git a/tests/browser/modules/drive-scanner.spec.js b/tests/browser/modules/drive-scanner.spec.js index ce797f42..597f759d 100644 --- a/tests/browser/modules/drive-scanner.spec.js +++ b/tests/browser/modules/drive-scanner.spec.js @@ -2,6 +2,7 @@ const m = require('mochainon'); const angular = require('angular'); +const drivelist = require('drivelist'); require('angular-mocks'); require('../../../lib/browser/modules/drive-scanner'); @@ -12,12 +13,14 @@ describe('Browser: DriveScanner', function() { describe('DriveScannerService', function() { let $interval; + let $rootScope; let $timeout; let $q; let DriveScannerService; - beforeEach(angular.mock.inject(function(_$interval_, _$timeout_, _$q_, _DriveScannerService_) { + beforeEach(angular.mock.inject(function(_$interval_, _$rootScope_, _$timeout_, _$q_, _DriveScannerService_) { $interval = _$interval_; + $rootScope = _$rootScope_; $timeout = _$timeout_; $q = _$q_; DriveScannerService = _DriveScannerService_; @@ -27,6 +30,148 @@ describe('Browser: DriveScanner', function() { m.chai.expect(DriveScannerService.drives).to.deep.equal([]); }); + describe('.scan()', function() { + + describe('given no available drives', function() { + + beforeEach(function() { + this.drivesListStub = m.sinon.stub(drivelist, 'list'); + this.drivesListStub.yields(null, []); + }); + + afterEach(function() { + this.drivesListStub.restore(); + }); + + it('should eventually equal an empty array', function(done) { + DriveScannerService.scan().then(function(drives) { + m.chai.expect(drives).to.deep.equal([]); + done(); + }); + + $rootScope.$apply(); + }); + + }); + + describe('given available system drives', function() { + + beforeEach(function() { + this.drives = [ + { + device: '/dev/sda', + description: 'WDC WD10JPVX-75J', + size: '931.5G', + mountpoint: '/', + system: true + } + ]; + + this.drivesListStub = m.sinon.stub(drivelist, 'list'); + this.drivesListStub.yields(null, this.drives); + }); + + afterEach(function() { + this.drivesListStub.restore(); + }); + + it('should eventually equal an empty array', function(done) { + DriveScannerService.scan().then(function(drives) { + m.chai.expect(drives).to.deep.equal([]); + done(); + }); + + $rootScope.$apply(); + }); + + }); + + describe('given available system and removable drives', function() { + + beforeEach(function() { + this.drives = [ + { + device: '/dev/sda', + description: 'WDC WD10JPVX-75J', + size: '931.5G', + mountpoint: '/', + system: true + }, + { + device: '/dev/sdb', + description: 'Foo', + size: '14G', + mountpoint: '/mnt/foo', + system: false + }, + { + device: '/dev/sdc', + description: 'Bar', + size: '14G', + mountpoint: '/mnt/bar', + system: false + } + ]; + + this.drivesListStub = m.sinon.stub(drivelist, 'list'); + this.drivesListStub.yields(null, this.drives); + }); + + afterEach(function() { + this.drivesListStub.restore(); + }); + + it('should eventually become the removable drives', function(done) { + DriveScannerService.scan().then(function(drives) { + m.chai.expect(drives).to.deep.equal([ + { + device: '/dev/sdb', + description: 'Foo', + size: '14G', + mountpoint: '/mnt/foo', + system: false + }, + { + device: '/dev/sdc', + description: 'Bar', + size: '14G', + mountpoint: '/mnt/bar', + system: false + } + ]); + done(); + }); + + $rootScope.$apply(); + }); + + }); + + describe('given an error when listing the drives', function() { + + beforeEach(function() { + this.drivesListStub = m.sinon.stub(drivelist, 'list'); + this.drivesListStub.yields(new Error('scan error')); + }); + + afterEach(function() { + this.drivesListStub.restore(); + }); + + it('should be rejected with the error', function(done) { + DriveScannerService.scan().catch(function(error) { + m.chai.expect(error).to.be.an.instanceof(Error); + m.chai.expect(error.message).to.equal('scan error'); + done(); + }); + + $rootScope.$apply(); + }); + + }); + + }); + describe('given no drives', function() { describe('.hasAvailableDrives()', function() { diff --git a/tests/src/drives.spec.js b/tests/src/drives.spec.js deleted file mode 100644 index 53f0a48e..00000000 --- a/tests/src/drives.spec.js +++ /dev/null @@ -1,135 +0,0 @@ -'use strict'; - -const m = require('mochainon'); -const Bluebird = require('bluebird'); -const drivelist = require('drivelist'); -const drives = require('../../lib/src/drives'); - -describe('Drives:', function() { - - describe('.listRemovable()', function() { - - describe('given no available drives', function() { - - beforeEach(function() { - this.drivesListStub = m.sinon.stub(drivelist, 'listAsync'); - this.drivesListStub.returns(Bluebird.resolve([])); - }); - - afterEach(function() { - this.drivesListStub.restore(); - }); - - it('should eventually equal an empty array', function() { - const promise = drives.listRemovable(); - m.chai.expect(promise).to.eventually.become([]); - }); - - }); - - describe('given available system drives', function() { - - beforeEach(function() { - this.drives = [ - { - device: '/dev/sda', - description: 'WDC WD10JPVX-75J', - size: '931.5G', - mountpoint: '/', - system: true - } - ]; - - this.drivesListStub = m.sinon.stub(drivelist, 'listAsync'); - this.drivesListStub.returns(Bluebird.resolve(this.drives)); - }); - - afterEach(function() { - this.drivesListStub.restore(); - }); - - it('should eventually equal an empty array', function() { - const promise = drives.listRemovable(); - m.chai.expect(promise).to.eventually.become([]); - }); - - }); - - describe('given available system and removable drives', function() { - - beforeEach(function() { - this.drives = [ - { - device: '/dev/sda', - description: 'WDC WD10JPVX-75J', - size: '931.5G', - mountpoint: '/', - system: true - }, - { - device: '/dev/sdb', - description: 'Foo', - size: '14G', - mountpoint: '/mnt/foo', - system: false - }, - { - device: '/dev/sdc', - description: 'Bar', - size: '14G', - mountpoint: '/mnt/bar', - system: false - } - ]; - - this.drivesListStub = m.sinon.stub(drivelist, 'listAsync'); - this.drivesListStub.returns(Bluebird.resolve(this.drives)); - }); - - afterEach(function() { - this.drivesListStub.restore(); - }); - - it('should eventually become the removable drives', function() { - const promise = drives.listRemovable(); - m.chai.expect(promise).to.eventually.become([ - { - device: '/dev/sdb', - description: 'Foo', - size: '14G', - mountpoint: '/mnt/foo', - system: false - }, - { - device: '/dev/sdc', - description: 'Bar', - size: '14G', - mountpoint: '/mnt/bar', - system: false - } - ]); - }); - - }); - - describe('given an error when listing the drives', function() { - - beforeEach(function() { - this.drivesListStub = m.sinon.stub(drivelist, 'listAsync'); - this.drivesListStub.returns(Bluebird.reject(new Error('scan error'))); - }); - - afterEach(function() { - this.drivesListStub.restore(); - }); - - it('should be rejected with the error', function() { - const promise = drives.listRemovable(); - m.chai.expect(promise).to.be.rejectedWith('scan error'); - }); - - }); - - }); - -});