From 6bb76c53b6bc09afbeac2be7df5ef159a114ede6 Mon Sep 17 00:00:00 2001
From: Rhys Arkins <rhys@arkins.net>
Date: Mon, 27 Apr 2020 09:57:02 +0200
Subject: [PATCH] fix(cargo): handle * getNewValue

---
 lib/versioning/cargo/index.spec.ts | 18 ++++++++++++++++++
 lib/versioning/cargo/index.ts      | 16 ++++++++++++++++
 2 files changed, 34 insertions(+)

diff --git a/lib/versioning/cargo/index.spec.ts b/lib/versioning/cargo/index.spec.ts
index a75fe37271..cc3f97701e 100644
--- a/lib/versioning/cargo/index.spec.ts
+++ b/lib/versioning/cargo/index.spec.ts
@@ -103,6 +103,24 @@ describe('semver.isSingleVersion()', () => {
   });
 });
 describe('semver.getNewValue()', () => {
+  it('returns if empty or *', () => {
+    expect(
+      semver.getNewValue({
+        currentValue: null,
+        rangeStrategy: 'bump',
+        fromVersion: '1.0.0',
+        toVersion: '1.1.0',
+      })
+    ).toEqual(null);
+    expect(
+      semver.getNewValue({
+        currentValue: '*',
+        rangeStrategy: 'bump',
+        fromVersion: '1.0.0',
+        toVersion: '1.1.0',
+      })
+    ).toEqual('*');
+  });
   it('bumps equals', () => {
     expect(
       semver.getNewValue({
diff --git a/lib/versioning/cargo/index.ts b/lib/versioning/cargo/index.ts
index cf605c34dd..2cdd3f8303 100644
--- a/lib/versioning/cargo/index.ts
+++ b/lib/versioning/cargo/index.ts
@@ -1,5 +1,6 @@
 import { api as npm } from '../npm';
 import { VersioningApi, NewValueConfig } from '../common';
+import { logger } from '../../logger';
 
 export const id = 'cargo';
 export const displayName = 'Cargo';
@@ -33,6 +34,10 @@ function notEmpty(s: string): boolean {
 }
 
 function npm2cargo(input: string): string {
+  // istanbul ignore if
+  if (!input) {
+    return input;
+  }
   // Note: this doesn't remove the ^
   const res = input
     .split(' ')
@@ -73,6 +78,9 @@ function getNewValue({
   fromVersion,
   toVersion,
 }: NewValueConfig): string {
+  if (!currentValue || currentValue === '*') {
+    return currentValue;
+  }
   if (rangeStrategy === 'pin' || isSingleVersion(currentValue)) {
     let res = '=';
     if (currentValue.startsWith('= ')) {
@@ -88,6 +96,14 @@ function getNewValue({
     toVersion,
   });
   let newCargo = npm2cargo(newSemver);
+  // istanbul ignore if
+  if (!newCargo) {
+    logger.info(
+      { currentValue, newSemver },
+      'Could not get cargo version from semver'
+    );
+    return currentValue;
+  }
   // Try to reverse any caret we added
   if (newCargo.startsWith('^') && !currentValue.startsWith('^')) {
     newCargo = newCargo.substring(1);
-- 
GitLab