Merge pull request #223 from resin-io/refactor/merge-drives-and-drive-scanner

Merge src/drives.js with DriveScannerService
This commit is contained in:
Juan Cruz Viotti 2016-03-31 08:49:36 -04:00
commit 50e0fcb7e9
4 changed files with 160 additions and 180 deletions

View File

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

View File

@ -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;
});
};

View File

@ -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() {

View File

@ -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');
});
});
});
});