Skip to content

Commit

Permalink
fix: getVersionFromTag tests to fix release process
Browse files Browse the repository at this point in the history
  • Loading branch information
davikawasaki authored and antongolub committed Feb 9, 2021
1 parent 576fe27 commit e7f1646
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
11 changes: 9 additions & 2 deletions lib/updateDeps.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,16 @@ const getNextVersion = (pkg) => {
*
* @param {Package} pkg Package object.
* @param {string} tag The tag containing the version to resolve
* @returns {string} The version of the package
* @returns {string|null} The version of the package or null if no tag was passed
* @internal
*/
const getVersionFromTag = (pkg, tag) => (pkg.name ? tag.replace(`${pkg.name}@`, "") : tag);
const getVersionFromTag = (pkg, tag) => {
if (!pkg.name) return tag || null;
if (!tag) return null;

const strMatch = tag.match(/[0-9].[0-9].[0-9].*/);
return strMatch && strMatch[0] && semver.valid(strMatch[0]) ? strMatch[0] : null;
};

/**
* Resolve next package version on prereleases.
Expand Down Expand Up @@ -328,4 +334,5 @@ module.exports = {
updateManifestDeps,
resolveReleaseType,
resolveNextVersion,
getVersionFromTag,
};
35 changes: 35 additions & 0 deletions test/lib/updateDeps.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const {
getNextVersion,
getNextPreVersion,
getPreReleaseTag,
getVersionFromTag,
} = require("../../lib/updateDeps");

describe("resolveNextVersion()", () => {
Expand Down Expand Up @@ -227,3 +228,37 @@ describe("getPreReleaseTag()", () => {
});
});
});

describe("getVersionFromTag()", () => {
// prettier-ignore
const cases = [
[{}, undefined, null],
[{ name: undefined }, undefined, null],
[{}, null, null],
[{ name: null }, null, null],
[{ name: undefined }, '1.0.0', '1.0.0'],
[{ name: null }, '1.0.0', '1.0.0'],
[{ name: 'abc' }, undefined, null],
[{ name: 'abc' }, null, null],
[{ name: 'abc' }, '1.0.0', '1.0.0'],
[{ name: 'dev' }, '1.0.0-dev.1', '1.0.0-dev.1'],
[{ name: 'app' }, 'app@1.0.0-dev.1', '1.0.0-dev.1'],
[{ name: 'app' }, 'app@1.0.0-devapp@.1', null],
[{ name: 'msr-test-a' }, 'msr-test-a@1.0.0-rc.1', '1.0.0-rc.1'],
[{ name: 'msr.test.a' }, 'msr.test.a@1.0.0', '1.0.0'],
[{ name: 'msr_test_a' }, 'msr_test_a@1.0.0', '1.0.0'],
[{ name: 'msr@test@a' }, 'msr@test@a@1.0.0', '1.0.0'],
[{ name: 'abc' }, 'a.b.c-rc.0', null],
[{ name: 'abc' }, '1-rc.0', null],
[{ name: 'abc' }, '1.0.x-rc.0', null],
[{ name: 'abc' }, '1.x.0-rc.0', null],
[{ name: 'abc' }, 'x.1.0-rc.0', null],
]

cases.forEach(([pkg, tag, versionFromTag]) => {
it(`${JSON.stringify(pkg)} pkg with tag ${tag} gives ${versionFromTag}`, () => {
// prettier-ignore
expect(getVersionFromTag(pkg, tag)).toBe(versionFromTag);
});
});
});

0 comments on commit e7f1646

Please sign in to comment.