Skip to content

Commit

Permalink
tests: increase test coverage for controller.js (build requesting)
Browse files Browse the repository at this point in the history
  • Loading branch information
rwaldron committed Oct 27, 2016
1 parent 3afd4ab commit db04230
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 16 deletions.
20 changes: 9 additions & 11 deletions lib/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -879,13 +879,13 @@ controller.updateWithLocalBuilds = function(opts, tessel) {
};

controller.updateWithRemoteBuilds = function(opts, tessel) {
return new Promise(function updateProcess(resolve, reject) {
return new Promise((resolve, reject) => {
// If it's not connected via USB, we can't update it
if (!tessel.usbConnection) {
return reject('Must have Tessel connected over USB to complete update. Aborting update.');
}

return updates.requestBuildList().then(function(builds) {
return updates.requestBuildList().then(builds => {

var version = opts.version || 'latest';
var versionFromSHA = Promise.resolve(version);
Expand All @@ -894,18 +894,16 @@ controller.updateWithRemoteBuilds = function(opts, tessel) {
if (!opts.force) {
// Once we have the Tessel
// Over-ride the resolved Promise
versionFromSHA = new Promise(function(resolve, reject) {
versionFromSHA = new Promise((resolve, reject) => {
// Figure out what commit SHA is running on it
return tessel.fetchCurrentBuildInfo()
// Once we have the current SHA, provide the version
.then(function(currentSHA) {
return resolve(updates.findBuild(builds, 'sha', currentSHA));
})
.catch(function(err) {
.then(currentSHA => resolve(updates.findBuild(builds, 'sha', currentSHA)))
.catch(error => {
// If there was an error because the version file doesn't exist
if (err.message.search('No such file or directory') !== -1) {
if (error.message.search('No such file or directory') !== -1) {
// Warn the user
log.warn('Could not find firmware version on', tessel.name);
log.warn(`Could not find firmware version on ${tessel.name}`);

if (opts.force !== false) {
// Force the update
Expand All @@ -916,11 +914,11 @@ controller.updateWithRemoteBuilds = function(opts, tessel) {
return resolve('unknown version');
} else {
// Reject because the user specifically did not want to force
return reject(err);
return reject(error);
}
} else {
// Reject because an unknown error occurred
return reject(err);
return reject(error);
}
});
});
Expand Down
1 change: 1 addition & 0 deletions test/.jshintrc
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
"lan": true,
"LAN": true,
"log": true,
"Menu": true,
"mdns": true,
"mkdirp": true,
"NodeRSA": true,
Expand Down
89 changes: 84 additions & 5 deletions test/unit/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -606,7 +606,9 @@ exports['Tessel.list'] = {
type: 'LAN'
});

var customOpts = Object.assign({}, this.standardOpts, { authorized: true });
var customOpts = Object.assign({}, this.standardOpts, {
authorized: true
});

Tessel.list(customOpts)
.then(() => {
Expand Down Expand Up @@ -809,7 +811,6 @@ exports['Tessel.get'] = {
});

var customOpts = {
timeout: this.standardOpts.timeout,
key: this.standardOpts.key,
name: 'the_name',
timeout: 0,
Expand Down Expand Up @@ -924,7 +925,9 @@ exports['Tessel.get'] = {
name: 'b'
});

var customOpts = Object.assign({}, this.standardOpts, { name: 'a' });
var customOpts = Object.assign({}, this.standardOpts, {
name: 'a'
});

Tessel.get(customOpts)
.then(() => {
Expand Down Expand Up @@ -1680,7 +1683,9 @@ exports['controller.uninstaller'] = {
optionsOperationCallThrough(test) {
test.expect(1);

controller.uninstaller({ operation: 'homedir' })
controller.uninstaller({
operation: 'homedir'
})
.then(() => {
test.equal(this.homedir.callCount, 1);
test.done();
Expand Down Expand Up @@ -1740,4 +1745,78 @@ exports['controller.reboot'] = {
test.done();
});
},
}
};

exports['controller.updateWithRemoteBuilds'] = {
setUp(done) {
this.sandbox = sinon.sandbox.create();
this.spinnerStart = this.sandbox.stub(log.spinner, 'start');
this.spinnerStop = this.sandbox.stub(log.spinner, 'stop');
this.warn = this.sandbox.stub(log, 'warn');
this.error = this.sandbox.stub(log, 'error');

this.tessel = newTessel({
sandbox: this.sandbox,
authorized: true,
type: 'LAN'
});

this.requestBuildList = this.sandbox.stub(updates, 'requestBuildList', () => {
return Promise.resolve(
[{
'released': '2015-08-20T16:20:08.704Z',
'sha': '78f2bd20af9eaf76796657186f3010f03a979dc8',
'version': '0.0.3',
}, {
'released': '2015-08-18T15:12:13.070Z',
'sha': 'bf327359b4a13b4da07bc5776fe8a22ae88d54f9',
'version': '0.0.2',
}, {
'released': '2015-08-12T03:01:57.856Z',
'sha': '9a85c84f5a03c715908921baaaa9e7397985bc7f',
'version': '0.0.1',
}]
);
});


done();
},

tearDown(done) {
this.tessel.mockClose();
this.sandbox.restore();
done();
},

noUsbRejection(test) {
test.expect(1);

controller.updateWithRemoteBuilds({}, this.tessel)
.catch(error => {
test.equal(error.toString(), 'Must have Tessel connected over USB to complete update. Aborting update.');

test.done();
});
},

noForceRejection(test) {
test.expect(3);

this.tessel = TesselSimulator({
name: 'TestTessel'
});

this.fetchCurrentBuildInfo = this.sandbox.stub(this.tessel, 'fetchCurrentBuildInfo').returns(Promise.reject(new Error('No such file or directory')));

controller.updateWithRemoteBuilds({
force: false
}, this.tessel)
.catch(error => {
test.equal(error.toString(), 'Error: No such file or directory');
test.equal(this.warn.callCount, 1);
test.equal(this.warn.lastCall.args[0], 'Could not find firmware version on TestTessel');
test.done();
});
},
};

0 comments on commit db04230

Please sign in to comment.