fix: widening support in composer with "less than" range (#10775)

This commit is contained in:
Phil Nelson 2021-07-10 22:52:24 +10:00 committed by GitHub
parent 6deaa50c91
commit d6b2a85efc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 52 additions and 1 deletions

View file

@ -312,6 +312,8 @@ describe('semver.getNewValue()', () => {
newVersion: '5.1.0', newVersion: '5.1.0',
}) })
).toEqual('~1.2 || ~2.0 || ~3.0 || ~5.0'); ).toEqual('~1.2 || ~2.0 || ~3.0 || ~5.0');
});
it('handles widen strategy', () => {
expect( expect(
semver.getNewValue({ semver.getNewValue({
currentValue: '^1.2', currentValue: '^1.2',
@ -352,6 +354,46 @@ describe('semver.getNewValue()', () => {
newVersion: '2.1.0', newVersion: '2.1.0',
}) })
).toEqual('^1.0 || ^2.0'); ).toEqual('^1.0 || ^2.0');
expect(
semver.getNewValue({
currentValue: '>=1.0 <3.0',
rangeStrategy: 'widen',
currentVersion: '2.9.0',
newVersion: '4.1.0',
})
).toEqual('>=1.0 <4.2');
expect(
semver.getNewValue({
currentValue: '>=1.0 <3.0',
rangeStrategy: 'widen',
currentVersion: '2.9.0',
newVersion: '2.9.5',
})
).toEqual('>=1.0 <3.0');
expect(
semver.getNewValue({
currentValue: '>=1.0 <3.0',
rangeStrategy: 'widen',
currentVersion: '2.9.0',
newVersion: '3.0',
})
).toEqual('>=1.0 <3.1');
expect(
semver.getNewValue({
currentValue: '>=1.0.0 <=3.0.4',
rangeStrategy: 'widen',
currentVersion: '2.9.0',
newVersion: '3.0.5',
})
).toEqual('>=1.0.0 <=3.0.5');
expect(
semver.getNewValue({
currentValue: '~1.0 || >=3.0 <=4.0',
rangeStrategy: 'widen',
currentVersion: '2.9.0',
newVersion: '5.0.0',
})
).toEqual('~1.0 || >=3.0 <=5.0');
}); });
it('returns newVersion if unsupported', () => { it('returns newVersion if unsupported', () => {
expect( expect(

View file

@ -1,4 +1,5 @@
import { coerce } from 'semver'; import { coerce } from 'semver';
import { parseRange } from 'semver-utils';
import { logger } from '../../logger'; import { logger } from '../../logger';
import { api as npm } from '../npm'; import { api as npm } from '../npm';
import type { NewValueConfig, VersioningApi } from '../types'; import type { NewValueConfig, VersioningApi } from '../types';
@ -189,11 +190,19 @@ function getNewValue({
}); });
if (rangeStrategy === 'replace') { if (rangeStrategy === 'replace') {
newValue = replacementValue; newValue = replacementValue;
} else {
const parsedRange = parseRange(replacementValue);
const element = parsedRange[parsedRange.length - 1];
if (element.operator?.startsWith('<')) {
const splitCurrent = currentValue.split(element.operator);
splitCurrent.pop();
newValue = splitCurrent.join(element.operator) + replacementValue;
} else { } else {
newValue = currentValue + ' || ' + replacementValue; newValue = currentValue + ' || ' + replacementValue;
} }
} }
} }
}
if (!newValue) { if (!newValue) {
logger.warn( logger.warn(