From f6cd8d609448ac999a23190859a19d9f39626dee Mon Sep 17 00:00:00 2001
From: Antoine Lambert <antoine.lambert@inria.fr>
Date: Thu, 11 Apr 2019 14:48:35 +0200
Subject: [PATCH] extract_npm_package_author: Handle list of dict authors
 layout

Related T1644
---
 swh/loader/npm/tests/test_utils.py | 41 ++++++++++++++++++++++++++++++
 swh/loader/npm/utils.py            | 26 +++++++++++--------
 2 files changed, 57 insertions(+), 10 deletions(-)

diff --git a/swh/loader/npm/tests/test_utils.py b/swh/loader/npm/tests/test_utils.py
index ec1b469..3537c19 100644
--- a/swh/loader/npm/tests/test_utils.py
+++ b/swh/loader/npm/tests/test_utils.py
@@ -225,3 +225,44 @@ class TestNpmClient(unittest.TestCase):
                 'email': b'evgeniy.pakalo@gmail.com'
             }
         )
+
+        package_json = json.loads('''
+        {
+            "name": "3-way-diff",
+            "version": "0.0.1",
+            "description": "3-way diffing of JavaScript objects",
+            "main": "index.js",
+            "authors": [
+                {
+                    "name": "Shawn Walsh",
+                    "url": "https://github.com/shawnpwalsh"
+                },
+                {
+                    "name": "Markham F Rollins IV",
+                    "url": "https://github.com/mrollinsiv"
+                }
+            ],
+            "keywords": [
+                "3-way diff",
+                "3 way diff",
+                "three-way diff",
+                "three way diff"
+            ],
+            "devDependencies": {
+                "babel-core": "^6.20.0",
+                "babel-preset-es2015": "^6.18.0",
+                "mocha": "^3.0.2"
+            },
+            "dependencies": {
+                "lodash": "^4.15.0"
+            }
+        }''')
+
+        self.assertEqual(
+            extract_npm_package_author(package_json),
+            {
+                'fullname': b'Shawn Walsh',
+                'name': b'Shawn Walsh',
+                'email': None
+            }
+        )
diff --git a/swh/loader/npm/utils.py b/swh/loader/npm/utils.py
index f09edd3..0bd09a6 100644
--- a/swh/loader/npm/utils.py
+++ b/swh/loader/npm/utils.py
@@ -77,19 +77,25 @@ def extract_npm_package_author(package_json):
             * email
 
     """
+
+    def _author_str(author_data):
+        if type(author_data) is dict:
+            author_str = ''
+            if 'name' in author_data:
+                author_str += author_data['name']
+            if 'email' in author_data:
+                author_str += ' <%s>' % author_data['email']
+            return author_str
+        else:
+            return author_data
+
     author_data = {}
     if 'author' in package_json:
-        if type(package_json['author']) is str:
-            author_data = parse_npm_package_author(package_json['author'])
-        elif type(package_json['author']) is dict:
-            author_str = ''
-            if 'name' in package_json['author']:
-                author_str += package_json['author']['name']
-            if 'email' in package_json['author']:
-                author_str += ' <%s>' % package_json['author']['email']
-            author_data = parse_npm_package_author(author_str)
+        author_str = _author_str(package_json['author'])
+        author_data = parse_npm_package_author(author_str)
     elif 'authors' in package_json and len(package_json['authors']) > 0:
-        author_data = parse_npm_package_author(package_json['authors'][0])
+        author_str = _author_str(package_json['authors'][0])
+        author_data = parse_npm_package_author(author_str)
 
     name = author_data.get('name')
     email = author_data.get('email')
-- 
GitLab