From ab042a936fa32e109e24651314414b57a4a03d08 Mon Sep 17 00:00:00 2001 From: Ruben Bridgewater Date: Sun, 15 Apr 2018 21:41:36 +0200 Subject: [PATCH 1/3] process: add release.compareVersion(str) functionality This overloads `release.compareVersion` by also accepting a single string argument to compare the version. It also fixes a former bug by comparing the pre-release tags properly. It also adds a stricter input validation to throw on negative values. --- doc/api/process.md | 12 +++ lib/internal/process.js | 55 +++++++++++--- test/parallel/test-process-release.js | 101 ++++++++++++++++++++------ 3 files changed, 133 insertions(+), 35 deletions(-) diff --git a/doc/api/process.md b/doc/api/process.md index 060d741804df54..65b0016a1514ee 100644 --- a/doc/api/process.md +++ b/doc/api/process.md @@ -1554,6 +1554,18 @@ Perform a SemVer comparison to the release version. version, `0` if the given version matches the process version, and `-1` if the given version is greater than the release version. +## process.release.compareVersion(version) + + +Perform a SemVer comparison to the release version. + +* `version` {string} The semver version to compare. +* Returns: {number} `1` if the given version is lower than the current release + version, `0` if the given version matches the process version, and `-1` + if the given version is greater than the release version. + ## process.send(message[, sendHandle[, options]][, callback]) -Perform a SemVer comparison to the release version. - * `major` {number} The major version to compare. * `minor` {number} The minor version to compare. * `patch` {number} The patch version to compare. @@ -1554,18 +1552,20 @@ Perform a SemVer comparison to the release version. version, `0` if the given version matches the process version, and `-1` if the given version is greater than the release version. +Perform a SemVer comparison to the release version. + ## process.release.compareVersion(version) -Perform a SemVer comparison to the release version. - * `version` {string} The semver version to compare. * Returns: {number} `1` if the given version is lower than the current release version, `0` if the given version matches the process version, and `-1` if the given version is greater than the release version. +Perform a SemVer comparison to the release version. + ## process.send(message[, sendHandle[, options]][, callback]) * `version` {string} The semver version to compare. -* Returns: {number} `1` if the given version is lower than the current release - version, `0` if the given version matches the process version, and `-1` +* Returns: {number} `-1` if the given version is lower than the current release + version, `0` if the given version matches the process version, and `1` if the given version is greater than the release version. Perform a SemVer comparison to the release version. diff --git a/lib/internal/process.js b/lib/internal/process.js index 75a3716043209d..79c44fdcd3e31c 100644 --- a/lib/internal/process.js +++ b/lib/internal/process.js @@ -326,28 +326,28 @@ function setupCompareVersion() { } if (major > majorVersion) - return -1; - if (major < majorVersion) return 1; + if (major < majorVersion) + return -1; if (minor > minorVersion) - return -1; - if (minor < minorVersion) return 1; + if (minor < minorVersion) + return -1; if (patch > patchVersion) - return -1; - if (patch < patchVersion) return 1; + if (patch < patchVersion) + return -1; if (prereleaseTag) { if (prereleaseTag === tag) return 0; // Note: we do not fully adhere to semver as we do not check numbers // properly. It applies to e.g., 'rc2' > 'rc10'. - return prereleaseTag < tag ? -1 : 1; + return prereleaseTag > tag ? -1 : 1; } if (tag) - return -1; + return 1; return 0; }; diff --git a/test/parallel/test-process-release.js b/test/parallel/test-process-release.js index 35d664f579a964..f5e1eb877aef37 100644 --- a/test/parallel/test-process-release.js +++ b/test/parallel/test-process-release.js @@ -29,12 +29,12 @@ const { [ [major, minor, patch, tag, 0], - [major + 1, minor, patch, tag, -1], - [major - 1, minor, patch, tag, 1], - [major, minor + 1, patch, tag, -1], - [major, minor - 1, patch, tag, 1], - [major, minor, patch + 1, tag, -1], - [major, minor, patch - 1, tag, 1] + [major + 1, minor, patch, tag, 1], + [major - 1, minor, patch, tag, -1], + [major, minor + 1, patch, tag, 1], + [major, minor - 1, patch, tag, -1], + [major, minor, patch + 1, tag, 1], + [major, minor, patch - 1, tag, -1] ].forEach(([major, minor, patch, tag, expected]) => { // Skip invalid entries. if (major < 0 || minor < 0 || patch < 0) @@ -47,12 +47,12 @@ const { if (tag) { assert.strictEqual(compareVersion(major, minor, patch), 1); assert.strictEqual(compareVersion(`${major}.${minor}.${patch}`), 1); - assert.strictEqual(compareVersion(major, minor, patch, `${tag}-fake`), -1); + assert.strictEqual(compareVersion(major, minor, patch, `${tag}-fake`), 1); assert.strictEqual( - compareVersion(major, minor, patch, `${tag.slice(0, -1)}`), 1); + compareVersion(major, minor, patch, `${tag.slice(0, -1)}`), -1); } else { - assert.strictEqual(compareVersion(major, minor, patch, 'fake-tag'), -1); - assert.strictEqual(compareVersion(`${major}.${minor}.${patch}-fake-tag`), -1); + assert.strictEqual(compareVersion(major, minor, patch, 'fake-tag'), 1); + assert.strictEqual(compareVersion(`${major}.${minor}.${patch}-fake-tag`), 1); assert.strictEqual(compareVersion(major, minor, patch), 0); }