mirror of
https://github.com/balena-io/etcher.git
synced 2025-07-25 12:16:37 +00:00
Merge pull request #223 from resin-io/refactor/merge-drives-and-drive-scanner
Merge src/drives.js with DriveScannerService
This commit is contained in:
commit
50e0fcb7e9
@ -24,14 +24,13 @@ const angular = require('angular');
|
|||||||
const _ = require('lodash');
|
const _ = require('lodash');
|
||||||
const EventEmitter = require('events').EventEmitter;
|
const EventEmitter = require('events').EventEmitter;
|
||||||
const electron = require('electron');
|
const electron = require('electron');
|
||||||
|
const drivelist = require('drivelist');
|
||||||
|
|
||||||
if (window.mocha) {
|
if (window.mocha) {
|
||||||
var path = require('path');
|
var path = require('path');
|
||||||
var srcPath = path.join(__dirname, '..', '..', 'src');
|
var srcPath = path.join(__dirname, '..', '..', 'src');
|
||||||
var drives = electron.remote.require(path.join(srcPath, 'drives'));
|
|
||||||
var dialog = electron.remote.require(path.join(srcPath, 'dialog'));
|
var dialog = electron.remote.require(path.join(srcPath, 'dialog'));
|
||||||
} else {
|
} else {
|
||||||
var drives = electron.remote.require('./src/drives');
|
|
||||||
var dialog = electron.remote.require('./src/dialog');
|
var dialog = electron.remote.require('./src/dialog');
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -100,7 +99,19 @@ driveScanner.service('DriveScannerService', function($q, $interval, $timeout) {
|
|||||||
* });
|
* });
|
||||||
*/
|
*/
|
||||||
this.scan = function() {
|
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;
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -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;
|
|
||||||
});
|
|
||||||
};
|
|
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
const m = require('mochainon');
|
const m = require('mochainon');
|
||||||
const angular = require('angular');
|
const angular = require('angular');
|
||||||
|
const drivelist = require('drivelist');
|
||||||
require('angular-mocks');
|
require('angular-mocks');
|
||||||
require('../../../lib/browser/modules/drive-scanner');
|
require('../../../lib/browser/modules/drive-scanner');
|
||||||
|
|
||||||
@ -12,12 +13,14 @@ describe('Browser: DriveScanner', function() {
|
|||||||
describe('DriveScannerService', function() {
|
describe('DriveScannerService', function() {
|
||||||
|
|
||||||
let $interval;
|
let $interval;
|
||||||
|
let $rootScope;
|
||||||
let $timeout;
|
let $timeout;
|
||||||
let $q;
|
let $q;
|
||||||
let DriveScannerService;
|
let DriveScannerService;
|
||||||
|
|
||||||
beforeEach(angular.mock.inject(function(_$interval_, _$timeout_, _$q_, _DriveScannerService_) {
|
beforeEach(angular.mock.inject(function(_$interval_, _$rootScope_, _$timeout_, _$q_, _DriveScannerService_) {
|
||||||
$interval = _$interval_;
|
$interval = _$interval_;
|
||||||
|
$rootScope = _$rootScope_;
|
||||||
$timeout = _$timeout_;
|
$timeout = _$timeout_;
|
||||||
$q = _$q_;
|
$q = _$q_;
|
||||||
DriveScannerService = _DriveScannerService_;
|
DriveScannerService = _DriveScannerService_;
|
||||||
@ -27,6 +30,148 @@ describe('Browser: DriveScanner', function() {
|
|||||||
m.chai.expect(DriveScannerService.drives).to.deep.equal([]);
|
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('given no drives', function() {
|
||||||
|
|
||||||
describe('.hasAvailableDrives()', function() {
|
describe('.hasAvailableDrives()', function() {
|
||||||
|
@ -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');
|
|
||||||
});
|
|
||||||
|
|
||||||
});
|
|
||||||
|
|
||||||
});
|
|
||||||
|
|
||||||
});
|
|
Loading…
x
Reference in New Issue
Block a user