-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Handle development version increments according to SemVer specif…
…ication (#93) * refactor: extract version change code to function for testability * test: add suite for applyChangesToVersion function * fix: do not increment major version in development versions According to SemVer 2.0.0 §4 breaking changes do not increment the major version. * test: semver incrementation on development versions
- Loading branch information
Showing
3 changed files
with
85 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
function applyChangesToVersion(version, changes) { | ||
// SemVer 2.0.0 §4: public api should not be considered stable when major version is 0 | ||
if (version.major === 0) { | ||
// never change major version in development releases | ||
if (changes.breaking > 0 || changes.feature > 0) { | ||
version.minor++; | ||
version.patch = 0; | ||
} else { | ||
version.patch++; | ||
} | ||
} else { | ||
if (changes.breaking > 0) { | ||
version.major++; | ||
version.minor = 0; | ||
version.patch = 0; | ||
} else if (changes.feature > 0) { | ||
version.minor++; | ||
version.patch = 0; | ||
} else { | ||
version.patch++; | ||
} | ||
} | ||
} | ||
|
||
module.exports = { | ||
applyChangesToVersion, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
const { applyChangesToVersion } = require("../lib/semver"); | ||
|
||
const createApplyChangesTest = (versionString, changesString, expectedString) => { | ||
test(`applyChangesToVersion - ${versionString} + ${changesString} -> ${expectedString}`, async () => { | ||
// GIVEN | ||
const p = s => parseInt(s, 10); | ||
const v = versionString.split('.').map(p); | ||
const c = changesString.split('.').map(p); | ||
const e = expectedString.split('.').map(p); | ||
const version = { | ||
major: v[0], | ||
minor: v[1], | ||
patch: v[2], | ||
}; | ||
const changes = { | ||
breaking: c[0], | ||
feature: c[1], | ||
patch: c[2], | ||
}; | ||
|
||
|
||
// WHEN | ||
applyChangesToVersion(version, changes); | ||
|
||
|
||
// THEN | ||
expect(version).toEqual({ | ||
major: e[0], | ||
minor: e[1], | ||
patch: e[2], | ||
}); | ||
}); | ||
}; | ||
|
||
// XXX | ||
// createApplyChangesTest("1.0.0", "0.0.0", "1.0.0"); | ||
|
||
// pure changes (either major, minor or patch) to stable version | ||
createApplyChangesTest("1.0.0", "0.0.1", "1.0.1"); | ||
createApplyChangesTest("1.0.0", "0.1.0", "1.1.0"); | ||
createApplyChangesTest("1.0.0", "1.0.0", "2.0.0"); | ||
|
||
// mixed changes (at least two of major, minor or patch) to stable version | ||
createApplyChangesTest("1.0.0", "0.1.1", "1.1.0"); | ||
createApplyChangesTest("1.0.0", "1.1.0", "2.0.0"); | ||
createApplyChangesTest("1.0.0", "1.0.1", "2.0.0"); | ||
|
||
// pure changes (either major, minor or patch) to development version | ||
createApplyChangesTest("0.1.0", "0.0.1", "0.1.1"); | ||
createApplyChangesTest("0.1.0", "0.1.0", "0.2.0"); | ||
createApplyChangesTest("0.1.0", "1.0.0", "0.2.0"); | ||
|
||
// mixed changes (at least two of major, minor or patch) to development version | ||
createApplyChangesTest("0.1.0", "0.1.1", "0.2.0"); | ||
createApplyChangesTest("0.1.0", "1.1.0", "0.2.0"); | ||
createApplyChangesTest("0.1.0", "1.0.1", "0.2.0"); |