Skip to content

Commit

Permalink
Validate CLI argument to be a valid semver version number
Browse files Browse the repository at this point in the history
  • Loading branch information
lo1tuma committed Mar 6, 2018
1 parent 7ef078b commit 34df8a9
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
15 changes: 12 additions & 3 deletions lib/cli.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import path from 'path';
import prepend from 'prepend';
import promisify from 'util.promisify';
import semver from 'semver';
import ensureCleanLocalGitState from './ensureCleanLocalGitState';
import getMergedPullRequests from './getMergedPullRequests';
import createChangelog from './createChangelog';
Expand All @@ -20,6 +21,16 @@ function stripTrailingEmptyLine(text) {
function getValidLabels(prLogConfig) {
return prLogConfig && prLogConfig.validLabels || defaultValidLabels;
}

function validateVersionnumber(versionNumber) {
if (!versionNumber) {
throw new Error('version-number not specified');
}
if (semver.valid(versionNumber) === null) {
throw new Error('version-number is invalid');
}
}

export default function createCliAgent(dependencies) {
async function generateChangelog(options, githubRepo, validLabels, newVersionNumber) {
if (!options.sloppy) {
Expand All @@ -40,9 +51,7 @@ export default function createCliAgent(dependencies) {
const prLogConfig = packageConfig['pr-log'];
const validLabels = getValidLabels(prLogConfig);

if (!newVersionNumber) {
throw new Error('version-number not specified');
}
validateVersionnumber(newVersionNumber);

const changelog = await generateChangelog(options, githubRepo, validLabels, newVersionNumber);
await prependFile(changelogPath, changelog);
Expand Down
10 changes: 7 additions & 3 deletions test/unit/lib/cliSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,23 @@ describe('CLI', function () {
});

it('should throw if no version number was specified', function () {
expect(cli.run()).to.be.rejectedWith('version-number not specified');
return expect(cli.run()).to.be.rejectedWith('version-number not specified');
});

it('should throw if an invalid version number was specified', function () {
return expect(cli.run('a.b.c')).to.be.rejectedWith('version-number is invalid');
});

it('should throw if the repository is dirty', function () {
ensureCleanLocalGitState.rejects(new Error('Local copy is not clean'));

return expect(cli.run('1.0 ', options)).to.be.rejectedWith('Local copy is not clean');
return expect(cli.run('1.0.0', options)).to.be.rejectedWith('Local copy is not clean');
});

it('should not throw if the repository is dirty', function () {
ensureCleanLocalGitState.rejects(new Error('Local copy is not clean'));
createChangelog.returns('sloppy changelog');
return cli.run('1.0 ', { sloppy: true })
return cli.run('1.0.0', { sloppy: true })
.then(function () {
expect(prependFile).to.have.been.calledOnce;
expect(prependFile).to.have.been.calledWith('/foo/CHANGELOG.md', 'sloppy changelog');
Expand Down

0 comments on commit 34df8a9

Please sign in to comment.