Skip to content

Commit

Permalink
fix: Handle development version increments according to SemVer specif…
Browse files Browse the repository at this point in the history
…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
bastidest committed Jul 11, 2022
1 parent e2aa89a commit 8226d92
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 10 deletions.
12 changes: 2 additions & 10 deletions lib/gitCommitConvention.js
@@ -1,4 +1,5 @@
const Git = require("./git");
const { applyChangesToVersion } = require("./semver");

module.exports = function(convention, commitAnchor = 'HEAD') {

Expand Down Expand Up @@ -57,16 +58,7 @@ module.exports = function(convention, commitAnchor = 'HEAD') {
}
});

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++;
}
applyChangesToVersion(version, changes);

return `${version.major}.${version.minor}.${version.patch}`;
}
Expand Down
27 changes: 27 additions & 0 deletions lib/semver.js
@@ -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,
};
56 changes: 56 additions & 0 deletions test/semver.test.js
@@ -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");

0 comments on commit 8226d92

Please sign in to comment.