From b0fdfeba5874fd9301a2bc01e3ff614b13c20fe4 Mon Sep 17 00:00:00 2001
From: Rhys Arkins <rhys@arkins.net>
Date: Mon, 22 Apr 2019 17:11:15 +0200
Subject: [PATCH] refactor(pipenv): write registryUrls to packageFile level
 (#3574)

---
 lib/manager/pipenv/extract.js                 |  21 +-
 .../pipenv/__snapshots__/extract.spec.js.snap | 219 +++++++-----------
 test/manager/pipenv/extract.spec.js           |  40 ++--
 3 files changed, 114 insertions(+), 166 deletions(-)

diff --git a/lib/manager/pipenv/extract.js b/lib/manager/pipenv/extract.js
index dc0119d6da..9d903b2d21 100644
--- a/lib/manager/pipenv/extract.js
+++ b/lib/manager/pipenv/extract.js
@@ -24,22 +24,22 @@ function extractPackageFile(content) {
     logger.debug({ err }, 'Error parsing Pipfile');
     return null;
   }
-  let registryUrls;
+  const res = {};
   if (pipfile.source) {
-    registryUrls = pipfile.source.map(source => source.url);
+    res.registryUrls = pipfile.source.map(source => source.url);
   }
 
-  const deps = [
-    ...extractFromSection(pipfile, 'packages', registryUrls),
-    ...extractFromSection(pipfile, 'dev-packages', registryUrls),
+  res.deps = [
+    ...extractFromSection(pipfile, 'packages'),
+    ...extractFromSection(pipfile, 'dev-packages'),
   ];
-  if (!deps.length) {
-    return null;
+  if (res.deps.length) {
+    return res;
   }
-  return { deps };
+  return null;
 }
 
-function extractFromSection(pipfile, section, registryUrls) {
+function extractFromSection(pipfile, section) {
   if (!(section in pipfile)) {
     return [];
   }
@@ -96,9 +96,6 @@ function extractFromSection(pipfile, section, registryUrls) {
         dep.datasource = 'pypi';
       }
       if (pipenvNestedVersion) dep.pipenvNestedVersion = pipenvNestedVersion;
-      if (registryUrls) {
-        dep.registryUrls = registryUrls;
-      }
       return dep;
     })
     .filter(Boolean);
diff --git a/test/manager/pipenv/__snapshots__/extract.spec.js.snap b/test/manager/pipenv/__snapshots__/extract.spec.js.snap
index 5f82e838a3..2d12887d66 100644
--- a/test/manager/pipenv/__snapshots__/extract.spec.js.snap
+++ b/test/manager/pipenv/__snapshots__/extract.spec.js.snap
@@ -1,69 +1,51 @@
 // Jest Snapshot v1, https://goo.gl/fbAQLP
 
 exports[`lib/manager/pipenv/extract extractPackageFile() extracts dependencies 1`] = `
-Array [
-  Object {
-    "currentValue": "==0.3.1",
-    "datasource": "pypi",
-    "depName": "some-package",
-    "depType": "packages",
-    "registryUrls": Array [
-      "https://pypi.org/simple",
-      "http://example.com/private-pypi/",
-    ],
-  },
-  Object {
-    "currentValue": "==1.0.0",
-    "datasource": "pypi",
-    "depName": "some-other-package",
-    "depType": "packages",
-    "registryUrls": Array [
-      "https://pypi.org/simple",
-      "http://example.com/private-pypi/",
-    ],
-  },
-  Object {
-    "currentValue": "==1.0.0",
-    "depName": "_invalid-package",
-    "depType": "packages",
-    "registryUrls": Array [
-      "https://pypi.org/simple",
-      "http://example.com/private-pypi/",
-    ],
-    "skipReason": "invalid-name",
-  },
-  Object {
-    "currentValue": "==0 0",
-    "depName": "invalid-version",
-    "depType": "packages",
-    "registryUrls": Array [
-      "https://pypi.org/simple",
-      "http://example.com/private-pypi/",
-    ],
-    "skipReason": "invalid-version",
-  },
-  Object {
-    "currentValue": "==1.0.0",
-    "datasource": "pypi",
-    "depName": "pytest-benchmark",
-    "depType": "packages",
-    "pipenvNestedVersion": true,
-    "registryUrls": Array [
-      "https://pypi.org/simple",
-      "http://example.com/private-pypi/",
-    ],
-  },
-  Object {
-    "currentValue": "==0.1.0",
-    "datasource": "pypi",
-    "depName": "dev-package",
-    "depType": "dev-packages",
-    "registryUrls": Array [
-      "https://pypi.org/simple",
-      "http://example.com/private-pypi/",
-    ],
-  },
-]
+Object {
+  "deps": Array [
+    Object {
+      "currentValue": "==0.3.1",
+      "datasource": "pypi",
+      "depName": "some-package",
+      "depType": "packages",
+    },
+    Object {
+      "currentValue": "==1.0.0",
+      "datasource": "pypi",
+      "depName": "some-other-package",
+      "depType": "packages",
+    },
+    Object {
+      "currentValue": "==1.0.0",
+      "depName": "_invalid-package",
+      "depType": "packages",
+      "skipReason": "invalid-name",
+    },
+    Object {
+      "currentValue": "==0 0",
+      "depName": "invalid-version",
+      "depType": "packages",
+      "skipReason": "invalid-version",
+    },
+    Object {
+      "currentValue": "==1.0.0",
+      "datasource": "pypi",
+      "depName": "pytest-benchmark",
+      "depType": "packages",
+      "pipenvNestedVersion": true,
+    },
+    Object {
+      "currentValue": "==0.1.0",
+      "datasource": "pypi",
+      "depName": "dev-package",
+      "depType": "dev-packages",
+    },
+  ],
+  "registryUrls": Array [
+    "https://pypi.org/simple",
+    "http://example.com/private-pypi/",
+  ],
+}
 `;
 
 exports[`lib/manager/pipenv/extract extractPackageFile() extracts example pipfile 1`] = `
@@ -72,9 +54,6 @@ Object {
     Object {
       "depName": "requests",
       "depType": "packages",
-      "registryUrls": Array [
-        "https://pypi.python.org/simple",
-      ],
       "skipReason": "any-version",
     },
     Object {
@@ -82,32 +61,20 @@ Object {
       "datasource": "pypi",
       "depName": "records",
       "depType": "packages",
-      "registryUrls": Array [
-        "https://pypi.python.org/simple",
-      ],
     },
     Object {
       "depName": "django",
       "depType": "packages",
-      "registryUrls": Array [
-        "https://pypi.python.org/simple",
-      ],
       "skipReason": "git-dependency",
     },
     Object {
       "depName": "e682b37",
       "depType": "packages",
-      "registryUrls": Array [
-        "https://pypi.python.org/simple",
-      ],
       "skipReason": "file-dependency",
     },
     Object {
       "depName": "e1839a8",
       "depType": "packages",
-      "registryUrls": Array [
-        "https://pypi.python.org/simple",
-      ],
       "skipReason": "local-dependency",
     },
     Object {
@@ -115,18 +82,12 @@ Object {
       "depName": "pywinusb",
       "depType": "packages",
       "pipenvNestedVersion": true,
-      "registryUrls": Array [
-        "https://pypi.python.org/simple",
-      ],
       "skipReason": "any-version",
     },
     Object {
       "currentValue": "*",
       "depName": "nose",
       "depType": "dev-packages",
-      "registryUrls": Array [
-        "https://pypi.python.org/simple",
-      ],
       "skipReason": "any-version",
     },
     Object {
@@ -135,60 +96,50 @@ Object {
       "depName": "unittest2",
       "depType": "dev-packages",
       "pipenvNestedVersion": true,
-      "registryUrls": Array [
-        "https://pypi.python.org/simple",
-      ],
     },
   ],
+  "registryUrls": Array [
+    "https://pypi.python.org/simple",
+  ],
 }
 `;
 
 exports[`lib/manager/pipenv/extract extractPackageFile() extracts multiple dependencies 1`] = `
-Array [
-  Object {
-    "currentValue": "==1",
-    "datasource": "pypi",
-    "depName": "Django",
-    "depType": "packages",
-    "registryUrls": Array [
-      "https://pypi.org/simple",
-    ],
-  },
-  Object {
-    "currentValue": "==0.6.27",
-    "datasource": "pypi",
-    "depName": "distribute",
-    "depType": "packages",
-    "registryUrls": Array [
-      "https://pypi.org/simple",
-    ],
-  },
-  Object {
-    "currentValue": "==0.2",
-    "datasource": "pypi",
-    "depName": "dj-database-url",
-    "depType": "packages",
-    "registryUrls": Array [
-      "https://pypi.org/simple",
-    ],
-  },
-  Object {
-    "currentValue": "==2.4.5",
-    "datasource": "pypi",
-    "depName": "psycopg2",
-    "depType": "packages",
-    "registryUrls": Array [
-      "https://pypi.org/simple",
-    ],
-  },
-  Object {
-    "currentValue": "==0.1.2",
-    "datasource": "pypi",
-    "depName": "wsgiref",
-    "depType": "packages",
-    "registryUrls": Array [
-      "https://pypi.org/simple",
-    ],
-  },
-]
+Object {
+  "deps": Array [
+    Object {
+      "currentValue": "==1",
+      "datasource": "pypi",
+      "depName": "Django",
+      "depType": "packages",
+    },
+    Object {
+      "currentValue": "==0.6.27",
+      "datasource": "pypi",
+      "depName": "distribute",
+      "depType": "packages",
+    },
+    Object {
+      "currentValue": "==0.2",
+      "datasource": "pypi",
+      "depName": "dj-database-url",
+      "depType": "packages",
+    },
+    Object {
+      "currentValue": "==2.4.5",
+      "datasource": "pypi",
+      "depName": "psycopg2",
+      "depType": "packages",
+    },
+    Object {
+      "currentValue": "==0.1.2",
+      "datasource": "pypi",
+      "depName": "wsgiref",
+      "depType": "packages",
+    },
+  ],
+  "registryUrls": Array [
+    "https://pypi.org/simple",
+  ],
+}
 `;
diff --git a/test/manager/pipenv/extract.spec.js b/test/manager/pipenv/extract.spec.js
index f85fcf2441..32dee60d20 100644
--- a/test/manager/pipenv/extract.spec.js
+++ b/test/manager/pipenv/extract.spec.js
@@ -31,10 +31,10 @@ describe('lib/manager/pipenv/extract', () => {
       expect(extractPackageFile('nothing here', config)).toBeNull();
     });
     it('extracts dependencies', () => {
-      const res = extractPackageFile(pipfile1, config).deps;
+      const res = extractPackageFile(pipfile1, config);
       expect(res).toMatchSnapshot();
-      expect(res).toHaveLength(6);
-      expect(res.filter(dep => !dep.skipReason)).toHaveLength(4);
+      expect(res.deps).toHaveLength(6);
+      expect(res.deps.filter(dep => !dep.skipReason)).toHaveLength(4);
     });
     it('marks packages with "extras" as skipReason === any-version', () => {
       const res = extractPackageFile(pipfile3, {
@@ -42,45 +42,45 @@ describe('lib/manager/pipenv/extract', () => {
         pipenv: { enabled: true },
         pip_setup: { enabled: true },
         labels: ['dependencies'],
-      }).deps;
-      expect(res.filter(r => !r.skipReason)).toHaveLength(0);
-      expect(res.filter(r => r.skipReason)).toHaveLength(6);
+      });
+      expect(res.deps.filter(r => !r.skipReason)).toHaveLength(0);
+      expect(res.deps.filter(r => r.skipReason)).toHaveLength(6);
     });
     it('extracts multiple dependencies', () => {
-      const res = extractPackageFile(pipfile2, config).deps;
+      const res = extractPackageFile(pipfile2, config);
       expect(res).toMatchSnapshot();
-      expect(res).toHaveLength(5);
+      expect(res.deps).toHaveLength(5);
     });
     it('ignores git dependencies', () => {
       const content =
         '[packages]\r\nflask = {git = "https://github.com/pallets/flask.git"}\r\nwerkzeug = ">=0.14"';
-      const res = extractPackageFile(content, config).deps;
-      expect(res.filter(r => !r.skipReason)).toHaveLength(1);
+      const res = extractPackageFile(content, config);
+      expect(res.deps.filter(r => !r.skipReason)).toHaveLength(1);
     });
     it('ignores invalid package names', () => {
       const content = '[packages]\r\nfoo = "==1.0.0"\r\n_invalid = "==1.0.0"';
-      const res = extractPackageFile(content, config).deps;
-      expect(res).toHaveLength(2);
-      expect(res.filter(dep => !dep.skipReason)).toHaveLength(1);
+      const res = extractPackageFile(content, config);
+      expect(res.deps).toHaveLength(2);
+      expect(res.deps.filter(dep => !dep.skipReason)).toHaveLength(1);
     });
     it('ignores relative path dependencies', () => {
       const content = '[packages]\r\nfoo = "==1.0.0"\r\ntest = {path = "."}';
-      const res = extractPackageFile(content, config).deps;
-      expect(res.filter(r => !r.skipReason)).toHaveLength(1);
+      const res = extractPackageFile(content, config);
+      expect(res.deps.filter(r => !r.skipReason)).toHaveLength(1);
     });
     it('ignores invalid versions', () => {
       const content = '[packages]\r\nfoo = "==1.0.0"\r\nsome-package = "==0 0"';
-      const res = extractPackageFile(content, config).deps;
-      expect(res).toHaveLength(2);
-      expect(res.filter(dep => !dep.skipReason)).toHaveLength(1);
+      const res = extractPackageFile(content, config);
+      expect(res.deps).toHaveLength(2);
+      expect(res.deps.filter(dep => !dep.skipReason)).toHaveLength(1);
     });
     it('extracts all sources', () => {
       const content =
         '[[source]]\r\nurl = "source-url"\r\n' +
         '[[source]]\r\nurl = "other-source-url"\r\n' +
         '[packages]\r\nfoo = "==1.0.0"\r\n';
-      const res = extractPackageFile(content, config).deps;
-      expect(res[0].registryUrls).toEqual(['source-url', 'other-source-url']);
+      const res = extractPackageFile(content, config);
+      expect(res.registryUrls).toEqual(['source-url', 'other-source-url']);
     });
     it('extracts example pipfile', () => {
       const res = extractPackageFile(pipfile4, config);
-- 
GitLab