From 51e340f44e39a1f0b3e55ae74d302e6a3ef08853 Mon Sep 17 00:00:00 2001 From: Pierre Vanduynslager Date: Wed, 21 Mar 2018 21:30:54 -0400 Subject: [PATCH] fix: handle case with no last release in history --- lib/get-last-release.js | 15 +++++---------- test/get-last-release.test.js | 17 +++++++++++++++++ 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/lib/get-last-release.js b/lib/get-last-release.js index 21e0c738..2a928c27 100644 --- a/lib/get-last-release.js +++ b/lib/get-last-release.js @@ -30,9 +30,7 @@ module.exports = async (tagFormat, logger) => { // so it's guaranteed to no be present in the `tagFormat`. const tagRegexp = escapeRegExp(template(tagFormat)({version: ' '})).replace(' ', '(.+)'); const tags = (await gitTags()) - .map(tag => { - return {gitTag: tag, version: (tag.match(tagRegexp) || new Array(2))[1]}; - }) + .map(tag => ({gitTag: tag, version: (tag.match(tagRegexp) || new Array(2))[1]})) .filter( tag => tag.version && semver.valid(semver.clean(tag.version)) && !semver.prerelease(semver.clean(tag.version)) ) @@ -40,14 +38,11 @@ module.exports = async (tagFormat, logger) => { debug('found tags: %o', tags); - if (tags.length > 0) { - const {gitTag, version} = await pLocate(tags, tag => isRefInHistory(tag.gitTag), { - concurrency: 1, - preserveOrder: true, - }); - logger.log('Found git tag %s associated with version %s', gitTag, version); + const tag = await pLocate(tags, tag => isRefInHistory(tag.gitTag), {concurrency: 1, preserveOrder: true}); - return {gitHead: await gitTagHead(gitTag), gitTag, version}; + if (tag) { + logger.log('Found git tag %s associated with version %s', tag.gitTag, tag.version); + return {gitHead: await gitTagHead(tag.gitTag), ...tag}; } logger.log('No git tag version found'); diff --git a/test/get-last-release.test.js b/test/get-last-release.test.js index 88548c9c..6d5d2ad2 100644 --- a/test/get-last-release.test.js +++ b/test/get-last-release.test.js @@ -79,6 +79,23 @@ test.serial('Return empty object if no valid tag is found', async t => { t.is(t.context.log.args[0][0], 'No git tag version found'); }); +test.serial('Return empty object if no valid tag is found in history', async t => { + // Create a git repository, set the current working directory at the root of the repo + await gitRepo(); + await gitCommits(['First']); + await gitCheckout('other-branch'); + await gitCommits(['Second']); + await gitTagVersion('v1.0.0'); + await gitTagVersion('v2.0.0'); + await gitTagVersion('v3.0.0'); + await gitCheckout('master', false); + + const result = await getLastRelease(`v\${version}`, t.context.logger); + + t.deepEqual(result, {}); + t.is(t.context.log.args[0][0], 'No git tag version found'); +}); + test.serial('Get the highest valid tag corresponding to the "tagFormat"', async t => { // Create a git repository, set the current working directory at the root of the repo await gitRepo();