Skip to content

Commit

Permalink
The parseVersion util should be forgiving to non-semver input (fixes #…
Browse files Browse the repository at this point in the history
  • Loading branch information
webpro committed Apr 11, 2021
1 parent 96602c3 commit 50216ad
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
5 changes: 3 additions & 2 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,14 @@ const hasAccess = path => {
};

const parseVersion = raw => {
if (!raw) return { version: null, isPreRelease: false, preReleaseId: null };
if (raw == null) return { version: raw, isPreRelease: false, preReleaseId: null };
const version = semver.valid(raw) ? raw : semver.coerce(raw);
if (!version) return { version: raw, isPreRelease: false, preReleaseId: null };
const parsed = semver.parse(version);
const isPreRelease = parsed.prerelease.length > 0;
const preReleaseId = isPreRelease && isNaN(parsed.prerelease[0]) ? parsed.prerelease[0] : null;
return {
version,
version: version.toString(),
isPreRelease,
preReleaseId
};
Expand Down
14 changes: 13 additions & 1 deletion test/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const { EOL } = require('os');
const test = require('ava');
const mockStdIo = require('mock-stdio');
const stripAnsi = require('strip-ansi');
const { format, truncateLines, parseGitUrl } = require('../lib/util');
const { format, truncateLines, parseGitUrl, parseVersion } = require('../lib/util');

test('format', t => {
t.is(format('release v${version}', { version: '1.0.0' }), 'release v1.0.0');
Expand Down Expand Up @@ -56,3 +56,15 @@ test('parseGitUrl', t => {
repository: 'owner/project'
});
});

test('parseVersion', t => {
t.deepEqual(parseVersion(), { version: undefined, isPreRelease: false, preReleaseId: null });
t.deepEqual(parseVersion(0), { version: '0.0.0', isPreRelease: false, preReleaseId: null });
t.deepEqual(parseVersion(1), { version: '1.0.0', isPreRelease: false, preReleaseId: null });
t.deepEqual(parseVersion('1'), { version: '1.0.0', isPreRelease: false, preReleaseId: null });
t.deepEqual(parseVersion('1.0'), { version: '1.0.0', isPreRelease: false, preReleaseId: null });
t.deepEqual(parseVersion('1.0.0'), { version: '1.0.0', isPreRelease: false, preReleaseId: null });
t.deepEqual(parseVersion('1.0.0-0'), { version: '1.0.0-0', isPreRelease: true, preReleaseId: null });
t.deepEqual(parseVersion('1.0.0-next.1'), { version: '1.0.0-next.1', isPreRelease: true, preReleaseId: 'next' });
t.deepEqual(parseVersion('21.04.1'), { version: '21.04.1', isPreRelease: false, preReleaseId: null });
});

0 comments on commit 50216ad

Please sign in to comment.