-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
26 changed files
with
921 additions
and
714 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
const { errors: { usageError } } = require('../app/command-processor'); | ||
const spinnerMixin = require('../lib/spinner-mixin'); | ||
const UI = require('../lib/ui'); | ||
|
||
const DEVICE_ID_PTN = /^[0-9a-f]{24}$/i; | ||
|
||
|
||
module.exports = class CLICommandBase { | ||
constructor({ | ||
stdin = process.stdin, | ||
stdout = process.stdout, | ||
stderr = process.stderr | ||
} = {}) { | ||
this.stdin = stdin; | ||
this.stdout = stdout; | ||
this.stderr = stderr; | ||
this.ui = new UI({ stdin, stdout, stderr }); | ||
spinnerMixin(this); | ||
} | ||
|
||
isDeviceId(x){ | ||
return DEVICE_ID_PTN.test(x); | ||
} | ||
|
||
showUsageError(msg){ | ||
return Promise.reject(usageError(msg)); | ||
} | ||
|
||
showProductDeviceNameUsageError(device){ | ||
return this.showUsageError( | ||
`\`device\` must be an id when \`--product\` flag is set - received: ${device}` | ||
); | ||
} | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
const { expect } = require('../../test/setup'); | ||
const CLICommandBase = require('./base'); | ||
const UI = require('../lib/ui'); | ||
|
||
|
||
describe('CLI Command Base Class', () => { | ||
let cmd; | ||
|
||
beforeEach(() => { | ||
cmd = new CLICommandBase(); | ||
}); | ||
|
||
it('Initializes', () => { | ||
expect(cmd).to.have.property('stdin', process.stdin); | ||
expect(cmd).to.have.property('stdout', process.stdout); | ||
expect(cmd).to.have.property('stderr', process.stderr); | ||
expect(cmd).to.have.property('ui').that.is.an.instanceof(UI); | ||
expect(cmd).to.respondTo('showUsageError'); | ||
expect(cmd).to.respondTo('isDeviceId'); | ||
}); | ||
|
||
it('Determines if input is a device id', () => { | ||
expect(cmd.isDeviceId(null)).to.equal(false); | ||
expect(cmd.isDeviceId(undefined)).to.equal(false); | ||
expect(cmd.isDeviceId('')).to.equal(false); | ||
expect(cmd.isDeviceId(666)).to.equal(false); | ||
expect(cmd.isDeviceId('nope')).to.equal(false); | ||
expect(cmd.isDeviceId(123456789123456789123456)).to.equal(false); | ||
expect(cmd.isDeviceId('0123456789abcdef0123456')).to.equal(false); | ||
expect(cmd.isDeviceId('0123456789abcdef01234567')).to.equal(true); | ||
expect(cmd.isDeviceId('0123456789ABCDEF01234567')).to.equal(true); | ||
}); | ||
|
||
it('Show a usage error', async () => { | ||
const promise = cmd.showUsageError('test'); | ||
|
||
expect(promise).to.be.an.instanceof(Promise); | ||
|
||
let error; | ||
|
||
try { | ||
await promise; | ||
} catch (e){ | ||
error = e; | ||
} | ||
|
||
expect(error).to.be.an.instanceof(Error); | ||
expect(error).to.have.property('message', 'test'); | ||
}); | ||
|
||
it('Show a product device name usage error', async () => { | ||
const promise = cmd.showProductDeviceNameUsageError('my-device-name'); | ||
|
||
expect(promise).to.be.an.instanceof(Promise); | ||
|
||
let error; | ||
|
||
try { | ||
await promise; | ||
} catch (e){ | ||
error = e; | ||
} | ||
|
||
expect(error).to.be.an.instanceof(Error); | ||
expect(error).to.have.property('message', '`device` must be an id when `--product` flag is set - received: my-device-name'); | ||
}); | ||
}); | ||
|
Oops, something went wrong.