From c85f4630a6768eb5620a668c74157a109ebeceac Mon Sep 17 00:00:00 2001
From: Rhys Arkins <rhys@arkins.net>
Date: Mon, 12 Sep 2022 14:36:44 +0300
Subject: [PATCH] fix(package-rules): matchCurrentVersion with locked versions
 (#17751)

---
 .../package-rules/current-version.spec.ts     | 41 +++++++++++++++++++
 lib/util/package-rules/current-version.ts     | 21 ++++++----
 2 files changed, 53 insertions(+), 9 deletions(-)

diff --git a/lib/util/package-rules/current-version.spec.ts b/lib/util/package-rules/current-version.spec.ts
index 929e4db345..077358af3b 100644
--- a/lib/util/package-rules/current-version.spec.ts
+++ b/lib/util/package-rules/current-version.spec.ts
@@ -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();
+    });
   });
 });
diff --git a/lib/util/package-rules/current-version.ts b/lib/util/package-rules/current-version.ts
index 0fb65ad8da..d93cb11e66 100644
--- a/lib/util/package-rules/current-version.ts
+++ b/lib/util/package-rules/current-version.ts
@@ -18,12 +18,8 @@ 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(
@@ -31,13 +27,20 @@ export class CurrentVersionMatcher extends Matcher {
     );
 
     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;
@@ -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)) {
-- 
GitLab