-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(inputs): add origin and port validators
- Loading branch information
1 parent
faa452d
commit 3cdf5eb
Showing
5 changed files
with
161 additions
and
8 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import fs from 'fs'; | ||
import { verifyRemoteOrigin } from './utils.js'; | ||
|
||
/** | ||
* @typedef {import('./user-flow-steps.js').Schema} Schema | ||
*/ | ||
|
||
export const errorMessages = Object.freeze({ | ||
origin: { | ||
INVALID: 'Enter a valid remote origin (https:// or git@) or local path', | ||
}, | ||
port: { | ||
IN_USE: 'Port already in use', | ||
INVALID: 'Enter a valid port number between 0 and 65535', | ||
}, | ||
}); | ||
|
||
/** | ||
* Validate if the input is a valid local path or remote origin | ||
* @function originValidator | ||
* @param {string} value - The value to validate | ||
* @returns {boolean|string} True if the value is valid, otherwise a string with the error message | ||
*/ | ||
export function originValidator(value) { | ||
const isLocalPath = fs.existsSync(value); | ||
const isRemoteOrigin = verifyRemoteOrigin(value); | ||
const result = isLocalPath || isRemoteOrigin || errorMessages.origin.INVALID; | ||
return result; | ||
} | ||
|
||
/** | ||
* Validate if the input is a valid port number | ||
* @function portValidator | ||
* @param {string} input - The value to validate | ||
* @param {Schema[]} selectedSchemas - The current schema | ||
* @returns {boolean|string} True if the value is valid, otherwise a string with the error message | ||
*/ | ||
export function portValidator(input, selectedSchemas) { | ||
const numericInput = Number(input); | ||
const isInteger = Number.isInteger(numericInput); | ||
if (!isInteger || input < 0 || input > 65535) { | ||
return errorMessages.port.INVALID; | ||
} | ||
const isPortAlreadySelected = selectedSchemas.some((schema) => schema.port === numericInput); | ||
if (isPortAlreadySelected) { | ||
return errorMessages.port.IN_USE; | ||
} | ||
return true; | ||
} |
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,90 @@ | ||
import { expect, use } from 'chai'; | ||
import fs from 'fs'; | ||
import { stub } from 'sinon'; | ||
import sinonChai from 'sinon-chai'; | ||
import { errorMessages, originValidator, portValidator } from '../../../src/services/inquirer-validators.js'; | ||
|
||
use(sinonChai); | ||
|
||
describe('unit:inquirer-validators', () => { | ||
describe('originValidator', () => { | ||
const validRemoteHttpsOrigin = 'https://github.com/user/repo.git'; | ||
const validRemoteGitOrigin = 'git@github.com:user/repo.git'; | ||
const validLocalPath = '/path/to/local'; | ||
|
||
it('should return true if the value is a valid local path', () => { | ||
let existsSyncStub = stub(fs, 'existsSync'); | ||
existsSyncStub.withArgs(validLocalPath).returns(true); | ||
expect(originValidator(validLocalPath)).to.be.true; | ||
existsSyncStub.restore(); | ||
}); | ||
|
||
it('should return true if the value is a valid remote origin with https', () => { | ||
expect(originValidator(validRemoteHttpsOrigin)).to.be.true; | ||
}); | ||
|
||
it('should return true if the value is a valid remote origin with git@', () => { | ||
expect(originValidator(validRemoteGitOrigin)).to.be.true; | ||
}); | ||
|
||
it('should return an error message if the value is not a valid local path nor remote origin', () => { | ||
expect(originValidator('invalid-value')).to.equal(errorMessages.origin.INVALID); | ||
}); | ||
|
||
it('should return an error message if the value is a valid remote origin with a starting space', () => { | ||
expect(originValidator(` ${validRemoteGitOrigin}`)).to.equal(errorMessages.origin.INVALID); | ||
}); | ||
|
||
it('should return an error message if the value is a valid remote origin with a starting tab', () => { | ||
expect(originValidator(`\t${validRemoteGitOrigin}`)).to.equal(errorMessages.origin.INVALID); | ||
}); | ||
|
||
it('should return an error message if the value is a valid remote origin with a starting newline character', () => { | ||
expect(originValidator(`\n${validRemoteGitOrigin}`)).to.equal(errorMessages.origin.INVALID); | ||
}); | ||
|
||
it('should return an error message if the value is a valid remote origin with an ending space', () => { | ||
expect(originValidator(`${validRemoteGitOrigin} `)).to.equal(errorMessages.origin.INVALID); | ||
}); | ||
|
||
it('should return an error message if the value is a valid remote origin with an ending tab', () => { | ||
expect(originValidator(`${validRemoteGitOrigin}\t`)).to.equal(errorMessages.origin.INVALID); | ||
}); | ||
|
||
it('should return an error message if the value is a valid remote origin with an ending newline character', () => { | ||
expect(originValidator(`${validRemoteGitOrigin}\n`)).to.equal(errorMessages.origin.INVALID); | ||
}); | ||
}); | ||
|
||
describe('portValidator', () => { | ||
const selectedSchemas = [ | ||
{ path: 'irrelevant', port: 5000 }, | ||
{ path: 'irrelevant', port: 5001 }, | ||
]; | ||
|
||
it('should return true if the value is an integer between 0 and 65535 and not already selected', () => { | ||
expect(portValidator('0', selectedSchemas)).to.be.true; | ||
expect(portValidator('65535', selectedSchemas)).to.be.true; | ||
expect(portValidator('5002', selectedSchemas)).to.be.true; | ||
}); | ||
|
||
it('should return an error message if the value is not an integer', () => { | ||
expect(portValidator('not an integer', selectedSchemas)).to.equal(errorMessages.port.INVALID); | ||
expect(portValidator('3.14', selectedSchemas)).to.equal(errorMessages.port.INVALID); | ||
}); | ||
|
||
it('should return an error message if the value is less than 0', () => { | ||
expect(portValidator('-1', selectedSchemas)).to.equal(errorMessages.port.INVALID); | ||
expect(portValidator('-100', selectedSchemas)).to.equal(errorMessages.port.INVALID); | ||
}); | ||
|
||
it('should return an error message if the value is greater than 65535', () => { | ||
expect(portValidator('65536', selectedSchemas)).to.equal(errorMessages.port.INVALID); | ||
expect(portValidator('100000', selectedSchemas)).to.equal(errorMessages.port.INVALID); | ||
}); | ||
|
||
it('should return an error message if the value is already selected', () => { | ||
expect(portValidator('5000', selectedSchemas)).to.equal(errorMessages.port.IN_USE); | ||
}); | ||
}); | ||
}); |