Skip to content

Commit

Permalink
Controller: refuse to rename in controller (moves test to controller)
Browse files Browse the repository at this point in the history
- The makes "Tessel.prototype.rename" simpler in that it's only responsible for the actual rename operation now.
- No reason to pretend that "getMACAddress" is private, or even that it needs to be.
  • Loading branch information
rwaldron committed Aug 20, 2018
1 parent 1ae15aa commit 4a298bd
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 38 deletions.
4 changes: 4 additions & 0 deletions lib/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -631,6 +631,10 @@ controller.rename = function(options) {
})
.then(() => {
return controller.standardTesselCommand(options, (tessel) => {
if (options.newName === tessel.name) {
log.warn(`Name of device is already ${tessel.displayName}`);
return Promise.resolve();
}
log.info(`Renaming ${tessel.displayName} to ${colors.magenta(options.newName)}`);
return tessel.rename(options);
});
Expand Down
36 changes: 14 additions & 22 deletions lib/tessel/name.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// ...

// Third Party Dependencies
// ...
const colors = require('colors');

// Internal
var commands = require('./commands');
Expand Down Expand Up @@ -63,52 +63,44 @@ Tessel.prototype.setName = function(name) {
});
};

Tessel.prototype.rename = function(opts) {
Tessel.prototype.rename = function(options) {
return new Promise((resolve, reject) => {

// If we are resetting this name to default
if (opts.reset) {
if (options.reset) {
// Get the mac address from the device
return this._getMACAddress()
return this.getMACAddress()
.then((addr) => {

// Create the name with the default prefix
var defaultName = defaultNamePrefix + addr;

// Set the new name
return this.setName(defaultName)
.then(function() {
log.info('Reset the name of the device to', defaultName);
.then(() => {
log.info(`Reset the name of the device to ${defaultName}`);
resolve();
});
});
} else {

if (!Tessel.isValidName(opts.newName)) {
if (!Tessel.isValidName(options.newName)) {
return reject('Invalid name');
}

this.getName()
.then((oldName) => {
// Don't set the new name if it's the same as the old name
if (oldName === opts.newName) {
log.warn('Name of device is already', oldName);
// Finish the process
return resolve();
} else {
// Set the name with the provided arg
return this.setName(opts.newName)
.then(function() {
log.info('Changed name of device', oldName, 'to', opts.newName);
resolve();
});
}
.then(oldName => {
return this.setName(options.newName)
.then(() => {
log.info(`Changed name of device ${colors.magenta(oldName)} to ${colors.magenta(options.newName)}`);
resolve();
});
});
}
});
};

Tessel.prototype._getMACAddress = function() {
Tessel.prototype.getMACAddress = function() {
return this.simpleExec(commands.getInterface('eth0'))
.then(function fetchedInterface(interfaceInfo) {
// Capture the mac address out of the info using a regexp
Expand Down
17 changes: 17 additions & 0 deletions test/unit/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -2442,6 +2442,23 @@ exports['controller.rename'] = {
done();
},

willNotRenameSameAsCurrent(test) {
test.expect(2);

var options = {
newName: this.tessel.name,
};

controller.rename(options)
.then(() => {
// Assert that a warning was logged when
// the new name is the same as the old name
test.equal(this.warn.callCount, 1);
test.equal(this.warn.lastCall.args[0], 'Name of device is already TestTessel');
test.done();
});
},

renameCallPipeline(test) {
test.expect(4);

Expand Down
18 changes: 2 additions & 16 deletions test/unit/name.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ exports['Tessel.prototype.rename'] = {
this.getName = this.sandbox.stub(Tessel.prototype, 'getName').callsFake(() => {
return Promise.resolve('TheFakeName');
});
this._getMACAddress = this.sandbox.stub(Tessel.prototype, '_getMACAddress').callsFake(() => {
this.getMACAddress = this.sandbox.stub(Tessel.prototype, 'getMACAddress').callsFake(() => {
return Promise.resolve('TheFakeMACAddress');
});
this.resetMDNS = this.sandbox.stub(Tessel.prototype, 'resetMDNS').callsFake(() => {
Expand Down Expand Up @@ -92,7 +92,7 @@ exports['Tessel.prototype.rename'] = {
// - the mac address is requested
// - setName is called
// - the connection executes the setHostName command
test.equal(this._getMACAddress.callCount, 1);
test.equal(this.getMACAddress.callCount, 1);
test.equal(this.setName.callCount, 1);
test.equal(this.setHostname.callCount, 1);
test.equal(this.commitHostname.callCount, 1);
Expand Down Expand Up @@ -129,20 +129,6 @@ exports['Tessel.prototype.rename'] = {
});
},

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

this.tessel.rename({
newName: 'TheFakeName'
})
.then(() => {
// When renamed with same current name:
// - warning is logged
test.equal(this.logWarn.callCount, 1);
test.done();
});
},

invalidRename(test) {
test.expect(2);

Expand Down

0 comments on commit 4a298bd

Please # to comment.