Skip to content
Snippets Groups Projects
Commit 91d3729b authored by Rhys Arkins's avatar Rhys Arkins
Browse files

feat: hashicorp semver

Hashicorp’s semver is same as npm’s except for handling of “~> 1.2”. For Hashicorp it means ^1.2 whereas for Hashicorp is means ~1.2.
parent efa67edf
No related branches found
No related tags found
No related merge requests found
const docker = require('./docker');
const semver = require('./semver');
const semverComposer = require('./semver-composer');
const semverHashicorp = require('./semver-hashicorp');
const pep440 = require('./pep440');
const schemes = {
docker,
semver,
semverComposer,
semverHashicorp,
pep440,
};
......
const semver = require('../semver');
function hashicorp2npm(input) {
// The only case incompatible with semver is a "short" ~>, e.g. ~> 1.2
return input.replace(/~>(\s*\d+\.\d+$)/, '^$1');
}
const matches = (version, range) =>
semver.matches(hashicorp2npm(version), hashicorp2npm(range));
const maxSatisfyingVersion = (versions, range) =>
semver.maxSatisfyingVersion(
versions.map(hashicorp2npm),
hashicorp2npm(range)
);
const minSatisfyingVersion = (versions, range) =>
semver.minSatisfyingVersion(
versions.map(hashicorp2npm),
hashicorp2npm(range)
);
function getNewValue(currentValue, rangeStrategy, fromVersion, toVersion) {
// handle specia. ~> 1.2 case
if (currentValue.match(/(~>\s*)\d+\.\d+$/)) {
return currentValue.replace(
/(~>\s*)\d+\.\d+$/,
`$1${semver.getMajor(toVersion)}.0`
);
}
return semver.getNewValue(
currentValue,
rangeStrategy,
fromVersion,
toVersion
);
}
module.exports = {
...semver,
matches,
maxSatisfyingVersion,
minSatisfyingVersion,
getNewValue,
};
const semver = require('../../lib/versioning')('semverHashicorp');
describe('semver.matches()', () => {
it('handles tilde greater than', () => {
expect(semver.matches('4.2.0', '~> 4.0')).toBe(true);
expect(semver.matches('4.2.0', '~> 4.0.0')).toBe(false);
});
});
describe('semver.maxSatisfyingVersion()', () => {
it('handles tilde greater than', () => {
expect(
semver.maxSatisfyingVersion(
['0.4.0', '0.5.0', '4.0.0', '4.2.0', '5.0.0'],
'~> 4.0'
)
).toBe('4.2.0');
expect(
semver.maxSatisfyingVersion(
['0.4.0', '0.5.0', '4.0.0', '4.2.0', '5.0.0'],
'~> 4.0.0'
)
).toBe('4.0.0');
});
});
describe('semver.minSatisfyingVersion()', () => {
it('handles tilde greater than', () => {
expect(
semver.minSatisfyingVersion(
['0.4.0', '0.5.0', '4.2.0', '5.0.0'],
'~> 4.0'
)
).toBe('4.2.0');
expect(
semver.minSatisfyingVersion(
['0.4.0', '0.5.0', '4.2.0', '5.0.0'],
'~> 4.0.0'
)
).toBe(null);
});
});
describe('semver.getNewValue()', () => {
it('handles tilde greater than', () => {
expect(semver.getNewValue('~> 1.2', 'replace', '1.2.3', '2.0.7')).toEqual(
'~> 2.0'
);
expect(semver.getNewValue('~> 1.2.0', 'replace', '1.2.3', '2.0.7')).toEqual(
'~> 2.0.0'
);
});
});
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment