From 4fad63ce542ac3597fc14d08cf86ed17d369cccf Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Mart=C3=ADn=20Fern=C3=A1ndez?= <fmartin91@gmail.com>
Date: Wed, 20 May 2020 13:28:11 -0300
Subject: [PATCH] feat(bundler): Allow setting different rangeStrategy (#6274)

Co-authored-by: Michael Kriese <michael.kriese@visualon.de>
---
 lib/manager/bundler/range.spec.ts | 10 ++++++++--
 lib/manager/bundler/range.ts      |  8 ++++++--
 lib/versioning/ruby/index.spec.ts | 17 +++++++++++++++++
 lib/versioning/ruby/index.ts      | 12 ++++++++++++
 4 files changed, 43 insertions(+), 4 deletions(-)

diff --git a/lib/manager/bundler/range.spec.ts b/lib/manager/bundler/range.spec.ts
index a14cce7bdb..e12af89204 100644
--- a/lib/manager/bundler/range.spec.ts
+++ b/lib/manager/bundler/range.spec.ts
@@ -1,9 +1,15 @@
+import { RangeConfig } from '../common';
 import { getRangeStrategy } from '.';
 
 describe('lib/manager/bundler/range', () => {
   describe('getRangeStrategy()', () => {
-    it('always returns replace', () => {
-      expect(getRangeStrategy()).toEqual('replace');
+    it('returns replace when rangeStrategy is auto', () => {
+      const config: RangeConfig = { rangeStrategy: 'auto' };
+      expect(getRangeStrategy(config)).toEqual('replace');
+    });
+    it('returns the config value when rangeStrategy is different than auto', () => {
+      const config: RangeConfig = { rangeStrategy: 'update-lockfile' };
+      expect(getRangeStrategy(config)).toEqual('update-lockfile');
     });
   });
 });
diff --git a/lib/manager/bundler/range.ts b/lib/manager/bundler/range.ts
index ae7626226f..2bd725bf79 100644
--- a/lib/manager/bundler/range.ts
+++ b/lib/manager/bundler/range.ts
@@ -1,4 +1,5 @@
 import { RangeStrategy } from '../../versioning';
+import { RangeConfig } from '../common';
 
 /*
  * The getRangeStrategy() function is optional and can be removed if not applicable.
@@ -12,7 +13,10 @@ import { RangeStrategy } from '../../versioning';
  * If this function is not present then the default 'replace' value will be used.
  *
  */
+export function getRangeStrategy(config: RangeConfig): RangeStrategy {
+  if (config.rangeStrategy === 'auto') {
+    return 'replace';
+  }
 
-export function getRangeStrategy(): RangeStrategy {
-  return 'replace';
+  return config.rangeStrategy;
 }
diff --git a/lib/versioning/ruby/index.spec.ts b/lib/versioning/ruby/index.spec.ts
index f93730f939..bef39d480f 100644
--- a/lib/versioning/ruby/index.spec.ts
+++ b/lib/versioning/ruby/index.spec.ts
@@ -529,5 +529,22 @@ describe('semverRuby', () => {
         }
       );
     });
+    it('returns correct version for update-lockfile strategy', () => {
+      [
+        ['~> 6.0.0', '~> 6.0.0', 'update-lockfile', '6.0.2', '6.0.3'],
+        ['~> 7.0.0', '~> 6.0.0', 'update-lockfile', '6.0.2', '7.0.0'],
+      ].forEach(
+        ([expected, currentValue, rangeStrategy, fromVersion, toVersion]) => {
+          expect(
+            semverRuby.getNewValue({
+              currentValue,
+              rangeStrategy: rangeStrategy as RangeStrategy,
+              fromVersion,
+              toVersion,
+            })
+          ).toEqual(expected);
+        }
+      );
+    });
   });
 });
diff --git a/lib/versioning/ruby/index.ts b/lib/versioning/ruby/index.ts
index 0f1960d3ed..b4c14327fb 100644
--- a/lib/versioning/ruby/index.ts
+++ b/lib/versioning/ruby/index.ts
@@ -91,6 +91,18 @@ const getNewValue = ({
     newValue = currentValue.replace(fromVersion, toVersion);
   } else {
     switch (rangeStrategy) {
+      case 'update-lockfile':
+        if (satisfies(toVersion, currentValue)) {
+          newValue = currentValue;
+        } else {
+          newValue = getNewValue({
+            currentValue,
+            rangeStrategy: 'replace',
+            fromVersion,
+            toVersion,
+          });
+        }
+        break;
       case 'pin':
         newValue = pin({ to: vtrim(toVersion) });
         break;
-- 
GitLab