Skip to content

Commit

Permalink
tests: increase test coverage for controller.js
Browse files Browse the repository at this point in the history
  • Loading branch information
rwaldron committed Oct 27, 2016
1 parent d870bc6 commit fb62450
Show file tree
Hide file tree
Showing 2 changed files with 425 additions and 106 deletions.
50 changes: 28 additions & 22 deletions lib/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,17 +129,18 @@ Tessel.list = function(opts) {
log.info('Multiple Tessels found.');
// Figure out which Tessel will be selected
return controller.runHeuristics(opts, foundTessels)
.then(function logSelected(tessel) {
.then(tessel => {
// Report that selected Tessel to the user
log.info('Will default to %s.', tessel.name);
})
.catch(function(err) {
if (!(err instanceof controller.HeuristicAmbiguityError)) {
.catch(error => {
/* istanbul ignore else */
if (!(error instanceof controller.HeuristicAmbiguityError)) {
return controller.closeTesselConnections(foundTessels)
.then(() => reject(err));
.then(() => reject(error));
}
})
.then(function() {
.then(() => {
// Helpful instructions on how to switch
log.info('Set default Tessel with environment variable (e.g. "export TESSEL=bulbasaur") or use the --name flag.');
// Close all opened connections and resolve
Expand All @@ -149,8 +150,10 @@ Tessel.list = function(opts) {
});

// Stop the search if CTRL+C is hit
/* istanbul ignore next */
process.once('SIGINT', function() {
// If the seeker exists (it should)
/* istanbul ignore if */
if (seeker !== undefined) {
// Stop looking for more Tessels
seeker.stop();
Expand Down Expand Up @@ -194,7 +197,7 @@ Tessel.get = function(opts) {
// the completion of discovery. So if we got to this point, no Tessel
// was found with that name
else if (opts.name !== undefined) {
return reject('No Tessel found by the name ' + opts.name);
return reject(`No Tessel found by the name ${opts.name}`);
}
// If there was only one Tessel
else if (tessels.length === 1) {
Expand All @@ -211,6 +214,7 @@ Tessel.get = function(opts) {
return controller.runHeuristics(opts, tessels)
.then(logAndFinish)
.catch(error => {
/* istanbul ignore else */
if (error instanceof controller.HeuristicAmbiguityError) {
var map = {};
// Open up an interactive menu for the user to choose
Expand All @@ -220,10 +224,11 @@ Tessel.get = function(opts) {
name: 'selected',
type: 'list',
message: 'Which Tessel do want to use?',
choices: tessels.map(function(tessel, i) {
choices: tessels.map((tessel, i) => {
var isLAN = !!tessel.lanConnection;
var isAuthorized = isLAN && tessel.lanConnection.authorized;
// show "(not authorized)" if there is a lanConnection
/* istanbul ignore next */
var authorization = isAuthorized || !!tessel.usbConnection ? '' : '(not authorized)';
var display = `\t${tessel.connection.connectionType}\t${tessel.name}\t${authorization}`;

Expand All @@ -233,17 +238,20 @@ Tessel.get = function(opts) {
return display;
})
},
translate: function(answer) {
// This code is not our responsibility to test
/* istanbul ignore next */
translate(answer) {
/* istanbul ignore next */
return tessels[map[answer.selected]];
}
}).then(function(tessel) {
}).then(tessel => {
/* istanbul ignore else */
if (!tessel) {
return controller.closeTesselConnections(tessels)
.then(function() {
return reject('No Tessel selected, mission aborted!');
});
.then(() => reject('No Tessel selected, mission aborted!'));
} else {
// Log we found it and return it to the caller
// This cannot be tested without human interaction
return logAndFinish(tessel);
}
});
Expand Down Expand Up @@ -288,19 +296,16 @@ Tessel.get = function(opts) {
function logAndFinish(tessel) {
// The Tessels that we won't be using should have their connections closed
var connectionsToClose = tessels;
/* istanbul ignore else */
if (tessel) {
log.info(`Connected to ${tessel.name}.`);
connectionsToClose.splice(tessels.indexOf(tessel), 1);
controller.closeTesselConnections(connectionsToClose)
.then(function() {
return resolve(tessel);
});
.then(() => resolve(tessel));
} else {
log.info('Please specify a Tessel by name [--name <tessel name>]');
controller.closeTesselConnections(connectionsToClose)
.then(function() {
return reject('Multiple possible Tessel connections found.');
});
.then(() => reject('Multiple possible Tessel connections found.'));
}
}
});
Expand Down Expand Up @@ -1031,10 +1036,11 @@ controller.tesselEnvVersions = opts => {
controller.reboot = opts => {
opts.authorized = true;
return controller.standardTesselCommand(opts, tessel => {
return tessel.reboot().then(() => {
log.info('Tessel Rebooting...');
}).catch(err => {
log.error(err);
return new Promise((resolve, reject) => {
tessel.reboot().then(() => {
log.info('Tessel Rebooting...');
resolve();
}).catch(error => reject(error));
});
});
};
Expand Down

0 comments on commit fb62450

Please sign in to comment.