Skip to content

Commit

Permalink
fix(package-rules): matchCurrentVersion with locked versions (#17751)
Browse files Browse the repository at this point in the history
  • Loading branch information
rarkins authored Sep 12, 2022
1 parent 93b66bd commit c85f463
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 9 deletions.
41 changes: 41 additions & 0 deletions lib/util/package-rules/current-version.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,46 @@ describe('util/package-rules/current-version', () => {
);
expect(result).toBeFalse();
});

it('return false for regex version non match', () => {
const result = matcher.matches(
{
versioning: 'ruby',
currentValue: '"~> 1.1.0"',
lockedVersion: '1.1.4',
},
{
matchCurrentVersion: '/^v?[~ -]?0/',
}
);
expect(result).toBeFalse();
});

it('return true for regex version match', () => {
const result = matcher.matches(
{
versioning: 'ruby',
currentValue: '"~> 0.1.0"',
lockedVersion: '0.1.0',
},
{
matchCurrentVersion: '/^v?[~ -]?0/',
}
);
expect(result).toBeTrue();
});

it('return false for regex value match', () => {
const result = matcher.matches(
{
versioning: 'ruby',
currentValue: '"~> 0.1.0"',
},
{
matchCurrentVersion: '/^v?[~ -]?0/',
}
);
expect(result).toBeFalse();
});
});
});
21 changes: 12 additions & 9 deletions lib/util/package-rules/current-version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,29 @@ export class CurrentVersionMatcher extends Matcher {
if (is.undefined(matchCurrentVersion)) {
return null;
}

if (is.nullOrUndefined(currentValue)) {
return false;
}

const isUnconstrainedValue = !!lockedVersion;
const isUnconstrainedValue =
!!lockedVersion && is.nullOrUndefined(currentValue);
const version = allVersioning.get(versioning);
const matchCurrentVersionStr = matchCurrentVersion.toString();
const matchCurrentVersionPred = configRegexPredicate(
matchCurrentVersionStr
);

if (matchCurrentVersionPred) {
return !(!isUnconstrainedValue && !matchCurrentVersionPred(currentValue));
const compareVersion = lockedVersion ?? currentVersion ?? currentValue;
return (
!is.nullOrUndefined(compareVersion) &&
matchCurrentVersionPred(compareVersion)
);
}
if (version.isVersion(matchCurrentVersionStr)) {
try {
return (
isUnconstrainedValue ||
version.matches(matchCurrentVersionStr, currentValue)
!!(
currentValue &&
version.matches(matchCurrentVersionStr, currentValue)
)
);
} catch (err) {
return false;
Expand All @@ -47,7 +50,7 @@ export class CurrentVersionMatcher extends Matcher {
const compareVersion = version.isVersion(currentValue)
? currentValue // it's a version so we can match against it
: lockedVersion ?? currentVersion; // need to match against this currentVersion, if available
if (is.undefined(compareVersion)) {
if (is.nullOrUndefined(compareVersion)) {
return false;
}
if (version.isVersion(compareVersion)) {
Expand Down

0 comments on commit c85f463

Please sign in to comment.