From 766d55e0013b455020a5e6b0fc0ee7899803bb0d Mon Sep 17 00:00:00 2001
From: Paul Melnikow <github@paulmelnikow.com>
Date: Thu, 4 Apr 2019 04:22:47 -0700
Subject: [PATCH] [pypi] Improve formatting of trove-based licenses (#3265)

Currently the trove-based license classifiers generate different formatting than the legacy license classifiers. This brings them into alignment.
---
 services/pypi/pypi-helpers.js        | 10 ++++-
 services/pypi/pypi-helpers.spec.js   |  2 +-
 services/pypi/pypi-license.tester.js | 55 ++++++++++++++++++++++++++++
 services/pypi/pypi.tester.js         | 54 ---------------------------
 4 files changed, 64 insertions(+), 57 deletions(-)
 create mode 100644 services/pypi/pypi-license.tester.js

diff --git a/services/pypi/pypi-helpers.js b/services/pypi/pypi-helpers.js
index 258652cdb2..0a108c9b07 100644
--- a/services/pypi/pypi-helpers.js
+++ b/services/pypi/pypi-helpers.js
@@ -57,13 +57,19 @@ function getLicenses(packageData) {
   if (license) {
     return [license]
   } else {
-    const acronymRegex = /\(([^)]+)\)/
+    const parenthesizedAcronymRegex = /\(([^)]+)\)/
+    const bareAcronymRegex = /^[a-z0-9]+$/
     let licenses = parseClassifiers(packageData, /^License :: (.+)$/)
       .map(classifier => classifier.split(' :: ').pop())
+      .map(license => license.replace(' license', ''))
       .map(license => {
-        const match = license.match(acronymRegex)
+        const match = license.match(parenthesizedAcronymRegex)
         return match ? match[1].toUpperCase() : license
       })
+      .map(license => {
+        const match = license.match(bareAcronymRegex)
+        return match ? license.toUpperCase() : license
+      })
     if (licenses.length > 1) {
       licenses = licenses.filter(l => l !== 'dfsg approved')
     }
diff --git a/services/pypi/pypi-helpers.spec.js b/services/pypi/pypi-helpers.spec.js
index 8ab9cae809..e121639229 100644
--- a/services/pypi/pypi-helpers.spec.js
+++ b/services/pypi/pypi-helpers.spec.js
@@ -121,7 +121,7 @@ describe('PyPI helpers', function() {
           ],
         },
       }),
-    ]).expect(['mit license'])
+    ]).expect(['MIT'])
     given({
       info: {
         license: '',
diff --git a/services/pypi/pypi-license.tester.js b/services/pypi/pypi-license.tester.js
new file mode 100644
index 0000000000..2626a85b59
--- /dev/null
+++ b/services/pypi/pypi-license.tester.js
@@ -0,0 +1,55 @@
+'use strict'
+
+const t = (module.exports = require('../tester').createServiceTester())
+
+t.create('license (valid, package version in request)')
+  .get('/requests/2.18.4.json')
+  .expectBadge({ label: 'license', message: 'Apache 2.0' })
+
+t.create('license (valid, no package version specified)')
+  .get('/requests.json')
+  .expectBadge({ label: 'license', message: 'Apache 2.0' })
+
+t.create('license (invalid)')
+  .get('/not-a-package.json')
+  .expectBadge({ label: 'license', message: 'package or version not found' })
+
+t.create('license (from trove classifier)')
+  .get('/mapi.json')
+  .intercept(nock =>
+    nock('https://pypi.org')
+      .get('/pypi/mapi/json')
+      .reply(200, {
+        info: {
+          version: '1.2.3',
+          license: '',
+          classifiers: ['License :: OSI Approved :: MIT License'],
+        },
+        releases: {},
+      })
+  )
+  .expectBadge({
+    label: 'license',
+    message: 'MIT',
+  })
+
+t.create('license (as acronym from trove classifier)')
+  .get('/magma.json')
+  .intercept(nock =>
+    nock('https://pypi.org')
+      .get('/pypi/magma/json')
+      .reply(200, {
+        info: {
+          version: '1.2.3',
+          license: '',
+          classifiers: [
+            'License :: OSI Approved :: GNU General Public License (GPL)',
+          ],
+        },
+        releases: {},
+      })
+  )
+  .expectBadge({
+    label: 'license',
+    message: 'GPL',
+  })
diff --git a/services/pypi/pypi.tester.js b/services/pypi/pypi.tester.js
index 8915cac77c..28d61829ae 100644
--- a/services/pypi/pypi.tester.js
+++ b/services/pypi/pypi.tester.js
@@ -97,60 +97,6 @@ t.create('no trove classifiers')
     message: 'v1.2.3',
   })
 
-// tests for license endpoint
-
-t.create('license (valid, package version in request)')
-  .get('/l/requests/2.18.4.json')
-  .expectBadge({ label: 'license', message: 'Apache 2.0' })
-
-t.create('license (valid, no package version specified)')
-  .get('/l/requests.json')
-  .expectBadge({ label: 'license', message: 'Apache 2.0' })
-
-t.create('license (invalid)')
-  .get('/l/not-a-package.json')
-  .expectBadge({ label: 'license', message: 'package or version not found' })
-
-t.create('license (from trove classifier)')
-  .get('/l/mapi.json')
-  .intercept(nock =>
-    nock('https://pypi.org')
-      .get('/pypi/mapi/json')
-      .reply(200, {
-        info: {
-          version: '1.2.3',
-          license: '',
-          classifiers: ['License :: OSI Approved :: MIT License'],
-        },
-        releases: {},
-      })
-  )
-  .expectBadge({
-    label: 'license',
-    message: 'mit license',
-  })
-
-t.create('license (as acronym from trove classifier)')
-  .get('/l/magma.json')
-  .intercept(nock =>
-    nock('https://pypi.org')
-      .get('/pypi/magma/json')
-      .reply(200, {
-        info: {
-          version: '1.2.3',
-          license: '',
-          classifiers: [
-            'License :: OSI Approved :: GNU General Public License (GPL)',
-          ],
-        },
-        releases: {},
-      })
-  )
-  .expectBadge({
-    label: 'license',
-    message: 'GPL',
-  })
-
 // tests for wheel endpoint
 
 t.create('wheel (has wheel, package version in request)')
-- 
GitLab