Skip to content

Commit 12a32bc

Browse files
author
Vikram Gaonkar
committed
Assume NPM person can be string
1 parent 4fd43e5 commit 12a32bc

File tree

2 files changed

+36
-14
lines changed

2 files changed

+36
-14
lines changed

hatch_nodejs_version/metadata_source.py

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77
import os.path
88
import re
99
import urllib.parse
10-
from typing import Any
10+
from typing import Any, Union
1111

1212
from hatchling.metadata.plugin.interface import MetadataHookInterface
1313

14-
AUTHOR_PATTERN = r"^([^<(]+?)?[ \t]*(?:<([^>(]+?)>)?[ \t]*(?:\(([^)]+?)\)|$)"
14+
AUTHOR_PATTERN = r"^(?P<name>[^<(]+?)?[ \t]*(?:<(?P<email>[^>(]+?)>)?[ \t]*(?:\((?P<url>[^)]+?)\)|$)"
1515
REPOSITORY_PATTERN = r"^(?:(gist|bitbucket|gitlab|github):)?(.*?)$"
1616
REPOSITORY_TABLE = {
1717
"gitlab": "https://gitlab.com",
@@ -137,19 +137,24 @@ def _parse_bugs(self, bugs: str | dict[str, str]) -> str | None:
137137
return bugs["url"]
138138

139139
def _parse_person(self, person: dict[str, str]) -> dict[str, str]:
140-
if {"url", "email"} & person.keys():
141-
result = {"name": person["name"]}
142-
if "email" in person:
143-
result["email"] = person["email"]
140+
result = {}
141+
if isinstance(person, dict):
142+
if {"url", "email"} & person.keys():
143+
result["name"] = person["name"]
144+
if "email" in person:
145+
result["email"] = person["email"]
146+
return result
147+
else:
148+
author = person["name"]
144149
else:
145-
match = re.match(AUTHOR_PATTERN, person["name"])
146-
if match is None:
147-
raise ValueError(f"Invalid author name: {person['name']}")
148-
name, email, _ = match.groups()
149-
result = {"name": name}
150-
if email is not None:
151-
result["email"] = email
152-
150+
author = person
151+
match = re.match(AUTHOR_PATTERN, author)
152+
if match is None:
153+
raise ValueError(f"Invalid author name: {author}")
154+
name, email, _ = match.groups()
155+
result = {"name": name}
156+
if email is not None:
157+
result["email"] = email
153158
return result
154159

155160
def _parse_repository(self, repository: str | dict[str, str]) -> str:

tests/test_metadata_config.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#
33
# SPDX-License-Identifier: MIT
44
import pytest
5+
import json
56

67
from hatch_nodejs_version.metadata_source import NodeJSMetadataHook
78

@@ -165,3 +166,19 @@ def test_labels(self, project):
165166
assert urls["the-repository"] == "https://github.com/some/code.git"
166167
assert urls["the-bug-tracker"] == "https://www.send-help.com"
167168
assert urls["the-homepage"] == "https://where-the-heart-is.com"
169+
170+
def test_authors_accepted_as_strings(self, project):
171+
original_package_content = json.loads(TRIVIAL_PACKAGE_CONTENTS)
172+
updated_package_content = original_package_content.copy()
173+
author_as_string = f"{original_package_content['author']['name']} " \
174+
f"<{original_package_content['author']['email']}>"
175+
updated_package_content['author'] = author_as_string
176+
(project / "pyproject.toml").write_text(TRIVIAL_PYPROJECT_CONTENTS)
177+
(project / "package.json").write_text(json.dumps(updated_package_content))
178+
179+
config = {}
180+
metadata = {}
181+
metadata_source = NodeJSMetadataHook(project, config=config)
182+
metadata_source.update(metadata)
183+
assert metadata == TRIVIAL_EXPECTED_METADATA
184+

0 commit comments

Comments
 (0)