mirror of
https://github.com/renovatebot/renovate.git
synced 2025-01-13 15:36:25 +00:00
fix(maven): Use consistent precision for extended ranges (#6623)
This commit is contained in:
parent
d144013af1
commit
c450621edb
2 changed files with 87 additions and 18 deletions
|
@ -113,7 +113,7 @@ const zeroToken: NumberToken = {
|
|||
isTransition: false,
|
||||
};
|
||||
|
||||
function tokenize(versionStr: string): Token[] {
|
||||
function tokenize(versionStr: string, preserveMinorZeroes = false): Token[] {
|
||||
let buf: Token[] = [];
|
||||
let result: Token[] = [];
|
||||
let leadingZero = true;
|
||||
|
@ -126,7 +126,7 @@ function tokenize(versionStr: string): Token[] {
|
|||
leadingZero = false;
|
||||
result = result.concat(buf);
|
||||
buf = [];
|
||||
} else if (leadingZero) {
|
||||
} else if (leadingZero || preserveMinorZeroes) {
|
||||
result = result.concat(buf);
|
||||
buf = [];
|
||||
}
|
||||
|
@ -444,6 +444,36 @@ function rangeToStr(fullRange: Range[]): string | null {
|
|||
return intervals.join(',');
|
||||
}
|
||||
|
||||
function tokensToStr(tokens: Token[]): string {
|
||||
return tokens.reduce((result, token, idx) => {
|
||||
const prefix = token.prefix === PREFIX_DOT ? '.' : '-';
|
||||
return `${result}${idx !== 0 && token.val !== '' ? prefix : ''}${
|
||||
token.val
|
||||
}`;
|
||||
}, '');
|
||||
}
|
||||
|
||||
function coerceRangeValue(prev: string, next: string): string {
|
||||
const prevTokens = tokenize(prev, true);
|
||||
const nextTokens = tokenize(next, true);
|
||||
const resultTokens = nextTokens.slice(0, prevTokens.length);
|
||||
const align = Math.max(0, prevTokens.length - nextTokens.length);
|
||||
if (align > 0) {
|
||||
resultTokens.push(...prevTokens.slice(prevTokens.length - align));
|
||||
}
|
||||
return tokensToStr(resultTokens);
|
||||
}
|
||||
|
||||
function incrementRangeValue(value: string): string {
|
||||
const tokens = tokenize(value);
|
||||
const lastToken = tokens[tokens.length - 1];
|
||||
if (typeof lastToken.val === 'number') {
|
||||
lastToken.val += 1;
|
||||
return coerceRangeValue(value, tokensToStr(tokens));
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
function autoExtendMavenRange(
|
||||
currentRepresentation: string,
|
||||
newValue: string
|
||||
|
@ -482,17 +512,40 @@ function autoExtendMavenRange(
|
|||
return currentRepresentation;
|
||||
}
|
||||
}
|
||||
|
||||
const interval = range[nearestIntervalIdx];
|
||||
if (interval.rightValue !== null) {
|
||||
interval.rightValue = newValue;
|
||||
const { leftValue, rightValue } = interval;
|
||||
if (
|
||||
leftValue !== null &&
|
||||
rightValue !== null &&
|
||||
incrementRangeValue(leftValue) === rightValue
|
||||
) {
|
||||
interval.leftValue = coerceRangeValue(leftValue, newValue);
|
||||
interval.rightValue = incrementRangeValue(interval.leftValue);
|
||||
} else if (rightValue !== null) {
|
||||
if (interval.rightType === INCLUDING_POINT) {
|
||||
const tokens = tokenize(rightValue);
|
||||
const lastToken = tokens[tokens.length - 1];
|
||||
if (typeof lastToken.val === 'number') {
|
||||
interval.rightValue = coerceRangeValue(rightValue, newValue);
|
||||
} else {
|
||||
interval.leftValue = newValue;
|
||||
interval.rightValue = newValue;
|
||||
}
|
||||
} else {
|
||||
interval.rightValue = incrementRangeValue(
|
||||
coerceRangeValue(rightValue, newValue)
|
||||
);
|
||||
}
|
||||
} else if (leftValue !== null) {
|
||||
interval.leftValue = coerceRangeValue(leftValue, newValue);
|
||||
}
|
||||
|
||||
if (interval.leftValue && interval.rightValue) {
|
||||
if (compare(interval.leftValue, interval.rightValue) !== 1) {
|
||||
return rangeToStr(range);
|
||||
}
|
||||
return currentRepresentation;
|
||||
const correctRepresentation =
|
||||
compare(interval.leftValue, interval.rightValue) !== 1
|
||||
? rangeToStr(range)
|
||||
: null;
|
||||
return correctRepresentation || currentRepresentation;
|
||||
}
|
||||
return rangeToStr(range);
|
||||
}
|
||||
|
|
|
@ -245,21 +245,37 @@ describe('versioning/maven/compare', () => {
|
|||
const sample = [
|
||||
['[1.2.3]', '1.2.3', '[1.2.3]'],
|
||||
['[1.2.3]', '1.2.4', '[1.2.4]'],
|
||||
['[1.0.0,1.2.3]', '0.0.1', '[1.0.0,1.2.3]'],
|
||||
['[1.0.0,1.2.3]', '1.2.4', '[1.0.0,1.2.4]'],
|
||||
['[1.0.0,1.2.23]', '1.1.0', '[1.0.0,1.2.23]'],
|
||||
['(,1.0]', '2.0', '(,2.0]'],
|
||||
['],1.0]', '2.0', '],2.0]'],
|
||||
['(,1.0)', '2.0', '(,2.0)'],
|
||||
['],1.0[', '2.0', '],2.0['],
|
||||
['[1.0,1.2],[1.3,1.5)', '1.2.4', '[1.0,1.2.4],[1.3,1.5)'],
|
||||
['[1.0,1.2],[1.3,1.5[', '1.2.4', '[1.0,1.2.4],[1.3,1.5['],
|
||||
['(,1.0)', '2.0', '(,3.0)'],
|
||||
['],1.0[', '2.0', '],3.0['],
|
||||
['[1.0,1.2.3],[1.3,1.5)', '1.2.4', '[1.0,1.2.4],[1.3,1.5)'],
|
||||
['[1.0,1.2.3],[1.3,1.5[', '1.2.4', '[1.0,1.2.4],[1.3,1.5['],
|
||||
['[1.2.3,)', '1.2.4', '[1.2.4,)'],
|
||||
['[1.2.3,[', '1.2.4', '[1.2.4,['],
|
||||
['[1.2.3,]', '1.2.4', '[1.2.3,]'], // invalid range
|
||||
['[0.21,0.22)', '0.20.21', '[0.21,0.22)'],
|
||||
['[0.21,0.22)', '0.20.21', '[0.20,0.21)'],
|
||||
['[0.21,0.22)', '0.21.1', '[0.21,0.22)'],
|
||||
['[0.21,0.22)', '0.22.1', '[0.21,0.22.1)'],
|
||||
['[0.21,0.22)', '0.23', '[0.21,0.23)'],
|
||||
['[0.21,0.22.0)', '0.22.1', '[0.21,0.22.2)'],
|
||||
['[0.21,0.22)', '0.23', '[0.23,0.24)'],
|
||||
|
||||
['[1.8,1.9)', '1.9.0.1', '[1.9,1.10)'],
|
||||
['[1.8a,1.9)', '1.9.0.1', '[1.8a,1.10)'],
|
||||
['[1.8,1.9.0)', '1.9.0.1', '[1.8,1.10.0)'],
|
||||
['[1.8,1.9.0.0)', '1.9.0.1', '[1.8,1.9.0.2)'],
|
||||
['[1.8,1.9.0.0)', '1.10.1', '[1.8,1.10.2.0)'],
|
||||
|
||||
['[1.8,1.9)', '1.9.1', '[1.9,1.10)'],
|
||||
['[1.8,1.9)', '1.10.0', '[1.10,1.11)'],
|
||||
['[1.8,1.9)', '1.10.1', '[1.10,1.11)'],
|
||||
|
||||
['(,1.0.0]', '2.0.0', '(,2.0.0]'],
|
||||
['(,1.0]', '2.0.0', '(,2.0]'],
|
||||
['(,1]', '2.0.0', '(,2]'],
|
||||
['(,1.0.0-foobar]', '2.0.0', '(,2.0.0]'],
|
||||
];
|
||||
sample.forEach(([oldRepr, newValue, newRepr]) => {
|
||||
expect(autoExtendMavenRange(oldRepr, newValue)).toEqual(newRepr);
|
||||
|
|
Loading…
Reference in a new issue